OnixS C++ ICE Binary Order Entry Handler 1.0.0
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
39bool isMessageValid(const SbeMessage& msg) noexcept
40{
41 return msg.valid();
42}
43
45inline
46bool isMessageValid(const TagBased::Message& msg) noexcept
47{
48 return static_cast<bool>(msg);
49}
50
52template<typename Message>
54{
55public:
56 using Container = std::shared_ptr<UInt8>;
57
60
62 template <class AnotherType>
63 explicit
65 : size_(rhs.size_)
66 , container_(rhs.container_)
67 , message_()
68 {
69 fromAnotherType(rhs);
70 }
71
73 template <class AnotherType>
74 explicit
76 : size_(rhs.size_)
77 , container_(std::move(rhs.container_))
78 , message_()
79 {
80 fromAnotherType(rhs);
81 }
82
85 : size_(0)
86 , container_()
87 , message_()
88 {
89 }
90
91 MessagePtr(const MessagePtr&) = default;
92 MessagePtr& operator=(const MessagePtr&) = default;
93
95 MessagePtr(MessagePtr&& rhs) noexcept
96 {
97 rhs.swap(*this);
98 rhs.reset();
99 }
100
103 {
104 if (&rhs == this)
105 return *this;
106
107 rhs.swap(*this);
108 rhs.reset();
109
110 return *this;
111 }
112
114 bool valid() const noexcept
115 {
116 return isMessageValid(message_);
117 }
118
120 void swap(MessagePtr& rhs) noexcept
121 {
122 std::swap(rhs.size_, size_);
123 std::swap(rhs.container_, container_);
124 std::swap(rhs.message_, message_);
125 }
126
128 Message* operator->() noexcept
129 {
130 assert(valid());
131 return &message_;
132 }
133
135 const Message* operator->() const noexcept
136 {
137 assert(valid());
138 return &message_;
139 }
140
142 Message& operator*() noexcept
143 {
144 return message_;
145 }
146
148 const Message& operator*() const noexcept
149 {
150 return message_;
151 }
152
154 Message& message() noexcept
155 {
156 assert(valid());
157 return message_;
158 }
159
161 const Message& message() const noexcept
162 {
163 assert(valid());
164 return message_;
165 }
166
168 operator Message() noexcept
169 {
170 return message();
171 }
172
174 operator const Message() const noexcept
175 {
176 return message();
177 }
178
180 UInt8* buf() noexcept
181 {
182 return container_.get();
183 }
184
186 void reset()
187 {
188 size_ = 0;
189 container_.reset();
190 message_ = Message();
191 }
192
194 std::string toString() const
195 {
196 return message_.toString();
197 }
198
199private:
200
201 template<typename MessageT>
202 static
203 constexpr
204 typename std::enable_if<!std::is_base_of<TagBased::Message, MessageT>::value, MessageTemplateId>::type
205 getTemplateId()
206 {
207 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, MessageT>::value);
208
209 return MessageT::TemplateId;
210 }
211
212 template<typename MessageT>
213 static
214 constexpr
215 typename std::enable_if<std::is_base_of<TagBased::Message, MessageT>::value, MessageTemplateId>::type
216 getTemplateId()
217 {
218 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, typename MessageT::Binary>::value);
219
220 return MessageT::Binary::TemplateId;
221 }
222
223 template<typename MessageT>
224 static
225 typename std::enable_if<!std::is_base_of<TagBased::Message, MessageT>::value, MessageT>::type
226 typify(SbeMessage message)
227 {
228 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, MessageT>::value);
229
230 return Messaging::typify<MessageT>(message);
231 }
232
233 template<typename MessageT>
234 static
235 typename std::enable_if<std::is_base_of<TagBased::Message, MessageT>::value, MessageT>::type
236 typify(SbeMessage message)
237 {
238 ONIXS_ICEBOE_STATIC_ASSERT(std::is_base_of<SbeMessage, typename MessageT::Binary>::value);
239
240 return MessageT(Messaging::typify<typename Message::Binary>(message));
241 }
242
243 template <class AnotherType>
244 void fromAnotherType(const MessagePtr<AnotherType>& rhs)
245 {
246 if(rhs->templateId() != getTemplateId<Message>())
247 throwWrongType(getTemplateId<Message>(), rhs->templateId());
248
249 message_ = this->typify<Message>(rhs);
250 }
251
252private:
253 MessageSize size_;
254 Container container_;
255 Message message_;
256
257 template<class T> friend class MessagePtr;
258};
259
260
261template<>
262inline MessagePtr<SbeMessage>::MessagePtr(Container&& container, MessageSize size)
263 : size_(size)
264 , container_(std::move(container))
265 , message_(container_.get(), size_)
266{
267}
268
270template <typename AnotherType, typename BaseType>
275
276}
#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:153
const Message & message() const noexcept
Definition MessagePtr.h:161
const Message & operator*() const noexcept
Definition MessagePtr.h:148
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition MessagePtr.h:75
const Message * operator->() const noexcept
Definition MessagePtr.h:135
MessagePtr(const MessagePtr &)=default
MessagePtr()
Creates an empty message container.
Definition MessagePtr.h:84
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition MessagePtr.h:64
void swap(MessagePtr &rhs) noexcept
Definition MessagePtr.h:120
MessagePtr(Container &&, MessageSize)
Creates from the given memory block.
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:102
MessagePtr & operator=(const MessagePtr &)=default
MessagePtr(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:95
void reset()
Resets the message.
Definition MessagePtr.h:186
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:39
void throwWrongType(UInt16, UInt16)
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:271