OnixS C++ CME MDP Streamlined Market Data Handler  1.2.0
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 text-based FIX field values.
40 {
41 public:
42  /// STL-like iterator.
43  typedef const Char* Iterator;
44 
45  /// Initializes blank instance.
47  : items_(0)
48  , size_(0)
49  {
50  }
51 
52  /// Full initialization.
54  const Char* chars,
55  size_t size)
56  : items_(chars)
57  , size_(size)
58  {
59  }
60 
61  /// Initializes as clone of other instance.
62  /// Data referenced by clone is not copied.
63  /// Instead both instances will refer to same
64  /// text segment.
66  const StrRef& other)
67  : items_(other.items_)
68  , size_(other.size_)
69  {
70  }
71 
72  /// Indicates whether array of zero length.
73  bool empty() const
74  {
75  return (0 == size_);
76  }
77 
78  /// Read-only content.
79  const Char* items() const
80  {
81  return items_;
82  }
83 
84  /// Number of chars.
85  size_t size() const
86  {
87  return size_;
88  }
89 
90  /// STL-like begin().
91  Iterator begin() const
92  {
93  return items_;
94  }
95 
96  /// STL-like end().
97  Iterator end() const
98  {
99  return (items_ + size_);
100  }
101 
102  /// Resets reference to nothing.
103  void reset()
104  {
105  reset(0, 0);
106  }
107 
108  /// Updates data being referenced.
109  void
111  const Char* chars,
112  size_t size)
113  {
114  items_ = chars;
115  size_ = size;
116  }
117 
118  const Char&
119  operator [](
120  size_t index) const
121  {
122  return items_[index];
123  }
124 
125  const Char&
126  at(
127  size_t index) const
128  {
129  if (index < size_)
130  return items_[index];
131 
132  throw std::invalid_argument("index");
133  }
134 
135  /// Reinitializes from another instance.
136  StrRef&
137  operator =(
138  const StrRef& other)
139  {
140  items_ = other.items_;
141  size_ = other.size_;
142 
143  return *this;
144  }
145 
146  /// Swaps content with other instance.
147  void
149  StrRef& other)
150  {
151  std::swap(items_, other.items_);
152  std::swap(size_, other.size_);
153  }
154 
155 private:
156  /// Items being referenced.
157  const Char* items_;
158 
159  /// Number of chars being referenced.
160  size_t size_;
161 };
162 
163 /// Constructs StrRef instance over std::string content.
164 inline
165 StrRef
167  const std::string& str)
168 {
169  return StrRef(str.data(), str.size());
170 }
171 
172 /// Initializes instance from zero-terminated/C-like string.
173 inline
174 StrRef
176  const Char* cStr)
177 {
178  return
179  StrRef(
180  cStr,
181  cStr
182  ? strlen(cStr)
183  : 0
184  );
185 }
186 
187 /// Constructs std::string instance from StrRef one.
188 inline
189 std::string
191  const StrRef& ref)
192 {
193  return std::string(ref.items(), ref.size());
194 }
195 
196 /// Appends text referenced by StrRef to given std::string instance.
197 inline
198 void
200  std::string& str,
201  const StrRef& ref)
202 {
203  str.append(ref.items(), ref.size());
204 }
205 
206 /// Constructs std::string from a character.
207 inline
208 std::string
210  Char character)
211 {
212  return std::string(1, character);
213 }
214 
215 /// Appends character to given std::string instance.
216 inline
217 void
219  std::string& str,
220  Char character)
221 {
222  str.append(1, character);
223 }
224 
225 /// Appends one string another one.
226 inline
227 void
229  std::string& str,
230  const std::string& value)
231 {
232  str.append(value);
233 }
234 
235 /// Compares StrRef instance with another one.
236 inline
237 bool
239  const StrRef& left,
240  const StrRef& right)
241 {
242  return (
243  left.size() == right.size()
244  &&
245  0 == memcmp(
246  left.items(), right.items(), left.size()));
247 }
248 
249 /// Compares with another instance.
250 inline
251 bool
253  const StrRef& left,
254  const StrRef& right)
255 {
256  return !(left == right);
257 }
258 
259 /// Compares StrRef with std::string.
260 inline
261 bool
263  const StrRef& ref,
264  const std::string& str)
265 {
266  return ref == toStrRef(str);
267 }
268 
269 /// Compares StrRef with std::string.
270 inline
271 bool
273  const StrRef& ref,
274  const std::string& str)
275 {
276  return ref != toStrRef(str);
277 }
278 
279 /// Compares StrRef with std::string.
280 inline
281 bool
283  const std::string& str,
284  const StrRef& ref)
285 {
286  return ref == toStrRef(str);
287 }
288 
289 /// Compares StrRef with std::string.
290 inline
291 bool
293  const std::string& str,
294  const StrRef& ref)
295 {
296  return ref != toStrRef(str);
297 }
298 
299 /// Compares StrRef with zero-terminated/C-like string.
300 inline
301 bool
303  const StrRef& ref,
304  const Char* str)
305 {
306  return ref == toStrRef(str);
307 }
308 
309 /// Compares StrRef with zero-terminated/C-like string.
310 inline
311 bool
313  const StrRef& ref,
314  const Char* str)
315 {
316  return ref != toStrRef(str);
317 }
318 
319 /// Compares StrRef with zero-terminated/C-like string.
320 inline
321 bool
323  const Char* str,
324  const StrRef& ref)
325 {
326  return ref == toStrRef(str);
327 }
328 
329 /// Compares StrRef with zero-terminated/C-like string.
330 inline
331 bool
333  const Char* str,
334  const StrRef& ref)
335 {
336  return ref != toStrRef(str);
337 }
338 
339 /// Establishes order over string refs.
340 inline
341 bool
343  const StrRef& left,
344  const StrRef& right)
345 {
346  ptrdiff_t
347  compareResult =
348  memcmp(
349  left.items(),
350  right.items(),
351  left.size() < right.size()
352  ? left.size()
353  : right.size());
354 
355  if (0 == compareResult)
356  {
357  compareResult =
358  static_cast<ptrdiff_t>(
359  left.size() - right.size());
360  }
361 
362  return (0 > compareResult);
363 }
364 
365 /// Establishes order over string refs.
366 inline
367 bool
369  const StrRef& left,
370  const StrRef& right)
371 {
372  return (right < left);
373 }
374 
375 /// StrRef serialization operator.
376 inline
377 std::ostream&
379  std::ostream& stream,
380  const StrRef& text)
381 {
382  stream.write(text.items(), text.size());
383 
384  return stream;
385 }
386 
void toStr(std::string &str, const std::string &value)
Appends one string another one.
Definition: String.h:228
bool operator>(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:368
void reset()
Resets reference to nothing.
Definition: String.h:103
void swap(StrRef &other)
Swaps content with other instance.
Definition: String.h:148
Iterator end() const
STL-like end().
Definition: String.h:97
bool operator!=(const Char *str, const StrRef &ref)
Compares StrRef with zero-terminated/C-like string.
Definition: String.h:332
size_t size() const
Number of chars.
Definition: String.h:85
StrRef toStrRef(const Char *cStr)
Initializes instance from zero-terminated/C-like string.
Definition: String.h:175
StrRef(const StrRef &other)
Initializes as clone of other instance.
Definition: String.h:65
StrRef()
Initializes blank instance.
Definition: String.h:46
bool operator<(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:342
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition: Bootstrap.h:173
#define ONIXS_CMESTREAMLINEDMDH_LTWT_CLASS
Definition: Bootstrap.h:111
char Char
Character type alias.
Definition: String.h:36
void reset(const Char *chars, size_t size)
Updates data being referenced.
Definition: String.h:110
std::ostream & operator<<(std::ostream &stream, const StrRef &text)
StrRef serialization operator.
Definition: String.h:378
const Char * Iterator
STL-like iterator.
Definition: String.h:43
const Char & at(size_t index) const
Definition: String.h:126
bool empty() const
Indicates whether array of zero length.
Definition: String.h:73
bool operator==(const Char *str, const StrRef &ref)
Compares StrRef with zero-terminated/C-like string.
Definition: String.h:322
const Char * items() const
Read-only content.
Definition: String.h:79
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:39
StrRef(const Char *chars, size_t size)
Full initialization.
Definition: String.h:53
Iterator begin() const
STL-like begin().
Definition: String.h:91
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:169