OnixS C++ CME iLink 3 Binary Order Entry Handler  1.18.0
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 
23 #include <OnixS/CME/iLink3/ABI.h>
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_ILINK3_CXX17)
38 # include <string_view>
39 #endif
40 
41 
43 
44 #if defined (ONIXS_ILINK3_CXX17)
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.
75  StrRef()
76  : items_(ONIXS_ILINK3_NULLPTR)
77  , size_(0)
78  {
79  }
80 
81  /// Explicit initialization.
83  StrRef(const Char* chars, size_t size) ONIXS_ILINK3_NOTHROW
84  : items_(chars)
85  , size_((ONIXS_ILINK3_ASSERT(size <= (std::numeric_limits<size_type>::max)()), static_cast<size_type>(size)))
86  {
87  }
88 
89  /// Explicit initialization.
90  StrRef(const Char* chars) ONIXS_ILINK3_NOTHROW
91  : items_(chars)
92  , size_(0)
93  {
94  assert(chars);
95 
96  ONIXS_ILINK3_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`.
113  bool empty() const ONIXS_ILINK3_NOTHROW
114  {
115  return (0 == size_);
116  }
117 
118  /// \return the read-only content.
121  const Char* data() const ONIXS_ILINK3_NOTHROW
122  {
123  return items_;
124  }
125 
126  /// \return the number of characters.
129  size_type size() const ONIXS_ILINK3_NOTHROW
130  {
131  return size_;
132  }
133 
136  /// \return the the number of characters.
137  size_type length() const ONIXS_ILINK3_NOTHROW
138  {
139  return size_;
140  }
141 
142  /// STL-like begin().
144  const_iterator begin() const ONIXS_ILINK3_NOTHROW
145  {
146  assert(items_);
147  return items_;
148  }
149 
150  /// STL-like end().
152  const_iterator end() const ONIXS_ILINK3_NOTHROW
153  {
154  assert(items_);
155  return (items_ + size_);
156  }
157 
158  /// STL-like cbegin().
160  const_iterator cbegin() const ONIXS_ILINK3_NOTHROW
161  {
162  return begin();
163  }
164 
165  /// STL-like cend().
167  const_iterator cend() const ONIXS_ILINK3_NOTHROW
168  {
169  return end();
170  }
171 
172  /// STL-like rbegin().
174  const_reverse_iterator rbegin() const ONIXS_ILINK3_NOTHROW
175  {
176  return const_reverse_iterator(end());
177  }
178 
179  /// STL-like rend().
181  const_reverse_iterator rend() const ONIXS_ILINK3_NOTHROW
182  {
183  return const_reverse_iterator(begin());
184  }
185 
186  /// STL-like crbegin().
188  const_reverse_iterator crbegin() const ONIXS_ILINK3_NOTHROW
189  {
190  return rbegin();
191  }
192 
193  /// STL-like crend().
195  const_reverse_iterator crend() const ONIXS_ILINK3_NOTHROW
196  {
197  return rend();
198  }
199 
200  /// Provides the per-item access.
202  const Char& operator [](size_type index) const ONIXS_ILINK3_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.
223  const Char& front() const ONIXS_ILINK3_NOTHROW
224  {
225  assert(!empty());
226  return (*this)[0];
227  }
228 
229  /// \return the reference to the last character in the view.
230  const Char& back() const ONIXS_ILINK3_NOTHROW
231  {
232  assert(!empty());
233  return (*this)[size_ - 1];
234  }
235 
236  /// Swaps the content with the given instance.
237  void swap(StrRef& other) ONIXS_ILINK3_NOTHROW
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 
324 ONIXS_ILINK3_NODISCARD StrRef toStrRef(const std::string&);
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.
403 {
404  return
405  (cStr != ONIXS_ILINK3_NULLPTR) ?
406  StrRef(cStr) :
407  StrRef()
408  ;
409 }
410 
411 template <size_t Size>
413 inline
415 StrRef constructStrRef(const char (&value)[Size])
417 {
418  return StrRef(value, Size - 1);
419 }
420 
421 template <size_t Size>
423 inline
425 StrRef strRefFromCharArray(const char (&value)[Size])
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 
#define ONIXS_ILINK3_CONSTEXPR
Definition: Compiler.h:179
#define ONIXS_ILINK3_NULLPTR
Definition: Compiler.h:182
#define ONIXS_ILINK3_CONST_OR_CONSTEXPR
Definition: Compiler.h:178
std::basic_string_view< Char > StrRef
Definition: StrRef.h:46
char Char
Character type alias.
Definition: String.h:30
constexpr StrRef constructStrRef(const char(&value)[Size]) noexcept
Definition: StrRef.h:415
#define ONIXS_ILINK3_NORETURN
Definition: Compiler.h:184
STL namespace.
StrRef toStrRef(const Char *cStr) noexcept
Initializes the instance from a zero-terminated/C-like string.
Definition: StrRef.h:402
#define ONIXS_ILINK3_LTWT_CLASS
Definition: ABI.h:84
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
#define ONIXS_ILINK3_EXPORTED
Definition: Compiler.h:175
std::ostream & operator<<(std::ostream &o, SessionStateId::Enum state)
constexpr StrRef strRefFromCharArray(const char(&value)[Size]) noexcept
Definition: StrRef.h:425
void toStr(std::string &str, const StrRef &ref)
Appends the text referenced by StrRef to the given std::string instance.
Definition: StrRef.h:441
#define ONIXS_ILINK3_COLDPATH
Definition: Compiler.h:188
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
#define ONIXS_ILINK3_NODISCARD
Definition: Compiler.h:185
#define ONIXS_ILINK3_NOTHROW
Definition: Compiler.h:176