OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
MessagePtr.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
25
26#include <memory>
27
29namespace Testing {
30
31using namespace Messaging;
32
33
34[[noreturn]] ONIXS_ICEBOE_EXPORTED
36
38inline bool isMessageValid(const SbeMessage msg) noexcept
39{
40 return msg.valid();
41}
42
44inline bool isMessageValid(const TagBased::Message& msg) noexcept
45{
46 return static_cast<bool>(msg);
47}
48
50template<typename Message>
52{
53public:
54 using Container = std::shared_ptr<UInt8>;
55
58
60 template <class AnotherType>
61 explicit
63 : size_(rhs.size_)
64 , container_(rhs.container_)
65 , message_()
66 {
67 fromAnotherType(rhs);
68 }
69
71 template <class AnotherType>
72 explicit
74 : size_(rhs.size_)
75 , container_(std::move(rhs.container_))
76 , message_()
77 {
78 fromAnotherType(rhs);
79 }
80
83 : size_(0)
84 , container_()
85 , message_()
86 {
87 }
88
89 MessagePtr(const MessagePtr&) = default;
90 MessagePtr& operator=(const MessagePtr&) = default;
91
93 MessagePtr(MessagePtr&& rhs) noexcept
94 {
95 rhs.swap(*this);
96 rhs.reset();
97 }
98
101 {
102 if (&rhs == this)
103 return *this;
104
105 rhs.swap(*this);
106 rhs.reset();
107
108 return *this;
109 }
110
112 bool valid() const noexcept
113 {
114 return isMessageValid(message_);
115 }
116
118 void swap(MessagePtr& rhs) noexcept
119 {
120 std::swap(rhs.size_, size_);
121 std::swap(rhs.container_, container_);
122 std::swap(rhs.message_, message_);
123 }
124
126 Message* operator->() noexcept
127 {
128 assert(valid());
129 return &message_;
130 }
131
133 const Message* operator->() const noexcept
134 {
135 assert(valid());
136 return &message_;
137 }
138
140 Message& operator*() noexcept
141 {
142 return message_;
143 }
144
146 const Message& operator*() const noexcept
147 {
148 return message_;
149 }
150
152 Message& message() noexcept
153 {
154 assert(valid());
155 return message_;
156 }
157
159 const Message& message() const noexcept
160 {
161 assert(valid());
162 return message_;
163 }
164
166 operator Message() noexcept
167 {
168 return message();
169 }
170
172 operator const Message() const noexcept
173 {
174 return message();
175 }
176
178 UInt8* buf() noexcept
179 {
180 return container_.get();
181 }
182
184 void reset()
185 {
186 size_ = 0;
187 container_.reset();
188 message_ = Message();
189 }
190
192 std::string toString() const
193 {
194 return message_.toString();
195 }
196
197private:
198
199 template<typename MessageT>
200 static constexpr typename std::enable_if<!std::is_base_of<TagBased::Message, MessageT>::value, MessageTemplateId>::type
201 getTemplateId()
202 {
203 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, MessageT>::value);
204
205 return MessageT::TemplateId;
206 }
207
208 template<typename MessageT>
209 static constexpr typename std::enable_if<std::is_base_of<TagBased::Message, MessageT>::value, MessageTemplateId>::type
210 getTemplateId()
211 {
212 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, typename MessageT::Binary>::value);
213
214 return MessageT::Binary::TemplateId;
215 }
216
217 template<typename MessageT>
218 static typename std::enable_if<!std::is_base_of<TagBased::Message, MessageT>::value, MessageT>::type
219 typify(SbeMessage message)
220 {
221 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, MessageT>::value);
222
223 return Messaging::typify<MessageT>(message);
224 }
225
226 template<typename MessageT>
227 static typename std::enable_if<std::is_base_of<TagBased::Message, MessageT>::value, MessageT>::type
228 typify(SbeMessage message)
229 {
230 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, typename MessageT::Binary>::value);
231
232 return MessageT(Messaging::typify<typename Message::Binary>(message));
233 }
234
235 template <class AnotherType>
236 void fromAnotherType(const MessagePtr<AnotherType>& rhs)
237 {
238 if(rhs->templateId() != getTemplateId<Message>())
239 throwWrongType(getTemplateId<Message>(), rhs->templateId());
240
241 message_ = this->typify<Message>(rhs);
242 }
243
244private:
245 MessageSize size_;
246 Container container_;
247 Message message_;
248
249 template<class T> friend class MessagePtr;
250};
251
252
253template<>
254inline MessagePtr<SbeMessage>::MessagePtr(Container&& container, MessageSize size)
255 : size_(size)
256 , container_(std::move(container))
257 , message_(container_.get(), size_)
258{
259}
260
262template <typename AnotherType, typename BaseType>
267
268}
#define ONIXS_ICEBOE_NAMESPACE_BEGIN
Definition ABI.h:94
#define ONIXS_ICEBOE_NAMESPACE_END
Definition ABI.h:98
#define ONIXS_ICEBOE_EXPORTED
Definition Compiler.h:155
const Message & message() const noexcept
Definition MessagePtr.h:159
const Message & operator*() const noexcept
Definition MessagePtr.h:146
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition MessagePtr.h:73
const Message * operator->() const noexcept
Definition MessagePtr.h:133
MessagePtr(const MessagePtr &)=default
MessagePtr()
Creates an empty message container.
Definition MessagePtr.h:82
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition MessagePtr.h:62
void swap(MessagePtr &rhs) noexcept
Definition MessagePtr.h:118
MessagePtr(Container &&, MessageSize)
Creates from the given memory block.
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:100
MessagePtr & operator=(const MessagePtr &)=default
MessagePtr(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:93
void reset()
Resets the message.
Definition MessagePtr.h:184
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
UInt16 MessageSize
Message length type.
Definition Aliases.h:29
MessageHeader::TemplateId MessageTemplateId
Message type (template) identification.
bool isMessageValid(const SbeMessage msg) noexcept
Definition MessagePtr.h:38
void throwWrongType(UInt16, UInt16)
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:263