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