OnixS C++ B3 BOE Binary Order Entry  1.2.0
API Documentation
MessageHolder.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is
4 // protected by copyright law and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable
7 // OnixS Software Services Agreement (the Agreement) and Customer end user license
8 // agreements granting a non-assignable, non-transferable and non-exclusive license
9 // to use the software for it's own data processing purposes under the terms defined
10 // in the Agreement.
11 //
12 // Except as otherwise granted within the terms of the Agreement, copying or
13 // reproduction of any part of this source code or associated reference material
14 // to any other location for further reproduction or redistribution, and any
15 // amendments to this copyright notice, are expressly prohibited.
16 //
17 // Any reproduction or redistribution for sale or hiring of the Software not in
18 // accordance with the terms of the Agreement is a violation of copyright law.
19 //
20 
21 #pragma once
22 
23 #include <vector>
24 
28 
29 namespace OnixS
30 {
31  namespace B3
32  {
33  namespace BOE
34  {
36  }
37  }
38 }
39 
41 
42 /// \return the version of the Session’s message schema.
45 
46 /// The policy to create messages with null-initialized optional fields.
48 {
49  /// \private
50  template <typename SbeMessageType>
51  inline static SbeMessageType createMessage(
52  void* data, MessageSize length, SchemaVersion version)
53  {
54  return SbeMessageType(data, length, version);
55  }
56 
57  /// \private
58  inline static const char* name() ONIXS_B3_BOE_NOTHROW
59  {
60  return "FieldsInitPolicy";
61  }
62 };
63 
64 /// The policy to create messages without initialized optional fields.
66 {
67  /// \private
68  template <typename SbeMessageType>
69  inline static SbeMessageType createMessage(
70  void* data, MessageSize length, SchemaVersion version)
71  {
72  return SbeMessageType(
73  data, length, SbeMessage::NoFieldsInit(), version);
74  }
75 
76  /// \private
77  inline static const char* name() ONIXS_B3_BOE_NOTHROW
78  {
79  return "FieldsNoInitPolicy";
80  }
81 };
82 
83 /// \private
84 template <typename MessageType>
85 struct HeldAdapter : public MessageType
86 {
87  ONIXS_B3_BOE_STATIC_ASSERT((
88  IsBaseOf<SbeMessage, MessageType>::value));
89 
90  enum { isFix = false };
91 
92  typedef MessageType SbeType;
93 
94  typedef typename
95  SbeType::EncodedLength
96  EncodedLength;
97 
98  HeldAdapter() ONIXS_B3_BOE_DEFAULT;
99 
100  HeldAdapter(
101  void* data,
102  EncodedLength length,
103  SchemaVersion version)
104  : MessageType(data, length, version)
105  {
106  }
107 
108  HeldAdapter(
109  void* data,
110  EncodedLength length,
112  SchemaVersion version)
113  : MessageType(data, length, version)
114  {
115  }
116 
117  HeldAdapter(
118  void* data,
119  EncodedLength length,
121  : MessageType(data, length)
122  {
123  }
124 
125  HeldAdapter(
126  void* data,
127  EncodedLength length,
131  : MessageType(
132  data,
133  length,
136  {
137  }
138 };
139 
140 /// \return a human-readable presentation.
141 template <typename, size_t, typename>
143 template <
144  template <typename, size_t, typename> class HolderType,
145  typename MsgType,
146  size_t MaxMessageSize,
147  typename MessageInitializer>
148 inline void toStr(
149  std::string& str,
150  const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
151 {
152  str += "MessageHolder[";
153 
154  str += "BufferSize=";
155  toStr(str, holder.BufferSize);
156 
157  str += ", MessageInitializer=";
158  str += MessageInitializer::name();
159 
160  str += ", ";
161  toStr(str, *holder.header());
162 
163  str += ", ";
164  toStr(str, holder.message());
165 
166  str += "]";
167 }
168 
169 /// \private
170 ONIXS_B3_BOE_CONST_OR_CONSTEXPR char magicDebugValue = 0x5A;
171 
172 /// Default maximum number of repeating group items.
174 
175 /**
176  * Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
177  */
178 template <
179  typename MessageTypeT,
180  size_t MaxMessageSize =
181  GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
182  typename MessageInitializer = FieldsInitPolicy>
183 class MessageHolder
184 {
185  typedef typename HeldAdapter<MessageTypeT>::SbeType SbeType;
186 
187  ONIXS_B3_BOE_STATIC_ASSERT_MSG(
188  (MaxMessageSize >= sizeof(MessageHeader)),
189  "MaxMessageSize template parameter is too small");
190 
191  ONIXS_B3_BOE_STATIC_ASSERT_MSG(
192  (MaxMessageSize >= GetMinMessageSize<SbeType>::Size),
193  "The buffer can not fit the message");
194 
195  ONIXS_B3_BOE_STATIC_ASSERT_MSG(
196  (MaxMessageSize <= MaxB3BOEMessageSize),
197  "The buffer is too large.");
198 
199 public:
200  /// Message type.
201  typedef HeldAdapter<MessageTypeT> MessageType;
202 
203  /// Size of the data buffer.
204  enum
205  {
206  BufferSize = MaxMessageSize + sizeof(SimpleOpenFramingHeader)
207  };
208 
209  explicit MessageHolder(SchemaVersion version = SbeType::Schema::Version)
210  {
211  init(version);
212  }
213 
214  explicit MessageHolder(const Session& session)
215  {
216  init(getMessagingVersion(session));
217  }
218 
220  {
221  copyFrom(r);
222  }
223 
225  {
226  copyFrom(r);
227  return *this;
228  }
229 
230  /// \return the buffer.
231  const unsigned char* buffer() const ONIXS_B3_BOE_NOTHROW
232  {
233  return buffer_;
234  }
235 
236  /// \return the used size of the buffer.
238  {
239  return header()->size();
240  }
241 
242  /// \return the SBE message.
244  {
245  return message_;
246  }
247 
248  /// \return the SBE message.
249  const MessageType& message() const ONIXS_B3_BOE_NOTHROW
250  {
251  return message_;
252  }
253 
254  /// \return the SBE message size
256  {
257  return bufferSize() - sizeof(SimpleOpenFramingHeader);
258  }
259 
261  {
262  return &message();
263  }
264 
265  const MessageType* operator->() const ONIXS_B3_BOE_NOTHROW
266  {
267  return &message();
268  }
269 
270  const MessageType& operator* () const ONIXS_B3_BOE_NOTHROW
271  {
272  return message();
273  }
274 
275  MessageType& operator* () ONIXS_B3_BOE_NOTHROW
276  {
277  return message();
278  }
279 
280  /// \return Simple Open Framing Header
282  {
283  return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
284  }
285 
286  /// \return Simple Open Framing Header
288  {
289  return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
290  }
291 
292  /// Calculates the binary size of the message and updates
293  /// the Simple Open Framing Header accordingly.
294  ///
295  /// \return SBE message size
297  {
298  const MessageSize calculatedMessageSize =
299  message().calculateBinarySize();
300 
301  assert(calculatedMessageSize <= MaxMessageSize);
302 
303  assert(
304  calculatedMessageSize >=
305  SbeType::blockLength(message().version()) +
307  SbeType::getMinimalVariableFieldsSize(message().version()));
308 
309  messageSize(calculatedMessageSize);
310  return calculatedMessageSize;
311  }
312 
313  ///
315  {
316  setHeader();
317 
318 #ifndef NDEBUG
319  NetworkMessage(buffer_, BufferSize);
320 #endif
321  return NetworkMessage(buffer_, BufferSize, SbeMessage::NoCheck());
322  }
323 
324  /// \return a human-readable presentation.
326  std::string toString() const
327  {
328  return toStr(*this);
329  }
330 
331 private:
332  void init(SchemaVersion version)
333  {
334 #ifdef ONIXS_B3_BOE_MEMCHECK_DEBUG
335  std::memset(buffer_, magicDebugValue, BufferSize);
336 #endif
337  messageSize(0);
338 
339  message_ = MessageInitializer::template createMessage<MessageType>(
340  buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, version);
341  }
342 
343  void messageSize(UInt16 size) ONIXS_B3_BOE_NOTHROW
344  {
345  header()->setup(
346  size + sizeof(SimpleOpenFramingHeader));
347  }
348 
349  void copyFrom(const MessageHolder& r)
350  {
351 #ifdef ONIXS_B3_BOE_MEMCHECK_DEBUG
352  std::memset(buffer_, magicDebugValue, BufferSize);
353 #endif
354 
355  const size_t sizeToCopy = r.message_.MessageType::calculateBinarySize() + sizeof(SimpleOpenFramingHeader);
356 
357  assert(r.message_.calculateBinarySize() <= MaxMessageSize);
358 
359  std::memcpy(
360  buffer_,
361  r.buffer_,
362  sizeToCopy);
363 
364  message_ =
365  MessageType(
366  buffer_ + sizeof(SimpleOpenFramingHeader),
367  MaxMessageSize,
370  }
371 
372 private:
373  MessageType message_;
374  unsigned char buffer_[BufferSize];
375 };
376 
377 template <
378  template <typename, size_t, typename> class HolderType,
379  typename MsgType,
380  size_t MaxMessageSize,
381  typename MessageInitializer>
382 inline std::string toStr(
383  const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
384 {
385  std::string res;
386  toStr(res, holder);
387  return res;
388 }
389 
390 template <
391  template <typename, size_t, typename> class HolderType,
392  typename MsgType,
393  size_t MaxMessageSize,
394  typename MessageInitializer>
395 std::ostream& operator<<(
396  std::ostream& stream,
397  const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
398 {
399  return stream << toStr(rhs);
400 }
401 
MessageHolder(SchemaVersion version=SbeType::Schema::Version)
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
The policy to create messages with null-initialized optional fields.
Definition: MessageHolder.h:47
MessageHeader::Version SchemaVersion
SBE-encoded data version type.
Definition: SchemaTraits.h:30
#define ONIXS_B3_BOE_NOTHROW
Definition: Compiler.h:182
std::ostream & operator<<(std::ostream &o, SessionStateId::Enum state)
const MessageType * operator->() const noexcept
HeldAdapter< MessageTypeT > MessageType
Message type.
#define ONIXS_B3_BOE_EXPORTED
Definition: Compiler.h:181
constexpr UInt8 DefaultMaxGroupItems
Default maximum number of repeating group items.
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
Definition: Defines.h:40
#define ONIXS_B3_BOE_NODISCARD
Definition: Compiler.h:191
MessageSize setHeader() noexcept
Calculates the binary size of the message and updates the Simple Open Framing Header accordingly...
constexpr UInt16 MaxB3BOEMessageSize
Maximum supported message size.
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
Message identifiers and length of message root.
Definition: Composites.h:33
SimpleOpenFramingHeader * header() noexcept
#define ONIXS_B3_BOE_DEFAULT
Definition: Compiler.h:208
Messaging::SchemaVersion getMessagingVersion(const Session &) noexcept
#define ONIXS_B3_BOE_EXPORTED_CLASS
Definition: ABI.h:44
A FIXP Session.
Definition: Session.h:47
const unsigned char * buffer() const noexcept
const SimpleOpenFramingHeader * header() const noexcept
std::string toStr(const HolderType< MsgType, MaxMessageSize, MessageInitializer > &holder)
The policy to create messages without initialized optional fields.
Definition: MessageHolder.h:65
MessageHolder & operator=(const MessageHolder &r)
const MessageType & message() const noexcept
#define ONIXS_B3_BOE_CONST_OR_CONSTEXPR
Definition: Compiler.h:184
Defines message type.
Definition: Fields.h:1006
NetworkMessage toNetworkMessage() noexcept