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