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