OnixS C++ FIX Engine  4.10.1
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 /// The insert mode of FlatMessage::insert() methods.
40 struct InsertMode
41 {
42  enum Enum
43  {
44  /// The new field will be inserted before the position tag.
46 
47  /// The new field will be inserted after the position tag.
49  };
50 };
51 
52 /// Field primary attributes (a tag and a reference to a value).
54 {
55  /// The field tag.
57 
58  /// The field value reference.
60 
61  /// Initializes the field which refers to nothing.
63  : tag(0) {}
64 
65  /// Initializes all members.
67  Tag fieldTag,
68  const StringRef & fieldValue)
69  : tag(fieldTag), value(fieldValue) {}
70 };
71 
72 /// Provides an access to FIX fields from a flat (tag=value) message.
73 ///
74 /// Fields can be accessed using temporary field references and using
75 /// special keys which remain constant during the lifetime of the single instance.
76 ///
77 /// To access a field, a reference must be obtained using the 'find' member.
78 /// The 'find' member searches for a field using a regular Tag identifier.
79 /// If the 'find' succeeds, a temporary reference is returned. That reference can
80 /// be either used to modify the field or to allocate a key for that field using
81 /// the 'allocateKey' member. Once the key is allocated it can be used like a tag
82 /// to quick access the field.
83 ///
84 /// There are pre-allocated keys to access service fields like MsgType, MsgSeqNum, SendingTime, etc.
85 ///
86 /// @note BodyLength and CheckSum fields are not synchronized each time other fields are updated.
87 /// Therefore, to bring the flat message (tag=value) into the valid state, it's necessary to invoke the 'adjust' member.
89 {
90 public:
91  /// Constructs the blank instance.
92  ///
93  /// @note This constructor does not support the zero-copy feature.
94  FlatMessage();
95 
96  /// Constructs an instance with empty required message header fields.
97  ///
98  /// @note This constructor does not support the zero-copy feature.
99  FlatMessage(OnixS::FIX::ProtocolVersion::Enum protocolVersion, const char * msgType);
100 
101  /// Constructs an instance from the tag=value form.
102  FlatMessage(const char * rawMessage, size_t rawMessageSize, bool useZeroCopyBuffer = true);
103 
104  /// Constructs an instance from the tag=value form without session-level fields.
105  /// Required fields will be added during the construction.
106  FlatMessage(
107  OnixS::FIX::ProtocolVersion::Enum protocolVersion,
108  const char * msgType,
109  const char * senderCompId,
110  const char * targetCompId,
111  const char * rawMessageWithoutHeaderTrailer,
112  size_t rawMessageWithoutHeaderTrailerSize,
113  bool useZeroCopyBuffer = true);
114 
115  /// Constructs an instance from the given Message object.
116  FlatMessage(const OnixS::FIX::Message & message, bool useZeroCopyBuffer = true);
117 
118  /// Initializes as a copy of the given instance.
119  FlatMessage(const FlatMessage & other);
120 
121  /// Utilizes internal resources.
122  ~FlatMessage();
123 
124  /// Returns the content of the flat message.
125  const char * chars() const;
126 
127  /// The size of the flat content.
128  size_t size() const;
129 
130  /// Returns a string that represents the flat message.
131  std::string toString() const;
132 
133  /// Looks for a field using the given tag number.
134  /// @return A valid reference in case of success, otherwise - an invalid one.
135  FlatFieldRef find(Tag) const;
136 
137  /// Looks for a field with assumption the field is located
138  /// after the given field using its tag number.
139  ///
140  /// The member is suitable to access same fields but from
141  /// different repeating group instances.
142  ///
143  /// @return A valid reference in case of success, otherwise - an invalid one.
144  FlatFieldRef find(Tag, const FlatFieldRef &) const;
145 
146  /// Allocates a key to the requested field for the further access.
147  FlatFieldKey allocateKey(const FlatFieldRef &);
148 
149  /// Finds and allocates a key to the requested field for the further access.
150  FlatFieldKey allocateKey(Tag);
151 
152  /// Provides an access to a field value by the given temporary reference.
153  StringRef operator[](const FlatFieldRef &) const;
154 
155  /// Provides an access to a field value by the given field key.
156  StringRef operator[](FlatFieldKey) const;
157 
158  /// Returns the reference to a repeating group - if exists.
159  ///
160  /// @param numberOfInstancesRef The reference of
161  /// the field that defines the number of instances
162  /// in this repeating group (the NoXXX field).
163  ///
164  /// @throw std::exception if the given field does not contain the number of instances.
165  FlatGroup getGroup(const FlatFieldRef & numberOfInstancesRef) const;
166 
167  /// Converts the StringRef value of the given FlatMessage to the FlatFieldRef object.
168  FlatFieldRef getFlatFieldRef(const StringRef & value) const;
169 
170  /// Updates the field value.
171  ///
172  /// @note Once the value is updated, only the reference used to
173  /// access the field remains valid, other references become invalid.
174  FlatMessage & set(FlatFieldRef &, const StringRef &);
175 
176  /// Updates the field value.
177  ///
178  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
179  FlatMessage & set(FlatFieldKey, const StringRef &);
180 
181  /// Updates the field value.
182  ///
183  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
184  FlatMessage & set(FlatFieldRef &, Char);
185 
186  /// Updates the field value.
187  ///
188  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
189  FlatMessage & set(FlatFieldKey, Char);
190 
191  /// Updates the field value.
192  ///
193  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
194  FlatMessage & set(FlatFieldRef &, Int32);
195 
196  /// Updates the field value.
197  ///
198  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
199  FlatMessage & set(FlatFieldKey, Int32);
200 
201  /// Updates the field value.
202  ///
203  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
204  FlatMessage & set(FlatFieldRef &, UInt32);
205 
206  /// Updates the field value.
207  ///
208  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
209  FlatMessage & set(FlatFieldKey, UInt32);
210 
211  /// Updates the field value.
212  ///
213  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
214  FlatMessage & set(FlatFieldRef &, Int64);
215 
216  /// Updates the field value.
217  ///
218  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
219  FlatMessage & set(FlatFieldKey, Int64);
220 
221  /// Updates the field value.
222  ///
223  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
224  FlatMessage & set(FlatFieldRef &, UInt64);
225 
226  /// Updates the field value.
227  ///
228  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
229  FlatMessage & set(FlatFieldKey, UInt64);
230 
231  /// Updates the field value.
232  ///
233  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
234  FlatMessage & set(FlatFieldRef &, const Decimal &);
235 
236  /// Updates the field value.
237  ///
238  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
239  FlatMessage & set(FlatFieldKey, const Decimal &);
240 
241  /// Updates the field value.
242  ///
243  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
244  FlatMessage & set(FlatFieldRef &, const Timestamp &, TimestampFormat::Enum);
245 
246  /// Updates the field value.
247  ///
248  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
249  FlatMessage & set(FlatFieldKey, const Timestamp &, TimestampFormat::Enum);
250 
251  /// Updates the field value.
252  ///
253  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
254  FlatMessage & set(FlatFieldRef &, const TimeSpan &, TimeSpanFormat::Enum);
255 
256  /// Updates the field value.
257  ///
258  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
259  FlatMessage & set(FlatFieldKey, const TimeSpan &, TimeSpanFormat::Enum);
260 
261  /// Updates the field value.
262  ///
263  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
264  FlatMessage & set(FlatFieldRef &, const FieldValueRef &);
265 
266  /// Updates the field value.
267  ///
268  /// @note Once the value is updated, only the reference used to access the field remains valid, other references become invalid.
269  FlatMessage & set(FlatFieldKey, const FieldValueRef &);
270 
271  /// Adds the tag/value pair to the end of the message.
272  ///
273  /// @note This method does not support the zero-copy feature.
274  FlatMessage & add(Tag, const StringRef &);
275 
276  /// Adds the tag/value pair to the end of the message.
277  ///
278  /// @note This method does not support the zero-copy feature.
279  FlatMessage & add(Tag, Char);
280 
281  /// Adds the tag/value pair to the end of the message.
282  ///
283  /// @note This method does not support the zero-copy feature.
284  FlatMessage & add(Tag, Int32);
285 
286  /// Adds the tag/value pair to the end of the message.
287  ///
288  /// @note This method does not support the zero-copy feature.
289  FlatMessage & add(Tag, UInt32);
290 
291  /// Adds the tag/value pair to the end of the message.
292  ///
293  /// @note This method does not support the zero-copy feature.
294  FlatMessage & add(Tag, Int64);
295 
296  /// Adds the tag/value pair to the end of the message.
297  ///
298  /// @note This method does not support the zero-copy feature.
299  FlatMessage & add(Tag, UInt64);
300 
301  /// Adds the tag/value pair to the end of the message.
302  ///
303  /// @note This method does not support the zero-copy feature.
304  FlatMessage & add(Tag, const Decimal &);
305 
306  /// Adds the tag/value pair to the end of the message.
307  ///
308  /// @note This method does not support the zero-copy feature.
309  FlatMessage & add(Tag, const Timestamp &, TimestampFormat::Enum);
310 
311  /// Adds the tag/value pair to the end of the message.
312  ///
313  /// @note This method does not support the zero-copy feature.
314  FlatMessage & add(Tag, const TimeSpan &, TimeSpanFormat::Enum);
315 
316  /// Inserts the tag/value pair to the message before or after the position field.
317  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
318  ///
319  /// @note This method does not support the zero-copy feature.
321 
322  /// Inserts the tag/value pair to the message before or after the position field.
323  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
324  ///
325  /// @note This method does not support the zero-copy feature.
327 
328  /// Inserts the tag/value pair to the message before or after the position field.
329  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
330  ///
331  /// @note This method does not support the zero-copy feature.
333 
334  /// Inserts the tag/value pair to the message before or after the position field.
335  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
336  ///
337  /// @note This method does not support the zero-copy feature.
339 
340  /// Inserts the tag/value pair to the message before or after the position field.
341  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
342  ///
343  /// @note This method does not support the zero-copy feature.
345 
346  /// Inserts the tag/value pair to the message before or after the position field.
347  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
348  ///
349  /// @note This method does not support the zero-copy feature.
351 
352  /// Inserts the tag/value pair to the message before or after the position field.
353  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
354  ///
355  /// @note This method does not support the zero-copy feature.
357 
358  /// Inserts the tag/value pair to the message before or after the position field.
359  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
360  ///
361  /// @note This method does not support the zero-copy feature.
363 
364  /// Inserts the tag/value pair to the message before or after the position field.
365  /// If the position tag is not found, it adds the tag/value pair to the end of the message.
366  ///
367  /// @note This method does not support the zero-copy feature.
369 
370  /// Removes the field value.
371  ///
372  /// Once value is removed, all references
373  /// to other fields become invalid.
374  ///
375  /// @note This method does not support the zero-copy feature.
376  FlatMessage & remove(Tag);
377 
378  /// Updates BodyLength and CheckSum fields.
379  void adjust();
380 
381  /// Resets the instance to the new tag=value form.
382  ///
383  /// @note This method does not support the zero-copy feature.
384  void reset(const char * rawMessage, size_t rawMessageSize);
385 
386  /// Resets the instance to the blank state.
387  ///
388  /// @note This method does not support the zero-copy feature.
389  void reset();
390 
391  // Returns the `ProtocolVersion`, which corresponds to `BeginString` and 'ApplVerID' tag values.
392  ProtocolVersion::Enum version() const;
393 
394  /// A user data associated with the message.
395  ///
396  /// @return A pointer to data was previously
397  /// attached to the instance or NULL.
398  void * userData() const;
399 
400  /// Attaches a user data to the message.
401  void userData(void * data);
402 
403  /// Re-initializes as a copy of the given instance.
404  FlatMessage & operator = (const FlatMessage &);
405 
406  /// The constant iterator to iterate over all fields in the given FlatMessage instance.
408  {
409  public:
410 
411  typedef std::forward_iterator_tag iterator_category;
414  typedef FlatField * pointer;
415  typedef FlatField & reference;
416 
417  /// Initializes an iterator by a first field from which you need to iterate.
418  ConstIterator(const FlatMessage *, Tag);
419 
420  const FlatField & operator*() const {
421  return currentField_;
422  }
423 
424  const FlatField * operator->() const {
425  return &currentField_;
426  }
427 
428  bool operator == (const ConstIterator &) const;
429  bool operator != (const ConstIterator &) const;
430 
431  ConstIterator & operator ++();
432 
433  private:
434  /// The FlatMessage instance to iterate.
435  const FlatMessage * container_;
436 
437  /// The current field.
438  FlatField currentField_;
439  };
440 
441  /// Returns the constant iterator to the first field in the FlatMessage instance.
442  ConstIterator begin() const;
443 
444  /// Returns the constant iterator to the field after the last one in the FlatMessage instance.
445  ConstIterator end() const;
446 
447 private:
448  friend class MessageOperator;
449  friend class FlatMessageWrapper;
450 
451  FlatMessage(const OnixS::FIX::Core::Messaging::Extras::FlatMessage & other);
452 
453  OnixS::FIX::Core::Messaging::Extras::FlatMessage * impl_;
454  const bool isOwned_;
455 };
456 
458 
459 inline
462 {
463  return find(tag, FlatFieldRef());
464 }
465 
466 /// The stream output.
468 std::ostream & operator << (std::ostream & os, const FlatMessage & message);
469 }
470 }
const FlatField * operator->() const
Definition: FlatMessage.h:424
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
std::forward_iterator_tag iterator_category
Definition: FlatMessage.h:411
unsigned int UInt32
Definition: Numeric.h:36
StringRef value
The field value reference.
Definition: FlatMessage.h:59
long long Int64
Definition: Numeric.h:38
const FlatField & operator*() const
Definition: FlatMessage.h:420
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:407
Provides an access to FIX fields from a flat (tag=value) message.
Definition: FlatMessage.h:88
Implements a concept of a read-only reference to a FIX field value.
Definition: FieldValueRef.h:32
Tag tag
The field tag.
Definition: FlatMessage.h:56
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)
unsigned Tag
Alias for tag numbers.
Definition: Tag.h:28
The new field will be inserted after the position tag.
Definition: FlatMessage.h:48
FlatMessage SerializedMessage
Definition: FlatMessage.h:457
FlatField(Tag fieldTag, const StringRef &fieldValue)
Initializes all members.
Definition: FlatMessage.h:66
The new field will be inserted before the position tag.
Definition: FlatMessage.h:45
The time span related functionality.
Definition: TimeSpan.h:93
bool operator==(const FieldValueRef &ref, const std::string &str)
Field primary attributes (a tag and a reference to a value).
Definition: FlatMessage.h:53
The Decimal type for a better precision.
Definition: Numeric.h:47
Encapsulates operations over a FIX Message.
Definition: Message.h:49
FlatFieldRef find(Tag) const
Looks for a field using the given tag number.
Definition: FlatMessage.h:461
The insert mode of FlatMessage::insert() methods.
Definition: FlatMessage.h:40
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:62
char Char
Definition: Numeric.h:30
bool operator!=(const FieldValueRef &ref, const std::string &str)