OnixS C++ FIX Engine  4.11.0
API Documentation
Message.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 <iosfwd>
23 #include <string>
24 
26 
29 
33 
35 
36 namespace OnixS {
37 namespace FIX {
38 /// Encapsulates operations over a FIX Message.
39 ///
40 /// Message supports the 'unconstructed' state which can be
41 /// treated as a pointer in the null state. However, in contrast to
42 /// OnixS::FIX::Group and OnixS::FIX::GroupInstance classes it
43 /// does NOT represent a light-weight wrapper over internal structures.
44 /// In fact, it holds all the data which is fully copied on an assignment
45 /// or a copy construction and disposed at the instance destruction.
46 ///
47 /// FIX field related operations now available via the OnixS::FIX::FieldSet
48 /// class from which the OnixS::FIX::Message class is now derived.
49 class
51  Message : public FieldSet
52 {
53 public:
54  /// Initializes a message in the unconstructed state.
55  ///
56  /// @param expectedMessageSize An expected size of the FIX message.
57  /// The 'expectedMessageSize' parameter is used as a hint for the optimization of the manipulation (parsing, setting, etc.)
58  /// of large FIX messages with a lot of repeating groups.
59  ///
60  /// @note The large value of the 'expectedMessageSize' parameter can increase the memory consumption by the `Message` object,
61  /// so try to increase the value only in case of a latency degradation of the large FIX messages manipulation.
62  explicit Message(size_t expectedMessageSize = DefaultExpectedMessageSize);
63 
64  /// Constructs a FIX message of the given type which belongs to
65  /// the given dictionary of the FIX protocol (messaging specification).
66  ///
67  /// @param type Defines the type of the message (MsgType field value).
68  /// @param dictionary The FIX protocol dictionary to which the message belongs to.
69  /// @param expectedMessageSize An expected size of the FIX message.
70  /// The 'expectedMessageSize' parameter is used as a hint for the optimization of the manipulation (parsing, setting, etc.)
71  /// of large FIX messages with a lot of repeating groups.
72  ///
73  /// @note The large value of the 'expectedMessageSize' parameter can increase the memory consumption by the `Message` object,
74  /// so try to increase the value only in case of a latency degradation of the large FIX messages manipulation.
75  ///
76  /// @warning The type of the message can't be changed by updating the MsgType
77  /// field value. Changing the MsgType field value have no effect for the known
78  /// message which are defined by the dictionary associated with message.
79  Message(const char * type, const Dictionary & dictionary, size_t expectedMessageSize = DefaultExpectedMessageSize);
80 
81  /// Initializes an instance as a deep copy of other one.
82  ///
83  /// @param other The message to be copied from.
84  Message(const Message & other);
85 
86 #ifdef ONIXS_FIXENGINE_CXX11
87 
88  /// The move constructor.
89  ///
90  /// @note The moved message should not be used after this.
91  ///
92  /// @param moved The message to be moved.
94 
95 #endif
96 
97  /// Disposes all internal data structures.
98  ///
99  /// @warning Once an instance is destructed, all instances of
100  /// OnixS::FIX::Group and OnixS::FIX::GroupInstance classes
101  /// obtained from this instance must not be used any more.
102  virtual ~Message();
103 
104  /// An instance of the FIX dictionary or standard FIX messages
105  /// dictionary to which the message belongs to.
106  Dictionary dictionary() const;
107 
108  /// Returns the message type (MsgType(35) field value).
109  FieldValueRef type() const;
110 
111  /// Returns the message sequence number
112  /// (the MsgSeqNum (tag=34) field value).
113  SequenceNumber seqNum() const;
114 
115  /// Sets the message sequence number
116  /// (the MsgSeqNum (tag=34) field value).
117  void seqNum(SequenceNumber value);
118 
119  /// Returns the assigned value used to identify
120  /// the firm sending message (SenderCompID (49) field value).
121  FieldValueRef senderCompId() const;
122 
123  /// Sets the assigned value used to identify the firm
124  /// sending message (SenderCompID (49) field value).
125  void senderCompId(const std::string &);
126 
127  /// Returns the assigned value used to identify
128  /// the receiving firm (TargetCompID(56) field value).
129  FieldValueRef targetCompId() const;
130 
131  /// Sets the assigned value used to identify
132  /// the receiving firm (TargetCompID(56) field value).
133  void targetCompId(const std::string &);
134 
135  /// A user data associated with the message.
136  ///
137  /// @return The pointer to data was previously
138  /// attached to the instance or NULL.
139  void * userData() const;
140 
141  /// Attaches a user data to the message.
142  void userData(void * data);
143 
144  /// Compares two messages. The comparison is performed
145  /// using 'tag=value' message presentations.
146  bool operator == (const Message &) const;
147 
148  /// Compares two messages. The comparison is performed
149  /// using 'tag=value' message presentations.
150  bool operator != (const Message &) const;
151 
152  /// Ensures the message satisfies basic FIX Specification requirements.
153  ///
154  /// @throw The std::exception if the validation fails.
155  ///
156  /// @note This method validates required fields for the application level only, without the message header/trailer
157  void validate() const;
158 
159  /// Validates the message according to specified criteria.
160  ///
161  /// @param validationFlags Specifies validation criteria.
162  ///
163  /// @throw The std::exception if the validation fails.
164  ///
165  /// @note This method validates required fields for the application level only, without the message header/trailer
166  void validate(MessageValidationFlags validationFlags) const;
167 
168  /// Tries to validate the message according to specified criteria.
169  /// Returns 'true' if the validation is successful, otherwise - 'false'.
170  ///
171  /// @param validationFlags Validation criteria.
172  /// @param errorDescription Contains the error description in case the validation fails.
173  ///
174  /// @note This method validates required fields for the application level only, without the message header/trailer
175  bool tryValidate(MessageValidationFlags validationFlags, std::string & errorDescription) const;
176 
177  /// Brings the message to the 'blank' state as it was just constructed.
178  ///
179  /// @warning For messages constructed with the certain message type
180  /// member wipes out field values for all the fields except several
181  /// service fields (MsgType, BeginString, etc). For the message in
182  /// the unconstructed state does actually nothing.
183  void clear();
184 
185  /// Builds the FIX-compliant 'tag=value' presentation of the message.
186  void toRaw(RawMessage &) const;
187 
188  /// Returns the string representation of the message using
189  /// the given delimiter and additional control flags.
190  ///
191  /// @param delimiter Defines the field delimiter to be used.
192  /// @param flags Affects how the message presentation looks like.
193  std::string
194  toString(
195  char delimiter = 0x1,
196  MessageStringingFlags flags =
198 
199  /// Appends the string representation of the message using
200  /// the given delimiter and additional control flags.
201  ///
202  /// @param str The string to which the presentation is appended.
203  /// @param delimiter Defines the field delimiter to be used.
204  /// @param flags Affects how the message presentation looks like.
205  void
206  toString(
207  std::string & str,
208  char delimiter = 0x1,
209  MessageStringingFlags flags =
211 
212  /// Returns the XML representation of the message.
213  ///
214  /// @param indent The indent of each nested XML node.
215  /// @param endOfLineDelimiter Defines the end of line delimiter to be used.
216  std::string
217  toXml(const std::string & indent = " ", const std::string & endOfLineDelimiter = "\n") const;
218 
219  /// Appends the XML representation of the message.
220  ///
221  /// @param str The string to which the presentation is appended.
222  /// @param indent The indent of each nested XML node.
223  /// @param endOfLineDelimiter Defines the end of line delimiter to be used.
224  void
225  toXml(std::string & str, const std::string & indent = " ", const std::string & endOfLineDelimiter = "\n") const;
226 
227  /// Returns the JSON representation of the message.
228  ///
229  /// @param indent The indent of each nested JSON object.
230  /// @param endOfLineDelimiter Defines the end of line delimiter to be used.
231  std::string
232  toJson(const std::string & indent = " ", const std::string & endOfLineDelimiter = "\n") const;
233 
234  /// Appends the JSON representation of the message.
235  ///
236  /// @param str The string to which the presentation is appended.
237  /// @param indent The indent of each nested JSON object.
238  /// @param endOfLineDelimiter Defines the end of line delimiter to be used.
239  void
240  toJson(std::string & str, const std::string & indent = " ", const std::string & endOfLineDelimiter = "\n") const;
241 
242  /// Calculates the message body length and checkSum and updates corresponding fields.
243  void updateBodyLengthAndCheckSum();
244 
245  /// Swaps the content with another instance.
246  void swap(Message & other) ONIXS_FIXENGINE_NOTHROW;
247 
248  // Reassigns the message as a copy of other one.
249  Message & operator = (const Message &);
250 
251 #ifdef ONIXS_FIXENGINE_CXX11
252 
253  /// The move assignment operator.
254  ///
255  /// @note The moved message should not be used after this.
256  Message & operator = (Message &&) ONIXS_FIXENGINE_NOTHROW;
257 
258 #endif
259 
260  // Copies all message fields (except the protocol version) from another message object.
261  void copyFields(const Message &);
262 
263  /// De-serializes the FIX message from its raw (tag=value) presentation.
264  ///
265  /// @param rawMessage The buffer in which the raw FIX message is stored.
266  /// @param rawMessageSize The size of the buffer in which the raw FIX message is stored.
267  /// @param message The parsed FIX message if the parsing succeeds.
268  ///
269  /// @throw The std::exception if the parsing fails.
270  static
271  void
272  parse(
273  const char * rawMessage,
274  size_t rawMessageSize,
275  Message & message);
276 
277  /// De-serializes the FIX message from its raw (tag=value) presentation.
278  ///
279  /// @param rawMessage The buffer in which the raw FIX message is stored.
280  /// @param rawMessageSize The Size of the buffer in which the raw FIX message is stored.
281  /// @param parsingFlags Flags which affect the parsing behavior.
282  /// @param message The parsed FIX message if the parsing succeeds.
283  ///
284  /// @throw The std::exception if the parsing fails.
285  static
286  void
287  parse(
288  const char * rawMessage,
289  size_t rawMessageSize,
290  MessageParsingFlags parsingFlags,
291  Message & message);
292 
293  /// De-serializes the FIX message from its raw (tag=value) presentation.
294  ///
295  /// @param rawMessage The buffer in which the raw FIX message is stored.
296  /// @param rawMessageSize The Size of the buffer in which the raw FIX message is stored.
297  /// @param dictionary The FIX dictionary to which the message supposed to belong.
298  /// @param parsingFlags Flags which affect the parsing behavior.
299  /// @param message The parsed FIX message if the parsing succeeds.
300  ///
301  /// @throw The std::exception if the parsing fails.
302  static
303  void
304  parse(
305  const char * rawMessage,
306  size_t rawMessageSize,
307  const Dictionary & dictionary,
308  MessageParsingFlags parsingFlags,
309  Message & message);
310 
311  /// De-serializes the FIX message from its raw (tag=value)
312  /// presentation which may be truncated.
313  ///
314  /// @param rawMessage The buffer in which the raw FIX message is stored.
315  /// @param rawMessageSize The Size of the buffer in which the raw FIX message is stored.
316  /// @param message The parsed FIX message if the parsing succeeds.
317  ///
318  /// @return true if the message can be recognized from the input.
319  static
320  bool
321  parsePartial(
322  const char * rawMessage,
323  size_t rawMessageSize,
324  Message & message);
325 
326  /// De-serializes the FIX message from its raw (tag=value)
327  /// presentation which may be truncated.
328  ///
329  /// @param rawMessage The buffer in which the raw FIX message is stored.
330  /// @param rawMessageSize The Size of the buffer in which the raw FIX message is stored.
331  /// @param dictionary The FIX dictionary to which the message supposed to belong.
332  /// @param message The parsed FIX message if the parsing succeeds.
333  ///
334  /// @return true if the message can be recognized from the input.
335  static
336  bool
337  parsePartial(
338  const char * rawMessage,
339  size_t rawMessageSize,
340  const Dictionary & dictionary,
341  Message & message);
342 
343  struct MakeShallowCopy {};
344 
345 protected:
346  /// Initializes an instance as a shallow copy of other one.
347  ///
348  /// @param other The message to be copied from.
349  Message(const Message & other, MakeShallowCopy);
350 
351 private:
352  friend class MessageWrapper;
353  friend class MessageOperator;
354 
355  Message(const void *);
356 
357 private:
358 
359  virtual void userDataImpl(void * data);
360 
361  unsigned char impl_[6 * sizeof(size_t)];
362  unsigned char allocator_[4 * sizeof(size_t)];
363 
364  static const size_t DefaultExpectedMessageSize = 10240;
365 
366  void construct(size_t expectedMessageSize = DefaultExpectedMessageSize);
367  void destruct();
368 };
369 
370 inline
371 std::string
373  char delimiter,
374  MessageStringingFlags flags) const
375 {
376  std::string str;
377 
378  toString(str, delimiter, flags);
379 
380  return str;
381 }
382 
383 /// The stream output.
385 std::ostream & operator << (std::ostream & os, const Message & message);
386 }
387 }
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
Encapsulates primary operations over the collection of FIX fields like a FIX message and a repeating ...
Definition: FieldSet.h:62
#define ONIXS_FIXENGINE_NOTHROW
Definition: Compiler.h:43
unsigned MessageStringingFlags
The collection of message stringing flags.
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Implements a concept of a read-only reference to a FIX field value.
Definition: FieldValueRef.h:32
std::string toString(char delimiter=0x1, MessageStringingFlags flags=MessageStringingFlag::IncludeFieldTagNumber) const
Returns the string representation of the message using the given delimiter and additional control fla...
Definition: Message.h:372
Field tag numbers are included during the serialization.
unsigned MessageValidationFlags
The collection of message validation flags.
Identifies the FIX messages dictionary.
Definition: Dictionary.h:73
virtual void clear()=0
Clears the storage.
bool operator==(const FieldValueRef &ref, const std::string &str)
The helper class to represent a raw FIX message.
Definition: RawMessage.h:29
unsigned int SequenceNumber
Alias for the sequence number.
Encapsulates operations over a FIX Message.
Definition: Message.h:49
unsigned MessageParsingFlags
The collection of message parsing flags.
bool operator!=(const FieldValueRef &ref, const std::string &str)