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