Brian Silverman | 70325d6 | 2015-09-20 17:00:43 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012, Olaf van der Spek <olafvdspek@gmail.com> |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // --- |
| 31 | // Author: Olaf van der Spek <olafvdspek@gmail.com> |
| 32 | |
| 33 | #ifndef TEMPLATE_STR_REF_H_ |
| 34 | #define TEMPLATE_STR_REF_H_ |
| 35 | |
| 36 | #include <cstddef> |
| 37 | |
| 38 | // NOTE: if you are statically linking the template library into your binary |
| 39 | // (rather than using the template .dll), set '/D CTEMPLATE_DLL_DECL=' |
| 40 | // as a compiler flag in your project file to turn off the dllimports. |
| 41 | #ifndef CTEMPLATE_DLL_DECL |
| 42 | # define CTEMPLATE_DLL_DECL __declspec(dllimport) |
| 43 | #endif |
| 44 | |
| 45 | namespace ctemplate { |
| 46 | |
| 47 | template <class T> |
| 48 | class str_ref_basic |
| 49 | { |
| 50 | public: |
| 51 | str_ref_basic() |
| 52 | { |
| 53 | clear(); |
| 54 | } |
| 55 | |
| 56 | template <class U> |
| 57 | str_ref_basic(const U& c) |
| 58 | { |
| 59 | if (c.end() != c.begin()) |
| 60 | assign(&*c.begin(), c.end() - c.begin() + &*c.begin()); |
| 61 | else |
| 62 | clear(); |
| 63 | } |
| 64 | |
| 65 | str_ref_basic(const void* b, const void* e) |
| 66 | { |
| 67 | assign(b, e); |
| 68 | } |
| 69 | |
| 70 | str_ref_basic(const void* b, size_t sz) |
| 71 | { |
| 72 | assign(b, sz); |
| 73 | } |
| 74 | |
| 75 | str_ref_basic(const char* b) |
| 76 | { |
| 77 | if (b) |
| 78 | assign(b, strlen(b)); |
| 79 | else |
| 80 | clear(); |
| 81 | } |
| 82 | |
| 83 | void clear() |
| 84 | { |
| 85 | begin_ = end_ = NULL; |
| 86 | } |
| 87 | |
| 88 | void assign(const void* b, const void* e) |
| 89 | { |
| 90 | begin_ = reinterpret_cast<T>(b); |
| 91 | end_ = reinterpret_cast<T>(e); |
| 92 | } |
| 93 | |
| 94 | void assign(const void* b, size_t sz) |
| 95 | { |
| 96 | begin_ = reinterpret_cast<T>(b); |
| 97 | end_ = begin_ + sz; |
| 98 | } |
| 99 | |
| 100 | T begin() const |
| 101 | { |
| 102 | return begin_; |
| 103 | } |
| 104 | |
| 105 | T end() const |
| 106 | { |
| 107 | return end_; |
| 108 | } |
| 109 | |
| 110 | T data() const |
| 111 | { |
| 112 | return begin(); |
| 113 | } |
| 114 | |
| 115 | size_t size() const |
| 116 | { |
| 117 | return end() - begin(); |
| 118 | } |
| 119 | |
| 120 | bool empty() const |
| 121 | { |
| 122 | return begin() == end(); |
| 123 | } |
| 124 | private: |
| 125 | T begin_; |
| 126 | T end_; |
| 127 | }; |
| 128 | |
| 129 | typedef str_ref_basic<const unsigned char*> data_ref; |
| 130 | typedef str_ref_basic<const char*> str_ref; |
| 131 | |
| 132 | } |
| 133 | |
| 134 | #endif // TEMPLATE_STR_REF_H_ |