OnixS C++ CME MDP Premium Market Data Handler  5.8.9
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)
47  , value_()
48  {
49  }
50 
51  /// Initializes instance according
52  /// to the given attributes.
53  TagValue(Tag tag, const Value& value)
54  : tag_(tag)
55  , value_(value)
56  {
57  }
58 
59  /// Tag component of the pair.
60  Tag tag() const
61  {
62  return tag_;
63  }
64 
65  /// Updates tag component.
66  void tag(Tag tag)
67  {
68  tag_ = tag;
69  }
70 
71  /// Value component of the pair.
72  const Value& value() const
73  {
74  return value_;
75  }
76 
77  /// Updates value component.
78  void value(const Value& value)
79  {
80  value_ = value;
81  }
82 
83  /// Updates value component.
84  void value(const Char* data, size_t size)
85  {
86  value_.reset(data, size);
87  }
88 };
89 
90 /// Serializes the given tag-value pair.
92 void toStr(std::string&, const TagValue&);
93 
94 /// Serializes the given tag-value pair.
95 inline std::string toStr(const TagValue& tagValue)
96 {
97  std::string str;
98 
99  toStr(str, tagValue);
100 
101  return str;
102 }
103 
104 //
105 
106 /// Sequence of tag-value pairs.
107 typedef std::vector<TagValue> TagValues;
108 
109 /// Iterator over the tag-value sequence.
110 typedef TagValues::iterator TagValueIterator;
111 
112 /// Constant iterator over the tag-value sequence.
113 typedef TagValues::const_iterator TagValueConstIterator;
114 
115 //
116 
117 /// Functor to be used in algorithms to find
118 /// tag-value pair in collections by tag component.
120 {
121  Tag tag_;
122 
123 public:
124  /// Initializes functor with the given tag.
125  explicit TagValueSelector(Tag tag)
126  : tag_(tag)
127  {
128  }
129 
130  /// Checks whether tag component of the given
131  /// tag-value matches value stored by the functor.
132  bool operator()(const TagValue& tagValue) const
133  {
134  return (tagValue.tag() == tag_);
135  }
136 };
137 
138 //
139 
140 /// Iterator over tag-value pairs having the same tag value.
142 {
143  const TagValues* container_;
145 
146 public:
147  /// Initializes the iterator pointing to nothing.
149  : container_(ONIXS_CMEMDH_NULLPTR)
150  , ref_()
151  {
152  }
153 
154  /// Initializes the instance to iterate tag-values
155  /// with the given tag value for the given collection.
156  TagValueSelectionIterator(const TagValues& container, Tag selection)
157  : container_(&container)
158  , ref_(std::find_if(container.begin(), container.end(), TagValueSelector(selection)))
159  {
160  }
161 
162  /// Indicates whether iterator refers
163  /// to a valid tag-value instance.
164  operator bool() const
165  {
166  return (container_ && ref_ != container_->end());
167  }
168 
169  /// Returns instance of the tag-value
170  /// pair referenced by the given instance.
171  const TagValue& operator*() const
172  {
173  assert(static_cast<bool>(*this));
174 
175  return *ref_;
176  }
177 
178  /// Advances the given instance to the next
179  /// tag-value instance in the selection or
180  /// invalidates the instance if no more tag-value
181  /// pairs are available in the selection.
183  {
184  assert(static_cast<bool>(*this));
185 
186  const Tag selection = ref_->tag();
187 
188  ref_ = std::find_if(++ref_, container_->end(), TagValueSelector(selection));
189 
190  return *this;
191  }
192 };
193 
194 //
195 
196 /// Sequence of tag-value pairs preserving order of
197 /// pairs and allowing presence of multiple tag-value
198 /// pairs with the same tag value.
199 ///
200 /// Primarily, designed to provide lightweight service
201 /// to transform serialized FIX messages into structural
202 /// presentation for further field access and other
203 /// manipulations over stored data.
205 {
206  TagValues tagValues_;
207 
208  // Builds sequence of tag-value pairs
209  // from the given FIX (tag=value) string.
211  static void deserialize(TagValues&, const Char*, size_t);
212 
213 public:
214  /// Alias for tag component which
215  /// serves like an entry key.
216  typedef Tag Key;
217 
218  /// Alias for value type.
220 
221  /// Iterator over container items.
222  typedef TagValues::const_iterator Iterator;
223 
224  /// Iterator over items having the same tag value.
226 
227  /// Initializes an empty instance.
229 
230  /// Finalizes the instance.
232 
233  /// Returns iterator pointing to
234  /// the first item of the container.
235  Iterator begin() const
236  {
237  return tagValues_.begin();
238  }
239 
240  /// Returns iterator pointing to
241  /// the item beyond the last one.
242  Iterator end() const
243  {
244  return tagValues_.end();
245  }
246 
247  /// Returns iterator pointing to the
248  /// first (of possibly multiple) item
249  /// having the given key (tag) value.
250  Iterator first(Key tag) const
251  {
252  return std::find_if(tagValues_.begin(), tagValues_.end(), TagValueSelector(tag));
253  }
254 
255  /// Allows to iterate all items in the
256  /// container having the given tag value.
257  SelectionIterator all(Key tag) const
258  {
259  return SelectionIterator(tagValues_, tag);
260  }
261 
262  /// Full-fill collection from the FIX
263  /// (tag=value) string presentation.
264  void deserialize(const Char* fixStr, size_t fixSize)
265  {
266  tagValues_.clear();
267 
268  deserialize(tagValues_, fixStr, fixSize);
269  }
270 };
271 
272 /// Finds a tag-value entry in the given collection
273 /// by the given tag and returns its value component.
275  const MultiContainer& container,
276  Tag tag,
277  const MultiContainer::Value& defaultValue = MultiContainer::Value()
278 )
279 {
280  const MultiContainer::Iterator result = container.first(tag);
281 
282  if (result != container.end())
283  {
284  return result->value();
285  }
286 
287  return defaultValue;
288 }
289 
290 /// Finds a tag-value entry in the given collection
291 /// by the given tag and returns its value component
292 /// transformed into a number.
293 ///
294 /// Throws an exception if a value is either absent or
295 /// can't be transformed into a number of the given type.
296 template <class Number>
297 bool value(Number& number, const MultiContainer& container, Tag tag)
298 {
299  const MultiContainer::Value strValue = valueOrDefault(container, tag);
300 
301  return fromStr(number, strValue.items(), strValue.size());
302 }
303 
304 /// Finds a tag-value entry in the given collection
305 /// by the given tag and returns its value component
306 /// transformed into a number of the given type.
307 ///
308 /// If value is absent, then returns the default one.
309 /// If value is found, but can't be transformed into a
310 /// number, then an exception is trown.
311 template <class Number>
312 bool valueOrDefault(Number& number, const MultiContainer& container, Tag tag, Number defaultValue = Number())
313 {
314  const MultiContainer::Value strValue = valueOrDefault(container, tag);
315 
316  if (strValue.empty())
317  {
318  number = defaultValue;
319 
320  return true;
321  }
322 
323  return fromStr(number, strValue.items(), strValue.size());
324 }
325 
326 //
327 
328 /// Serializes the given tag-value pair.
330 void toStr(std::string&, const MultiContainer&);
331 
332 /// Serializes the given tag-value pair.
333 inline std::string toStr(const MultiContainer& container)
334 {
335  std::string str;
336 
337  toStr(str, container);
338 
339  return str;
340 }
341 
342 //
343 
TagValueSelectionIterator(const TagValues &container, Tag selection)
Initializes the instance to iterate tag-values with the given tag value for the given collection...
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:71
#define ONIXS_CMEMDH_NULLPTR
Definition: Compiler.h:167
#define ONIXS_CMEMDHFIX_NAMESPACE_BEGIN
Definition: Bootstrap.h:70
STL namespace.
~MultiContainer()
Finalizes the instance.
StrRef Value
Alias for the value type.
#define ONIXS_CMEMDH_LTWT
Definition: Bootstrap.h:46
Iterator end() const
Returns iterator pointing to the item beyond the last one.
void deserialize(const Char *fixStr, size_t fixSize)
Full-fill collection from the FIX (tag=value) string presentation.
bool operator()(const TagValue &tagValue) const
Checks whether tag component of the given tag-value matches value stored by the functor.
Iterator over tag-value pairs having the same tag value.
Tag tag() const
Tag component of the pair.
SelectionIterator all(Key tag) const
Allows to iterate all items in the container having the given tag value.
char Char
Character type alias.
Definition: String.h:36
Functor to be used in algorithms to find tag-value pair in collections by tag component.
Represents a pair of a tag and a value.
TagValueSelectionIterator()
Initializes the iterator pointing to nothing.
bool value(Number &number, const MultiContainer &container, Tag tag)
Finds a tag-value entry in the given collection by the given tag and returns its value component tran...
#define ONIXS_CMEMDHFIX_NAMESPACE_END
Definition: Bootstrap.h:71
bool valueOrDefault(Number &number, const MultiContainer &container, Tag tag, Number defaultValue=Number())
Finds a tag-value entry in the given collection by the given tag and returns its value component tran...
void tag(Tag tag)
Updates tag component.
TagValue(Tag tag, const Value &value)
Initializes instance according to the given attributes.
const Char * items() const
Read-only content.
Definition: String.h:77
std::vector< TagValue > TagValues
Sequence of tag-value pairs.
Provides efficient way of accessing text-based values without copying content of the text being refer...
Definition: String.h:41
Iterator first(Key tag) const
Returns iterator pointing to the first (of possibly multiple) item having the given key (tag) value...
TagValue::Value Value
Alias for value type.
TagValueSelectionIterator & operator++()
Advances the given instance to the next tag-value instance in the selection or invalidates the instan...
bool fromStr(Decimal &, const Char *, size_t)
Deserializes a decimal number from the given text presentation.
MultiContainer()
Initializes an empty instance.
#define ONIXS_CMEMDH_EXPORTED
Definition: Compiler.h:160
const Value & value() const
Value component of the pair.
UInt32 Tag
The type whose values are used to locate fields in the FIX-like messages.
Definition: Tag.h:29
Iterator begin() const
Returns iterator pointing to the first item of the container.
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition: Bootstrap.h:47
const TagValue & operator*() const
Returns instance of the tag-value pair referenced by the given instance.
TagValueSelectionIterator SelectionIterator
Iterator over items having the same tag value.
std::string toStr(const MultiContainer &container)
Serializes the given tag-value pair.
Sequence of tag-value pairs preserving order of pairs and allowing presence of multiple tag-value pai...
TagValue()
Initialies instance with zero tag and empty value.
TagValues::const_iterator TagValueConstIterator
Constant iterator over the tag-value sequence.
Tag Key
Alias for tag component which serves like an entry key.
TagValues::iterator TagValueIterator
Iterator over the tag-value sequence.
size_t size() const
Number of chars.
Definition: String.h:83
void reset()
Resets the reference to nothing.
Definition: String.h:101
void value(const Value &value)
Updates value component.