OnixS C++ FIX Engine  4.2.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 header/trailer
146  void validate() const;
147 
148  /// Validates message according to specified criterias.
149  ///
150  /// @param validationFlags Specifies validation criterias.
151  ///
152  /// @throw std::exception if validation fails.
153  ///
154  /// @note This method validates required fields for the application level only, without header/trailer
155  void validate(MessageValidationFlags validationFlags) const;
156 
157  /// Brings message to 'blank' state as it was just constructed.
158  ///
159  /// @warning For messages constructed with certain message type
160  /// member wipes out field values for all the fields except several
161  /// service fields (MsgType, BeginString, etc). For message in
162  /// un-constructed state does actually nothing.
163  void clear();
164 
165  /// Builds FIX-compliant 'tag=value' presentation of the message.
166  void toRaw(RawMessage &) const;
167 
168  /// Returns the string representation of the message using
169  /// the given delimiter and additional control flags.
170  ///
171  /// @param delimiter Defines field delimiter to be used.
172  /// @param flags Affect how message presentation looks like.
173  std::string
174  toString(
175  char delimiter = 0x1,
176  MessageStringingFlags flags =
178 
179  /// Appends string representation of the message using
180  /// the given delimiter and additional control flags.
181  ///
182  /// @param str String to which presentation is appended.
183  /// @param delimiter Defines field delimiter to be used.
184  /// @param flags Affect how message presentation looks like.
185  void
186  toString(
187  std::string & str,
188  char delimiter = 0x1,
189  MessageStringingFlags flags =
191 
192  /// Calculates the message body length and checkSum and updates corresponding fields.
193  void updateBodyLengthAndCheckSum();
194 
195  // Reassigns message as copy of other one.
196  Message & operator = (const Message &);
197 
198  // Copies all message fields (except protocol version) from another message object.
199  void copyFields(const Message &);
200 
201  /// De-serializes FIX message from its raw (tag=value) presentation.
202  ///
203  /// @param rawMessage buffer in which raw FIX message is stored.
204  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
205  /// @param message parsed FIX message if parsing succeeds.
206  ///
207  /// @throw std::exception if parsing fails.
208  static
209  void
210  parse(
211  const char * rawMessage,
212  size_t rawMessageSize,
213  Message & message);
214 
215  /// De-serializes FIX message from its raw (tag=value) presentation.
216  ///
217  /// @param rawMessage buffer in which raw FIX message is stored.
218  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
219  /// @param parsingFlags flags which affect parsing behavior.
220  /// @param message parsed FIX message if parsing succeeds.
221  ///
222  /// @throw std::exception if parsing fails.
223  static
224  void
225  parse(
226  const char * rawMessage,
227  size_t rawMessageSize,
228  MessageParsingFlags parsingFlags,
229  Message & message);
230 
231  /// De-serializes FIX message from its raw (tag=value) presentation.
232  ///
233  /// @param rawMessage buffer in which raw FIX message is stored.
234  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
235  /// @param dictionary FIX dictionary to which message supposed to belong.
236  /// @param parsingFlags flags which affect parsing behavior.
237  /// @param message parsed FIX message if parsing succeeds.
238  ///
239  /// @throw std::exception if parsing fails.
240  static
241  void
242  parse(
243  const char * rawMessage,
244  size_t rawMessageSize,
245  const Dictionary & dictionary,
246  MessageParsingFlags parsingFlags,
247  Message & message);
248 
249  /// De-serializes FIX message from its raw (tag=value)
250  /// presentation which may be truncated.
251  ///
252  /// @param rawMessage buffer in which raw FIX message is stored.
253  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
254  /// @param message parsed FIX message if parsing succeeds.
255  ///
256  /// @return true if message can be recognized from the input.
257  static
258  bool
259  parsePartial(
260  const char * rawMessage,
261  size_t rawMessageSize,
262  Message & message);
263 
264  /// De-serializes FIX message from its raw (tag=value)
265  /// presentation which may be truncated.
266  ///
267  /// @param rawMessage buffer in which raw FIX message is stored.
268  /// @param rawMessageSize Size of buffer in which raw FIX message is stored.
269  /// @param dictionary FIX dictionary to which message supposed to belong.
270  /// @param message parsed FIX message if parsing succeeds.
271  ///
272  /// @return true if message can be recognized from the input.
273  static
274  bool
275  parsePartial(
276  const char * rawMessage,
277  size_t rawMessageSize,
278  const Dictionary & dictionary,
279  Message & message);
280 
281 private:
282  friend class MessageWrapper;
283  friend class MessageOperator;
284 
285  Message(const void *);
286 
287 private:
288  unsigned char impl_[6 * sizeof(size_t)];
289  unsigned char allocator_[4 * sizeof(size_t)];
290 
291  static const size_t DefaultExpectedMessageSize = 10240;
292 
293  void construct(size_t expectedMessageSize = DefaultExpectedMessageSize);
294  void destruct();
295 };
296 
297 inline
298 std::string
300  char delimiter,
301  MessageStringingFlags flags) const
302 {
303  std::string str;
304 
305  toString(str, delimiter, flags);
306 
307  return str;
308 }
309 
310 /// Stream output.
312 std::ostream & operator << (std::ostream & os, const Message & message);
313 }
314 }
unsigned int SequenceNumber
Alias for sequence mumber.
unsigned MessageStringingFlags
Collection of message stringing flags.
Identifies FIX messages dictionary.
Definition: Dictionary.h:79
bool operator!=(const FieldValueRef &ref, const std::string &str)
Encapsulates primary operations over collection of FIX fields like FIX message and repeating group in...
Definition: FieldSet.h:61
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
#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:299
unsigned MessageParsingFlags
Collection of message parsing flags.
Helper class to represent a raw FIX message.
Definition: RawMessage.h:29
Field tag numbers are included during serialization.
Encapsulates operations over a FIX Message.
Definition: Message.h:49
bool operator==(const FieldValueRef &ref, const std::string &str)
unsigned MessageValidationFlags
Collection of message validation flags.