OnixS C++ eSpeed ITCH Market Data Handler  1.7.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 
33 
35 
36 /// Character type alias.
37 typedef char Char;
38 
39 /// Provides efficient way of accessing text-based FIX field values.
40 class StrRef
41 {
42 public:
43  /// STL-like iterator.
44  typedef const Char* Iterator;
45 
46  /// Initializes blank instance.
47  ONIXS_ESPEED_ITCH_CONSTEXPR
50  : items_(ONIXS_ESPEED_ITCH_NULLPTR)
51  , size_(0)
52  {
53  }
54 
55  /// Full initialization.
57  const Char* chars,
58  size_t size)
60  : items_(chars)
61  , size_(size)
62  {
63  }
64 
65  /// Full initialization.
66  template <size_t Size>
67  explicit
68  ONIXS_ESPEED_ITCH_CONSTEXPR
69  StrRef(const char (&value)[Size])
71  : items_(value)
72  , size_(Size - 1)
73  {
74  }
75 
76  /// Full initialization.
77  explicit
79  const std::string& string)
81  : items_(string.c_str())
82  , size_(string.size())
83  {
84  }
85 
86  /// Initializes as clone of other instance.
87  /// Data referenced by clone is not copied.
88  /// Instead both instances will refer to same
89  /// text segment.
91  const StrRef& other)
93  : items_(other.items_)
94  , size_(other.size_)
95  {
96  }
97 
98 #if defined(ONIXS_ESPEED_ITCH_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_ESPEED_ITCH_COMPILER_CXX_RVALUE_REFERENCES
99 
100  ONIXS_ESPEED_ITCH_CONSTEXPR
101  StrRef(
102  StrRef&& other)
104  : items_(std::move(other.items_))
105  , size_(std::move(other.size_))
106  {
107  }
108 
109  StrRef& operator= (StrRef&& other)
111  {
112  if (this == &other)
113  return *this;
114 
115  items_ = std::move(other.items_);
116  size_ = std::move(other.size_);
117 
118  return *this;
119  }
120 
121 #endif
122 
123  /// Indicates whether array of zero length.
124  bool empty() const
126  {
127  return (0 == size_);
128  }
129 
130  /// Read-only content.
131  const Char* items() const
133  {
134  return items_;
135  }
136 
137  /// Number of chars.
138  size_t size() const
140  {
141  return size_;
142  }
143 
144  /// STL-like begin().
145  Iterator begin() const
147  {
148  return items_;
149  }
150 
151  /// STL-like end().
152  Iterator end() const
154  {
155  return (items_ + size_);
156  }
157 
158  /// Resets reference to nothing.
159  void reset()
161  {
162  reset(ONIXS_ESPEED_ITCH_NULLPTR, 0);
163  }
164 
165  /// Updates data being referenced.
166  void
168  const Char* chars,
169  size_t size)
171  {
172  items_ = chars;
173  size_ = size;
174  }
175 
176  const Char&
178  size_t index) const
179  {
180  return items_[index];
181  }
182 
183  const Char&
184  at(
185  size_t index) const
186  {
187  if (index < size_)
188  return items_[index];
189 
190  throw std::invalid_argument("index");
191  }
192 
193  /// Reinitializes from another instance.
194  StrRef&
196  const StrRef& other)
198  {
199  items_ = other.items_;
200  size_ = other.size_;
201 
202  return *this;
203  }
204 
205  /// Swaps content with other instance.
206  void
208  StrRef& other)
210  {
211  std::swap(items_, other.items_);
212  std::swap(size_, other.size_);
213  }
214 
215  // Get trimmed presentation of the current string
216  StrRef trim() const
218  {
219  size_t length = size();
220 
221  while(length > 0 && isspace(operator[](length - 1)))
222  --length;
223 
224  return StrRef(items(), length);
225  }
226 
227 private:
228  /// Items being referenced.
229  const Char* items_;
230 
231  /// Number of chars being referenced.
232  size_t size_;
233 };
234 
235 /// Constructs StrRef instance over std::string content.
236 inline
237  StrRef
239  const std::string& str)
240 {
241  return StrRef(str.data(), str.size());
242 }
243 
244 /// Initializes instance from zero-terminated/C-like string.
245 inline
246  StrRef
248  const Char* cStr)
249 {
250  return
251  StrRef(
252  cStr,
253  cStr
254  ? strlen(cStr)
255  : 0
256  );
257 }
258 
259 /// Constructs std::string instance from StrRef one.
260 inline
261  std::string
263  const StrRef& ref)
264 {
265  return std::string(ref.items(), ref.size());
266 }
267 
268 /// Appends text referenced by StrRef to given std::string instance.
269 inline
270  void
272  std::string& str,
273  const StrRef& ref)
274 {
275  str.append(ref.items(), ref.size());
276 }
277 
278 /// Constructs std::string from a character.
279 inline
280  std::string
282  Char character)
283 {
284  return std::string(1, character);
285 }
286 
287 /// Appends character to given std::string instance.
288 inline
289  void
291  std::string& str,
292  Char character)
293 {
294  str.append(1, character);
295 }
296 
297 /// Appends one string another one.
298 inline
299  void
301  std::string& str,
302  const std::string& value)
303 {
304  str.append(value);
305 }
306 
307 /// Compares StrRef instance with another one.
308 inline
309  bool
311  const StrRef& left,
312  const StrRef& right)
313 {
314  return (
315  left.size() == right.size()
316  &&
317  0 == memcmp(
318  left.items(), right.items(), left.size()));
319 }
320 
321 /// Compares with another instance.
322 inline
323  bool
325  const StrRef& left,
326  const StrRef& right)
327 {
328  return !(left == right);
329 }
330 
331 /// Compares StrRef with std::string.
332 inline
333  bool
335  const StrRef& ref,
336  const std::string& str)
337 {
338  return ref == toStrRef(str);
339 }
340 
341 /// Compares StrRef with std::string.
342 inline
343  bool
345  const StrRef& ref,
346  const std::string& str)
347 {
348  return ref != toStrRef(str);
349 }
350 
351 /// Compares StrRef with std::string.
352 inline
353  bool
355  const std::string& str,
356  const StrRef& ref)
357 {
358  return ref == toStrRef(str);
359 }
360 
361 /// Compares StrRef with std::string.
362 inline
363  bool
365  const std::string& str,
366  const StrRef& ref)
367 {
368  return ref != toStrRef(str);
369 }
370 
371 /// Compares StrRef with zero-terminated/C-like string.
372 inline
373  bool
375  const StrRef& ref,
376  const Char* str)
377 {
378  return ref == toStrRef(str);
379 }
380 
381 /// Compares StrRef with zero-terminated/C-like string.
382 inline
383  bool
385  const StrRef& ref,
386  const Char* str)
387 {
388  return ref != toStrRef(str);
389 }
390 
391 /// Compares StrRef with zero-terminated/C-like string.
392 inline
393  bool
395  const Char* str,
396  const StrRef& ref)
397 {
398  return ref == toStrRef(str);
399 }
400 
401 /// Compares StrRef with zero-terminated/C-like string.
402 inline
403  bool
405  const Char* str,
406  const StrRef& ref)
407 {
408  return ref != toStrRef(str);
409 }
410 
411 /// Establishes order over string refs.
412 inline
413  bool
415  const StrRef& left,
416  const StrRef& right)
417 {
418  ptrdiff_t
419  compareResult =
420  memcmp(
421  left.items(),
422  right.items(),
423  left.size() < right.size()
424  ? left.size()
425  : right.size());
426 
427  if (0 == compareResult)
428  {
429  compareResult =
430  static_cast<ptrdiff_t>(
431  left.size() - right.size());
432  }
433 
434  return (0 > compareResult);
435 }
436 
437 /// Establishes order over string refs.
438 inline
439  bool
441  const StrRef& left,
442  const StrRef& right)
443 {
444  return (right < left);
445 }
446 
447 /// StrRef serialization operator.
448 inline
449  std::ostream&
451  std::ostream& stream,
452  const StrRef& text)
453 {
454  stream.write(text.items(), text.size());
455 
456  return stream;
457 }
458 
#define ONIXS_ESPEED_ITCH_NAMESPACE_END
Definition: Bootstrap.h:31
bool operator<(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:414
#define ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN
Definition: Bootstrap.h:27
void swap(StrRef &other) ONIXS_ESPEED_ITCH_NOTHROW
Swaps content with other instance.
Definition: String.h:207
std::ostream & operator<<(std::ostream &stream, const StrRef &text)
StrRef serialization operator.
Definition: String.h:450
StrRef trim() const ONIXS_ESPEED_ITCH_NOTHROW
Definition: String.h:216
void reset() ONIXS_ESPEED_ITCH_NOTHROW
Resets reference to nothing.
Definition: String.h:159
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition: String.h:238
std::string toStr(const StrRef &ref)
Constructs std::string instance from StrRef one.
Definition: String.h:262
bool operator!=(const StrRef &left, const StrRef &right)
Compares with another instance.
Definition: String.h:324
const Char * items() const ONIXS_ESPEED_ITCH_NOTHROW
Read-only content.
Definition: String.h:131
ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN typedef char Char
Character type alias.
Definition: String.h:37
bool operator>(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:440
const Char & operator[](size_t index) const
Definition: String.h:177
bool empty() const ONIXS_ESPEED_ITCH_NOTHROW
Indicates whether array of zero length.
Definition: String.h:124
const Char & at(size_t index) const
Definition: String.h:184
ONIXS_ESPEED_ITCH_CONSTEXPR StrRef(const char(&value)[Size]) ONIXS_ESPEED_ITCH_NOTHROW
Full initialization.
Definition: String.h:69
size_t size() const ONIXS_ESPEED_ITCH_NOTHROW
Number of chars.
Definition: String.h:138
ONIXS_ESPEED_ITCH_CONSTEXPR StrRef() ONIXS_ESPEED_ITCH_NOTHROW
Initializes blank instance.
Definition: String.h:48
Iterator begin() const ONIXS_ESPEED_ITCH_NOTHROW
STL-like begin().
Definition: String.h:145
StrRef(const std::string &string) ONIXS_ESPEED_ITCH_NOTHROW
Full initialization.
Definition: String.h:78
void reset(const Char *chars, size_t size) ONIXS_ESPEED_ITCH_NOTHROW
Updates data being referenced.
Definition: String.h:167
StrRef(const Char *chars, size_t size) ONIXS_ESPEED_ITCH_NOTHROW
Full initialization.
Definition: String.h:56
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:40
Iterator end() const ONIXS_ESPEED_ITCH_NOTHROW
STL-like end().
Definition: String.h:152
#define ONIXS_ESPEED_ITCH_NOTHROW
Definition: Compiler.h:27
bool operator==(const StrRef &left, const StrRef &right)
Compares StrRef instance with another one.
Definition: String.h:310
StrRef(const StrRef &other) ONIXS_ESPEED_ITCH_NOTHROW
Definition: String.h:90
const Char * Iterator
STL-like iterator.
Definition: String.h:44
StrRef & operator=(const StrRef &other) ONIXS_ESPEED_ITCH_NOTHROW
Reinitializes from another instance.
Definition: String.h:195