blob: 68b591aab7263387840aa82582f4056f3044f419 [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// 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@ac_windows_dllexport_defines@
39
40namespace ctemplate {
41
42template <class T>
43class str_ref_basic
44{
45public:
46 str_ref_basic()
47 {
48 clear();
49 }
50
51 template <class U>
52 str_ref_basic(const U& c)
53 {
54 if (c.end() != c.begin())
55 assign(&*c.begin(), c.end() - c.begin() + &*c.begin());
56 else
57 clear();
58 }
59
60 str_ref_basic(const void* b, const void* e)
61 {
62 assign(b, e);
63 }
64
65 str_ref_basic(const void* b, size_t sz)
66 {
67 assign(b, sz);
68 }
69
70 str_ref_basic(const char* b)
71 {
72 if (b)
73 assign(b, strlen(b));
74 else
75 clear();
76 }
77
78 void clear()
79 {
80 begin_ = end_ = NULL;
81 }
82
83 void assign(const void* b, const void* e)
84 {
85 begin_ = reinterpret_cast<T>(b);
86 end_ = reinterpret_cast<T>(e);
87 }
88
89 void assign(const void* b, size_t sz)
90 {
91 begin_ = reinterpret_cast<T>(b);
92 end_ = begin_ + sz;
93 }
94
95 T begin() const
96 {
97 return begin_;
98 }
99
100 T end() const
101 {
102 return end_;
103 }
104
105 T data() const
106 {
107 return begin();
108 }
109
110 size_t size() const
111 {
112 return end() - begin();
113 }
114
115 bool empty() const
116 {
117 return begin() == end();
118 }
119private:
120 T begin_;
121 T end_;
122};
123
124typedef str_ref_basic<const unsigned char*> data_ref;
125typedef str_ref_basic<const char*> str_ref;
126
127}
128
129#endif // TEMPLATE_STR_REF_H_