OnixS C++ FIX Engine  4.12.0
API Documentation
StringRef.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
4 *
5 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
6 * and international copyright treaties.
7 *
8 * Access to and use of the software is governed by the terms of the applicable OnixS Software
9 * Services Agreement (the Agreement) and Customer end user license agreements granting
10 * a non-assignable, non-transferable and non-exclusive license to use the software
11 * for it's own data processing purposes under the terms defined in the Agreement.
12 *
13 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
14 * of this source code or associated reference material to any other location for further reproduction
15 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
16 *
17 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
18 * the terms of the Agreement is a violation of copyright law.
19 */
20 
21 #include <cstring>
22 #include <functional>
23 #include <ostream>
24 #include <string>
25 
28 
29 #ifdef ONIXS_FIXENGINE_CXX17
30 
31 #include <string_view>
32 
33 #endif
34 
35 namespace OnixS {
36 namespace FIX {
37 
38 #ifdef ONIXS_FIXENGINE_CXX17
39 
40 using StringRef = std::basic_string_view<Char>;
41 
42 #else
43 
44 /// Provides an efficient way of accessing text-based FIX field values.
45 ///
46 /// By default, OnixS::FIX::Message and OnixS::FIX::GroupInstance classes
47 /// convert all values assigned to the fields into their text presentations
48 /// and store textual data. This approach optimizes message reuse when the same
49 /// message instance is used to update a few fields and is sent over the FIX
50 /// session multiple times. Also, when a message comes from the network and is
51 /// deserialized from its raw (tag=value) presentation, all field values
52 /// represent text segments. To access textual values, the StringRef class
53 /// was added together with new access members (FieldSet::getStringRef()).
54 /// If value is accessed using FieldSet::getStringRef() member, instance of
55 /// StringRef class is returned. That instance holds a reference to a read-only
56 /// segment of text. An important aspect is that no memory is allocated and
57 /// copied while accessing field values that way.
58 
59 class
61  StringRef
62 {
63 public:
64  /// An immutable iterator over chars.
65  typedef const char * ConstIterator;
66 
67  /// Initializes the blank instance.
69  : chars_(ONIXS_FIXENGINE_NULLPTR),
70  size_(0) {
71  }
72 
73  /// The full initialization.
74  ONIXS_FIXENGINE_CONSTEXPR
76  const char * chars,
77  size_t size)
78  : chars_(chars),
79  size_(size) {
80  }
81 
82  /// Initializes an instance from the string content.
83  explicit StringRef(
84  const std::string & stdStr)
85  : chars_(stdStr.c_str()),
86  size_(stdStr.size()) {
87  }
88 
89  /// Initializes an instance from the zero-terminated string.
90  StringRef(const char * cStr)
91  : chars_(cStr),
92  size_(cStr ? strlen(cStr) : 0) {
93  }
94 
95  /// Indicates whether the array has zero length.
96  bool empty() const {
97  return (0 == size_);
98  }
99 
100  /// Returns the read-only content.
101  const char * data() const {
102  return chars_;
103  }
104 
105  /// The STL-like begin().
106  ConstIterator begin() const {
107  return chars_;
108  }
109 
110  /// The STL-like end().
111  ConstIterator end() const {
112  return chars_ + size_;
113  }
114 
115  /// The number of chars.
116  size_t size() const {
117  return size_;
118  }
119 
120  /// The number of chars.
121  size_t length() const {
122  return size_;
123  }
124 
125  /// Resets the reference to nothing.
126  void reset() {
127  reset(ONIXS_FIXENGINE_NULLPTR, 0);
128  }
129 
130  /// Updates data being referenced.
131  void
133  const char * chars,
134  size_t size) {
135  chars_ = chars;
136  size_ = size;
137  }
138 
139  const char &
140  operator [](size_t index) const {
141  return chars_[index];
142  }
143 
144  const char &
145  at(size_t index) const {
146  if(index < size_)
147  return chars_[index];
148 
149  throw std::invalid_argument("index");
150  }
151 
152  /// Returns the number if the text is a string
153  /// representation of an integer.
154  template
155  <
156  typename NumericType
157  >
158  bool
159  toNumber(NumericType & number) const {
160  return OnixS::FIX::Number::
161  tryParse(chars_, size_, number);
162  }
163 
164  // Appends a copy of the text to the std::string.
165  void
166  toString(std::string & str) const {
167  str.append(chars_, size_);
168  }
169 
170  // Returns a copy of the text as the std::string.
171  std::string
172  toString() const {
173  return std::string(chars_, size_);
174  }
175 
176  /// Compares with another instance.
177  bool
179  const StringRef & other) const {
180  return (
181  size_ == other.size_
182  &&
183  0 == std::memcmp(
184  chars_, other.chars_, size_));
185  }
186 
187  /// Compares with another instance.
188  bool
190  const StringRef & other) const {
191  return (
192  size_ != other.size_
193  ||
194  0 != std::memcmp(
195  chars_, other.chars_, size_));
196  }
197 
198  /// Swaps the content with another instance.
199  void
200  swap(StringRef & other) {
201  std::swap(chars_, other.chars_);
202  std::swap(size_, other.size_);
203  }
204 
205  friend std::ostream & operator<< (std::ostream & out, const StringRef & ref);
206 
207 private:
208  /// Items being referenced.
209  const char * chars_;
210 
211  /// The number of chars being referenced.
212  size_t size_;
213 };
214 
215 inline
216 bool
218  const StringRef & ref,
219  const std::string & str)
220 {
221  return ref == StringRef(str);
222 }
223 
224 inline
225 bool
227  const StringRef & ref,
228  const std::string & str)
229 {
230  return ref != StringRef(str);
231 }
232 
233 inline
234 bool
236  const std::string & str,
237  const StringRef & ref)
238 {
239  return ref == StringRef(str);
240 }
241 
242 inline
243 bool
245  const std::string & str,
246  const StringRef & ref)
247 {
248  return ref != StringRef(str);
249 }
250 
251 //
252 
253 inline
254 bool
256  const StringRef & ref,
257  const char * str)
258 {
259  return ref == StringRef(str);
260 }
261 
262 inline
263 bool
265  const StringRef & ref,
266  const char * str)
267 {
268  return ref != StringRef(str);
269 }
270 
271 inline
272 bool
274  const char * str,
275  const StringRef & ref)
276 {
277  return ref == StringRef(str);
278 }
279 
280 inline
281 bool
283  const char * str,
284  const StringRef & ref)
285 {
286  return ref != StringRef(str);
287 }
288 
289 inline std::ostream & operator<< (std::ostream & out, const StringRef & ref)
290 {
291  if(! ref.empty())
292  out.write(ref.chars_, ref.size_);
293 
294  return out;
295 }
296 
297 #endif
298 
299 template <size_t Size>
301 inline
302 ONIXS_FIXENGINE_CONSTEXPR
304 {
305  return StringRef(value, Size - 1);
306 }
307 
308 }
309 }
310 
311 namespace std {
312 /// Allows the use of the StringRef in collections like std::map.
313 template<>
314 struct
317  bool
319  const OnixS::FIX::StringRef & left,
320  const OnixS::FIX::StringRef & right) const {
321  ptrdiff_t
322  compareResult = std::memcmp(
323  left.data(), right.data(),
324  left.size() < right.size()
325  ? left.size() : right.size());
326 
327  if(0 == compareResult) {
328  compareResult =
329  static_cast<ptrdiff_t>(
330  left.size() - right.size());
331  }
332 
333  return (0 > compareResult);
334  }
335 };
336 }
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
const char & at(size_t index) const
Definition: StringRef.h:145
ONIXS_FIXENGINE_CONSTEXPR StringRef(const char *chars, size_t size)
The full initialization.
Definition: StringRef.h:75
bool operator()(const OnixS::FIX::StringRef &left, const OnixS::FIX::StringRef &right) const
Definition: StringRef.h:318
void swap(StringRef &other)
Swaps the content with another instance.
Definition: StringRef.h:200
ConstIterator begin() const
The STL-like begin().
Definition: StringRef.h:106
ONIXS_FIXENGINE_NODISCARD ONIXS_FIXENGINE_CONSTEXPR StringRef constructStrRef(const char(&value)[Size]) ONIXS_FIXENGINE_NOTHROW
Definition: StringRef.h:303
StringRef(const std::string &stdStr)
Initializes an instance from the string content.
Definition: StringRef.h:83
#define ONIXS_FIXENGINE_NOTHROW
Definition: Compiler.h:43
STL namespace.
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
bool toNumber(NumericType &number) const
Returns the number if the text is a string representation of an integer.
Definition: StringRef.h:159
void reset(const char *chars, size_t size)
Updates data being referenced.
Definition: StringRef.h:132
std::string toString() const
Definition: StringRef.h:172
StringRef()
Initializes the blank instance.
Definition: StringRef.h:68
Provides an efficient way of accessing text-based FIX field values.
Definition: StringRef.h:59
Allows the use of the StringRef in collections like std::map.
Definition: StringRef.h:314
void toString(std::string &str) const
Definition: StringRef.h:166
ConstIterator end() const
The STL-like end().
Definition: StringRef.h:111
const char * ConstIterator
An immutable iterator over chars.
Definition: StringRef.h:65
bool operator==(const FieldValueRef &ref, const std::string &str)
size_t length() const
The number of chars.
Definition: StringRef.h:121
bool empty() const
Indicates whether the array has zero length.
Definition: StringRef.h:96
size_t size() const
The number of chars.
Definition: StringRef.h:116
#define ONIXS_FIXENGINE_NODISCARD
Definition: Compiler.h:120
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
void reset()
Resets the reference to nothing.
Definition: StringRef.h:126
StringRef(const char *cStr)
Initializes an instance from the zero-terminated string.
Definition: StringRef.h:90
const char * data() const
Returns the read-only content.
Definition: StringRef.h:101
bool operator!=(const FieldValueRef &ref, const std::string &str)