OnixS C++ CME MDP Conflated TCP Handler  1.3.6
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 
30 
31 /// The policy to create messages with null-initialized optional fields.
33 {
34  /// \private
35  template <typename SbeMessageType>
36  inline static SbeMessageType createMessage(
37  void* data, MessageSize length, SchemaVersion version)
38  {
39  return SbeMessageType(data, length, version);
40  }
41 
42  /// \private
43  inline static const char* name() ONIXS_CONFLATEDTCP_NOTHROW
44  {
45  return "FieldsInitPolicy";
46  }
47 };
48 
49 /// The policy to create messages without initialized optional fields.
51 {
52  /// \private
53  template <typename SbeMessageType>
54  inline static SbeMessageType createMessage(
55  void* data, MessageSize length, SchemaVersion version)
56  {
57  return SbeMessageType(
58  data, length, SbeMessage::NoFieldsInit(), version);
59  }
60 
61  /// \private
62  inline static const char* name() ONIXS_CONFLATEDTCP_NOTHROW
63  {
64  return "FieldsNoInitPolicy";
65  }
66 };
67 
68 template <typename MessageType>
70  : public MessageType
71 {
72  ONIXS_CONFLATEDTCP_STATIC_ASSERT((
73  IsBaseOf<SbeMessage, MessageType>::value));
74 
75  typedef MessageType SbeType;
76 
77  typedef typename
78  SbeType::EncodedLength
80 
82 
84  void* data,
85  EncodedLength length,
86  SchemaVersion version)
87  : MessageType(data, length, version)
88  {
89  }
90 
92  void* data,
93  EncodedLength length,
95  SchemaVersion version)
96  : MessageType(data, length, version)
97  {
98  }
99 
101  void* data,
102  EncodedLength length,
104  : MessageType(data, length)
105  {
106  }
107 
109  void* data,
110  EncodedLength length,
114  : MessageType(
115  data,
116  length,
117  SbeMessage::NoInit(),
118  SbeMessage::NoCheck())
119  {
120  }
121 };
122 
123 /// \return a human-readable presentation.
124 template <typename, size_t, typename>
126 template <
127  template <typename, size_t, typename> class HolderType,
128  typename MsgType,
129  size_t MaxMessageSize,
130  typename MessageInitializer>
131 inline void toStr(
132  std::string& str,
133  const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
134 {
135  str += "MessageHolder[";
136 
137  str += "BufferSize=";
138  toStr(str, holder.BufferSize);
139 
140  str += ", MessageInitializer=";
141  str += MessageInitializer::name();
142 
143  str += ", ";
144  toStr(str, *holder.header());
145 
146  str += ", ";
147  toStr(str, holder.message());
148 
149  str += "]";
150 }
151 
152 /// \private
153 ONIXS_CONFLATEDTCP_CONST_OR_CONSTEXPR char magicDebugValue = 0x5A;
154 
155 /// Default maximum number of repeating group items.
157 
158 /**
159  * Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
160  */
161 template <
162  typename MessageTypeT,
163  size_t MaxMessageSize =
164  GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
165  typename MessageInitializer = FieldsInitPolicy>
166 class MessageHolder
167 {
168  typedef typename HeldAdapter<MessageTypeT>::SbeType SbeType;
169 
170  ONIXS_CONFLATEDTCP_STATIC_ASSERT_MSG(
171  (MaxMessageSize >= sizeof(MessageHeader)),
172  "MaxMessageSize template parameter is too small");
173 
174  ONIXS_CONFLATEDTCP_STATIC_ASSERT_MSG(
175  (MaxMessageSize >= GetMinMessageSize<SbeType>::Size),
176  "The buffer can not fit the message");
177 
178  ONIXS_CONFLATEDTCP_STATIC_ASSERT_MSG(
179  (MaxMessageSize <= MaxConflatedTcpMessageSize),
180  "The buffer is too large.");
181 
182 public:
183  /// Message type.
185 
186  /// Size of the data buffer.
187  enum
188  {
189  BufferSize = MaxMessageSize + sizeof(SimpleOpenFramingHeader)
190  };
191 
192  explicit MessageHolder(SchemaVersion version = SbeType::Schema::Version)
193  {
194 #ifdef ONIXS_CONFLATEDTCP_MEMCHECK_DEBUG
195  std::memset(buffer_, magicDebugValue, BufferSize);
196 #endif
197  messageSize(0);
198 
199  message_ = MessageInitializer::template createMessage<MessageType>(
200  buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, version);
201  }
202 
204  {
205  copyFrom(r);
206  }
207 
209  {
210  copyFrom(r);
211  return *this;
212  }
213 
214  /// \return the buffer.
215  const unsigned char* buffer() const ONIXS_CONFLATEDTCP_NOTHROW
216  {
217  return buffer_;
218  }
219 
220  /// \return the used size of the buffer.
222  {
223  return header()->size();
224  }
225 
226  /// \return the SBE message.
228  {
229  return message_;
230  }
231 
232  /// \return the SBE message.
233  const MessageType& message() const ONIXS_CONFLATEDTCP_NOTHROW
234  {
235  return message_;
236  }
237 
238  /// \return the SBE message size
240  {
241  return bufferSize() - sizeof(SimpleOpenFramingHeader);
242  }
243 
245  {
246  return &message();
247  }
248 
249  const MessageType* operator->() const ONIXS_CONFLATEDTCP_NOTHROW
250  {
251  return &message();
252  }
253 
254  const MessageType& operator* () const ONIXS_CONFLATEDTCP_NOTHROW
255  {
256  return message();
257  }
258 
259  MessageType& operator* () ONIXS_CONFLATEDTCP_NOTHROW
260  {
261  return message();
262  }
263 
264  /// \return Simple Open Framing Header
266  {
267  return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
268  }
269 
270  /// \return Simple Open Framing Header
272  {
273  return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
274  }
275 
276  /// Calculates the binary size of the message and updates
277  /// the Simple Open Framing Header accordingly.
278  ///
279  /// \return SBE message size
281  {
282  const MessageSize calculatedMessageSize =
283  message().calculateBinarySize();
284 
285  assert(calculatedMessageSize <= MaxMessageSize);
286 
287  assert(
288  calculatedMessageSize >=
289  SbeType::blockLength(message().version()) +
290  MessageHeaderBuilder::Size +
291  SbeType::getMinimalVariableFieldsSize(message().version()));
292 
293  messageSize(calculatedMessageSize);
294  return calculatedMessageSize;
295  }
296 
297  /// \return a human-readable presentation.
299  std::string toString() const
300  {
301  return toStr(*this);
302  }
303 
304 private:
305  void messageSize(UInt16 size) ONIXS_CONFLATEDTCP_NOTHROW
306  {
307  header()->setup(size + 2); //size contains only 2 bytes (message size) from SimpleOpenFramingHeader
308  }
309 
310  void copyFrom(const MessageHolder& r)
311  {
312 #ifdef ONIXS_CONFLATEDTCP_MEMCHECK_DEBUG
313  std::memset(buffer_, magicDebugValue, BufferSize);
314 #endif
315 
316  const size_t sizeToCopy = r.message_.MessageType::calculateBinarySize() + sizeof(SimpleOpenFramingHeader);
317 
318  assert(r.message_.calculateBinarySize() <= MaxMessageSize);
319 
320  std::memcpy(
321  buffer_,
322  r.buffer_,
323  sizeToCopy);
324 
325  message_ =
326  MessageType(
327  buffer_ + sizeof(SimpleOpenFramingHeader),
328  MaxMessageSize,
331  }
332 
333 private:
334  MessageType message_;
335  unsigned char buffer_[BufferSize];
336 };
337 
338 template <
339  template <typename, size_t, typename> class HolderType,
340  typename MsgType,
341  size_t MaxMessageSize,
342  typename MessageInitializer>
343 inline std::string toStr(
344  const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
345 {
346  std::string res;
347  toStr(res, holder);
348  return res;
349 }
350 
351 template <
352  template <typename, size_t, typename> class HolderType,
353  typename MsgType,
354  size_t MaxMessageSize,
355  typename MessageInitializer>
356 std::ostream& operator<<(
357  std::ostream& stream,
358  const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
359 {
360  return stream << toStr(rhs);
361 }
362 
363 #if defined(ONIXS_CONFLATEDTCP_CXX11)
364 
365 template <
366  typename MessageTypeT,
367  size_t MaxMessageSize =
368  GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
369  typename MessageInitializer = FieldsInitPolicy>
370 using MessageHolders = std::vector<MessageHolder<MessageTypeT, MaxMessageSize, MessageInitializer> >;
371 
372 #endif
373 
const MessageType & message() const noexcept
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_END
Definition: ABI.h:143
The policy to create messages without initialized optional fields.
Definition: MessageHolder.h:50
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition: Compiler.h:176
Template ID and length of message root.
Definition: Composites.h:144
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit)
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
#define ONIXS_CONFLATEDTCP_CONST_OR_CONSTEXPR
Definition: Compiler.h:178
const MessageType * operator->() const noexcept
std::ostream & operator<<(std::ostream &stream, const HolderType< MsgType, MaxMessageSize, MessageInitializer > &rhs)
const unsigned char * buffer() const noexcept
The policy to create messages with null-initialized optional fields.
Definition: MessageHolder.h:32
#define ONIXS_CONFLATEDTCP_NODISCARD
Definition: Compiler.h:185
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoFieldsInit, SchemaVersion version)
Definition: MessageHolder.h:91
SimpleOpenFramingHeader * header() noexcept
std::string toStr(const HolderType< MsgType, MaxMessageSize, MessageInitializer > &holder)
#define ONIXS_CONFLATEDTCP_DEFAULT
Definition: Compiler.h:202
constexpr UInt16 MaxConflatedTcpMessageSize
Maximum supported message size.
#define ONIXS_CONFLATEDTCP_FINAL
Definition: Compiler.h:181
MessageSize setHeader() noexcept
Calculates the binary size of the message and updates the Simple Open Framing Header accordingly...
MessageHeader::Version SchemaVersion
SBE-encoded data version type.
Definition: SchemaTraits.h:30
HeldAdapter< MessageTypeT > MessageType
Message type.
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
std::vector< MessageHolder< MessageTypeT, MaxMessageSize, MessageInitializer > > MessageHolders
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
constexpr UInt8 DefaultMaxGroupItems
Default maximum number of repeating group items.
const SimpleOpenFramingHeader * header() const noexcept
MessageHolder(SchemaVersion version=SbeType::Schema::Version)
MessageHolder & operator=(const MessageHolder &r)
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit, SbeMessage::NoCheck) noexcept