OnixS C++ CME MDP Premium Market Data Handler 5.9.0
API Documentation
Loading...
Searching...
No Matches
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
30
32
35{
36 Tag tag_;
37 StrRef value_;
38
39public:
41 typedef StrRef Value;
42
46 : tag_(0)
47 , value_()
48 {
49 }
50
54 : tag_(tag)
55 , value_(value)
56 {
57 }
58
60 Tag tag() const
61 {
62 return tag_;
63 }
64
66 void tag(Tag tag)
67 {
68 tag_ = tag;
69 }
70
72 const Value& value() const
73 {
74 return value_;
75 }
76
78 void value(const Value& value)
79 {
80 value_ = value;
81 }
82
84 void value(const Char* data, size_t size)
85 {
86 value_.reset(data, size);
87 }
88};
89
92void toStr(std::string&, const TagValue&);
93
95inline std::string toStr(const TagValue& tagValue)
96{
97 std::string str;
98
99 toStr(str, tagValue);
100
101 return str;
102}
103
104//
105
107typedef std::vector<TagValue> TagValues;
108
110typedef TagValues::iterator TagValueIterator;
111
113typedef TagValues::const_iterator TagValueConstIterator;
114
115//
116
120{
121 Tag tag_;
122
123public:
125 explicit TagValueSelector(Tag tag)
126 : tag_(tag)
127 {
128 }
129
132 bool operator()(const TagValue& tagValue) const
133 {
134 return (tagValue.tag() == tag_);
135 }
136};
137
138//
139
142{
143 const TagValues* container_;
145
146public:
149 : container_(ONIXS_CMEMDH_NULLPTR)
150 , ref_()
151 {
152 }
153
156 TagValueSelectionIterator(const TagValues& container, Tag selection)
157 : container_(&container)
158 , ref_(std::find_if(container.begin(), container.end(), TagValueSelector(selection)))
159 {
160 }
161
164 operator bool() const
165 {
166 return (container_ && ref_ != container_->end());
167 }
168
171 const TagValue& operator*() const
172 {
173 assert(static_cast<bool>(*this));
174
175 return *ref_;
176 }
177
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
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
213public:
216 typedef Tag Key;
217
220
222 typedef TagValues::const_iterator Iterator;
223
226
229
232
236 {
237 return tagValues_.begin();
238 }
239
242 Iterator end() const
243 {
244 return tagValues_.end();
245 }
246
250 Iterator first(Key tag) const
251 {
252 return std::find_if(tagValues_.begin(), tagValues_.end(), TagValueSelector(tag));
253 }
254
258 {
259 return SelectionIterator(tagValues_, tag);
260 }
261
264 void deserialize(const Char* fixStr, size_t fixSize)
265 {
266 tagValues_.clear();
267
268 deserialize(tagValues_, fixStr, fixSize);
269 }
270};
271
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
296template <class Number>
297bool 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
311template <class Number>
312bool 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
330void toStr(std::string&, const MultiContainer&);
331
333inline std::string toStr(const MultiContainer& container)
334{
335 std::string str;
336
337 toStr(str, container);
338
339 return str;
340}
341
342//
343
#define ONIXS_CMEMDHFIX_NAMESPACE_BEGIN
Definition Bootstrap.h:70
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:46
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition Bootstrap.h:47
#define ONIXS_CMEMDHFIX_NAMESPACE_END
Definition Bootstrap.h:71
#define ONIXS_CMEMDH_NULLPTR
Definition Compiler.h:178
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:171
Sequence of tag-value pairs preserving order of pairs and allowing presence of multiple tag-value pai...
Iterator begin() const
Returns iterator pointing to the first item of the container.
MultiContainer()
Initializes an empty instance.
~MultiContainer()
Finalizes the instance.
Iterator first(Key tag) const
Returns iterator pointing to the first (of possibly multiple) item having the given key (tag) value.
SelectionIterator all(Key tag) const
Allows to iterate all items in the container having the given tag value.
void deserialize(const Char *fixStr, size_t fixSize)
Full-fill collection from the FIX (tag=value) string presentation.
Iterator end() const
Returns iterator pointing to the item beyond the last one.
TagValueSelectionIterator SelectionIterator
Iterator over items having the same tag value.
Tag Key
Alias for tag component which serves like an entry key.
TagValues::const_iterator Iterator
Iterator over container items.
TagValue::Value Value
Alias for value type.
Iterator over tag-value pairs having the same tag value.
const TagValue & operator*() const
Returns instance of the tag-value pair referenced by the given instance.
TagValueSelectionIterator(const TagValues &container, Tag selection)
Initializes the instance to iterate tag-values with the given tag value for the given collection.
TagValueSelectionIterator()
Initializes the iterator pointing to nothing.
TagValueSelectionIterator & operator++()
Advances the given instance to the next tag-value instance in the selection or invalidates the instan...
Functor to be used in algorithms to find tag-value pair in collections by tag component.
bool operator()(const TagValue &tagValue) const
Checks whether tag component of the given tag-value matches value stored by the functor.
TagValueSelector(Tag tag)
Initializes functor with the given tag.
Represents a pair of a tag and a value.
TagValue()
Initialies instance with zero tag and empty value.
void value(const Char *data, size_t size)
Updates value component.
Tag tag() const
Tag component of the pair.
StrRef Value
Alias for the value type.
void value(const Value &value)
Updates value component.
TagValue(Tag tag, const Value &value)
Initializes instance according to the given attributes.
void tag(Tag tag)
Updates tag component.
const Value & value() const
Value component of the pair.
Provides efficient way of accessing text-based values without copying content of the text being refer...
Definition String.h:42
const Char * items() const
Read-only content.
Definition String.h:77
size_t size() const
Number of chars.
Definition String.h:83
bool empty() const
Indicates whether the referenced text is empty.
Definition String.h:71
UInt32 Tag
The type whose values are used to locate fields in the FIX-like messages.
Definition Tag.h:29
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition Message.h:318
TagValues::const_iterator TagValueConstIterator
Constant iterator over the tag-value sequence.
std::vector< TagValue > TagValues
Sequence of tag-value pairs.
TagValues::iterator TagValueIterator
Iterator over the tag-value sequence.
MultiContainer::Value valueOrDefault(const MultiContainer &container, Tag tag, const MultiContainer::Value &defaultValue=MultiContainer::Value())
Finds a tag-value entry in the given collection by the given tag and returns its value component.
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...
bool fromStr(Decimal &, const Char *, size_t)
Deserializes a decimal number from the given text presentation.
char Char
Character type alias.
Definition String.h:36