OnixS C++ CME MDP Premium Market Data Handler 5.10.2
Users' manual and API documentation
Loading...
Searching...
No Matches
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/MDH/ABI.h>
28
29#include <algorithm>
30#include <cassert>
31#include <cstddef>
32#include <cstring>
33#include <limits>
34#include <ostream>
35#include <stdexcept>
36#include <string>
37
38#if defined(ONIXS_CMEMDH_CXX17)
39# include <string_view>
40#endif
41
43
44#if defined(ONIXS_CMEMDH_CXX17)
45
46using StrRef = std::basic_string_view<Char>;
47
48#else
49
55void throwInvalidIndex(size_t, size_t);
56
62class StrRef
63{
64public:
65 typedef size_t size_type;
66
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
74 constexpr StrRef()
75 : items_(nullptr)
76 , size_(0)
77 {
78 }
79
81 constexpr StrRef(const Char* chars, size_t size) noexcept
82 : items_(chars)
83 , size_((ONIXS_CMEMDH_ASSERT(size <= (std::numeric_limits<size_type>::max)()), static_cast<size_type>(size)))
84 {
85 }
86
88 StrRef(const Char* chars) noexcept
89 : items_(chars)
90 , size_(0)
91 {
92 assert(chars);
93
94 constexpr size_type maxLen = (std::numeric_limits<UInt16>::max)();
95
96 size_ = numericCast<size_type>(strnlen(chars, maxLen));
97
98 assert(size_ < maxLen);
99 }
100
102 StrRef(const std::string& string)
103 : items_(string.c_str())
104 , size_(static_cast<size_type>(string.length()))
105 {
106 assert(string.length() <= (std::numeric_limits<size_type>::max)());
107 }
108
111 bool empty() const noexcept
112 {
113 return (0 == size_);
114 }
115
118 constexpr const Char* data() const noexcept
119 {
120 return items_;
121 }
122
125 constexpr size_type size() const noexcept
126 {
127 return size_;
128 }
129
130 constexpr ONIXS_CMEMDH_NODISCARD
132 size_type
133 length() const noexcept
134 {
135 return size_;
136 }
137
140 const_iterator begin() const noexcept
141 {
142 assert(items_);
143 return items_;
144 }
145
148 const_iterator end() const noexcept
149 {
150 assert(items_);
151 return (items_ + size_);
152 }
153
156 const_iterator cbegin() const noexcept
157 {
158 return begin();
159 }
160
163 const_iterator cend() const noexcept
164 {
165 return end();
166 }
167
170 const_reverse_iterator rbegin() const noexcept
171 {
172 return const_reverse_iterator(end());
173 }
174
177 const_reverse_iterator rend() const noexcept
178 {
179 return const_reverse_iterator(begin());
180 }
181
184 const_reverse_iterator crbegin() const noexcept
185 {
186 return rbegin();
187 }
188
191 const_reverse_iterator crend() const noexcept
192 {
193 return rend();
194 }
195
198 const Char& operator[](size_type index) const noexcept
199 {
200 assert(index < size_);
201 assert(items_);
202
203 return items_[index];
204 }
205
208 const Char& at(size_type index) const
209 {
210 assert(items_);
211
212 if (index < size_)
213 return items_[index];
214
215 throwInvalidIndex(index, size_);
216 }
217
219 const Char& front() const noexcept
220 {
221 assert(!empty());
222 return (*this)[0];
223 }
224
226 const Char& back() const noexcept
227 {
228 assert(!empty());
229 return (*this)[size_ - 1];
230 }
231
233 void swap(StrRef& other) noexcept
234 {
235 std::swap(items_, other.items_);
236
237 std::swap(size_, other.size_);
238 }
239
241 operator std::basic_string<Char>() const
242 {
243 return std::basic_string<Char>(data(), size());
244 }
245
246private:
248 const Char* items_;
249
251 size_type size_;
252};
253
256inline bool operator==(const StrRef& left, const StrRef& right)
257{
258 if (left.size() != right.size())
259 return false;
260
261 if (left.empty())
262 {
263 assert(right.empty());
264 return true;
265 }
266
267 assert(left.data());
268 assert(right.data());
269
270 return (0 == memcmp(left.data(), right.data(), left.size()));
271}
272
275inline bool operator!=(const StrRef& left, const StrRef& right)
276{
277 return !(left == right);
278}
279
282inline bool operator<(const StrRef& left, const StrRef& right)
283{
284 const int result = memcmp(left.data(), right.data(), left.size() < right.size() ? left.size() : right.size());
285
286 return (0 != result ? (0 > result) : (left.size() < right.size()));
287}
288
291inline bool operator>(const StrRef& left, const StrRef& right)
292{
293 return (right < left);
294}
295
297inline std::ostream& operator<<(std::ostream& stream, const StrRef& text)
298{
299 stream.write(text.data(), text.size());
300
301 return stream;
302}
303
304ONIXS_CMEMDH_NODISCARD StrRef toStrRef(const std::string&);
305ONIXS_CMEMDH_NODISCARD StrRef toStrRef(const Char*) noexcept;
306
309inline bool operator==(const StrRef& ref, const std::string& str)
310{
311 return ref == toStrRef(str);
312}
313
316inline bool operator!=(const StrRef& ref, const std::string& str)
317{
318 return ref != toStrRef(str);
319}
320
323inline bool operator==(const std::string& str, const StrRef& ref)
324{
325 return ref == toStrRef(str);
326}
327
330inline bool operator!=(const std::string& str, const StrRef& ref)
331{
332 return ref != toStrRef(str);
333}
334
337inline bool operator==(const StrRef& ref, const Char* str)
338{
339 return ref == toStrRef(str);
340}
341
344inline bool operator!=(const StrRef& ref, const Char* str)
345{
346 return ref != toStrRef(str);
347}
348
351inline bool operator==(const Char* str, const StrRef& ref)
352{
353 return ref == toStrRef(str);
354}
355
358inline bool operator!=(const Char* str, const StrRef& ref)
359{
360 return ref != toStrRef(str);
361}
362
363#endif
364
367inline StrRef toStrRef(const std::string& str)
368{
369 return StrRef(str);
370}
371
374inline StrRef toStrRef(const Char* cStr) noexcept
375{
376 return (cStr != nullptr) ? StrRef(cStr) : StrRef();
377}
378
380inline StrRef toStrRef(const Char* begin, const Char* end) noexcept
381{
382 assert(begin <= end);
383
384 return Messaging::StrRef(begin, byteDistance(end, begin));
385}
386
387template <size_t Size>
389inline constexpr StrRef constructStrRef(const char (&value)[Size]) noexcept
390{
391 return StrRef(value, Size - 1);
392}
393
394template <size_t Size>
395ONIXS_CMEMDH_NODISCARD inline constexpr StrRef strRefFromCharArray(const char (&value)[Size]) noexcept
396{
397 return StrRef(value, Size);
398}
399
402inline std::string toStr(StrRef ref)
403{
404 return std::string(ref.data(), ref.size());
405}
406
408inline void toStr(std::string& str, StrRef ref)
409{
410 str.append(ref.data(), ref.size());
411}
412
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:57
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:58
#define ONIXS_CMEMDH_COLDPATH
Definition Compiler.h:153
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:148
#define ONIXS_CMEMDH_NORETURN
Definition Compiler.h:149
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:150
constexpr StrRef constructStrRef(const char(&value)[Size]) noexcept
Definition StrRef.h:389
bool operator!=(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:312
char Char
Character type alias.
Definition String.h:30
bool operator==(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:305
bool operator<(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:319
std::basic_string_view< Char > StrRef
Definition StrRef.h:46
constexpr StrRef strRefFromCharArray(const char(&value)[Size]) noexcept
Definition StrRef.h:395
StrRef toStrRef(const std::string &str)
Constructs a StrRef instance over th std::string content.
Definition StrRef.h:367
bool operator>(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:326
void toStr(std::string &, Int8)
Serializes given integer into a string.
std::ostream & operator<<(std::ostream &os, const Timestamp &value)
Definition Time.h:746