OnixS C++ B3 BOE Binary Order Entry  1.2.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_B3_BOE_HAS_GATEWAY_EMULATOR)
26 
27 #include <OnixS/B3/BOE/Messaging.h>
28 
29 #include <memory>
30 
31 namespace OnixS {
32 namespace B3 {
33 namespace BOE {
34 namespace Testing {
35 
36 using namespace Messaging;
37 
40 void throwWrongType(UInt16, UInt16);
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 /// Message container.
50 template<typename Message>
52 {
53 public:
54  using Container = std::shared_ptr<UInt8>;
55 
56  /// Creates from the given memory block.
58 
59  /// Create from another type.
60  template <class AnotherType>
61  explicit
63  : size_(rhs.size_)
64  , container_(rhs.container_)
65  , message_()
66  {
67  fromAnotherType(rhs);
68  }
69 
70  /// Creates from the given type.
71  template <class AnotherType>
72  explicit
74  : size_(rhs.size_)
75  , container_(std::move(rhs.container_))
76  , message_()
77  {
78  fromAnotherType(rhs);
79  }
80 
81  /// Creates an empty message container.
83  : size_(0)
84  , container_()
85  , message_()
86  {
87  }
88 
89  MessagePtr(const MessagePtr&) = default;
90  MessagePtr& operator=(const MessagePtr&) = default;
91 
92  ///
93  MessagePtr(MessagePtr&& rhs) noexcept
94  {
95  rhs.swap(*this);
96  rhs.reset();
97  }
98 
99  ///
100  MessagePtr& operator=(MessagePtr&& rhs) noexcept
101  {
102  if (&rhs == this)
103  return *this;
104 
105  rhs.swap(*this);
106  rhs.reset();
107 
108  return *this;
109  }
110 
111  ///
112  bool valid() const noexcept
113  {
114  return isMessageValid(message_);
115  }
116 
117  ///
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 
125  ///
126  Message* operator->() noexcept
127  {
128  assert(valid());
129  return &message_;
130  }
131 
132  ///
133  const Message* operator->() const noexcept
134  {
135  assert(valid());
136  return &message_;
137  }
138 
139  ///
140  Message& operator*() noexcept
141  {
142  return message_;
143  }
144 
145  ///
146  const Message& operator*() const noexcept
147  {
148  return message_;
149  }
150 
151  /// \return the underlying message.
152  Message& message() noexcept
153  {
154  assert(valid());
155  return message_;
156  }
157 
158  /// \return the underlying message.
159  const Message& message() const noexcept
160  {
161  assert(valid());
162  return message_;
163  }
164 
165  ///
166  operator Message() noexcept
167  {
168  return message();
169  }
170 
171  ///
172  operator const Message() const noexcept
173  {
174  return message();
175  }
176 
177  /// \return the underlying buffer.
178  UInt8* buf() noexcept
179  {
180  return container_.get();
181  }
182 
183  /// Resets the message.
184  void reset()
185  {
186  size_ = 0;
187  container_.reset();
188  message_ = Message();
189  }
190 
191  /// \return the human-readable presentation.
192  std::string toString() const
193  {
194  return message_.toString();
195  }
196 
197 private:
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 
208 private:
209  MessageSize size_;
210  Container container_;
211  Message message_;
212 
213  template<class T> friend class MessagePtr;
214 };
215 
216 
217 template<>
219  : size_(size)
220  , container_(std::move(container))
221  , message_(container_.get(), size_)
222 {
223 }
224 
225 /// Tries to cast to another type.
226 template <typename AnotherType, typename BaseType>
228 {
229  return MessagePtr<AnotherType>(ptr);
230 }
231 
232 }}}}
233 
234 #endif
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition: MessagePtr.h:62
MessagePtr()
Creates an empty message container.
Definition: MessagePtr.h:82
void swap(MessagePtr &rhs) noexcept
Definition: MessagePtr.h:118
Message & operator*() noexcept
Definition: MessagePtr.h:140
std::shared_ptr< UInt8 > Container
Definition: MessagePtr.h:54
#define ONIXS_B3_BOE_NORETURN
Definition: Compiler.h:190
void reset()
Resets the message.
Definition: MessagePtr.h:184
#define ONIXS_B3_BOE_EXPORTED
Definition: Compiler.h:181
STL namespace.
MessagePtr(MessagePtr &&rhs) noexcept
Definition: MessagePtr.h:93
void throwWrongType(UInt16, UInt16)
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
Definition: Defines.h:40
const Message & message() const noexcept
Definition: MessagePtr.h:159
bool isMessageValid(const SbeMessage &msg) noexcept
Definition: MessagePtr.h:44
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition: MessagePtr.h:227
Message * operator->() noexcept
Definition: MessagePtr.h:126
bool valid() const noexcept
Definition: MessagePtr.h:112
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition: MessagePtr.h:100
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition: MessagePtr.h:73
Message & message() noexcept
Definition: MessagePtr.h:152
std::string toString() const
Definition: MessagePtr.h:192
const Message * operator->() const noexcept
Definition: MessagePtr.h:133
const Message & operator*() const noexcept
Definition: MessagePtr.h:146