OnixS C++ Tradeweb Approved Publication Arrangement (APA) Handler  1.2.2.18
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 namespace OnixS
32 {
33  namespace Tradeweb
34  {
35  namespace MarketData
36  {
37  namespace Apa
38  {
39  /// Character type alias.
40  typedef char Char;
41 
42  /// Provides efficient way of accessing text-based FIX field values.
43  class StrRef
44  {
45  public:
46  /// STL-like iterator.
47  typedef const Char* Iterator;
48 
49  /// Initializes blank instance.
51  : items_(0)
52  , size_(0)
53  {
54  }
55 
56  /// Full initialization.
58  const Char* chars,
59  size_t size)
60  : items_(chars)
61  , size_(size)
62  {
63  }
64 
65  /// Initializes as clone of other instance.
66  /// Data referenced by clone is not copied.
67  /// Instead both instances will refer to same
68  /// text segment.
70  const StrRef& other)
71  : items_(other.items_)
72  , size_(other.size_)
73  {
74  }
75 
76  /// Indicates whether array of zero length.
77  bool empty() const
78  {
79  return (0 == size_);
80  }
81 
82  /// Read-only content.
83  const Char* items() const
84  {
85  return items_;
86  }
87 
88  /// Number of chars.
89  size_t size() const
90  {
91  return size_;
92  }
93 
94  /// STL-like begin().
95  Iterator begin() const
96  {
97  return items_;
98  }
99 
100  /// STL-like end().
101  Iterator end() const
102  {
103  return (items_ + size_);
104  }
105 
106  /// Resets reference to nothing.
107  void reset()
108  {
109  reset(0, 0);
110  }
111 
112  /// Updates data being referenced.
113  void
115  const Char* chars,
116  size_t size)
117  {
118  items_ = chars;
119  size_ = size;
120  }
121 
122  const Char&
124  size_t index) const
125  {
126  return items_[index];
127  }
128 
129  const Char&
130  at(
131  size_t index) const
132  {
133  if (index < size_)
134  return items_[index];
135 
136  throw std::invalid_argument("index");
137  }
138 
139  /// Reinitializes from another instance.
140  StrRef&
142  const StrRef& other)
143  {
144  items_ = other.items_;
145  size_ = other.size_;
146 
147  return *this;
148  }
149 
150  /// Swaps content with other instance.
151  void
153  StrRef& other)
154  {
155  std::swap(items_, other.items_);
156  std::swap(size_, other.size_);
157  }
158 
159  private:
160  /// Items being referenced.
161  const Char* items_;
162 
163  /// Number of chars being referenced.
164  size_t size_;
165  };
166 
167  /// Constructs StrRef instance over std::string content.
168  inline
169  StrRef
171  const std::string& str)
172  {
173  return StrRef(str.data(), str.size());
174  }
175 
176  /// Initializes instance from zero-terminated/C-like string.
177  inline
178  StrRef
180  const Char* cStr)
181  {
182  return
183  StrRef(
184  cStr,
185  cStr
186  ? strlen(cStr)
187  : 0
188  );
189  }
190 
191  /// Constructs std::string instance from StrRef one.
192  inline
193  std::string
195  const StrRef& ref)
196  {
197  return std::string(ref.items(), ref.size());
198  }
199 
200  /// Appends text referenced by StrRef to given std::string instance.
201  inline
202  void
204  std::string& str,
205  const StrRef& ref)
206  {
207  str.append("'");
208  str.append(ref.items(), ref.size());
209  str.append("'");
210  }
211 
212  /// Constructs std::string from a character.
213  inline
214  std::string
216  Char character)
217  {
218  return std::string(1, character);
219  }
220 
221  /// Appends character to given std::string instance.
222  inline
223  void
225  std::string& str,
226  Char character)
227  {
228  str.append(1, character);
229  }
230 
231  /// Appends one string another one.
232  inline
233  void
235  std::string& str,
236  const std::string& value)
237  {
238  str.append(value);
239  }
240 
241  /// Compares StrRef instance with another one.
242  inline
243  bool
245  const StrRef& left,
246  const StrRef& right)
247  {
248  return (
249  left.size() == right.size()
250  &&
251  0 == memcmp(
252  left.items(), right.items(), left.size()));
253  }
254 
255  /// Compares with another instance.
256  inline
257  bool
259  const StrRef& left,
260  const StrRef& right)
261  {
262  return !(left == right);
263  }
264 
265  /// Compares StrRef with std::string.
266  inline
267  bool
269  const StrRef& ref,
270  const std::string& str)
271  {
272  return ref == toStrRef(str);
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 std::string& str,
290  const StrRef& ref)
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 zero-terminated/C-like string.
306  inline
307  bool
309  const StrRef& ref,
310  const Char* str)
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 Char* str,
330  const StrRef& ref)
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  /// Establishes order over string refs.
346  inline
347  bool
349  const StrRef& left,
350  const StrRef& right)
351  {
352  ptrdiff_t
353  compareResult =
354  memcmp(
355  left.items(),
356  right.items(),
357  left.size() < right.size()
358  ? left.size()
359  : right.size());
360 
361  if (0 == compareResult)
362  {
363  compareResult =
364  static_cast<ptrdiff_t>(
365  left.size() - right.size());
366  }
367 
368  return (0 > compareResult);
369  }
370 
371  /// Establishes order over string refs.
372  inline
373  bool
375  const StrRef& left,
376  const StrRef& right)
377  {
378  return (right < left);
379  }
380 
381  /// StrRef serialization operator.
382  inline
383  std::ostream&
385  std::ostream& stream,
386  const StrRef& text)
387  {
388  stream.write(text.items(), text.size());
389 
390  return stream;
391  }
392 
393  }
394  }
395  }
396 }
void reset(const Char *chars, size_t size)
Updates data being referenced.
Definition: String.h:114
bool operator==(const StrRef &left, const StrRef &right)
Compares StrRef instance with another one.
Definition: String.h:244
StrRef(const Char *chars, size_t size)
Full initialization.
Definition: String.h:57
char Char
Character type alias.
Definition: String.h:40
bool empty() const
Indicates whether array of zero length.
Definition: String.h:77
const Char & at(size_t index) const
Definition: String.h:130
StrRef & operator=(const StrRef &other)
Reinitializes from another instance.
Definition: String.h:141
const Char * Iterator
STL-like iterator.
Definition: String.h:47
bool operator!=(const StrRef &left, const StrRef &right)
Compares with another instance.
Definition: String.h:258
ONIXS_TRADEWEB_APA_API bool operator>(const Decimal &l, const Decimal &r)
Iterator end() const
STL-like end().
Definition: String.h:101
ONIXS_TRADEWEB_APA_API bool operator<(const Decimal &l, const Decimal &r)
Iterator begin() const
STL-like begin().
Definition: String.h:95
const Char * items() const
Read-only content.
Definition: String.h:83
void swap(StrRef &other)
Swaps content with other instance.
Definition: String.h:152
void reset()
Resets reference to nothing.
Definition: String.h:107
ONIXS_TRADEWEB_APA_API std::ostream & operator<<(std::ostream &stream, const DataSource &ds)
const Char & operator[](size_t index) const
Definition: String.h:123
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:43
StrRef()
Initializes blank instance.
Definition: String.h:50
void toStr(std::string &str, const Decimal &value)
Definition: Decimal.h:251
size_t size() const
Number of chars.
Definition: String.h:89
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition: String.h:170