OnixS C++ CME MDP Premium Market Data Handler  5.8.3
API Documentation
String.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 <cstddef>
24 #include <cstring>
25 #include <string>
26 #include <stdexcept>
27 
28 #include <algorithm>
29 #include <ostream>
30 
32 
34 
35 /// Character type alias.
36 typedef char Char;
37 
38 /// Provides efficient way of accessing
39 /// text-based values without copying
40 /// content of the text being referred.
42 {
43 public:
44  /// STL-like iterator.
45  typedef const Char* Iterator;
46 
47  /// Initializes the instance referring to nothing.
49  : items_(ONIXS_CMEMDH_NULLPTR)
50  , size_(0)
51  {
52  }
53 
54  /// Explicit reference initialization.
55  StrRef(const Char* chars, size_t size)
56  : items_(chars)
57  , size_(size)
58  {
59  }
60 
61  /// Initializes as the clone of other instance.
62  /// Data referenced by the instance is not copied.
63  /// Instead both instances will refer to same text.
64  StrRef(const StrRef& other)
65  : items_(other.items_)
66  , size_(other.size_)
67  {
68  }
69 
70  /// Indicates whether the referenced text is empty.
71  bool empty() const
72  {
73  return (0 == size_);
74  }
75 
76  /// Read-only content.
77  const Char* items() const
78  {
79  return items_;
80  }
81 
82  /// Number of chars.
83  size_t size() const
84  {
85  return size_;
86  }
87 
88  /// STL-like begin().
89  Iterator begin() const
90  {
91  return items_;
92  }
93 
94  /// STL-like end().
95  Iterator end() const
96  {
97  return (items_ + size_);
98  }
99 
100  /// Resets the reference to nothing.
101  void reset()
102  {
103  reset(ONIXS_CMEMDH_NULLPTR, 0);
104  }
105 
106  /// Updates the given instance
107  /// to refer to the new text.
108  void reset(const Char* chars, size_t size)
109  {
110  items_ = chars;
111  size_ = size;
112  }
113 
114  /// Provides per-item access.
115  const Char& operator[](size_t index) const
116  {
117  return items_[index];
118  }
119 
120  /// Provides bound-checked per-item access.
121  const Char& at(size_t index) const
122  {
123  if (index < size_)
124  return items_[index];
125 
126  throw std::invalid_argument("index");
127  }
128 
129  /// Reinitializes from the other instance.
130  StrRef& operator=(const StrRef& other)
131  {
132  items_ = other.items_;
133  size_ = other.size_;
134 
135  return *this;
136  }
137 
138  /// Swaps content with the other instance.
139  void swap(StrRef& other)
140  {
141  std::swap(items_, other.items_);
142 
143  std::swap(size_, other.size_);
144  }
145 
146 private:
147  // Items being referenced.
148  const Char* items_;
149 
150  // Number of chars being referenced.
151  size_t size_;
152 };
153 
154 /// Constructs StrRef instance over std::string content.
155 inline StrRef toStrRef(const std::string& str)
156 {
157  return StrRef(str.data(), str.size());
158 }
159 
160 /// Initializes instance from zero-terminated/C-like string.
161 inline StrRef toStrRef(const Char* cStr)
162 {
163  return StrRef(cStr, cStr ? strlen(cStr) : 0);
164 }
165 
166 /// Constructs std::string instance from StrRef one.
167 inline std::string toStr(const StrRef& ref)
168 {
169  return std::string(ref.items(), ref.size());
170 }
171 
172 /// Appends text referenced by StrRef to given std::string instance.
173 inline void toStr(std::string& str, const StrRef& ref)
174 {
175  str.append(ref.items(), ref.size());
176 }
177 
178 /// Constructs std::string from a character.
179 inline std::string toStr(Char character)
180 {
181  return std::string(1, character);
182 }
183 
184 /// Appends character to given std::string instance.
185 inline void toStr(std::string& str, Char character)
186 {
187  str.append(1, character);
188 }
189 
190 /// Appends one string another one.
191 inline void toStr(std::string& str, const std::string& value)
192 {
193  str.append(value);
194 }
195 
196 /// Compares StrRef instance with another one.
197 inline bool operator==(const StrRef& left, const StrRef& right)
198 {
199  return (left.size() == right.size() && 0 == memcmp(left.items(), right.items(), left.size()));
200 }
201 
202 /// Compares with another instance.
203 inline bool operator!=(const StrRef& left, const StrRef& right)
204 {
205  return !(left == right);
206 }
207 
208 /// Compares StrRef with std::string.
209 inline bool operator==(const StrRef& ref, const std::string& str)
210 {
211  return ref == toStrRef(str);
212 }
213 
214 /// Compares StrRef with std::string.
215 inline bool operator!=(const StrRef& ref, const std::string& str)
216 {
217  return ref != toStrRef(str);
218 }
219 
220 /// Compares StrRef with std::string.
221 inline bool operator==(const std::string& str, const StrRef& ref)
222 {
223  return ref == toStrRef(str);
224 }
225 
226 /// Compares StrRef with std::string.
227 inline bool operator!=(const std::string& str, const StrRef& ref)
228 {
229  return ref != toStrRef(str);
230 }
231 
232 /// Compares StrRef with zero-terminated/C-like string.
233 inline bool operator==(const StrRef& ref, const Char* str)
234 {
235  return ref == toStrRef(str);
236 }
237 
238 /// Compares StrRef with zero-terminated/C-like string.
239 inline bool operator!=(const StrRef& ref, const Char* str)
240 {
241  return ref != toStrRef(str);
242 }
243 
244 /// Compares StrRef with zero-terminated/C-like string.
245 inline bool operator==(const Char* str, const StrRef& ref)
246 {
247  return ref == toStrRef(str);
248 }
249 
250 /// Compares StrRef with zero-terminated/C-like string.
251 inline bool operator!=(const Char* str, const StrRef& ref)
252 {
253  return ref != toStrRef(str);
254 }
255 
256 /// Establishes order over string refs.
257 inline bool operator<(const StrRef& left, const StrRef& right)
258 {
259  const int result = memcmp(left.items(), right.items(), left.size() < right.size() ? left.size() : right.size());
260 
261  return (0 != result ? (0 > result) : (left.size() < right.size()));
262 }
263 
264 /// Establishes order over string refs.
265 inline bool operator>(const StrRef& left, const StrRef& right)
266 {
267  return (right < left);
268 }
269 
270 /// StrRef serialization operator.
271 inline std::ostream& operator<<(std::ostream& stream, const StrRef& text)
272 {
273  stream.write(text.items(), text.size());
274 
275  return stream;
276 }
277 
Iterator begin() const
STL-like begin().
Definition: String.h:89
StrRef()
Initializes the instance referring to nothing.
Definition: String.h:48
void swap(StrRef &other)
Swaps content with the other instance.
Definition: String.h:139
bool empty() const
Indicates whether the referenced text is empty.
Definition: String.h:71
#define ONIXS_CMEMDH_NULLPTR
Definition: Compiler.h:145
void toStr(std::string &str, const std::string &value)
Appends one string another one.
Definition: String.h:191
const Char * Iterator
STL-like iterator.
Definition: String.h:45
StrRef toStrRef(const Char *cStr)
Initializes instance from zero-terminated/C-like string.
Definition: String.h:161
std::ostream & operator<<(std::ostream &stream, const StrRef &text)
StrRef serialization operator.
Definition: String.h:271
const Char & at(size_t index) const
Provides bound-checked per-item access.
Definition: String.h:121
bool operator!=(const Char *str, const StrRef &ref)
Compares StrRef with zero-terminated/C-like string.
Definition: String.h:251
bool operator==(const Char *str, const StrRef &ref)
Compares StrRef with zero-terminated/C-like string.
Definition: String.h:245
bool operator<(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:257
#define ONIXS_CMEMDH_LTWT
Definition: Bootstrap.h:46
const Char & operator[](size_t index) const
Provides per-item access.
Definition: String.h:115
char Char
Character type alias.
Definition: String.h:36
bool value(Number &number, const MultiContainer &container, Tag tag)
Finds a tag-value entry in the given collection by the given tag and returns its value component tran...
void reset(const Char *chars, size_t size)
Updates the given instance to refer to the new text.
Definition: String.h:108
const Char * items() const
Read-only content.
Definition: String.h:77
Provides efficient way of accessing text-based values without copying content of the text being refer...
Definition: String.h:41
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
StrRef(const StrRef &other)
Initializes as the clone of other instance.
Definition: String.h:64
bool operator>(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:265
StrRef & operator=(const StrRef &other)
Reinitializes from the other instance.
Definition: String.h:130
Iterator end() const
STL-like end().
Definition: String.h:95
size_t size() const
Number of chars.
Definition: String.h:83
void reset()
Resets the reference to nothing.
Definition: String.h:101
StrRef(const Char *chars, size_t size)
Explicit reference initialization.
Definition: String.h:55
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68