OnixS C++ CME MDP Conflated TCP Handler 1.3.6
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_CONFLATEDTCP_HAS_GATEWAY_EMULATOR)
26
28
29#include <memory>
30
31namespace OnixS {
32namespace CME {
33namespace ConflatedTCP {
34namespace Testing {
35
36using namespace Messaging;
37
40void throwWrongType(UInt16, UInt16);
41
43template<typename Message>
45{
46public:
47 using Container = std::shared_ptr<UInt8>;
48
51
53 template <class AnotherType>
54 explicit
56 : size_(rhs.size_)
57 , container_(rhs.container_)
58 , message_()
59 {
60 fromAnotherType(rhs);
61 }
62
64 template <class AnotherType>
65 explicit
67 : size_(rhs.size_)
68 , container_(std::move(rhs.container_))
69 , message_()
70 {
71 fromAnotherType(rhs);
72 }
73
76 : size_(0)
77 , container_()
78 , message_()
79 {
80 }
81
82 MessagePtr(const MessagePtr&) = default;
83 MessagePtr& operator=(const MessagePtr&) = default;
84
86 MessagePtr(MessagePtr&& rhs) noexcept
87 {
88 rhs.swap(*this);
89 rhs.reset();
90 }
91
94 {
95 if (&rhs == this)
96 return *this;
97
98 rhs.swap(*this);
99 rhs.reset();
100
101 return *this;
102 }
103
105 void swap(MessagePtr& rhs) noexcept
106 {
107 std::swap(rhs.size_, size_);
108 std::swap(rhs.container_, container_);
109 std::swap(rhs.message_, message_);
110 }
111
113 Message* operator->() noexcept
114 {
115 return &message_;
116 }
117
119 const Message* operator->() const noexcept
120 {
121 return &message_;
122 }
123
125 Message& message() noexcept
126 {
127 return message_;
128 }
129
131 const Message& message() const noexcept
132 {
133 return message_;
134 }
135
137 operator Message()
138 {
139 return message();
140 }
141
143 operator const Message() const
144 {
145 return message();
146 }
147
149 UInt8* buf() noexcept
150 {
151 return container_.get();
152 }
153
155 void reset()
156 {
157 size_ = 0;
158 container_.reset();
159 message_ = Message();
160 }
161
163 std::string toString() const
164 {
165 return message_.toString();
166 }
167
168private:
169
170 template<typename MessageT>
171 static
172 constexpr
174 getTemplateId()
175 {
176 return MessageT::TemplateId;
177 }
178
179 template<typename MessageT>
180 static
181 MessageT
183 {
185 }
186
187 template <class AnotherType>
188 void fromAnotherType(const MessagePtr<AnotherType>& rhs)
189 {
190 if(rhs->templateId() != getTemplateId<Message>())
191 throwWrongType(getTemplateId<Message>(), rhs->templateId());
192
193 message_ = this->typify<Message>(rhs);
194 }
195
196private:
197 MessageSize size_;
198 Container container_;
199 Message message_;
200
201 template<class T> friend class MessagePtr;
202};
203
204
205template<>
206inline MessagePtr<SbeMessage>::MessagePtr(Container&& container, MessageSize size)
207 : size_(size)
208 , container_(std::move(container))
209 , message_(container_.get(), size_)
210{
211}
212
214template <typename AnotherType, typename BaseType>
219
220}}}}
221
222#endif
#define ONIXS_CONFLATEDTCP_NORETURN
Definition Compiler.h:184
#define ONIXS_CONFLATEDTCP_EXPORTED
Definition Compiler.h:175
const Message & message() const noexcept
Definition MessagePtr.h:131
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition MessagePtr.h:66
const Message * operator->() const noexcept
Definition MessagePtr.h:119
MessagePtr(const MessagePtr &)=default
MessagePtr()
Creates an empty message container.
Definition MessagePtr.h:75
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition MessagePtr.h:55
void swap(MessagePtr &rhs) noexcept
Definition MessagePtr.h:105
MessagePtr(Container &&, MessageSize)
Creates from the given memory block.
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:93
MessagePtr & operator=(const MessagePtr &)=default
MessagePtr(MessagePtr &&rhs) noexcept
Definition MessagePtr.h:86
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.
void throwWrongType(UInt16, UInt16)
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:215