OnixS C++ ICE Binary Order Entry Handler 1.0.0
API Documentation
Loading...
Searching...
No Matches
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
32
34
38
41{
42
43 template <typename SbeMessageType>
44 inline static SbeMessageType createMessage(
45 void* data, MessageSize length, SchemaVersion version)
46 {
47 return SbeMessageType(data, length, version);
48 }
49
50
51 inline static const char* name() noexcept
52 {
53 return "FieldsInitPolicy";
54 }
55};
56
59{
60
61 template <typename SbeMessageType>
62 inline static SbeMessageType createMessage(
63 void* data, MessageSize length, SchemaVersion version)
64 {
65 return SbeMessageType(
66 data, length, SbeMessage::NoFieldsInit(), version);
67 }
68
69
70 inline static const char* name() noexcept
71 {
72 return "FieldsNoInitPolicy";
73 }
74};
75
76
77template
78<
79 typename MessageType,
80 bool isFix =
81 std::is_base_of<TagBased::Message, MessageType>::value
82>
84
85template <typename MessageType>
86struct HeldAdapter<MessageType, true> final
87 : public MessageType
88{
90 std::is_base_of<TagBased::Message, MessageType>::value));
91
92 enum { isFix = true };
93
94 typedef typename
95 MessageType::Binary
97
98 typedef typename
99 SbeType::EncodedLength
101
102 HeldAdapter() = default;
103
105 void* data,
106 EncodedLength length,
107 SchemaVersion version)
108 : MessageType(SbeType(data, length, version))
109 {
110 }
111
113 void* data,
114 EncodedLength length,
116 SchemaVersion version)
117 : MessageType(SbeType(data, length, version))
118 {
119 }
120
122 void* data,
123 EncodedLength length,
125 : MessageType(SbeType(data, length))
126 {
127 }
128
130 void* data,
131 EncodedLength length,
134 noexcept
135 : MessageType(
136 SbeType(
137 data,
138 length,
141 {
142 }
143};
144
145template <typename MessageType>
146struct HeldAdapter<MessageType, false> final
147 : public MessageType
148{
150 std::is_base_of<SbeMessage, MessageType>::value));
151
152 enum { isFix = false };
153
154 typedef MessageType SbeType;
155
156 typedef typename
157 SbeType::EncodedLength
159
160 HeldAdapter() = default;
161
163 void* data,
164 EncodedLength length,
165 SchemaVersion version)
166 : MessageType(data, length, version)
167 {
168 }
169
171 void* data,
172 EncodedLength length,
174 SchemaVersion version)
175 : MessageType(data, length, version)
176 {
177 }
178
180 void* data,
181 EncodedLength length,
183 : MessageType(data, length)
184 {
185 }
186
188 void* data,
189 EncodedLength length,
192 noexcept
193 : MessageType(
194 data,
195 length,
198 {
199 }
200};
201
203template <typename, size_t, typename>
204class MessageHolder;
205template <
206 template <typename, size_t, typename> class HolderType,
207 typename MsgType,
208 size_t MaxMessageSize,
209 typename MessageInitializer>
210inline void toStr(
211 std::string& str,
212 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
213{
214 str += "MessageHolder[";
215
216 str += "BufferSize=";
217 toStr(str, holder.BufferSize);
218
219 str += ", MessageInitializer=";
220 str += MessageInitializer::name();
221
222 str += ", ";
223 toStr(str, *holder.header());
224
225 str += ", ";
226 toStr(str, holder.message());
227
228 str += "]";
229}
230
231
232constexpr char magicDebugValue = 0x5A;
233
236
240template <
241 typename MessageTypeT,
242 size_t MaxMessageSize =
244 typename MessageInitializer = FieldsInitPolicy>
246{
247 typedef typename HeldAdapter<MessageTypeT>::SbeType SbeType;
248
249 static_assert((MaxMessageSize >= sizeof(MessageHeader)),"MaxMessageSize template parameter is too small");
250 static_assert((MaxMessageSize >= GetMinMessageSize<SbeType>::Size), "The buffer can not fit the message");
251 static_assert((MaxMessageSize <= MaxSbeMessageSize), "The buffer is too large.");
252
253public:
256
258 enum
259 {
260 BufferSize = MaxMessageSize + sizeof(SimpleOpenFramingHeader)
261 };
262
263 explicit MessageHolder(SchemaVersion version = SbeType::Schema::Version)
264 {
265 init(version);
266 }
267
268 explicit MessageHolder(const Session& session)
269 {
270 init(getMessagingVersion(session));
271 }
272
274 {
275 copyFrom(r);
276 }
277
279 {
280 copyFrom(r);
281 return *this;
282 }
283
285 const unsigned char* buffer() const noexcept
286 {
287 return buffer_;
288 }
289
291 UInt16 bufferSize() const noexcept
292 {
293 return header()->size();
294 }
295
298 {
299 return message_;
300 }
301
303 const MessageType& message() const noexcept
304 {
305 return message_;
306 }
307
309 UInt16 messageSize() const noexcept
310 {
311 return bufferSize() - sizeof(SimpleOpenFramingHeader);
312 }
313
315 {
316 return &message();
317 }
318
319 const MessageType* operator->() const noexcept
320 {
321 return &message();
322 }
323
324 const MessageType& operator* () const noexcept
325 {
326 return message();
327 }
328
329 MessageType& operator* () noexcept
330 {
331 return message();
332 }
333
335 const SimpleOpenFramingHeader* header() const noexcept
336 {
337 return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
338 }
339
342 {
343 return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
344 }
345
351 {
352 const MessageSize calculatedMessageSize =
353 message().calculateBinarySize();
354
355 assert(calculatedMessageSize <= MaxMessageSize);
356
357 assert(
358 calculatedMessageSize >=
359 SbeType::blockLength(message().version()) +
361 SbeType::getMinimalVariableFieldsSize(message().version()));
362
363 messageSize(calculatedMessageSize);
364 return calculatedMessageSize;
365 }
366
369 {
370 setHeader();
371
372#ifndef NDEBUG
373 NetworkMessage(buffer_, BufferSize);
374#endif
376 }
377
380 std::string toString() const
381 {
382 return toStr(*this);
383 }
384
385private:
386 void init(SchemaVersion version)
387 {
388#ifdef ONIXS_ICEBOE_MEMCHECK_DEBUG
389 std::memset(buffer_, magicDebugValue, BufferSize);
390#endif
391 messageSize(0);
392
393 message_ = MessageInitializer::template createMessage<MessageType>(
394 buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, version);
395 }
396
397 void messageSize(UInt16 size) noexcept
398 {
399 header()->setup(size + sizeof(SimpleOpenFramingHeader));
400 }
401
402 void copyFrom(const MessageHolder& r)
403 {
404#ifdef ONIXS_ICEBOE_MEMCHECK_DEBUG
405 std::memset(buffer_, magicDebugValue, BufferSize);
406#endif
407
408 const size_t sizeToCopy = r.message_.MessageType::calculateBinarySize() + sizeof(SimpleOpenFramingHeader);
409
410 assert(r.message_.calculateBinarySize() <= MaxMessageSize);
411
412 std::memcpy(buffer_, r.buffer_, sizeToCopy);
413
414 message_ = MessageType(buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, SbeMessage::NoInit{}, SbeMessage::NoCheck{});
415 }
416
417private:
418 MessageType message_;
419 unsigned char buffer_[BufferSize];
420};
421
422template <
423 template <typename, size_t, typename> class HolderType,
424 typename MsgType,
425 size_t MaxMessageSize,
426 typename MessageInitializer>
427inline std::string toStr(
428 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
429{
430 std::string res;
431 toStr(res, holder);
432 return res;
433}
434
435template <
436 template <typename, size_t, typename> class HolderType,
437 typename MsgType,
438 size_t MaxMessageSize,
439 typename MessageInitializer>
440std::ostream& operator<<(
441 std::ostream& stream,
442 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
443{
444 return stream << toStr(rhs);
445}
446
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:102
#define ONIXS_ICEBOE_NAMESPACE_BEGIN
Definition ABI.h:94
#define ONIXS_ICEBOE_EXPORTED_CLASS
Definition ABI.h:41
#define ONIXS_ICEBOE_NAMESPACE_END
Definition ABI.h:98
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_END
Definition ABI.h:106
#define ONIXS_ICEBOE_EXPORTED
Definition Compiler.h:153
#define ONIXS_ICEBOE_NODISCARD
Definition Compiler.h:154
Message identifiers and length of message root.
Definition Composites.h:57
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
NetworkMessage toNetworkMessage() noexcept
MessageHolder & operator=(const MessageHolder &r)
SimpleOpenFramingHeader * header() noexcept
const MessageType & message() const noexcept
MessageHolder(SchemaVersion version=SbeType::Schema::Version)
const unsigned char * buffer() const noexcept
MessageSize setHeader() noexcept
Calculates the binary size of the message and updates the Simple Open Framing Header accordingly.
const MessageType * operator->() const noexcept
constexpr UInt8 DefaultMaxGroupItems
Default maximum number of repeating group items.
std::ostream & operator<<(std::ostream &stream, const FloatingPointDecimal< Mantissa, Exponent > &value)
Serializes into a stream.
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
MessageHeader::Version SchemaVersion
SBE-encoded data version type.
constexpr UInt16 MaxSbeMessageSize
Maximum supported message size.
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.
UInt16 MessageSize
Message length type.
Definition Aliases.h:29
Messaging::SchemaVersion getMessagingVersion(const Session &) noexcept
The policy to create messages with null-initialized optional fields.
static SbeMessageType createMessage(void *data, MessageSize length, SchemaVersion version)
static const char * name() noexcept
The policy to create messages without initialized optional fields.
static SbeMessageType createMessage(void *data, MessageSize length, SchemaVersion version)
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoFieldsInit, SchemaVersion version)
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit, SbeMessage::NoCheck) noexcept
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit)
ONIXS_ICEBOE_STATIC_ASSERT((std::is_base_of< SbeMessage, MessageType >::value))
HeldAdapter(void *data, EncodedLength length, SchemaVersion version)
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoFieldsInit, SchemaVersion version)
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit, SbeMessage::NoCheck) noexcept
HeldAdapter(void *data, EncodedLength length, SbeMessage::NoInit)
HeldAdapter(void *data, EncodedLength length, SchemaVersion version)
ONIXS_ICEBOE_STATIC_ASSERT((std::is_base_of< TagBased::Message, MessageType >::value))