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