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