OnixS C++ CME Market Data Handler  5.4.0
API documentation
MultiContainer.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 <cassert>
24 
25 #include <vector>
26 #include <algorithm>
27 
28 #include <OnixS/CME/MDH/String.h>
29 #include <OnixS/CME/MDH/FIX/Tag.h>
30 
32 
33 /// Represents a pair of a tag and a value.
35 {
36  Tag tag_;
37  StrRef value_;
38 
39 public:
40  /// Alias for the value type.
41  typedef StrRef Value;
42 
43  /// Initialies instance with
44  /// zero tag and empty value.
46  : tag_(0), value_()
47  {
48  }
49 
50  /// Initializes instance according
51  /// to the given attributes.
53  Tag tag,
54  const Value& value)
55  : tag_(tag)
56  , value_(value)
57  {
58  }
59 
60  /// Tag component of the pair.
61  Tag tag() const
62  {
63  return tag_;
64  }
65 
66  /// Updates tag component.
67  void
68  tag(
69  Tag tag)
70  {
71  tag_ = tag;
72  }
73 
74  /// Value component of the pair.
75  const Value& value() const
76  {
77  return value_;
78  }
79 
80  /// Updates value component.
81  void
83  const Value& value)
84  {
85  value_ = value;
86  }
87 
88  /// Updates value component.
89  void
91  const Char* data,
92  size_t size)
93  {
94  value_.reset(data, size);
95  }
96 };
97 
98 /// Serializes the given tag-value pair.
99 ONIXS_CMEMDH_EXPORTED
100 void
101 toStr(
102  std::string&,
103  const TagValue&);
104 
105 /// Serializes the given tag-value pair.
106 inline
107 std::string
109  const TagValue& tagValue)
110 {
111  std::string str;
112 
113  toStr(str, tagValue);
114 
115  return str;
116 }
117 
118 //
119 
120 /// Sequence of tag-value pairs.
121 typedef
122 std::vector<TagValue>
124 
125 /// Iterator over the tag-value sequence.
126 typedef
127 TagValues::iterator
129 
130 /// Constant iterator over the tag-value sequence.
131 typedef
132 TagValues::const_iterator
134 
135 //
136 
137 /// Functor to be used in algorythms to find
138 /// tag-value pair in collections by tag component.
140 {
141  Tag tag_;
142 
143 public:
144  /// Initializes functor with the given tag.
145  explicit
147  Tag tag)
148  : tag_(tag)
149  {
150  }
151 
152  /// Checks whether tag component of the given
153  /// tag-value matches value stored by the functor.
154  bool
155  operator ()(
156  const TagValue& tagValue) const
157  {
158  return (
159  tagValue.tag() ==
160  tag_);
161  }
162 };
163 
164 //
165 
166 /// Iterator over tag-value pairs having the same tag value.
169 {
170  const TagValues* container_;
172 
173 public:
174  /// Initializes the interator pointing to nothing.
176  : container_(NULL)
177  , ref_()
178  {
179  }
180 
181  /// Initializes the instance to iterate tag-values
182  /// with the given tag value for the given collection.
184  const TagValues& container,
185  Tag selection)
186  : container_(
187  &container)
188  , ref_(
189  std::find_if(
190  container.begin(),
191  container.end(),
192  TagValueSelector(selection)))
193  {
194  }
195 
196  /// Indicates whether iterator refers
197  /// to a valid tag-value instance.
198  operator bool() const
199  {
200  return (
201  container_ &&
202  ref_ != container_->end());
203  }
204 
205  /// Returns instance of the tag-value
206  /// pair referenced by the given instance.
207  const TagValue& operator *() const
208  {
209  assert(static_cast<bool>(*this));
210 
211  return *ref_;
212  }
213 
214  /// Advances the given instance to the next
215  /// tag-value instance in the selection or
216  /// invalidates the instance if no more tag-value
217  /// pairs are available in the selection.
219  operator ++()
220  {
221  assert(static_cast<bool>(*this));
222 
223  const Tag selection = ref_->tag();
224 
225  ref_ =
226  std::find_if
227  (
228  ++ref_,
229  container_->end(),
230  TagValueSelector(selection)
231  );
232 
233  return *this;
234  }
235 };
236 
237 //
238 
239 /// Sequence of tag-value pairs preserving order of
240 /// pairs and allowing presence of multiple tag-value
241 /// pairs with the same tag value.
242 ///
243 /// Primarily, designed to provide lightweight service
244 /// to transform serialized FIX messages into structural
245 /// presentation for furhter field access and other
246 /// manipulations over stored data.
248 {
249  TagValues tagValues_;
250 
251  // Builds sequence of tag-value pairs
252  // from the given FIX (tag=value) string.
254  static
255  void
256  deserialize(
257  TagValues&,
258  const Char*,
259  size_t);
260 
261 public:
262  /// Alias for tag component which
263  /// serves like an entry key.
264  typedef Tag Key;
265 
266  /// Alias for value type.
268 
269  /// Iterator over container items.
270  typedef
271  TagValues::const_iterator
273 
274  /// Iterator over items having the same tag value.
275  typedef
278 
279  /// Initializes an empty instance.
281  {
282  }
283 
284  /// Finalizes the instance.
286  {
287  }
288 
289  /// Returns iterator pointing to
290  /// the first item of the container.
291  Iterator begin() const
292  {
293  return tagValues_.begin();
294  }
295 
296  /// Returns iterator pointing to
297  /// the item beyond the last one.
298  Iterator end() const
299  {
300  return tagValues_.end();
301  }
302 
303  /// Returns iterator pointing to the
304  /// first (of possibly multiple) item
305  /// having the given key (tag) value.
306  Iterator
308  Key tag) const
309  {
310  return std::find_if
311  (
312  tagValues_.begin(),
313  tagValues_.end(),
314  TagValueSelector(tag)
315  );
316  }
317 
318  /// Allows to iterate all items in the
319  /// container having the given tag value.
322  Key tag) const
323  {
324  return
326  (
327  tagValues_,
328  tag
329  );
330  }
331 
332  /// Full-fill collection from the FIX
333  /// (tag=value) string presentation.
334  void
336  const Char* fixStr,
337  size_t fixSize)
338  {
339  tagValues_.clear();
340 
341  deserialize
342  (
343  tagValues_,
344  fixStr,
345  fixSize
346  );
347  }
348 };
349 
350 /// Finds a tag-value entry in the given collection
351 /// by the given tag and returns its value component.
352 inline
355  const MultiContainer& container,
356  Tag tag,
357  const
359  defaultValue =
361 {
362  const
364  result =
365  container.first(tag);
366 
367  if (result !=
368  container.end())
369  {
370  return result->value();
371  }
372 
373  return defaultValue;
374 }
375 
376 /// Finds a tag-value entry in the given collection
377 /// by the given tag and returns its value component
378 /// transformed into a number.
379 ///
380 /// Throws an exception if a value is either absent or
381 /// can't be transformed into a number of the given type.
382 template
383 <
384  class Number
385 >
386 bool
388  Number& number,
389  const MultiContainer& container,
390  Tag tag)
391 {
392  const
394  strValue =
396  container,
397  tag);
398 
399  return fromStr
400  (
401  number,
402  strValue.items(),
403  strValue.size()
404  );
405 }
406 
407 /// Finds a tag-value entry in the given collection
408 /// by the given tag and returns its value component
409 /// transformed into a number of the given type.
410 ///
411 /// If value is absent, then returns the default one.
412 /// If value is found, but can't be transformed into a
413 /// number, then an exception is trown.
414 template
415 <
416  class Number
417 >
418 bool
420  Number& number,
421  const MultiContainer& container,
422  Tag tag, Number defaultValue = Number())
423 {
424  const
426  strValue =
428  container,
429  tag);
430 
431  if (strValue.empty())
432  {
433  number = defaultValue;
434 
435  return true;
436  }
437 
438  return fromStr
439  (
440  number,
441  strValue.items(),
442  strValue.size()
443  );
444 }
445 
446 //
447 
448 /// Serializes the given tag-value pair.
449 ONIXS_CMEMDH_EXPORTED
450 void
451 toStr(
452  std::string&,
453  const MultiContainer&);
454 
455 /// Serializes the given tag-value pair.
456 inline
457 std::string
459  const MultiContainer& container)
460 {
461  std::string str;
462 
463  toStr(str, container);
464 
465  return str;
466 }
467 
468 //
469 
TagValueSelectionIterator(const TagValues &container, Tag selection)
#define ONIXS_CMEMDH_LTWT_CLASS
Definition: Bootstrap.h:94
ONIXS_CMEMDH_EXPORTED bool fromStr(Decimal &, const Char *, size_t)
TagValueSelector(Tag tag)
Initializes functor with the given tag.
void value(const Char *data, size_t size)
Updates value component.
TagValues::const_iterator Iterator
Iterator over container items.
bool empty() const
Indicates whether the referenced text is empty.
Definition: String.h:80
#define ONIXS_CMEMDHFIX_NAMESPACE_BEGIN
Definition: Bootstrap.h:160
STL namespace.
~MultiContainer()
Finalizes the instance.
StrRef Value
Alias for the value type.
void deserialize(const Char *fixStr, size_t fixSize)
Iterator over tag-value pairs having the same tag value.
Tag tag() const
Tag component of the pair.
SelectionIterator all(Key tag) const
char Char
Character type alias.
Definition: String.h:36
Represents a pair of a tag and a value.
TagValueSelectionIterator()
Initializes the interator pointing to nothing.
bool value(Number &number, const MultiContainer &container, Tag tag)
#define ONIXS_CMEMDHFIX_NAMESPACE_END
Definition: Bootstrap.h:164
bool valueOrDefault(Number &number, const MultiContainer &container, Tag tag, Number defaultValue=Number())
void tag(Tag tag)
Updates tag component.
TagValue(Tag tag, const Value &value)
const Char * items() const
Read-only content.
Definition: String.h:86
std::vector< TagValue > TagValues
Sequence of tag-value pairs.
Iterator first(Key tag) const
TagValue::Value Value
Alias for value type.
MultiContainer()
Initializes an empty instance.
const Value & value() const
Value component of the pair.
UInt32 Tag
Definition: Tag.h:29
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition: Bootstrap.h:102
TagValueSelectionIterator SelectionIterator
Iterator over items having the same tag value.
std::string toStr(const MultiContainer &container)
Serializes the given tag-value pair.
TagValues::const_iterator TagValueConstIterator
Constant iterator over the tag-value sequence.
TagValues::iterator TagValueIterator
Iterator over the tag-value sequence.
size_t size() const
Number of chars.
Definition: String.h:92
void reset()
Resets the reference to nothing.
Definition: String.h:110
void value(const Value &value)
Updates value component.