OGLplus (0.52.0) a C++ wrapper for OpenGL

ref_tpl.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_STRING_REF_TPL_1107121519_HPP
14 #define OGLPLUS_STRING_REF_TPL_1107121519_HPP
15 
16 #include <oglplus/string/utf8.hpp>
17 #include <oglplus/config/compiler.hpp>
18 #include <array>
19 #include <vector>
20 #include <string>
21 #include <cstring>
22 #include <cstddef>
23 #include <cassert>
24 
25 namespace oglplus {
26 
27 // helper class used for implementation of concatenation of string references
28 template <typename Char, typename Left, typename Right>
29 class StrCRefChainTpl
30 {
31 private:
32  Left _left;
33  Right _right;
34 public:
35  StrCRefChainTpl(Left left, Right right)
36  : _left(left)
37  , _right(right)
38  { }
39 
40  bool empty(void) const
41  {
42  return _left.empty() && _right.empty();
43  }
44 
45  std::size_t size(void) const
46  {
47  return _left.size() + _right.size();
48  }
49 
50  void append_to(std::basic_string<Char>& dest) const
51  {
52  _left.append_to(dest);
53  _right.append_to(dest);
54  }
55 
56  std::basic_string<Char> str(void) const
57  {
58  std::basic_string<Char> result;
59  result.reserve(this->size()+1);
60  this->append_to(result);
61  return result;
62  }
63 
64  OGLPLUS_EXPLICIT operator std::basic_string<Char>(void) const
65  {
66  return str();
67  }
68 };
69 
71 template <typename Char>
73 {
74 private:
75  static_assert(
76  sizeof(Char) == sizeof(char),
77  "Characters with different size that sizeof(char) "
78  "are not supported by StrCRefTpl"
79  );
80 
81  const Char* _c_str;
82  mutable std::size_t _size;
83 
84  static const Char* _ecs(void)
85  OGLPLUS_NOEXCEPT(true)
86  { return ""; }
87 
88  void _validate(void) const
89  OGLPLUS_NOEXCEPT(true)
90  {
91  assert(aux::ValidUTF8(
92  (const char*)begin(),
93  (const char*)end()
94  ));
95  }
96 public:
98  StrCRefTpl(void)
99  OGLPLUS_NOEXCEPT(true)
100  : _c_str(nullptr)
101  , _size(0)
102  { }
103 
105  StrCRefTpl(const Char* cstr)
106  OGLPLUS_NOEXCEPT(true)
107  : _c_str(cstr)
108  , _size(0)
109  { _validate(); }
110 
112  StrCRefTpl(const Char* cstr, std::size_t ssize)
113  OGLPLUS_NOEXCEPT(true)
114  : _c_str(cstr)
115  , _size(ssize)
116  { _validate(); }
117 
119  template <std::size_t N>
120  StrCRefTpl(const Char (&cary)[N])
121  OGLPLUS_NOEXCEPT(true)
122  : _c_str(cary)
123  , _size(N)
124  { _validate(); }
125 
127  StrCRefTpl(const std::basic_string<Char>& sstr)
128  OGLPLUS_NOEXCEPT(true)
129  : _c_str(sstr.c_str())
130  , _size(sstr.size())
131  { _validate(); }
132 
134  StrCRefTpl(const std::vector<Char>& cvec)
135  OGLPLUS_NOEXCEPT(true)
136  : _c_str(cvec.data())
137  , _size(cvec.size())
138  { _validate(); }
139 
141  template <std::size_t N>
142  StrCRefTpl(const std::array<Char, N>& cvec)
143  OGLPLUS_NOEXCEPT(true)
144  : _c_str(cvec.data())
145  , _size(cvec.size())
146  { _validate(); }
147 
149  std::size_t size(void) const
150  OGLPLUS_NOEXCEPT(true)
151  {
152  if((_size == 0) && (_c_str != nullptr))
153  _size = std::strlen(_c_str);
154  return _size;
155  }
156 
158  bool empty(void) const
159  OGLPLUS_NOEXCEPT(true)
160  {
161  return size() == 0;
162  }
163 
165  typedef const Char* iterator;
168 
170  const_iterator begin(void) const
171  OGLPLUS_NOEXCEPT(true)
172  {
173  return _c_str?_c_str:_ecs();
174  }
175 
177  const_iterator end(void) const
178  OGLPLUS_NOEXCEPT(true)
179  {
180  return begin()+size();
181  }
182 
184  bool is_nts(void) const
185  OGLPLUS_NOEXCEPT(true)
186  {
187  return *end() == '\0';
188  }
189 
191  std::basic_string<Char> str(void) const
192  {
193  return std::basic_string<Char>(begin(), end());
194  }
195 
197 
200  const Char* c_str(void) const
201  OGLPLUS_NOEXCEPT(true)
202  {
203  assert(is_nts());
204  return begin();
205  }
206 
207  static bool Equal(const StrCRefTpl& a, const StrCRefTpl& b)
208  {
209  return (a.size() == b.size()) &&
210  (std::strncmp(a.begin(), b.begin(), a.size()) == 0);
211  }
212 
213  static bool Equal(const StrCRefTpl& a, const Char* b)
214  {
215  if(a.is_nts()) return std::strcmp(a.c_str(), b) == 0;
216 
217  std::size_t size = std::strlen(b);
218  return (a.size() == size) &&
219  (std::strncmp(a.begin(), b, a.size()) == 0);
220  }
221 
222  friend bool operator == (const StrCRefTpl& a, const StrCRefTpl& b)
223  {
224  return StrCRefTpl::Equal(a, b);
225  }
226 
227  friend bool operator == (const StrCRefTpl& a, const Char* b)
228  {
229  return StrCRefTpl::Equal(a, b);
230  }
231 
232  friend bool operator == (const Char* a, const StrCRefTpl& b)
233  {
234  return StrCRefTpl::Equal(b, a);
235  }
236 
237  friend bool operator != (const StrCRefTpl& a, const StrCRefTpl& b)
238  {
239  return !StrCRefTpl::Equal(a, b);
240  }
241 
242  friend bool operator != (const StrCRefTpl& a, const Char* b)
243  {
244  return !StrCRefTpl::Equal(a, b);
245  }
246 
247  friend bool operator != (const Char* a, const StrCRefTpl& b)
248  {
249  return !StrCRefTpl::Equal(b, a);
250  }
251 };
252 
253 template <typename Char>
254 inline
255 StrCRefChainTpl<Char, StrCRefTpl<Char>, StrCRefTpl<Char>>
256 operator + (StrCRefTpl<Char> left, StrCRefTpl<Char> right)
257 {
258  return StrCRefChainTpl<Char, StrCRefTpl<Char>, StrCRefTpl<Char>>(
259  left,
260  right
261  );
262 }
263 
264 template <typename Char, typename Left, typename Right>
265 inline
266 StrCRefChainTpl<Char, StrCRefChainTpl<Char, Left, Right>, StrCRefTpl<Char> >
267 operator + (StrCRefChainTpl<Char, Left, Right> left, StrCRefTpl<Char> right)
268 {
269  return StrCRefChainTpl<
270  Char,
271  StrCRefChainTpl<Char,Left,Right>,
272  StrCRefTpl<Char>
273  >(left, right);
274 }
275 
276 } // namespace oglplus
277 
278 #endif // include guard
const Char * c_str(void) const
Returns the null-terminated c-string.
Definition: ref_tpl.hpp:200
StrCRefTpl(const Char *cstr, std::size_t ssize)
Construction from a c-string and size.
Definition: ref_tpl.hpp:112
const_iterator end(void) const
Returns iterator past the last character.
Definition: ref_tpl.hpp:177
StrCRefTpl(const Char *cstr)
Construction from a null-terminated string.
Definition: ref_tpl.hpp:105
StrCRefTpl(void)
Default construction (reference to an empty c-string)
Definition: ref_tpl.hpp:98
const_iterator begin(void) const
Returns iterator to the first character.
Definition: ref_tpl.hpp:170
StrCRefTpl(const std::vector< Char > &cvec)
Construction from a std::vector<Char>
Definition: ref_tpl.hpp:134
std::basic_string< Char > str(void) const
Returns a String.
Definition: ref_tpl.hpp:191
StrCRefTpl(const std::array< Char, N > &cvec)
Construction from a std::array<Char, N>
Definition: ref_tpl.hpp:142
std::size_t size(void) const
Return the size (length) string.
Definition: ref_tpl.hpp:149
String const reference wrapper template.
Definition: ref_tpl.hpp:72
StrCRefTpl(const std::basic_string< Char > &sstr)
Construction from a std::basic_string<Char>
Definition: ref_tpl.hpp:127
bool empty(void) const
Returns true if the string is empty.
Definition: ref_tpl.hpp:158
iterator const_iterator
Const iterator type.
Definition: ref_tpl.hpp:167
bool is_nts(void) const
Returns true if the string is null-terminated.
Definition: ref_tpl.hpp:184
StrCRefTpl(const Char(&cary)[N])
Construction from a character array with known size.
Definition: ref_tpl.hpp:120
const Char * iterator
Iterator type.
Definition: ref_tpl.hpp:165

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).