OnixS C++ Euronext Optiq MDG Handler  1.3.1
API documentation
StrRef.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is
4 // protected by copyright law and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable
7 // OnixS Software Services Agreement (the Agreement) and Customer end user license
8 // agreements granting a non-assignable, non-transferable and non-exclusive license
9 // to use the software for it's own data processing purposes under the terms defined
10 // in the Agreement.
11 //
12 // Except as otherwise granted within the terms of the Agreement, copying or
13 // reproduction of any part of this source code or associated reference material
14 // to any other location for further reproduction or redistribution, and any
15 // amendments to this copyright notice, are expressly prohibited.
16 //
17 // Any reproduction or redistribution for sale or hiring of the Software not in
18 // accordance with the terms of the Agreement is a violation of copyright law.
19 //
20 
21 #pragma once
22 
27 
28 #include <algorithm>
29 #include <cassert>
30 #include <cstddef>
31 #include <cstring>
32 #include <limits>
33 #include <ostream>
34 #include <stdexcept>
35 #include <string>
36 
37 #if defined(ONIXS_EURONEXT_OPTIQMDG_HAS_STRING_VIEW)
38 #include <string_view>
39 #endif
40 
41 
43 
44 #if defined(ONIXS_EURONEXT_OPTIQMDG_HAS_STRING_VIEW)
45 
46 using StrRef = std::basic_string_view<Char>;
47 
48 #else
49 
50 /// \private
51 /// Throws exception on invalid index
55 void throwInvalidIndex(size_t, size_t);
56 
57 /// String reference.
58 ///
59 /// Provides the efficient way of accessing
60 /// text-based values without copying
61 /// content of the text being referred.
63 {
64 public:
65  typedef size_t size_type;
66 
67  /// STL-like iterators.
68  typedef const Char* const_iterator;
69  typedef const_iterator iterator;
70  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
71  typedef const_reverse_iterator reverse_iterator;
72 
73  /// Initializes the instance referring to nothing.
77  , size_(0)
78  {
79  }
80 
81  /// Explicit initialization.
83  StrRef(const Char* chars, size_t size) ONIXS_EURONEXT_OPTIQMDG_NOTHROW
84  : items_(chars)
85  , size_((ONIXS_EURONEXT_OPTIQMDG_ASSERT(size <= (std::numeric_limits<size_type>::max)()), static_cast<size_type>(size)))
86  {
87  }
88 
89  /// Explicit initialization.
91  : items_(chars)
92  , size_(0)
93  {
94  assert(chars);
95 
96  ONIXS_EURONEXT_OPTIQMDG_CONST_OR_CONSTEXPR size_type maxLen = (std::numeric_limits<UInt16>::max)();
97 
98  size_ = numericCast<size_type>(strnlen(chars, maxLen));
99 
100  assert(size_ < maxLen);
101  }
102 
103  /// Explicit initialization.
104  StrRef(const std::string& string)
105  : items_(string.c_str())
106  , size_(static_cast<size_type>(string.length()))
107  {
108  assert(string.length() <= (std::numeric_limits<size_type>::max)());
109  }
110 
111  /// \return `true` if the reference is empty, otherwise - `false`.
114  {
115  return (0 == size_);
116  }
117 
118  /// \return the read-only content.
122  {
123  return items_;
124  }
125 
126  /// \return the number of characters.
130  {
131  return size_;
132  }
133 
136  /// \return the the number of characters.
138  {
139  return size_;
140  }
141 
142  /// STL-like begin().
144  const_iterator begin() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
145  {
146  assert(items_);
147  return items_;
148  }
149 
150  /// STL-like end().
152  const_iterator end() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
153  {
154  assert(items_);
155  return (items_ + size_);
156  }
157 
158  /// STL-like cbegin().
161  {
162  return begin();
163  }
164 
165  /// STL-like cend().
167  const_iterator cend() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
168  {
169  return end();
170  }
171 
172  /// STL-like rbegin().
174  const_reverse_iterator rbegin() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
175  {
176  return const_reverse_iterator(end());
177  }
178 
179  /// STL-like rend().
181  const_reverse_iterator rend() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
182  {
183  return const_reverse_iterator(begin());
184  }
185 
186  /// STL-like crbegin().
188  const_reverse_iterator crbegin() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
189  {
190  return rbegin();
191  }
192 
193  /// STL-like crend().
195  const_reverse_iterator crend() const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
196  {
197  return rend();
198  }
199 
200  /// Provides the per-item access.
202  const Char& operator [](size_type index) const ONIXS_EURONEXT_OPTIQMDG_NOTHROW
203  {
204  assert(index < size_);
205  assert(items_);
206 
207  return items_[index];
208  }
209 
210  /// Provides the bound-checked per-item access.
212  const Char& at(size_type index) const
213  {
214  assert(items_);
215 
216  if (index < size_)
217  return items_[index];
218 
219  throwInvalidIndex(index, size_);
220  }
221 
222  /// \return the reference to the first character in the view.
224  {
225  assert(!empty());
226  return (*this)[0];
227  }
228 
229  /// \return the reference to the last character in the view.
231  {
232  assert(!empty());
233  return (*this)[size_ - 1];
234  }
235 
236  /// Swaps the content with the given instance.
238  {
239  std::swap(items_, other.items_);
240 
241  std::swap(size_, other.size_);
242  }
243 
244  /// Converts to std::basic_string
245  operator std::basic_string<Char>() const
246  {
247  return std::basic_string<Char>(data(), size());
248  }
249 
250 private:
251  /// Items being referenced.
252  const Char* items_;
253 
254  /// the number of characters being referenced.
255  size_type size_;
256 };
257 
258 /// Compares instances.
260 inline
261 bool operator ==(const StrRef& left, const StrRef& right)
262 {
263  if(left.size() != right.size())
264  return false;
265 
266  if(left.empty())
267  {
268  assert(right.empty());
269  return true;
270  }
271 
272  assert(left.data());
273  assert(right.data());
274 
275  return (0 == memcmp(
276  left.data(), right.data(), left.size()));
277 }
278 
279 /// Compares instances.
281 inline
282 bool operator !=(const StrRef& left, const StrRef& right)
283 {
284  return !(left == right);
285 }
286 
287 /// Compares instances.
289 inline
290 bool operator <(const StrRef& left, const StrRef& right)
291 {
292  const int
293  result =
294  memcmp(
295  left.data(),
296  right.data(),
297  left.size() < right.size()
298  ? left.size()
299  : right.size());
300 
301  return (
302  0 != result
303  ? (0 > result)
304  : (left.size() < right.size()));
305 }
306 
307 /// Compares instances.
309 inline
310 bool operator >(const StrRef& left, const StrRef& right)
311 {
312  return (right < left);
313 }
314 
315 /// StrRef serialization operator.
316 inline
317 std::ostream& operator <<(std::ostream& stream, const StrRef& text)
318 {
319  stream.write(text.data(), text.size());
320 
321  return stream;
322 }
323 
326 
327 /// Compares StrRef with std::string.
329 inline
330 bool operator ==(const StrRef& ref, const std::string& str)
331 {
332  return ref == toStrRef(str);
333 }
334 
335 /// Compares StrRef with std::string.
337 inline
338 bool operator !=(const StrRef& ref, const std::string& str)
339 {
340  return ref != toStrRef(str);
341 }
342 
343 /// Compares StrRef with std::string.
345 inline
346 bool operator ==(const std::string& str, const StrRef& ref)
347 {
348  return ref == toStrRef(str);
349 }
350 
351 /// Compares StrRef with std::string.
353 inline
354 bool operator !=(const std::string& str, const StrRef& ref)
355 {
356  return ref != toStrRef(str);
357 }
358 
359 /// Compares StrRef with a zero-terminated/C-like string.
361 inline
362 bool operator ==(const StrRef& ref, const Char* str)
363 {
364  return ref == toStrRef(str);
365 }
366 
367 /// Compares StrRef with a zero-terminated/C-like string.
369 inline
370 bool operator !=(const StrRef& ref, const Char* str)
371 {
372  return ref != toStrRef(str);
373 }
374 
375 /// Compares StrRef with a zero-terminated/C-like string.
377 inline
378 bool operator ==(const Char* str, const StrRef& ref)
379 {
380  return ref == toStrRef(str);
381 }
382 
383 /// Compares StrRef with a zero-terminated/C-like string.
385 inline
386 bool operator !=( const Char* str, const StrRef& ref)
387 {
388  return ref != toStrRef(str);
389 }
390 
391 #endif
392 
393 /// Constructs a StrRef instance over th std::string content.
395 inline StrRef toStrRef(const std::string& str)
396 {
397  return StrRef(str);
398 }
399 
400 /// Initializes the instance from a zero-terminated/C-like string.
402 inline StrRef toStrRef(const Char* cStr) ONIXS_EURONEXT_OPTIQMDG_NOTHROW
403 {
404  return
406  StrRef(cStr) :
407  StrRef()
408  ;
409 }
410 
411 template <size_t Size>
413 inline
415 StrRef constructStrRef(const char (&value)[Size])
416  ONIXS_EURONEXT_OPTIQMDG_NOTHROW
417 {
418  return StrRef(value, Size - 1);
419 }
420 
421 template <size_t Size>
423 inline
425 StrRef strRefFromCharArray(const char (&value)[Size])
426 ONIXS_EURONEXT_OPTIQMDG_NOTHROW
427 {
428  return StrRef(value, Size);
429 }
430 
431 /// Constructs a std::string instance from the StrRef one.
433 inline
434 std::string toStr(const StrRef& ref)
435 {
436  return std::string(ref.data(), ref.size());
437 }
438 
439 /// Appends the text referenced by StrRef to the given std::string instance.
440 inline
441 void toStr(std::string& str, const StrRef& ref)
442 {
443  str.append(ref.data(), ref.size());
444 }
445 
constexpr StrRef()
Initializes the instance referring to nothing.
Definition: StrRef.h:75
#define ONIXS_EURONEXT_OPTIQMDG_MESSAGING_NAMESPACE_END
Definition: ABI.h:151
const_reverse_iterator crend() const noexcept
STL-like crend().
Definition: StrRef.h:195
const Char * const_iterator
STL-like iterators.
Definition: StrRef.h:68
const Char & at(size_type index) const
Provides the bound-checked per-item access.
Definition: StrRef.h:212
#define ONIXS_EURONEXT_OPTIQMDG_CONST_OR_CONSTEXPR
Definition: Compiler.h:188
#define ONIXS_EURONEXT_OPTIQMDG_LTWT_CLASS
Definition: ABI.h:90
StrRef toStrRef(const Char *) noexcept
Initializes the instance from a zero-terminated/C-like string.
Definition: StrRef.h:402
const_reverse_iterator rend() const noexcept
STL-like rend().
Definition: StrRef.h:181
#define ONIXS_EURONEXT_OPTIQMDG_NOTHROW
Definition: Compiler.h:186
#define ONIXS_EURONEXT_OPTIQMDG_EXPORTED
Definition: ABI.h:37
#define ONIXS_EURONEXT_OPTIQMDG_CONSTEXPR
Definition: Compiler.h:189
#define ONIXS_EURONEXT_OPTIQMDG_NORETURN
Definition: Compiler.h:194
bool operator!=(const Char *str, const StrRef &ref)
Compares StrRef with a zero-terminated/C-like string.
Definition: StrRef.h:386
bool operator==(const Char *str, const StrRef &ref)
Compares StrRef with a zero-terminated/C-like string.
Definition: StrRef.h:378
#define ONIXS_EURONEXT_OPTIQMDG_NODISCARD
Definition: Compiler.h:195
void swap(StrRef &other) noexcept
Swaps the content with the given instance.
Definition: StrRef.h:237
#define ONIXS_EURONEXT_OPTIQMDG_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:146
constexpr StrRef strRefFromCharArray(const char(&value)[Size]) noexcept
Definition: StrRef.h:425
const_iterator end() const noexcept
STL-like end().
Definition: StrRef.h:152
constexpr size_type size() const noexcept
Definition: StrRef.h:129
constexpr const Char * data() const noexcept
Definition: StrRef.h:121
const_iterator cend() const noexcept
STL-like cend().
Definition: StrRef.h:167
std::ostream & operator<<(std::ostream &stream, const StrRef &text)
StrRef serialization operator.
Definition: StrRef.h:317
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: StrRef.h:70
const_iterator begin() const noexcept
STL-like begin().
Definition: StrRef.h:144
constexpr StrRef constructStrRef(const char(&value)[Size]) noexcept
Definition: StrRef.h:415
#define ONIXS_EURONEXT_OPTIQMDG_COLDPATH
Definition: Compiler.h:198
bool operator>(const StrRef &left, const StrRef &right)
Compares instances.
Definition: StrRef.h:310
const_reverse_iterator rbegin() const noexcept
STL-like rbegin().
Definition: StrRef.h:174
bool operator<(const StrRef &left, const StrRef &right)
Compares instances.
Definition: StrRef.h:290
const_iterator cbegin() const noexcept
STL-like cbegin().
Definition: StrRef.h:160
#define ONIXS_EURONEXT_OPTIQMDG_NULLPTR
Definition: Compiler.h:192
const_reverse_iterator crbegin() const noexcept
STL-like crbegin().
Definition: StrRef.h:188
StrRef(const Char *chars) noexcept
Explicit initialization.
Definition: StrRef.h:90
void toStr(std::string &str, const StrRef &ref)
Appends the text referenced by StrRef to the given std::string instance.
Definition: StrRef.h:441
constexpr StrRef(const Char *chars, size_t size) noexcept
Explicit initialization.
Definition: StrRef.h:83
constexpr size_type length() const noexcept
Definition: StrRef.h:137
StrRef(const std::string &string)
Explicit initialization.
Definition: StrRef.h:104