OnixS C++ CME iLink 3 Binary Order Entry Handler  1.18.0
API Documentation
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 
31 namespace OnixS {
32 namespace CME {
33 namespace iLink3 {
34 namespace Testing {
35 
36 using namespace Messaging;
37 
41 
42 /// \return `true` if the instance refers to a valid message, otherwise - `false`.
43 inline
44 bool isMessageValid(const SbeMessage& msg) noexcept
45 {
46  return msg.valid();
47 }
48 
49 /// \return `true` if the instance refers to a valid message, otherwise - `false`.
50 inline
51 bool isMessageValid(const TagBased::Message& msg) noexcept
52 {
53  return static_cast<bool>(msg);
54 }
55 
56 /// Message container.
57 template<typename Message>
59 {
60 public:
61  using Container = std::shared_ptr<UInt8>;
62 
63  /// Creates from the given memory block.
65 
66  /// Create from another type.
67  template <class AnotherType>
68  explicit
70  : size_(rhs.size_)
71  , container_(rhs.container_)
72  , message_()
73  {
74  fromAnotherType(rhs);
75  }
76 
77  /// Creates from the given type.
78  template <class AnotherType>
79  explicit
81  : size_(rhs.size_)
82  , container_(std::move(rhs.container_))
83  , message_()
84  {
85  fromAnotherType(rhs);
86  }
87 
88  /// Creates an empty message container.
90  : size_(0)
91  , container_()
92  , message_()
93  {
94  }
95 
96  MessagePtr(const MessagePtr&) = default;
97  MessagePtr& operator=(const MessagePtr&) = default;
98 
99  ///
100  MessagePtr(MessagePtr&& rhs) noexcept
101  {
102  rhs.swap(*this);
103  rhs.reset();
104  }
105 
106  ///
107  MessagePtr& operator=(MessagePtr&& rhs) noexcept
108  {
109  if (&rhs == this)
110  return *this;
111 
112  rhs.swap(*this);
113  rhs.reset();
114 
115  return *this;
116  }
117 
118  ///
119  bool valid() const noexcept
120  {
121  return isMessageValid(message_);
122  }
123 
124  ///
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 
132  ///
133  Message* operator->() noexcept
134  {
135  assert(valid());
136  return &message_;
137  }
138 
139  ///
140  const Message* operator->() const noexcept
141  {
142  assert(valid());
143  return &message_;
144  }
145 
146  ///
147  Message& operator*() noexcept
148  {
149  return message_;
150  }
151 
152  ///
153  const Message& operator*() const noexcept
154  {
155  return message_;
156  }
157 
158  /// \return the underlying message.
159  Message& message() noexcept
160  {
161  assert(valid());
162  return message_;
163  }
164 
165  /// \return the underlying message.
166  const Message& message() const noexcept
167  {
168  assert(valid());
169  return message_;
170  }
171 
172  ///
173  operator Message() noexcept
174  {
175  return message();
176  }
177 
178  ///
179  operator const Message() const noexcept
180  {
181  return message();
182  }
183 
184  /// \return the underlying buffer.
185  UInt8* buf() noexcept
186  {
187  return container_.get();
188  }
189 
190  /// Resets the message.
191  void reset()
192  {
193  size_ = 0;
194  container_.reset();
195  message_ = Message();
196  }
197 
198  /// \return the human-readable presentation.
199  std::string toString() const
200  {
201  return message_.toString();
202  }
203 
204 private:
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 
235  return Messaging::typify<MessageT>(message);
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 
245  return MessageT(Messaging::typify<typename Message::Binary>(message));
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 
257 private:
258  MessageSize size_;
259  Container container_;
260  Message message_;
261 
262  template<class T> friend class MessagePtr;
263 };
264 
265 
266 template<>
268  : size_(size)
269  , container_(std::move(container))
270  , message_(container_.get(), size_)
271 {
272 }
273 
274 /// Tries to cast to another type.
275 template <typename AnotherType, typename BaseType>
277 {
278  return MessagePtr<AnotherType>(ptr);
279 }
280 
281 }}}}
282 
283 #endif
MessageHeader::TemplateId MessageTemplateId
Message type (template) identification.
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition: MessagePtr.h:80
void throwWrongType(UInt16, UInt16)
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition: MessagePtr.h:69
Message typify(const SbeMessage &message)
Casts SBE-encoded message to a given type.
Definition: Typification.h:36
#define ONIXS_ILINK3_NORETURN
Definition: Compiler.h:184
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition: MessagePtr.h:107
STL namespace.
const Message & message() const noexcept
Definition: MessagePtr.h:166
const Message & operator*() const noexcept
Definition: MessagePtr.h:153
UInt16 UInt16
uInt16.
Definition: Fields.h:296
const Message * operator->() const noexcept
Definition: MessagePtr.h:140
MessagePtr()
Creates an empty message container.
Definition: MessagePtr.h:89
Definition: Defines.h:40
std::shared_ptr< UInt8 > Container
Definition: MessagePtr.h:61
void reset()
Resets the message.
Definition: MessagePtr.h:191
MessagePtr(MessagePtr &&rhs) noexcept
Definition: MessagePtr.h:100
#define ONIXS_ILINK3_EXPORTED
Definition: Compiler.h:175
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
bool isMessageValid(const SbeMessage &msg) noexcept
Definition: MessagePtr.h:44
Implements a tag-based interface over an SBE-encoded message.
Definition: Message.h:662
void swap(MessagePtr &rhs) noexcept
Definition: MessagePtr.h:125
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition: MessagePtr.h:276