OnixS C++ B3 BOE Binary Order Entry 1.4.0
Users' manual and 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, bool skipHeader = false, bool skipEmptyFields = false)
151{
152 str += "MessageHolder[";
153
154 str += "BufferSize=";
155 toStr(str, holder.BufferSize);
156
157 str += ", MessageInitializer=";
158 str += MessageInitializer::name();
159
160 if(!skipHeader)
161 {
162 str += ", ";
163 toStr(str, *holder.header());
164 }
165
166 str += ", ";
167 toStr(
168 str,
169 static_cast<const MsgType&>(holder.message()),
170 skipEmptyFields);
171
172 str += "]";
173}
174
176ONIXS_B3_BOE_CONST_OR_CONSTEXPR char magicDebugValue = 0x5A;
177
180
184template <
185 typename MessageTypeT,
186 size_t MaxMessageSize =
187 GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
188 typename MessageInitializer = FieldsInitPolicy>
190{
191 typedef typename HeldAdapter<MessageTypeT>::SbeType SbeType;
192
193 ONIXS_B3_BOE_STATIC_ASSERT_MSG(
194 (MaxMessageSize >= sizeof(MessageHeader)),
195 "MaxMessageSize template parameter is too small");
196
197 ONIXS_B3_BOE_STATIC_ASSERT_MSG(
198 (MaxMessageSize >= GetMinMessageSize<SbeType>::Size),
199 "The buffer can not fit the message");
200
201 ONIXS_B3_BOE_STATIC_ASSERT_MSG(
202 (MaxMessageSize <= MaxB3BOEMessageSize),
203 "The buffer is too large.");
204
205public:
207 typedef HeldAdapter<MessageTypeT> MessageType;
208
210 enum
211 {
212 BufferSize = MaxMessageSize + sizeof(SimpleOpenFramingHeader)
213 };
214
215 explicit MessageHolder(SchemaVersion version = SbeType::Schema::Version)
216 {
217 init(version);
218 }
219
220 explicit MessageHolder(const Session& session)
221 {
222 init(getMessagingVersion(session));
223 }
224
226 {
227 copyFrom(r);
228 }
229
231 {
232 copyFrom(r);
233 return *this;
234 }
235
237 const unsigned char* buffer() const ONIXS_B3_BOE_NOTHROW
238 {
239 return buffer_;
240 }
241
244 {
245 return header()->size();
246 }
247
250 {
251 return message_;
252 }
253
256 {
257 return message_;
258 }
259
262 {
263 return bufferSize() - sizeof(SimpleOpenFramingHeader);
264 }
265
267 {
268 return &message();
269 }
270
272 {
273 return &message();
274 }
275
276 const MessageType& operator* () const ONIXS_B3_BOE_NOTHROW
277 {
278 return message();
279 }
280
282 {
283 return message();
284 }
285
288 {
289 return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
290 }
291
294 {
295 return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
296 }
297
303 {
304 const MessageSize calculatedMessageSize =
305 message().calculateBinarySize();
306
307 assert(calculatedMessageSize <= MaxMessageSize);
308
309 assert(
310 calculatedMessageSize >=
311 SbeType::blockLength(message().version()) +
313 SbeType::getMinimalVariableFieldsSize(message().version()));
314
315 messageSize(calculatedMessageSize);
316 return calculatedMessageSize;
317 }
318
321 {
322 setHeader();
323
324#ifndef NDEBUG
325 NetworkMessage(buffer_, BufferSize);
326#endif
328 }
329
332 std::string toString(bool skipHeader = false, bool skipEmptyFields = false) const
333 {
334 return toStr(*this, skipHeader, skipEmptyFields);
335 }
336
337private:
338 void init(SchemaVersion version)
339 {
340#ifdef ONIXS_B3_BOE_MEMCHECK_DEBUG
341 std::memset(buffer_, magicDebugValue, BufferSize);
342#endif
343 messageSize(0);
344
345 message_ = MessageInitializer::template createMessage<MessageType>(
346 buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, version);
347 }
348
349 void messageSize(UInt16 size) ONIXS_B3_BOE_NOTHROW
350 {
351 header()->setup(
352 size + sizeof(SimpleOpenFramingHeader));
353 }
354
355 void copyFrom(const MessageHolder& r)
356 {
357#ifdef ONIXS_B3_BOE_MEMCHECK_DEBUG
358 std::memset(buffer_, magicDebugValue, BufferSize);
359#endif
360
361 const size_t sizeToCopy = r.message_.MessageType::calculateBinarySize() + sizeof(SimpleOpenFramingHeader);
362
363 assert(r.message_.calculateBinarySize() <= MaxMessageSize);
364
365 std::memcpy(
366 buffer_,
367 r.buffer_,
368 sizeToCopy);
369
370 message_ =
371 MessageType(
372 buffer_ + sizeof(SimpleOpenFramingHeader),
373 MaxMessageSize,
374 SbeMessage::NoInit(),
375 SbeMessage::NoCheck());
376 }
377
378private:
379 MessageType message_;
380 unsigned char buffer_[BufferSize];
381};
382
383template <
384 template <typename, size_t, typename> class HolderType,
385 typename MsgType,
386 size_t MaxMessageSize,
387 typename MessageInitializer>
388inline std::string toStr(
389 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder, bool skipHeader = false, bool skipEmptyFields = false)
390{
391 std::string res;
392 toStr(res, holder, skipHeader, skipEmptyFields);
393 return res;
394}
395
396template <
397 template <typename, size_t, typename> class HolderType,
398 typename MsgType,
399 size_t MaxMessageSize,
400 typename MessageInitializer>
401std::ostream& operator<<(
402 std::ostream& stream,
403 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
404{
405 return stream << toStr(rhs);
406}
407
#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
const MessageType * operator->() const noexcept
std::string toString(bool skipHeader=false, bool skipEmptyFields=false) const
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.