OnixS C++ FIX Engine  4.8.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 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 same
49 /// instance of message is used to update a few fields and is sent over FIX
50 /// session multiple times. Also, when message comes from the network and is
51 /// deserialized from its raw (tag=value) presentation, all field values in
52 /// fact represent segments of text. To have access to textual values, StringRef
53 /// class 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 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 class
60  StringRef
61 {
62 public:
63  /// Immutable iterator over chars.
64  typedef const char * ConstIterator;
65 
66  /// Initializes blank instance.
68  : chars_(ONIXS_FIXENGINE_NULLPTR),
69  size_(0) {
70  }
71 
72  /// Full initialization.
73  ONIXS_FIXENGINE_CONSTEXPR
75  const char * chars,
76  size_t size)
77  : chars_(chars),
78  size_(size) {
79  }
80 
81  /// Initializes instance from string content.
82  explicit StringRef(
83  const std::string & stdStr)
84  : chars_(stdStr.c_str()),
85  size_(stdStr.size()) {
86  }
87 
88  /// Initializes instance from zero-terminated string.
89  StringRef(const char * cStr)
90  : chars_(cStr),
91  size_(cStr ? strlen(cStr) : 0) {
92  }
93 
94  /// Indicates whether array of zero length.
95  bool empty() const {
96  return (0 == size_);
97  }
98 
99  /// Read-only content.
100  const char * data() const {
101  return chars_;
102  }
103 
104  /// STL-like begin().
105  ConstIterator begin() const {
106  return chars_;
107  }
108 
109  /// STL-like end().
110  ConstIterator end() const {
111  return chars_ + size_;
112  }
113 
114  /// Number of chars.
115  size_t size() const {
116  return size_;
117  }
118 
119  /// Number of chars.
120  size_t length() const {
121  return size_;
122  }
123 
124  /// Resets reference to nothing.
125  void reset() {
126  reset(ONIXS_FIXENGINE_NULLPTR, 0);
127  }
128 
129  /// Updates data being referenced.
130  void
132  const char * chars,
133  size_t size) {
134  chars_ = chars;
135  size_ = size;
136  }
137 
138  const char &
139  operator [](size_t index) const {
140  return chars_[index];
141  }
142 
143  const char &
144  at(size_t index) const {
145  if(index < size_)
146  return chars_[index];
147 
148  throw std::invalid_argument("index");
149  }
150 
151  /// Returns number if text is string
152  /// representation of an integer.
153  template
154  <
155  typename NumericType
156  >
157  bool
158  toNumber(NumericType & number) const {
159  return OnixS::FIX::Number::
160  tryParse(chars_, size_, number);
161  }
162 
163  // Appends copy of text to the std::string.
164  void
165  toString(std::string & str) const {
166  str.append(chars_, size_);
167  }
168 
169  // Return copy of text as std::string.
170  std::string
171  toString() const {
172  return std::string(chars_, size_);
173  }
174 
175  /// Compares with another instance.
176  bool
178  const StringRef & other) const {
179  return (
180  size_ == other.size_
181  &&
182  0 == std::memcmp(
183  chars_, other.chars_, size_));
184  }
185 
186  /// Compares with another instance.
187  bool
189  const StringRef & other) const {
190  return (
191  size_ != other.size_
192  ||
193  0 != std::memcmp(
194  chars_, other.chars_, size_));
195  }
196 
197  /// Swaps content with other instance.
198  void
199  swap(StringRef & other) {
200  std::swap(chars_, other.chars_);
201  std::swap(size_, other.size_);
202  }
203 
204  friend std::ostream & operator<< (std::ostream & out, const StringRef & ref);
205 
206 private:
207  /// Items being referenced.
208  const char * chars_;
209 
210  /// Number of chars being referenced.
211  size_t size_;
212 };
213 
214 inline
215 bool
217  const StringRef & ref,
218  const std::string & str)
219 {
220  return ref == StringRef(str);
221 }
222 
223 inline
224 bool
226  const StringRef & ref,
227  const std::string & str)
228 {
229  return ref != StringRef(str);
230 }
231 
232 inline
233 bool
235  const std::string & str,
236  const StringRef & ref)
237 {
238  return ref == StringRef(str);
239 }
240 
241 inline
242 bool
244  const std::string & str,
245  const StringRef & ref)
246 {
247  return ref != StringRef(str);
248 }
249 
250 //
251 
252 inline
253 bool
255  const StringRef & ref,
256  const char * str)
257 {
258  return ref == StringRef(str);
259 }
260 
261 inline
262 bool
264  const StringRef & ref,
265  const char * str)
266 {
267  return ref != StringRef(str);
268 }
269 
270 inline
271 bool
273  const char * str,
274  const StringRef & ref)
275 {
276  return ref == StringRef(str);
277 }
278 
279 inline
280 bool
282  const char * str,
283  const StringRef & ref)
284 {
285  return ref != StringRef(str);
286 }
287 
288 inline std::ostream & operator<< (std::ostream & out, const StringRef & ref)
289 {
290  if(! ref.empty())
291  out.write(ref.chars_, ref.size_);
292 
293  return out;
294 }
295 
296 #endif
297 
298 template <size_t Size>
300 inline
301 ONIXS_FIXENGINE_CONSTEXPR
303 {
304  return StringRef(value, Size - 1);
305 }
306 
307 }
308 }
309 
310 namespace std {
311 /// Allows using of StringRef in collections like std::map.
312 template<>
313 struct
316  bool
318  const OnixS::FIX::StringRef & left,
319  const OnixS::FIX::StringRef & right) const {
320  ptrdiff_t
321  compareResult = std::memcmp(
322  left.data(), right.data(),
323  left.size() < right.size()
324  ? left.size() : right.size());
325 
326  if(0 == compareResult) {
327  compareResult =
328  static_cast<ptrdiff_t>(
329  left.size() - right.size());
330  }
331 
332  return (0 > compareResult);
333  }
334 };
335 }
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
const char & at(size_t index) const
Definition: StringRef.h:144
ONIXS_FIXENGINE_CONSTEXPR StringRef(const char *chars, size_t size)
Full initialization.
Definition: StringRef.h:74
bool operator()(const OnixS::FIX::StringRef &left, const OnixS::FIX::StringRef &right) const
Definition: StringRef.h:317
void swap(StringRef &other)
Swaps content with other instance.
Definition: StringRef.h:199
ConstIterator begin() const
STL-like begin().
Definition: StringRef.h:105
ONIXS_FIXENGINE_NODISCARD ONIXS_FIXENGINE_CONSTEXPR StringRef constructStrRef(const char(&value)[Size]) ONIXS_FIXENGINE_NOTHROW
Definition: StringRef.h:302
StringRef(const std::string &stdStr)
Initializes instance from string content.
Definition: StringRef.h:82
#define ONIXS_FIXENGINE_NOTHROW
Definition: Compiler.h:43
STL namespace.
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
bool toNumber(NumericType &number) const
Returns number if text is string representation of an integer.
Definition: StringRef.h:158
void reset(const char *chars, size_t size)
Updates data being referenced.
Definition: StringRef.h:131
std::string toString() const
Definition: StringRef.h:171
StringRef()
Initializes blank instance.
Definition: StringRef.h:67
Provides efficient way of accessing text-based FIX field values.
Definition: StringRef.h:58
Allows using of StringRef in collections like std::map.
Definition: StringRef.h:313
void toString(std::string &str) const
Definition: StringRef.h:165
ConstIterator end() const
STL-like end().
Definition: StringRef.h:110
const char * ConstIterator
Immutable iterator over chars.
Definition: StringRef.h:64
bool operator==(const FieldValueRef &ref, const std::string &str)
size_t length() const
Number of chars.
Definition: StringRef.h:120
bool empty() const
Indicates whether array of zero length.
Definition: StringRef.h:95
size_t size() const
Number of chars.
Definition: StringRef.h:115
#define ONIXS_FIXENGINE_NODISCARD
Definition: Compiler.h:120
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
void reset()
Resets reference to nothing.
Definition: StringRef.h:125
StringRef(const char *cStr)
Initializes instance from zero-terminated string.
Definition: StringRef.h:89
const char * data() const
Read-only content.
Definition: StringRef.h:100
bool operator!=(const FieldValueRef &ref, const std::string &str)