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