OnixS C++ CME iLink 3 Binary Order Entry Handler 1.18.9
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
24
25#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
26
28
29#include <memory>
30
31namespace OnixS {
32namespace CME {
33namespace iLink3 {
34namespace Testing {
35
36using namespace Messaging;
37
41
43inline
44bool isMessageValid(const SbeMessage& msg) noexcept
45{
46 return msg.valid();
47}
48
50inline
51bool isMessageValid(const TagBased::Message& msg) noexcept
52{
53 return static_cast<bool>(msg);
54}
55
57template<typename Message>
59{
60public:
61 using Container = std::shared_ptr<UInt8>;
62
65
67 template <class AnotherType>
68 explicit
70 : size_(rhs.size_)
71 , container_(rhs.container_)
72 , message_()
73 {
74 fromAnotherType(rhs);
75 }
76
78 template <class AnotherType>
79 explicit
81 : size_(rhs.size_)
82 , container_(std::move(rhs.container_))
83 , message_()
84 {
85 fromAnotherType(rhs);
86 }
87
90 : size_(0)
91 , container_()
92 , message_()
93 {
94 }
95
96 MessagePtr(const MessagePtr&) = default;
97 MessagePtr& operator=(const MessagePtr&) = default;
98
100 MessagePtr(MessagePtr&& rhs) noexcept
101 {
102 rhs.swap(*this);
103 rhs.reset();
104 }
105
108 {
109 if (&rhs == this)
110 return *this;
111
112 rhs.swap(*this);
113 rhs.reset();
114
115 return *this;
116 }
117
119 bool valid() const noexcept
120 {
121 return isMessageValid(message_);
122 }
123
125 void swap(MessagePtr& rhs) noexcept
126 {
127 std::swap(rhs.size_, size_);
128 std::swap(rhs.container_, container_);
129 std::swap(rhs.message_, message_);
130 }
131
133 Message* operator->() noexcept
134 {
135 assert(valid());
136 return &message_;
137 }
138
140 const Message* operator->() const noexcept
141 {
142 assert(valid());
143 return &message_;
144 }
145
147 Message& operator*() noexcept
148 {
149 return message_;
150 }
151
153 const Message& operator*() const noexcept
154 {
155 return message_;
156 }
157
159 Message& message() noexcept
160 {
161 assert(valid());
162 return message_;
163 }
164
166 const Message& message() const noexcept
167 {
168 assert(valid());
169 return message_;
170 }
171
173 operator Message() noexcept
174 {
175 return message();
176 }
177
179 operator const Message() const noexcept
180 {
181 return message();
182 }
183
185 UInt8* buf() noexcept
186 {
187 return container_.get();
188 }
189
191 void reset()
192 {
193 size_ = 0;
194 container_.reset();
195 message_ = Message();
196 }
197
199 std::string toString() const
200 {
201 return message_.toString();
202 }
203
204private:
205
206 template<typename MessageT>
207 static
208 constexpr
209 typename std::enable_if<!std::is_base_of<TagBased::Message, MessageT>::value, MessageTemplateId>::type
210 getTemplateId()
211 {
212 static_assert(std::is_base_of<SbeMessage, MessageT>::value, "");
213
214 return MessageT::TemplateId;
215 }
216
217 template<typename MessageT>
218 static
219 constexpr
220 typename std::enable_if<std::is_base_of<TagBased::Message, MessageT>::value, MessageTemplateId>::type
221 getTemplateId()
222 {
223 static_assert(std::is_base_of<SbeMessage, typename MessageT::Binary>::value, "");
224
225 return MessageT::Binary::TemplateId;
226 }
227
228 template<typename MessageT>
229 static
230 typename std::enable_if<!std::is_base_of<TagBased::Message, MessageT>::value, MessageT>::type
231 typify(SbeMessage message)
232 {
233 static_assert(std::is_base_of<SbeMessage, MessageT>::value, "");
234
236 }
237
238 template<typename MessageT>
239 static
240 typename std::enable_if<std::is_base_of<TagBased::Message, MessageT>::value, MessageT>::type
241 typify(SbeMessage message)
242 {
243 static_assert(std::is_base_of<SbeMessage, typename MessageT::Binary>::value, "");
244
246 }
247
248 template <class AnotherType>
249 void fromAnotherType(const MessagePtr<AnotherType>& rhs)
250 {
251 if(rhs->templateId() != getTemplateId<Message>())
252 throwWrongType(getTemplateId<Message>(), rhs->templateId());
253
254 message_ = this->typify<Message>(rhs);
255 }
256
257private:
258 MessageSize size_;
259 Container container_;
260 Message message_;
261
262 template<class T> friend class MessagePtr;
263};
264
265
266template<>
267inline MessagePtr<SbeMessage>::MessagePtr(Container&& container, MessageSize size)
268 : size_(size)
269 , container_(std::move(container))
270 , message_(container_.get(), size_)
271{
272}
273
275template <typename AnotherType, typename BaseType>
280
281}}}}
282
283#endif
#define ONIXS_ILINK3_NORETURN
Definition Compiler.h:184
#define ONIXS_ILINK3_EXPORTED
Definition Compiler.h:175
Implements a tag-based interface over an SBE-encoded message.
Definition Message.h:664
const Message & message() const noexcept
Definition MessagePtr.h:166
const Message & operator*() const noexcept
Definition MessagePtr.h:153
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition MessagePtr.h:80
const Message * operator->() const noexcept
Definition MessagePtr.h:140
MessagePtr(const MessagePtr &)=default
MessagePtr()
Creates an empty message container.
Definition MessagePtr.h:89
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition MessagePtr.h:69
void swap(MessagePtr &rhs) noexcept
Definition MessagePtr.h:125
MessagePtr(Container &&, MessageSize)
Creates from the given memory block.
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:107
MessagePtr & operator=(const MessagePtr &)=default
MessagePtr(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:100
void reset()
Resets the message.
Definition MessagePtr.h:191
Message typify(const SbeMessage &message)
Casts SBE-encoded message to a given type.
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:44
void throwWrongType(UInt16, UInt16)
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:276