OnixS C++ B3 BOE Binary Order Entry 1.3.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
24
25#if defined(ONIXS_B3_BOE_HAS_GATEWAY_EMULATOR)
26
28
29#include <memory>
30
31namespace OnixS {
32namespace B3 {
33namespace BOE {
34namespace Testing {
35
36using namespace Messaging;
37
40void throwWrongType(UInt16, UInt16);
41
43inline
44bool isMessageValid(const SbeMessage& msg) noexcept
45{
46 return msg.valid();
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 <class AnotherType>
200 void fromAnotherType(const MessagePtr<AnotherType>& rhs)
201 {
202 if(rhs->templateId() != Message::TemplateId)
203 throwWrongType(Message::TemplateId, rhs->templateId());
204
205 message_ = Messaging::typify<Message>(rhs);
206 }
207
208private:
209 MessageSize size_;
210 Container container_;
211 Message message_;
212
213 template<class T> friend class MessagePtr;
214};
215
216
217template<>
218inline MessagePtr<SbeMessage>::MessagePtr(Container&& container, MessageSize size)
219 : size_(size)
220 , container_(std::move(container))
221 , message_(container_.get(), size_)
222{
223}
224
226template <typename AnotherType, typename BaseType>
231
232}}}}
233
234#endif
#define ONIXS_B3_BOE_NORETURN
Definition Compiler.h:190
#define ONIXS_B3_BOE_EXPORTED
Definition Compiler.h:181
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
Message * operator->() noexcept
Definition MessagePtr.h:126
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
Message typify(const SbeMessage &message)
Casts SBE-encoded message to a given type.
UInt16 MessageSize
Message length type.
Definition Aliases.h:29
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:227