OnixS C++ CME MDP Premium Market Data Handler 5.9.0
Users' manual and 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
105typedef std::vector<TagValue> TagValues;
106
108typedef TagValues::iterator TagValueIterator;
109
111typedef TagValues::const_iterator TagValueConstIterator;
112
116{
117 Tag tag_;
118
119public:
121 explicit TagValueSelector(Tag tag)
122 : tag_(tag)
123 {
124 }
125
128 bool operator()(const TagValue& tagValue) const
129 {
130 return (tagValue.tag() == tag_);
131 }
132};
133
136{
137 const TagValues* container_;
139
140public:
143 : container_(ONIXS_CMEMDH_NULLPTR)
144 , ref_()
145 {
146 }
147
150 TagValueSelectionIterator(const TagValues& container, Tag selection)
151 : container_(&container)
152 , ref_(std::find_if(container.begin(), container.end(), TagValueSelector(selection)))
153 {
154 }
155
158 operator bool() const
159 {
160 return (container_ && ref_ != container_->end());
161 }
162
165 const TagValue& operator*() const
166 {
167 assert(static_cast<bool>(*this));
168
169 return *ref_;
170 }
171
177 {
178 assert(static_cast<bool>(*this));
179
180 const Tag selection = ref_->tag();
181
182 ref_ = std::find_if(++ref_, container_->end(), TagValueSelector(selection));
183
184 return *this;
185 }
186};
187
197{
198 TagValues tagValues_;
199
200 // Builds sequence of tag-value pairs
201 // from the given FIX (tag=value) string.
203 static void deserialize(TagValues&, const Char*, size_t);
204
205public:
208 typedef Tag Key;
209
212
214 typedef TagValues::const_iterator Iterator;
215
218
221
224
228 {
229 return tagValues_.begin();
230 }
231
234 Iterator end() const
235 {
236 return tagValues_.end();
237 }
238
242 Iterator first(Key tag) const
243 {
244 return std::find_if(tagValues_.begin(), tagValues_.end(), TagValueSelector(tag));
245 }
246
250 {
251 return SelectionIterator(tagValues_, tag);
252 }
253
256 void deserialize(const Char* fixStr, size_t fixSize)
257 {
258 tagValues_.clear();
259
260 deserialize(tagValues_, fixStr, fixSize);
261 }
262};
263
267 const MultiContainer& container,
268 Tag tag,
269 const MultiContainer::Value& defaultValue = MultiContainer::Value()
270)
271{
272 const MultiContainer::Iterator result = container.first(tag);
273
274 if (result != container.end())
275 {
276 return result->value();
277 }
278
279 return defaultValue;
280}
281
288template <class Number>
289bool value(Number& number, const MultiContainer& container, Tag tag)
290{
291 const MultiContainer::Value strValue = valueOrDefault(container, tag);
292
293 return fromStr(number, strValue.items(), strValue.size());
294}
295
303template <class Number>
304bool valueOrDefault(Number& number, const MultiContainer& container, Tag tag, Number defaultValue = Number())
305{
306 const MultiContainer::Value strValue = valueOrDefault(container, tag);
307
308 if (strValue.empty())
309 {
310 number = defaultValue;
311
312 return true;
313 }
314
315 return fromStr(number, strValue.items(), strValue.size());
316}
317
320void toStr(std::string&, const MultiContainer&);
321
323inline std::string toStr(const MultiContainer& container)
324{
325 std::string str;
326
327 toStr(str, container);
328
329 return str;
330}
331
#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
MultiContainer()
Initializes an empty instance.
~MultiContainer()
Finalizes the instance.
SelectionIterator all(Key tag) const
void deserialize(const Char *fixStr, size_t fixSize)
TagValueSelectionIterator SelectionIterator
Iterator over items having the same tag value.
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.
TagValueSelectionIterator(const TagValues &container, Tag selection)
TagValueSelectionIterator()
Initializes the iterator pointing to nothing.
bool operator()(const TagValue &tagValue) const
TagValueSelector(Tag tag)
Initializes functor with the given tag.
Represents a pair of a tag and a 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)
void tag(Tag tag)
Updates tag component.
const Value & value() const
Value component of the pair.
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
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())
bool value(Number &number, const MultiContainer &container, Tag tag)
bool fromStr(Decimal &, const Char *, size_t)
char Char
Character type alias.
Definition String.h:36