OnixS C++ FIX Engine  4.7.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 'unconstructed' state which can be
41 /// treated as pointer in null state. However, in contrast to
42 /// OnixS::FIX::Group and OnixS::FIX::GroupInstance classes it
43 /// does NOT represent light-weight wrapper over internal structures.
44 /// In fact, it holds all the data which is fully copied on assignment
45 /// or copy construction and disposed at instance destruction.
46 ///
47 /// FIX field related operations now available via OnixS::FIX::FieldSet
48 /// class from which OnixS::FIX::Message class is now derived.
49 class
51  Message : public FieldSet
52 {
53 public:
54  /// Initializes message in un-constructed 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 FIX message of given type which belongs to
65  /// given dictionary of FIX protocol (messaging specification).
66  ///
67  /// @param type Defines type of message (MsgType field value).
68  /// @param dictionary FIX protocol dictionary to which 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 Type of the message can't be changed by updating MsgType
77  /// field value. Changing MsgType field value have no effect for 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 instance as deep copy of other one.
82  ///
83  /// @param other Message to be copied from.
84  Message(const Message & other);
85 
86  /// Disposes all internal data structures.
87  ///
88  /// @warning Once 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  ~Message();
92 
93  /// Instance of 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  /// firm sending message (SenderCompID (49) field value).
110  FieldValueRef senderCompId() const;
111 
112  /// Sets the assigned value used to identify firm
113  /// sending message (SenderCompID (49) field value).
114  void senderCompId(const std::string &);
115 
116  /// Returns the assigned value used to identify
117  /// receiving firm (TargetCompID(56) field value).
118  FieldValueRef targetCompId() const;
119 
120  /// Sets the assigned value used to identify
121  /// receiving firm (TargetCompID(56) field value).
122  void targetCompId(const std::string &);
123 
124  /// User data associated with the message.
125  ///
126  /// @return Pointer to data was previously
127  /// attached to the instance or NULL.
128  void * userData() const;
129 
130  /// Attaches user data to the message.
131  void userData(void * data);
132 
133  /// Compares two messages. Comparison is performed
134  /// using 'tag=value' message presentations.
135  bool operator == (const Message &) const;
136 
137  /// Compares two messages. Comparison is performed
138  /// using 'tag=value' message presentations.
139  bool operator != (const Message &) const;
140 
141  /// Ensures message satisfies basic FIX Specification requirements.
142  ///
143  /// @throw std::exception if 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 message according to specified criteria.
149  ///
150  /// @param validationFlags Specifies validation criteria.
151  ///
152  /// @throw std::exception if 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 message to 'blank' state as it was just constructed.
167  ///
168  /// @warning For messages constructed with certain message type
169  /// member wipes out field values for all the fields except several
170  /// service fields (MsgType, BeginString, etc). For message in
171  /// un-constructed state does actually nothing.
172  void clear();
173 
174  /// Builds 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 field delimiter to be used.
181  /// @param flags Affect how message presentation looks like.
182  std::string
183  toString(
184  char delimiter = 0x1,
185  MessageStringingFlags flags =
187 
188  /// Appends string representation of the message using
189  /// the given delimiter and additional control flags.
190  ///
191  /// @param str String to which presentation is appended.
192  /// @param delimiter Defines field delimiter to be used.
193  /// @param flags Affect how 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 String to which 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 String to which 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 message as copy of other one.
225  Message & operator = (const Message &);
226 
227  // Copies all message fields (except protocol version) from another message object.
228  void copyFields(const Message &);
229 
230  /// De-serializes FIX message from its raw (tag=value) presentation.
231  ///
232  /// @param rawMessage buffer in which raw FIX message is stored.
233  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
234  /// @param message parsed FIX message if parsing succeeds.
235  ///
236  /// @throw std::exception if parsing fails.
237  static
238  void
239  parse(
240  const char * rawMessage,
241  size_t rawMessageSize,
242  Message & message);
243 
244  /// De-serializes FIX message from its raw (tag=value) presentation.
245  ///
246  /// @param rawMessage buffer in which raw FIX message is stored.
247  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
248  /// @param parsingFlags flags which affect parsing behavior.
249  /// @param message parsed FIX message if parsing succeeds.
250  ///
251  /// @throw std::exception if 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 FIX message from its raw (tag=value) presentation.
261  ///
262  /// @param rawMessage buffer in which raw FIX message is stored.
263  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
264  /// @param dictionary FIX dictionary to which message supposed to belong.
265  /// @param parsingFlags flags which affect parsing behavior.
266  /// @param message parsed FIX message if parsing succeeds.
267  ///
268  /// @throw std::exception if 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 FIX message from its raw (tag=value)
279  /// presentation which may be truncated.
280  ///
281  /// @param rawMessage buffer in which raw FIX message is stored.
282  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
283  /// @param message parsed FIX message if parsing succeeds.
284  ///
285  /// @return true if 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 FIX message from its raw (tag=value)
294  /// presentation which may be truncated.
295  ///
296  /// @param rawMessage buffer in which raw FIX message is stored.
297  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
298  /// @param dictionary FIX dictionary to which message supposed to belong.
299  /// @param message parsed FIX message if parsing succeeds.
300  ///
301  /// @return true if 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 instance as shallow copy of other one.
314  ///
315  /// @param other 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  unsigned char impl_[6 * sizeof(size_t)];
326  unsigned char allocator_[4 * sizeof(size_t)];
327 
328  static const size_t DefaultExpectedMessageSize = 10240;
329 
330  void construct(size_t expectedMessageSize = DefaultExpectedMessageSize);
331  void destruct();
332 };
333 
334 inline
335 std::string
337  char delimiter,
338  MessageStringingFlags flags) const
339 {
340  std::string str;
341 
342  toString(str, delimiter, flags);
343 
344  return str;
345 }
346 
347 /// Stream output.
349 std::ostream & operator << (std::ostream & os, const Message & message);
350 }
351 }
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
Encapsulates primary operations over collection of FIX fields like FIX message and repeating group in...
Definition: FieldSet.h:62
unsigned MessageStringingFlags
Collection of message stringing flags.
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Implements 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:336
Field tag numbers are included during serialization.
unsigned MessageValidationFlags
Collection of message validation flags.
Identifies FIX messages dictionary.
Definition: Dictionary.h:73
bool operator==(const FieldValueRef &ref, const std::string &str)
Helper class to represent a raw FIX message.
Definition: RawMessage.h:29
unsigned int SequenceNumber
Alias for sequence mumber.
Encapsulates operations over a FIX Message.
Definition: Message.h:49
unsigned MessageParsingFlags
Collection of message parsing flags.
bool operator!=(const FieldValueRef &ref, const std::string &str)