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