OnixS C++ Cboe CFE Multicast PITCH Market Data Handler  1.12.1
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 CboeCFE
36  {
37  namespace MarketData
38  {
39  namespace Pitch
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_BATS_PITCH_NULLPTR)
55  , size_(0)
56  {
57  }
58 
59  /// Full initialization.
61  const Char* chars,
62  size_t size)
64  : items_(chars)
65  , size_(size)
66  {
67  }
68 
69  template <size_t Size>
70  explicit
72  StrRef(const char (&value)[Size])
74  : items_(value)
75  , size_(Size - 1)
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  /// Indicates whether array of zero length.
92  bool empty() const
94  {
95  return (0 == size_);
96  }
97 
98  /// Read-only content.
99  const Char* items() const
101  {
102  return items_;
103  }
104 
105  /// Number of chars.
106  size_t size() const
108  {
109  return size_;
110  }
111 
112  /// STL-like begin().
113  Iterator begin() const
115  {
116  return items_;
117  }
118 
119  /// STL-like end().
120  Iterator end() const
122  {
123  return (items_ + size_);
124  }
125 
126  /// Resets reference to nothing.
127  void reset()
129  {
131  }
132 
133  /// Updates data being referenced.
134  void
136  const Char* chars,
137  size_t size)
139  {
140  items_ = chars;
141  size_ = size;
142  }
143 
144  const Char&
146  size_t index) const
147  {
148  return items_[index];
149  }
150 
151  const Char&
152  at(
153  size_t index) const
154  {
155  if (index < size_)
156  return items_[index];
157 
158  throw std::invalid_argument("index");
159  }
160 
161  /// Reinitializes from another instance.
162  StrRef&
164  const StrRef& other)
166  {
167  items_ = other.items_;
168  size_ = other.size_;
169 
170  return *this;
171  }
172 
173  /// Swaps content with other instance.
174  void
176  StrRef& other)
178  {
179  std::swap(items_, other.items_);
180  std::swap(size_, other.size_);
181  }
182 
183 
184  // Get trimmed presentation of the current string
185  StrRef trim() const
187  {
188  size_t length = size();
189 
190  while(length > 0 && isspace(operator[](length - 1)))
191  --length;
192 
193  return StrRef(items(), length);
194  }
195 
196  private:
197  /// Items being referenced.
198  const Char* items_;
199 
200  /// Number of chars being referenced.
201  size_t size_;
202  };
203 
204  /// Constructs StrRef instance over std::string content.
205  inline
206  StrRef
208  const std::string& str)
209  {
210  return StrRef(str.data(), str.size());
211  }
212 
213  /// Initializes instance from zero-terminated/C-like string.
214  inline
215  StrRef
217  const Char* cStr)
218  {
219  return
220  StrRef(
221  cStr,
222  cStr
223  ? strlen(cStr)
224  : 0
225  );
226  }
227 
228  /// Constructs std::string instance from StrRef one.
229  inline
230  std::string
232  const StrRef& ref)
233  {
234  return std::string(ref.items(), ref.size());
235  }
236 
237  /// Appends text referenced by StrRef to given std::string instance.
238  inline
239  void
241  std::string& str,
242  const StrRef& ref)
243  {
244  str.append(ref.items(), ref.size());
245  }
246 
247  /// Constructs std::string from a character.
248  inline
249  std::string
251  Char character)
252  {
253  return std::string(1, character);
254  }
255 
256  /// Appends character to given std::string instance.
257  inline
258  void
260  std::string& str,
261  Char character)
262  {
263  str.append(1, character);
264  }
265 
266  /// Appends one string another one.
267  inline
268  void
270  std::string& str,
271  const std::string& value)
272  {
273  str.append(value);
274  }
275 
276  /// Compares StrRef instance with another one.
277  inline
278  bool
280  const StrRef& left,
281  const StrRef& right)
282  {
283  return (
284  left.size() == right.size()
285  &&
286  0 == memcmp(
287  left.items(), right.items(), left.size()));
288  }
289 
290  /// Compares with another instance.
291  inline
292  bool
294  const StrRef& left,
295  const StrRef& right)
296  {
297  return !(left == right);
298  }
299 
300  /// Compares StrRef with std::string.
301  inline
302  bool
304  const StrRef& ref,
305  const std::string& str)
306  {
307  return ref == toStrRef(str);
308  }
309 
310  /// Compares StrRef with std::string.
311  inline
312  bool
314  const StrRef& ref,
315  const std::string& str)
316  {
317  return ref != toStrRef(str);
318  }
319 
320  /// Compares StrRef with std::string.
321  inline
322  bool
324  const std::string& str,
325  const StrRef& ref)
326  {
327  return ref == toStrRef(str);
328  }
329 
330  /// Compares StrRef with std::string.
331  inline
332  bool
334  const std::string& str,
335  const StrRef& ref)
336  {
337  return ref != toStrRef(str);
338  }
339 
340  /// Compares StrRef with zero-terminated/C-like string.
341  inline
342  bool
344  const StrRef& ref,
345  const Char* str)
346  {
347  return ref == toStrRef(str);
348  }
349 
350  /// Compares StrRef with zero-terminated/C-like string.
351  inline
352  bool
354  const StrRef& ref,
355  const Char* str)
356  {
357  return ref != toStrRef(str);
358  }
359 
360  /// Compares StrRef with zero-terminated/C-like string.
361  inline
362  bool
364  const Char* str,
365  const StrRef& ref)
366  {
367  return ref == toStrRef(str);
368  }
369 
370  /// Compares StrRef with zero-terminated/C-like string.
371  inline
372  bool
374  const Char* str,
375  const StrRef& ref)
376  {
377  return ref != toStrRef(str);
378  }
379 
380  /// Establishes order over string refs.
381  inline
382  bool
384  const StrRef& left,
385  const StrRef& right)
386  {
387  ptrdiff_t
388  compareResult =
389  memcmp(
390  left.items(),
391  right.items(),
392  left.size() < right.size()
393  ? left.size()
394  : right.size());
395 
396  if (0 == compareResult)
397  {
398  compareResult =
399  static_cast<ptrdiff_t>(
400  left.size() - right.size());
401  }
402 
403  return (0 > compareResult);
404  }
405 
406  /// Establishes order over string refs.
407  inline
408  bool
410  const StrRef& left,
411  const StrRef& right)
412  {
413  return (right < left);
414  }
415 
416  /// StrRef serialization operator.
417  inline
418  std::ostream&
420  std::ostream& stream,
421  const StrRef& text)
422  {
423  stream.write(text.items(), text.size());
424 
425  return stream;
426  }
427 
428  }
429  }
430  }
431 }
const Char * items() const ONIXS_BATS_PITCH_NOTHROW
Read-only content.
Definition: String.h:99
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:144
bool empty() const ONIXS_BATS_PITCH_NOTHROW
Indicates whether array of zero length.
Definition: String.h:92
StrRef(const StrRef &other) ONIXS_BATS_PITCH_NOTHROW
Definition: String.h:83
StrRef trim() const ONIXS_BATS_PITCH_NOTHROW
Definition: String.h:185
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:126
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:45
void reset(const Char *chars, size_t size) ONIXS_BATS_PITCH_NOTHROW
Updates data being referenced.
Definition: String.h:135
#define ONIXS_BATS_PITCH_NULLPTR
Definition: Compiler.h:121
const Char & operator[](size_t index) const
Definition: String.h:145
Iterator end() const ONIXS_BATS_PITCH_NOTHROW
STL-like end().
Definition: String.h:120
void reset() ONIXS_BATS_PITCH_NOTHROW
Resets reference to nothing.
Definition: String.h:127
const Char * Iterator
STL-like iterator.
Definition: String.h:49
char Char
Character type alias.
Definition: String.h:42
ONIXS_CBOECFE_PITCH_API std::ostream & operator<<(std::ostream &stream, const ServiceDescriptor &descriptor)
const Char & at(size_t index) const
Definition: String.h:152
#define ONIXS_BATS_PITCH_CONSTEXPR
Definition: Compiler.h:113
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:135
StrRef & operator=(const StrRef &other) ONIXS_BATS_PITCH_NOTHROW
Reinitializes from another instance.
Definition: String.h:163
size_t size() const ONIXS_BATS_PITCH_NOTHROW
Number of chars.
Definition: String.h:106
StrRef() ONIXS_BATS_PITCH_NOTHROW
Initializes blank instance.
Definition: String.h:52
void swap(StrRef &other) ONIXS_BATS_PITCH_NOTHROW
Swaps content with other instance.
Definition: String.h:175
bool operator==(const PriceLevel &l, const PriceLevel &r) ONIXS_BATS_PITCH_NOTHROW
compare
Definition: OrderBook.h:327
StrRef(const Char *chars, size_t size) ONIXS_BATS_PITCH_NOTHROW
Full initialization.
Definition: String.h:60
#define ONIXS_BATS_PITCH_NOTHROW
Definition: Compiler.h:107
ONIXS_BATS_PITCH_CONSTEXPR StrRef(const char(&value)[Size]) ONIXS_BATS_PITCH_NOTHROW
Definition: String.h:72
ONIXS_CBOECFE_PITCH_API void toStr(std::string &, SideIndicator::Enum)
Appends string presentation of object.
Iterator begin() const ONIXS_BATS_PITCH_NOTHROW
STL-like begin().
Definition: String.h:113
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition: String.h:207