OnixS C++ FIX Engine  4.12.0
API Documentation
FlatMessage.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 #pragma once
21 
22 #include <iterator>
23 
28 
31 
33 
34 namespace OnixS {
35 namespace FIX {
36 ONIXS_FIXENGINE_API_DECL(class, Message);
37 ONIXS_FIXENGINE_API_DECL(class, FlatGroup);
38 
39 namespace Core {
40 namespace Messaging {
41 namespace Extras {
42 class FlatMessage;
43 }
44 }
45 }
46 
47 /// The insert mode of FlatMessage::insert() methods.
48 struct InsertMode
49 {
50  enum Enum
51  {
52  /// The new field will be inserted before the position tag.
54 
55  /// The new field will be inserted after the position tag.
56  After
57  };
58 };
59 
60 /// Field primary attributes (a tag and a reference to a value).
62 {
63 public:
64 
65  /// Initializes the field which refers to nothing.
67  : tag_(0), value_(ONIXS_FIXENGINE_NULLPTR), size_(0) {}
68 
69  /// Initializes all members.
71  Tag fieldTag,
72  const StringRef & fieldValue)
73  : tag_(fieldTag), value_(fieldValue.data()), size_(fieldValue.size()) {}
74 
75  /// Gets the field tag.
76  Tag tag() const { return tag_; }
77 
78  /// Gets the field value reference.
79  StringRef value() const { return StringRef(value_, size_); }
80 
81 private:
82 
83  /// The field tag.
84  Tag tag_;
85 
86  /// The field value reference.
87  const char * value_;
88 
89  /// The field value size.
90  size_t size_;
91 };
92 
93 /// Provides an access to FIX fields from a flat (tag=value) message.
94 ///
95 /// Fields can be accessed using temporary field references and using
96 /// special keys which remain constant during the lifetime of the single instance.
97 ///
98 /// To access a field, a reference must be obtained using the 'find' member.
99 /// The 'find' member searches for a field using a regular Tag identifier.
100 /// If the 'find' succeeds, a temporary reference is returned. That reference can
101 /// be either used to modify the field or to allocate a key for that field using
102 /// the 'allocateKey' member. Once the key is allocated it can be used like a tag
103 /// to quick access the field.
104 ///
105 /// There are pre-allocated keys to access service fields like MsgType, MsgSeqNum, SendingTime, etc.
106 ///
107 /// @note BodyLength and CheckSum fields are not synchronized each time other fields are updated.
108 /// Therefore, to bring the flat message (tag=value) into the valid state, it's necessary to invoke the 'adjust' member.
110 {
111 public:
112  /// Constructs the blank instance.
113  ///
114  /// @note This constructor does not support the zero-copy feature.
115  FlatMessage();
116 
117  /// Constructs an instance with empty required message header fields.
118  ///
119  /// @note This constructor does not support the zero-copy feature.
120  FlatMessage(OnixS::FIX::ProtocolVersion::Enum protocolVersion, const char * msgType);
121 
122  /// Constructs an instance from the tag=value form.
123  FlatMessage(const char * rawMessage, size_t rawMessageSize, bool useZeroCopyBuffer = true);
124 
125  /// Constructs an instance from the tag=value form without session-level fields.
126  /// Required fields will be added during the construction.
127  FlatMessage(
128  OnixS::FIX::ProtocolVersion::Enum protocolVersion,
129  const char * msgType,
130  const char * senderCompId,
131  const char * targetCompId,
132  const char * rawMessageWithoutHeaderTrailer,
133  size_t rawMessageWithoutHeaderTrailerSize,
134  bool useZeroCopyBuffer = true);
135 
136  /// Constructs an instance from the given Message object.
137  FlatMessage(const OnixS::FIX::Message & message, bool useZeroCopyBuffer = true);
138 
139  /// Initializes as a copy of the given instance.
140  FlatMessage(const FlatMessage & other);
141 
142 #ifdef ONIXS_FIXENGINE_CXX11
143 
144  /// The move constructor.
145  ///
146  /// @note The moved message should not be used after this.
147  ///
148  /// @param moved The message to be moved.
150 
151 #endif
152 
153  /// Utilizes internal resources.
154  ~FlatMessage();
155 
156  /// Indicates whether an instance refers to a valid set of fields.
157  ///
158  /// If the given instance doesn't refer to a valid fields set,
159  /// none of other members must be used.
160  bool valid() const;
161 
162  /// Returns the content of the flat message.
163  const char * chars() const;
164 
165  /// The size of the flat content.
166  size_t size() const;
167 
168  /// Returns a string that represents the flat message.
169  std::string toString() const;
170 
171  /// Looks for a field using the given tag number.
172  /// @return A valid reference in case of success, otherwise - an invalid one.
173  FlatFieldRef find(Tag) const;
174 
175  /// Looks for a field with assumption the field is located
176  /// after the given field using its tag number.
177  ///
178  /// The member is suitable to access same fields but from
179  /// different repeating group instances.
180  ///
181  /// @return A valid reference in case of success, otherwise - an invalid one.
182  FlatFieldRef find(Tag, const FlatFieldRef &) const;
183 
184  /// Allocates a key to the requested field for the further access.
185  FlatFieldKey allocateKey(const FlatFieldRef &);
186 
187  /// Finds and allocates a key to the requested field for the further access.
188  FlatFieldKey allocateKey(Tag);
189 
190  /// Provides an access to a field value by the given temporary reference.
191  StringRef operator[](const FlatFieldRef &) const;
192 
193  /// Provides an access to a field value by the given field key.
194  StringRef operator[](FlatFieldKey) const;
195 
196  /// Returns the reference to a repeating group - if exists.
197  ///
198  /// @param numberOfInstancesRef The reference of
199  /// the field that defines the number of instances
200  /// in this repeating group (the NoXXX field).
201  ///
202  /// @throw std::exception if the given field does not contain the number of instances.
203  FlatGroup getGroup(const FlatFieldRef & numberOfInstancesRef) const;
204 
205  /// Converts the StringRef value of the given FlatMessage to the FlatFieldRef object.
206  FlatFieldRef getFlatFieldRef(const StringRef & value) const;
207 
208  /// Updates the field value.
209  ///
210  /// @note Once the value is updated, only the reference used to
211  /// access the field remains valid, other references become invalid.
212  FlatMessage & set(FlatFieldRef &, const StringRef &);
213 
214  /// Updates the field value.
215  ///
216  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
217  FlatMessage & set(FlatFieldKey, const StringRef &);
218 
219  /// Updates the field value.
220  ///
221  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
222  FlatMessage & set(FlatFieldRef &, Char);
223 
224  /// Updates the field value.
225  ///
226  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
227  FlatMessage & set(FlatFieldKey, Char);
228 
229  /// Updates the field value.
230  ///
231  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
232  FlatMessage & set(FlatFieldRef &, Int32);
233 
234  /// Updates the field value.
235  ///
236  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
237  FlatMessage & set(FlatFieldKey, Int32);
238 
239  /// Updates the field value.
240  ///
241  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
242  FlatMessage & set(FlatFieldRef &, UInt32);
243 
244  /// Updates the field value.
245  ///
246  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
247  FlatMessage & set(FlatFieldKey, UInt32);
248 
249  /// Updates the field value.
250  ///
251  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
252  FlatMessage & set(FlatFieldRef &, Int64);
253 
254  /// Updates the field value.
255  ///
256  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
257  FlatMessage & set(FlatFieldKey, Int64);
258 
259  /// Updates the field value.
260  ///
261  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
262  FlatMessage & set(FlatFieldRef &, UInt64);
263 
264  /// Updates the field value.
265  ///
266  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
267  FlatMessage & set(FlatFieldKey, UInt64);
268 
269  /// Updates the field value.
270  ///
271  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
272  FlatMessage & set(FlatFieldRef &, const Decimal &);
273 
274  /// Updates the field value.
275  ///
276  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
277  FlatMessage & set(FlatFieldKey, const Decimal &);
278 
279  /// Updates the field value.
280  ///
281  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
282  FlatMessage & set(FlatFieldRef &, const Timestamp &, TimestampFormat::Enum);
283 
284  /// Updates the field value.
285  ///
286  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
287  FlatMessage & set(FlatFieldKey, const Timestamp &, TimestampFormat::Enum);
288 
289  /// Updates the field value.
290  ///
291  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
292  FlatMessage & set(FlatFieldRef &, const TimeSpan &, TimeSpanFormat::Enum);
293 
294  /// Updates the field value.
295  ///
296  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
297  FlatMessage & set(FlatFieldKey, const TimeSpan &, TimeSpanFormat::Enum);
298 
299  /// Updates the field value.
300  ///
301  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
302  FlatMessage & set(FlatFieldRef &, const FieldValueRef &);
303 
304  /// Updates the field value.
305  ///
306  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
307  FlatMessage & set(FlatFieldKey, const FieldValueRef &);
308 
309  /// Adds the tag/value pair to the end of the message.
310  ///
311  /// @note This method does not support the zero-copy feature.
312  FlatMessage & add(Tag, const StringRef &);
313 
314  /// Adds the tag/value pair to the end of the message.
315  ///
316  /// @note This method does not support the zero-copy feature.
317  FlatMessage & add(Tag, Char);
318 
319  /// Adds the tag/value pair to the end of the message.
320  ///
321  /// @note This method does not support the zero-copy feature.
322  FlatMessage & add(Tag, Int32);
323 
324  /// Adds the tag/value pair to the end of the message.
325  ///
326  /// @note This method does not support the zero-copy feature.
327  FlatMessage & add(Tag, UInt32);
328 
329  /// Adds the tag/value pair to the end of the message.
330  ///
331  /// @note This method does not support the zero-copy feature.
332  FlatMessage & add(Tag, Int64);
333 
334  /// Adds the tag/value pair to the end of the message.
335  ///
336  /// @note This method does not support the zero-copy feature.
337  FlatMessage & add(Tag, UInt64);
338 
339  /// Adds the tag/value pair to the end of the message.
340  ///
341  /// @note This method does not support the zero-copy feature.
342  FlatMessage & add(Tag, const Decimal &);
343 
344  /// Adds the tag/value pair to the end of the message.
345  ///
346  /// @note This method does not support the zero-copy feature.
347  FlatMessage & add(Tag, const Timestamp &, TimestampFormat::Enum);
348 
349  /// Adds the tag/value pair to the end of the message.
350  ///
351  /// @note This method does not support the zero-copy feature.
352  FlatMessage & add(Tag, const TimeSpan &, TimeSpanFormat::Enum);
353 
354  /// Inserts the tag/value pair to the message before or after the position field.
355  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
356  ///
357  /// @note This method does not support the zero-copy feature.
359 
360  /// Inserts the tag/value pair to the message before or after the position field.
361  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
362  ///
363  /// @note This method does not support the zero-copy feature.
365 
366  /// Inserts the tag/value pair to the message before or after the position field.
367  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
368  ///
369  /// @note This method does not support the zero-copy feature.
371 
372  /// Inserts the tag/value pair to the message before or after the position field.
373  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
374  ///
375  /// @note This method does not support the zero-copy feature.
377 
378  /// Inserts the tag/value pair to the message before or after the position field.
379  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
380  ///
381  /// @note This method does not support the zero-copy feature.
383 
384  /// Inserts the tag/value pair to the message before or after the position field.
385  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
386  ///
387  /// @note This method does not support the zero-copy feature.
389 
390  /// Inserts the tag/value pair to the message before or after the position field.
391  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
392  ///
393  /// @note This method does not support the zero-copy feature.
395 
396  /// Inserts the tag/value pair to the message before or after the position field.
397  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
398  ///
399  /// @note This method does not support the zero-copy feature.
401 
402  /// Inserts the tag/value pair to the message before or after the position field.
403  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
404  ///
405  /// @note This method does not support the zero-copy feature.
407 
408  /// Removes the field value.
409  ///
410  /// Once value is removed, all references
411  /// to other fields become invalid.
412  ///
413  /// @note This method does not support the zero-copy feature.
414  FlatMessage & remove(Tag);
415 
416  /// Updates BodyLength and CheckSum fields.
417  void adjust();
418 
419  /// Resets the instance to the new tag=value form.
420  ///
421  /// @note This method does not support the zero-copy feature.
422  void reset(const char * rawMessage, size_t rawMessageSize);
423 
424  /// Resets the instance to the blank state.
425  ///
426  /// @note This method does not support the zero-copy feature.
427  void reset();
428 
429  // Returns the `ProtocolVersion`, which corresponds to `BeginString` and 'ApplVerID' tag values.
430  ProtocolVersion::Enum version() const;
431 
432  /// A user data associated with the message.
433  ///
434  /// @return A pointer to data was previously
435  /// attached to the instance or NULL.
436  void * userData() const;
437 
438  /// Attaches a user data to the message.
439  void userData(void * data);
440 
441  /// Swaps the content with another instance.
442  void swap(FlatMessage & other) ONIXS_FIXENGINE_NOTHROW;
443 
444  /// Re-initializes as a copy of the given instance.
445  FlatMessage & operator = (const FlatMessage &);
446 
447 #ifdef ONIXS_FIXENGINE_CXX11
448 
449  /// The move assignment operator.
450  ///
451  /// @note The moved message should not be used after this.
453 
454 #endif
455 
456  /// The constant iterator to iterate over all fields in the given FlatMessage instance.
458  {
459  public:
460 
461  typedef std::forward_iterator_tag iterator_category;
463  typedef std::ptrdiff_t difference_type;
464  typedef FlatField * pointer;
465  typedef FlatField & reference;
466 
467  /// Initializes an iterator by a first field from which you need to iterate.
468  ConstIterator(const FlatMessage *, Tag);
469 
470  const FlatField & operator*() const {
471  return currentField_;
472  }
473 
474  const FlatField * operator->() const {
475  return &currentField_;
476  }
477 
478  bool operator == (const ConstIterator &) const;
479  bool operator != (const ConstIterator &) const;
480 
481  ConstIterator & operator ++();
482 
483  private:
484  /// The FlatMessage instance to iterate.
485  const FlatMessage * container_;
486 
487  /// The current field.
488  FlatField currentField_;
489  };
490 
491  /// Returns the constant iterator to the first field in the FlatMessage instance.
492  ConstIterator begin() const;
493 
494  /// Returns the constant iterator to the field after the last one in the FlatMessage instance.
495  ConstIterator end() const;
496 
497 private:
498  friend class MessageOperator;
499  friend class FlatMessageWrapper;
500 
501  FlatMessage(const OnixS::FIX::Core::Messaging::Extras::FlatMessage & other);
502 
503  OnixS::FIX::Core::Messaging::Extras::FlatMessage * impl_;
504  bool isOwned_;
505 };
506 
508 
509 inline
512 {
513  return find(tag, FlatFieldRef());
514 }
515 
516 /// The stream output.
518 std::ostream & operator << (std::ostream & os, const FlatMessage & message);
519 }
520 }
const FlatField * operator->() const
Definition: FlatMessage.h:474
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
std::forward_iterator_tag iterator_category
Definition: FlatMessage.h:461
#define ONIXS_FIXENGINE_NOTHROW
Definition: Compiler.h:43
unsigned int UInt32
Definition: Numeric.h:36
long long Int64
Definition: Numeric.h:38
const FlatField & operator*() const
Definition: FlatMessage.h:470
Represents a temporary reference to a field in an editable serialized message.
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
The constant iterator to iterate over all fields in the given FlatMessage instance.
Definition: FlatMessage.h:457
Provides an access to FIX fields from a flat (tag=value) message.
Definition: FlatMessage.h:109
Implements a concept of a read-only reference to a FIX field value.
Definition: FieldValueRef.h:32
Provides an efficient way of accessing text-based FIX field values.
Definition: StringRef.h:59
int Int32
Definition: Numeric.h:35
Key to a serialized field - represents another way of accessing fields in an editable serialized mess...
unsigned long long UInt64
Definition: Numeric.h:39
ONIXS_FIXENGINE_API_DECL(class, IEngineListener)
Field primary attributes (a tag and a reference to a value).
Definition: FlatMessage.h:61
unsigned Tag
Alias for tag numbers.
Definition: Tag.h:28
FlatMessage SerializedMessage
Definition: FlatMessage.h:507
The new field will be inserted before the position tag.
Definition: FlatMessage.h:53
The time span related functionality.
Definition: TimeSpan.h:93
bool operator==(const FieldValueRef &ref, const std::string &str)
The Decimal type for a better precision.
Definition: Numeric.h:47
StringRef value() const
Gets the field value reference.
Definition: FlatMessage.h:79
Tag tag() const
Gets the field tag.
Definition: FlatMessage.h:76
Encapsulates operations over a FIX Message.
Definition: Message.h:49
FlatField(Tag fieldTag, const StringRef &fieldValue)
Initializes all members.
Definition: FlatMessage.h:70
FlatFieldRef find(Tag) const
Looks for a field using the given tag number.
Definition: FlatMessage.h:511
The insert mode of FlatMessage::insert() methods.
Definition: FlatMessage.h:48
The timestamps related functionality.
Definition: Timestamp.h:91
Encapsulates operations over the FIX Repeating Group.
Definition: FlatGroup.h:75
FlatField()
Initializes the field which refers to nothing.
Definition: FlatMessage.h:66
char Char
Definition: Numeric.h:30
bool operator!=(const FieldValueRef &ref, const std::string &str)