OnixS C++ CME iLink 3 Binary Order Entry Handler 1.19.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
29
30namespace OnixS
31{
32 namespace CME
33 {
34 namespace iLink3
35 {
37 }
38 }
39}
40
42
46
49{
51 template <typename SbeMessageType>
52 inline static SbeMessageType createMessage(
53 void* data, MessageSize length, SchemaVersion version)
54 {
55 return SbeMessageType(data, length, version);
56 }
57
59 inline static const char* name() noexcept
60 {
61 return "FieldsInitPolicy";
62 }
63};
64
67{
69 template <typename SbeMessageType>
70 inline static SbeMessageType createMessage(
71 void* data, MessageSize length, SchemaVersion version)
72 {
73 return SbeMessageType(
74 data, length, SbeMessage::NoFieldsInit(), version);
75 }
76
78 inline static const char* name() noexcept
79 {
80 return "FieldsNoInitPolicy";
81 }
82};
83
85template
86<
87 typename MessageType,
88 bool isFix =
89 IsBaseOf<TagBased::Message, MessageType>::value
90>
91struct HeldAdapter;
92
93template <typename MessageType>
94struct HeldAdapter<MessageType, true> final
95 : public MessageType
96{
97 static_assert(IsBaseOf<TagBased::Message, MessageType>::value, "Not a message.");
98
99 enum { isFix = true };
100
101 typedef typename
102 MessageType::Binary
104
105 typedef typename
106 SbeType::EncodedLength
108
109 HeldAdapter() = default;
110
112 void* data,
113 EncodedLength length,
115 : MessageType(SbeType(data, length, version))
116 {
117 }
118
120 void* data,
121 EncodedLength length,
124 : MessageType(SbeType(data, length, version))
125 {
126 }
127
129 void* data,
130 EncodedLength length,
132 : MessageType(SbeType(data, length))
133 {
134 }
135
137 void* data,
138 EncodedLength length,
141 noexcept
142 : MessageType(
143 SbeType(
144 data,
145 length,
148 {
149 }
150};
151
152template <typename MessageType>
153struct HeldAdapter<MessageType, false> final
154 : public MessageType
155{
156 static_assert(IsBaseOf<SbeMessage, MessageType>::value, "Not a message.");
157
158 enum { isFix = false };
159
160 typedef MessageType SbeType;
161
162 typedef typename
163 SbeType::EncodedLength
165
166 HeldAdapter() = default;
167
169 void* data,
170 EncodedLength length,
172 : MessageType(data, length, version)
173 {
174 }
175
177 void* data,
178 EncodedLength length,
181 : MessageType(data, length, version)
182 {
183 }
184
186 void* data,
187 EncodedLength length,
189 : MessageType(data, length)
190 {
191 }
192
194 void* data,
195 EncodedLength length,
198 noexcept
199 : MessageType(
200 data,
201 length,
204 {
205 }
206};
207
209template <typename, size_t, typename>
210class MessageHolder;
211template <
212 template <typename, size_t, typename> class HolderType,
213 typename MsgType,
214 size_t MaxMessageSize,
215 typename MessageInitializer>
216inline void toStr(
217 std::string& str,
218 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
219{
220 str += "MessageHolder[";
221
222 str += "BufferSize=";
223 toStr(str, holder.BufferSize);
224
225 str += ", MessageInitializer=";
226 str += MessageInitializer::name();
227
228 str += ", ";
229 toStr(str, *holder.header());
230
231 str += ", ";
232 toStr(str, holder.message());
233
234 str += "]";
235}
236
238constexpr char magicDebugValue = 0x5A;
239
242
246template <
247 typename MessageTypeT,
248 size_t MaxMessageSize =
249 GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
250 typename MessageInitializer = FieldsInitPolicy>
252{
253 typedef typename HeldAdapter<MessageTypeT>::SbeType SbeType;
254
255 static_assert(
256 (MaxMessageSize >= sizeof(MessageHeader)),
257 "MaxMessageSize template parameter is too small");
258
259 static_assert(
260 (MaxMessageSize >= GetMinMessageSize<SbeType>::Size),
261 "The buffer can not fit the message");
262
263 static_assert(
264 (MaxMessageSize <= MaxILink3MessageSize),
265 "The buffer is too large.");
266
267public:
269 typedef HeldAdapter<MessageTypeT> MessageType;
270
272 enum
273 {
274 BufferSize = MaxMessageSize + sizeof(SimpleOpenFramingHeader)
275 };
276
277 explicit MessageHolder(SchemaVersion version = SbeType::Schema::Version)
278 {
279 init(version);
280 }
281
282 explicit MessageHolder(const Session& session)
283 {
284 init(getMessagingVersion(session));
285 }
286
288 {
289 copyFrom(r);
290 }
291
293 {
294 copyFrom(r);
295 return *this;
296 }
297
299 const unsigned char* buffer() const noexcept
300 {
301 return buffer_;
302 }
303
305 UInt16 bufferSize() const noexcept
306 {
307 return header()->size();
308 }
309
312 {
313 return message_;
314 }
315
317 const MessageType& message() const noexcept
318 {
319 return message_;
320 }
321
323 UInt16 messageSize() const noexcept
324 {
325 return bufferSize() - sizeof(SimpleOpenFramingHeader);
326 }
327
329 {
330 return &message();
331 }
332
333 const MessageType* operator->() const noexcept
334 {
335 return &message();
336 }
337
338 const MessageType& operator* () const noexcept
339 {
340 return message();
341 }
342
343 MessageType& operator* () noexcept
344 {
345 return message();
346 }
347
349 const SimpleOpenFramingHeader* header() const noexcept
350 {
351 return reinterpret_cast<const SimpleOpenFramingHeader*>(buffer_);
352 }
353
356 {
357 return reinterpret_cast<SimpleOpenFramingHeader*>(buffer_);
358 }
359
365 {
366 const MessageSize calculatedMessageSize =
367 message().calculateBinarySize();
368
369 assert(calculatedMessageSize <= MaxMessageSize);
370
371 assert(
372 calculatedMessageSize >=
373 SbeType::blockLength(message().version()) +
375 SbeType::getMinimalVariableFieldsSize(message().version()));
376
377 messageSize(calculatedMessageSize);
378 return calculatedMessageSize;
379 }
380
383 {
384 setHeader();
385
386#ifndef NDEBUG
387 NetworkMessage(buffer_, BufferSize);
388#endif
390 }
391
394 std::string toString() const
395 {
396 return toStr(*this);
397 }
398
399private:
400 void init(SchemaVersion version)
401 {
402#ifdef ONIXS_ILINK3_MEMCHECK_DEBUG
403 std::memset(buffer_, magicDebugValue, BufferSize);
404#endif
405 messageSize(0);
406
407 message_ = MessageInitializer::template createMessage<MessageType>(
408 buffer_ + sizeof(SimpleOpenFramingHeader), MaxMessageSize, version);
409 }
410
411 void messageSize(UInt16 size) noexcept
412 {
413 header()->setup(
414 size + sizeof(SimpleOpenFramingHeader));
415 }
416
417 void copyFrom(const MessageHolder& r)
418 {
419#ifdef ONIXS_ILINK3_MEMCHECK_DEBUG
420 std::memset(buffer_, magicDebugValue, BufferSize);
421#endif
422
423 const size_t sizeToCopy = r.message_.MessageType::calculateBinarySize() + sizeof(SimpleOpenFramingHeader);
424
425 assert(r.message_.calculateBinarySize() <= MaxMessageSize);
426
427 std::memcpy(
428 buffer_,
429 r.buffer_,
430 sizeToCopy);
431
432 message_ =
433 MessageType(
434 buffer_ + sizeof(SimpleOpenFramingHeader),
435 MaxMessageSize,
436 SbeMessage::NoInit(),
437 SbeMessage::NoCheck());
438 }
439
440private:
441 MessageType message_;
442 unsigned char buffer_[BufferSize];
443};
444
445template <
446 template <typename, size_t, typename> class HolderType,
447 typename MsgType,
448 size_t MaxMessageSize,
449 typename MessageInitializer>
450inline std::string toStr(
451 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& holder)
452{
453 std::string res;
454 toStr(res, holder);
455 return res;
456}
457
458template <
459 template <typename, size_t, typename> class HolderType,
460 typename MsgType,
461 size_t MaxMessageSize,
462 typename MessageInitializer>
463std::ostream& operator<<(
464 std::ostream& stream,
465 const HolderType<MsgType, MaxMessageSize, MessageInitializer>& rhs)
466{
467 return stream << toStr(rhs);
468}
469
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_ILINK3_EXPORTED_CLASS
Definition ABI.h:44
#define ONIXS_ILINK3_NODISCARD
Definition Compiler.h:147
#define ONIXS_ILINK3_EXPORTED
Definition Compiler.h:145
Template ID and length of message root.
Definition Composites.h:192
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
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
An iLink 3 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.
MessageHeader::Version SchemaVersion
SBE-encoded data version type.
constexpr UInt16 MaxILink3MessageSize
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
const char * version() noexcept
The policy to create messages with null-initialized optional fields.
The policy to create messages without initialized optional fields.
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)
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)