OnixS C++ B3 BOE Binary Order Entry 1.3.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
29namespace OnixS
30{
31 namespace B3
32 {
33 namespace BOE
34 {
36 }
37 }
38}
39
41
45
48{
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
58 inline static const char* name() ONIXS_B3_BOE_NOTHROW
59 {
60 return "FieldsInitPolicy";
61 }
62};
63
66{
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
77 inline static const char* name() ONIXS_B3_BOE_NOTHROW
78 {
79 return "FieldsNoInitPolicy";
80 }
81};
82
84template <typename MessageType>
85struct 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,
111 SbeMessage::NoFieldsInit,
112 SchemaVersion version)
113 : MessageType(data, length, version)
114 {
115 }
116
117 HeldAdapter(
118 void* data,
119 EncodedLength length,
120 SbeMessage::NoInit)
121 : MessageType(data, length)
122 {
123 }
124
125 HeldAdapter(
126 void* data,
127 EncodedLength length,
128 SbeMessage::NoInit,
129 SbeMessage::NoCheck)
131 : MessageType(
132 data,
133 length,
134 SbeMessage::NoInit(),
135 SbeMessage::NoCheck())
136 {
137 }
138};
139
141template <typename, size_t, typename>
142class MessageHolder;
143template <
144 template <typename, size_t, typename> class HolderType,
145 typename MsgType,
146 size_t MaxMessageSize,
147 typename MessageInitializer>
148inline 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
170ONIXS_B3_BOE_CONST_OR_CONSTEXPR char magicDebugValue = 0x5A;
171
174
178template <
179 typename MessageTypeT,
180 size_t MaxMessageSize =
181 GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
182 typename MessageInitializer = FieldsInitPolicy>
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
199public:
201 typedef HeldAdapter<MessageTypeT> MessageType;
202
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
231 const unsigned char* buffer() const ONIXS_B3_BOE_NOTHROW
232 {
233 return buffer_;
234 }
235
238 {
239 return header()->size();
240 }
241
244 {
245 return message_;
246 }
247
250 {
251 return message_;
252 }
253
256 {
257 return bufferSize() - sizeof(SimpleOpenFramingHeader);
258 }
259
261 {
262 return &message();
263 }
264
266 {
267 return &message();
268 }
269
270 const MessageType& operator* () const ONIXS_B3_BOE_NOTHROW
271 {
272 return message();
273 }
274
276 {
277 return message();
278 }
279
282 {
283 return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
284 }
285
288 {
289 return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
290 }
291
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
315 {
316 setHeader();
317
318#ifndef NDEBUG
319 NetworkMessage(buffer_, BufferSize);
320#endif
322 }
323
326 std::string toString() const
327 {
328 return toStr(*this);
329 }
330
331private:
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,
368 SbeMessage::NoInit(),
369 SbeMessage::NoCheck());
370 }
371
372private:
373 MessageType message_;
374 unsigned char buffer_[BufferSize];
375};
376
377template <
378 template <typename, size_t, typename> class HolderType,
379 typename MsgType,
380 size_t MaxMessageSize,
381 typename MessageInitializer>
382inline std::string toStr(
383 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
384{
385 std::string res;
386 toStr(res, holder);
387 return res;
388}
389
390template <
391 template <typename, size_t, typename> class HolderType,
392 typename MsgType,
393 size_t MaxMessageSize,
394 typename MessageInitializer>
395std::ostream& operator<<(
396 std::ostream& stream,
397 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
398{
399 return stream << toStr(rhs);
400}
401
#define ONIXS_B3_BOE_EXPORTED_CLASS
Definition ABI.h:44
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_B3_BOE_NODISCARD
Definition Compiler.h:191
#define ONIXS_B3_BOE_DEFAULT
Definition Compiler.h:208
#define ONIXS_B3_BOE_EXPORTED
Definition Compiler.h:181
#define ONIXS_B3_BOE_CONST_OR_CONSTEXPR
Definition Compiler.h:184
#define ONIXS_B3_BOE_NOTHROW
Definition Compiler.h:182
Message identifiers and length of message root.
Definition Composites.h:34
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
A FIXP Session.
Definition Session.h:48
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 UInt16 MaxB3BOEMessageSize
Maximum supported message size.
MessageHeader::Version SchemaVersion
SBE-encoded data version type.
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.
The policy to create messages without initialized optional fields.