OnixS C++ FIX Engine  4.2.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 
27 
28 namespace OnixS {
29 namespace FIX {
30 /// Provides efficient way of accessing text-based FIX field values.
31 ///
32 /// By default, OnixS::FIX::Message and OnixS::FIX::GroupInstance classes
33 /// convert all values assigned to the fields into their text presentations
34 /// and store textual data. This approach optimizes message reuse when same
35 /// instance of message is used to update a few fields and is sent over FIX
36 /// session multiple times. Also, when message comes from the network and is
37 /// deserialized from its raw (tag=value) presentation, all field values in
38 /// fact represent segments of text. To have access to textual values, StringRef
39 /// class was added together with new access members (FieldSet::getStringRef()).
40 /// If value is accessed using FieldSet::getStringRef() member, instance of
41 /// StringRef class is returned. That instance holds reference to a read-only
42 /// segment of text. An important aspect is that no memory is allocated and
43 /// copied while accessing field values that way.
44 class
46  StringRef
47 {
48 public:
49  /// Immutable iterator over chars.
50  typedef const char * ConstIterator;
51 
52  /// Initializes blank instance.
54  : chars_(NULL),
55  size_(0) {
56  }
57 
58  /// Full initialization.
60  const char * chars,
61  size_t size)
62  : chars_(chars),
63  size_(size) {
64  }
65 
66  /// Initializes as clone of other instance.
67  /// Data referenced by clone is not copied.
68  /// Instead both instances will refer to same
69  /// text segment.
71  const StringRef & other)
72  : chars_(other.chars_),
73  size_(other.size_) {
74  }
75 
76  /// Initializes instance from string content.
77  explicit StringRef(
78  const std::string & stdStr)
79  : chars_(stdStr.c_str()),
80  size_(stdStr.size()) {
81  }
82 
83  /// Initializes instance from zero-terminated string.
84  StringRef(const char * cStr)
85  : chars_(cStr),
86  size_(cStr ? strlen(cStr) : 0) {
87  }
88 
89  /// Indicates whether array of zero length.
90  bool empty() const {
91  return (0 == size_);
92  }
93 
94  /// Read-only content.
95  const char * data() const {
96  return chars_;
97  }
98 
99  /// STL-like begin().
100  ConstIterator begin() const {
101  return chars_;
102  }
103 
104  /// STL-like end().
105  ConstIterator end() const {
106  return chars_ + size_;
107  }
108 
109  /// Number of chars.
110  size_t size() const {
111  return size_;
112  }
113 
114  /// Resets reference to nothing.
115  void reset() {
116  reset(NULL, 0);
117  }
118 
119  /// Updates data being referenced.
120  void
122  const char * chars,
123  size_t size) {
124  chars_ = chars;
125  size_ = size;
126  }
127 
128  const char &
129  operator [](size_t index) const {
130  return chars_[index];
131  }
132 
133  const char &
134  at(size_t index) const {
135  if(index < size_)
136  return chars_[index];
137 
138  throw std::invalid_argument("index");
139  }
140 
141  /// Returns number if text is string
142  /// representation of an integer.
143  template
144  <
145  typename NumericType
146  >
147  bool
148  toNumber(NumericType & number) const {
149  return OnixS::FIX::Number::
150  tryParse(chars_, size_, number);
151  }
152 
153  // Appends copy of text to the std::string.
154  void
155  toString(std::string & str) const {
156  str.append(chars_, size_);
157  }
158 
159  // Return copy of text as std::string.
160  std::string
161  toString() const {
162  return std::string(chars_, size_);
163  }
164 
165  /// Compares with another instance.
166  bool
168  const StringRef & other) const {
169  return (
170  size_ == other.size_
171  &&
172  0 == memcmp(
173  chars_, other.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 != memcmp(
184  chars_, other.chars_, size_));
185  }
186 
187  /// Reinitializes from another instance.
188  StringRef &
189  operator = (
190  const StringRef & other) {
191  chars_ = other.chars_;
192  size_ = other.size_;
193 
194  return *this;
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 }
297 
298 namespace std {
299 /// Allows using of StringRef in collections like std::map.
300 template<>
301 struct
304  bool
306  const OnixS::FIX::StringRef & left,
307  const OnixS::FIX::StringRef & right) const {
308  ptrdiff_t
309  compareResult = memcmp(
310  left.data(), right.data(),
311  left.size() < right.size()
312  ? left.size() : right.size());
313 
314  if(0 == compareResult) {
315  compareResult =
316  static_cast<ptrdiff_t>(
317  left.size() - right.size());
318  }
319 
320  return (0 > compareResult);
321  }
322 };
323 }
size_t size() const
Number of chars.
Definition: StringRef.h:110
bool toNumber(NumericType &number) const
Returns number if text is string representation of an integer.
Definition: StringRef.h:148
const char * ConstIterator
Immutable iterator over chars.
Definition: StringRef.h:50
StringRef()
Initializes blank instance.
Definition: StringRef.h:53
StringRef(const std::string &stdStr)
Initializes instance from string content.
Definition: StringRef.h:77
STL namespace.
bool operator!=(const FieldValueRef &ref, const std::string &str)
void reset(const char *chars, size_t size)
Updates data being referenced.
Definition: StringRef.h:121
ConstIterator end() const
STL-like end().
Definition: StringRef.h:105
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
const char & at(size_t index) const
Definition: StringRef.h:134
Allows using of StringRef in collections like std::map.
Definition: StringRef.h:301
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
StringRef(const char *chars, size_t size)
Full initialization.
Definition: StringRef.h:59
bool operator()(const OnixS::FIX::StringRef &left, const OnixS::FIX::StringRef &right) const
Definition: StringRef.h:305
Provides efficient way of accessing text-based FIX field values.
Definition: StringRef.h:44
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
void swap(StringRef &other)
Swaps content with other instance.
Definition: StringRef.h:199
ConstIterator begin() const
STL-like begin().
Definition: StringRef.h:100
const char * data() const
Read-only content.
Definition: StringRef.h:95
void reset()
Resets reference to nothing.
Definition: StringRef.h:115
bool empty() const
Indicates whether array of zero length.
Definition: StringRef.h:90
std::string toString() const
Definition: StringRef.h:161
StringRef(const StringRef &other)
Initializes as clone of other instance.
Definition: StringRef.h:70
bool operator==(const FieldValueRef &ref, const std::string &str)
void toString(std::string &str) const
Definition: StringRef.h:155
StringRef(const char *cStr)
Initializes instance from zero-terminated string.
Definition: StringRef.h:84