OnixS C++ CME iLink 3 Binary Order Entry Handler  1.18.3
API Documentation
Field.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 
25 
27 
28 /// A field in a tag-based message.
30 {
31 public:
32  /// Initializes a field with no value.
34  : value_()
35  , converter_(&NullConverter::self())
36  {
37  }
38 
39  /// Tells whether the field has
40  /// an associated (non-null) value.
42  {
43  return (converter_ != &NullConverter::self());
44  }
45 
46  /// \return a string presentation.
47  ONIXS_ILINK3_EXPLICIT ONIXS_ILINK3_NODISCARD operator std::string() const
48  {
49  return toString();
50  }
51 
52  /// Compares the presentation for
53  /// equality with the given text reference.
55  {
56  ONIXS_ILINK3_CONST_OR_CONSTEXPR size_t bufSize = 512;
57  Char buffer[bufSize];
58 
59  return this->toString(buffer, bufSize) == r;
60  }
61 
62  /// Compares the presentation for not equality with given text reference.
64  {
65  return !this->operator==(r);
66  }
67 
68  /// Casts the stored value to the requested type.
69  ///
70  /// The value may be converted into the value of the
71  /// requested type if a conversion is possible. Otherwise,
72  /// an exception is thrown. For example, if the field value
73  /// represents a reference to a string (e.g. instance of the
74  /// StrRef class), then it can be successfully converted into
75  /// a number if the string referred represents a number.
76  ///
77  /// @throw std::runtime_error if conversion is impossible.
78  template
79  <
80  class Value
81  >
83  typename
85  {
87 
88  assert(converter_);
89 
90  return convert(*converter_, value_);
91  }
92 
93  /// Tries to cast the stored value into a value of the StrRef type.
94  ///
95  /// \return `false` if the conversion fails.
97  {
98  assert(converter_);
99 
100  return converter_->convert(str, value_);
101  }
102 
103  /// Tries to cast the stored value into a
104  /// value of the Char type.
105  ///
106  /// \return `false` if the conversion fails.
107  ONIXS_ILINK3_NODISCARD bool toChar(Char& value) const
108  {
109  assert(converter_);
110 
111  return converter_->convert(value, value_);
112  }
113 
114  /// Tries to cast the stored value into a
115  /// value of the Int8 type.
116  ///
117  /// \return `false` if the conversion fails.
118  ONIXS_ILINK3_NODISCARD bool toNumber(Int8& value) const
119  {
120  assert(converter_);
121 
122  return converter_->convert(value, value_);
123  }
124 
125  /// Tries to cast the stored value into a
126  /// value of the UInt8 type.
127  ///
128  /// \return `false` if the conversion fails.
130  {
131  assert(converter_);
132 
133  return converter_->convert(value, value_);
134  }
135 
136  /// Tries to cast the stored value into a
137  /// value of the Int16 type.
138  ///
139  /// \return `false` if the conversion fails.
140  ONIXS_ILINK3_NODISCARD bool toNumber(Int16& value) const
141  {
142  assert(converter_);
143 
144  return converter_->convert(value, value_);
145  }
146 
147  /// Tries to cast the stored value into a
148  /// value of the UInt16 type.
149  ///
150  /// \return `false` if the conversion fails.
152  {
153  assert(converter_);
154 
155  return converter_->convert(value, value_);
156  }
157 
158  /// Tries to cast the stored value into a
159  /// value of the Int32 type.
160  ///
161  /// \return `false` if the conversion fails.
163  {
164  assert(converter_);
165 
166  return converter_->convert(value, value_);
167  }
168 
169  /// Tries to cast the stored value into a
170  /// value of the UInt32 type.
171  ///
172  /// \return `false` if the conversion fails.
174  {
175  assert(converter_);
176 
177  return converter_->convert(value, value_);
178  }
179 
180  /// Tries to cast the stored value into a
181  /// value of the Int64 type.
182  ///
183  /// \return `false` if the conversion fails.
184  ONIXS_ILINK3_NODISCARD bool toNumber(Int64& value) const
185  {
186  assert(converter_);
187 
188  return converter_->convert(value, value_);
189  }
190 
191  /// Tries to cast the stored value into a
192  /// value of the UInt64 type.
193  ///
194  /// \return `false` if the conversion fails.
196  {
197  assert(converter_);
198 
199  return converter_->convert(value, value_);
200  }
201 
202  /// Tries to cast the stored value into a
203  /// value of the Decimal type.
204  ///
205  /// \return `false` if the conversion fails.
207  {
208  assert(converter_);
209 
210  return converter_->convert(value, value_);
211  }
212 
213  /// Tries to cast the stored value into a
214  /// value of the Timestamp type.
215  ///
216  /// \return `false` if the conversion fails.
218  {
219  assert(converter_);
220 
221  return converter_->convert(value, value_);
222  }
223 
224  /// Tries to cast the stored value into a value
225  /// of the MaturityMonthYear type.
226  ///
227  /// \return `false` if the conversion fails.
229  {
230  assert(converter_);
231 
232  return converter_->convert(value, value_);
233  }
234 
235  /// Tries to cast the stored value into a value
236  /// of the given Enumeration type.
237  ///
238  /// \return `false` if the conversion fails.
239  template<class Enumeration>
240  ONIXS_ILINK3_NODISCARD bool toEnumeration(typename Enumeration::Enum& value) const
241  {
242  typename Enumeration::Base integral;
243 
244  assert(converter_);
245 
246  if (converter_->convert(integral,value_))
247  {
248  value = static_cast<typename Enumeration::Enum>(integral);
249 
250  return true;
251  }
252 
253  return false;
254  }
255 
256  /// Tries to cast the stored value into a value
257  /// of the type representing a BitSet.
258  ///
259  /// \return `false` if the conversion fails.
260  template <class BitSet>
261  ONIXS_ILINK3_NODISCARD bool toBitSet(BitSet& value) const
262  {
263  typename BitSet::Bits bits;
264 
265  assert(converter_);
266 
267  if (converter_->convert(bits, value_))
268  {
269  value = BitSet(bits);
270 
271  return true;
272  }
273 
274  return false;
275  }
276 
277  /// \return the text representation.
278  ONIXS_ILINK3_NODISCARD std::string toString() const
279  {
280  std::string str;
281 
282  toString(str);
283 
284  return str;
285  }
286 
287  /// Outputs the text representation into the given string.
288  void toString(std::string& str) const
289  {
290  assert(converter_);
291 
292  converter_->toStr(str, value_);
293  }
294 
295  /// \return the text presentation of the value
296  /// stored in the given container as a StrRef.
297  StrRef toString(Char* buf, size_t size) const
298  {
299  assert(converter_);
300 
301  return converter_->toStr(buf, size, value_);
302  }
303 
304 protected:
305  /// Initializes the field.
306  explicit Field(const ValueConverter& converter) ONIXS_ILINK3_NOTHROW
307  : value_()
308  , converter_(&converter)
309  {
310  }
311 
312  /// Exposes the value storage for further value manipulations.
314  {
315  return value_;
316  }
317 
318  /// Exposes the value storage for further value manipulations.
320  {
321  return value_;
322  }
323 
324 private:
325  ValueContainer value_;
326  const ValueConverter* converter_;
327 };
328 
329 inline bool operator == (const Field& ref, const std::string& str)
330 {
331  return ref == StrRef(str);
332 }
333 
334 inline bool operator != (const Field& ref, const std::string& str)
335 {
336  return ref != StrRef(str);
337 }
338 
339 inline bool operator == (const std::string& str, const Field & ref)
340 {
341  return ref == StrRef(str);
342 }
343 
344 inline bool operator != (const std::string& str, const Field& ref)
345 {
346  return ref != StrRef(str);
347 }
348 
349 inline bool operator == (const Field& ref, const char* str)
350 {
351  return ref == StrRef(str);
352 }
353 
354 inline bool operator != (const Field& ref, const char* str)
355 {
356  return ref != StrRef(str);
357 }
358 
359 inline bool operator == (const char* str, const Field& ref)
360 {
361  return ref == StrRef(str);
362 }
363 
364 inline bool operator != (const char* str, const Field& ref)
365 {
366  return ref != StrRef(str);
367 }
368 
369 /// Serializes into the given stream.
370 inline std::ostream& operator<<(std::ostream& stream, const Field& field)
371 {
372  return stream << field.toString();
373 }
374 
#define ONIXS_ILINK3_CONST_OR_CONSTEXPR
Definition: Compiler.h:178
bool toChar(Char &value) const
Tries to cast the stored value into a value of the Char type.
Definition: Field.h:107
std::ostream & operator<<(std::ostream &stream, const Field &field)
Serializes into the given stream.
Definition: Field.h:370
bool toNumber(UInt8 &value) const
Tries to cast the stored value into a value of the UInt8 type.
Definition: Field.h:129
std::basic_string_view< Char > StrRef
Definition: StrRef.h:46
bool toBitSet(BitSet &value) const
Tries to cast the stored value into a value of the type representing a BitSet.
Definition: Field.h:261
char Char
Character type alias.
Definition: String.h:30
A real number with a floating exponent.
Definition: Decimal.h:32
#define ONIXS_ILINK3_EXPLICIT
Definition: Compiler.h:177
bool toNumber(UInt32 &value) const
Tries to cast the stored value into a value of the UInt32 type.
Definition: Field.h:173
#define ONIXS_ILINK3_MESSAGING_TAGBASED_NAMESPACE_END
Definition: ABI.h:153
bool toNumber(UInt16 &value) const
Tries to cast the stored value into a value of the UInt16 type.
Definition: Field.h:151
ValueContainer & value() noexcept
Exposes the value storage for further value manipulations.
Definition: Field.h:319
UInt16 UInt16
uInt16.
Definition: Fields.h:296
StrRef toString(Char *buf, size_t size) const
Definition: Field.h:297
ValueConversion< Value >::Result cast() const
Casts the stored value to the requested type.
Definition: Field.h:84
bool operator!=(const char *str, const Field &ref)
Definition: Field.h:364
Implements value conversion operations through value conversion traits.
#define ONIXS_ILINK3_LTWT_CLASS
Definition: ABI.h:84
A field in a tag-based message.
Definition: Field.h:29
bool toNumber(Decimal &value) const
Tries to cast the stored value into a value of the Decimal type.
Definition: Field.h:206
bool toNumber(Int16 &value) const
Tries to cast the stored value into a value of the Int16 type.
Definition: Field.h:140
bool toTimestamp(Timestamp &value) const
Tries to cast the stored value into a value of the Timestamp type.
Definition: Field.h:217
bool toNumber(Int32 &value) const
Tries to cast the stored value into a value of the Int32 type.
Definition: Field.h:162
void toString(std::string &str) const
Outputs the text representation into the given string.
Definition: Field.h:288
bool toStringRef(StrRef &str) const
Tries to cast the stored value into a value of the StrRef type.
Definition: Field.h:96
bool toEnumeration(typename Enumeration::Enum &value) const
Tries to cast the stored value into a value of the given Enumeration type.
Definition: Field.h:240
Field(const ValueConverter &converter) noexcept
Initializes the field.
Definition: Field.h:306
UInt64 UInt64
uInt64.
Definition: Fields.h:308
bool toNumber(UInt64 &value) const
Tries to cast the stored value into a value of the UInt64 type.
Definition: Field.h:195
bool toMaturityMonthYear(MaturityMonthYear &value) const
Tries to cast the stored value into a value of the MaturityMonthYear type.
Definition: Field.h:228
UInt32 UInt32
uInt32.
Definition: Fields.h:302
bool operator==(const char *str, const Field &ref)
Definition: Field.h:359
bool toNumber(Int8 &value) const
Tries to cast the stored value into a value of the Int8 type.
Definition: Field.h:118
const ValueContainer & value() const noexcept
Exposes the value storage for further value manipulations.
Definition: Field.h:313
The time point without the time-zone information.
Definition: Time.h:467
bool toNumber(Int64 &value) const
Tries to cast the stored value into a value of the Int64 type.
Definition: Field.h:184
#define ONIXS_ILINK3_MESSAGING_TAGBASED_NAMESPACE_BEGIN
Definition: ABI.h:149
Abstraction gathering operations over a value of a particular type stored as a field in a message...
#define ONIXS_ILINK3_NODISCARD
Definition: Compiler.h:185
Field()
Initializes a field with no value.
Definition: Field.h:33
#define ONIXS_ILINK3_NOTHROW
Definition: Compiler.h:176
void convert(FixedPointDecimal< MantissaType, ExponentType > &res, const Decimal &number)