OnixS C++ CME MDP Conflated TCP Handler  1.3.6
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_CONFLATEDTCP_HAS_GATEWAY_EMULATOR)
26 
28 
29 #include <memory>
30 
31 namespace OnixS {
32 namespace CME {
33 namespace ConflatedTCP {
34 namespace Testing {
35 
36 using namespace Messaging;
37 
41 
42 /// Message container.
43 template<typename Message>
45 {
46 public:
47  using Container = std::shared_ptr<UInt8>;
48 
49  /// Creates from the given memory block.
51 
52  /// Create from another type.
53  template <class AnotherType>
54  explicit
56  : size_(rhs.size_)
57  , container_(rhs.container_)
58  , message_()
59  {
60  fromAnotherType(rhs);
61  }
62 
63  /// Creates from the given type.
64  template <class AnotherType>
65  explicit
67  : size_(rhs.size_)
68  , container_(std::move(rhs.container_))
69  , message_()
70  {
71  fromAnotherType(rhs);
72  }
73 
74  /// Creates an empty message container.
76  : size_(0)
77  , container_()
78  , message_()
79  {
80  }
81 
82  MessagePtr(const MessagePtr&) = default;
83  MessagePtr& operator=(const MessagePtr&) = default;
84 
85  ///
86  MessagePtr(MessagePtr&& rhs) noexcept
87  {
88  rhs.swap(*this);
89  rhs.reset();
90  }
91 
92  ///
93  MessagePtr& operator=(MessagePtr&& rhs) noexcept
94  {
95  if (&rhs == this)
96  return *this;
97 
98  rhs.swap(*this);
99  rhs.reset();
100 
101  return *this;
102  }
103 
104  ///
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 
112  ///
113  Message* operator->() noexcept
114  {
115  return &message_;
116  }
117 
118  ///
119  const Message* operator->() const noexcept
120  {
121  return &message_;
122  }
123 
124  /// \return the underlying message.
125  Message& message() noexcept
126  {
127  return message_;
128  }
129 
130  /// \return the underlying message.
131  const Message& message() const noexcept
132  {
133  return message_;
134  }
135 
136  ///
137  operator Message()
138  {
139  return message();
140  }
141 
142  ///
143  operator const Message() const
144  {
145  return message();
146  }
147 
148  /// \return the underlying buffer.
149  UInt8* buf() noexcept
150  {
151  return container_.get();
152  }
153 
154  /// Resets the message.
155  void reset()
156  {
157  size_ = 0;
158  container_.reset();
159  message_ = Message();
160  }
161 
162  /// \return the human-readable presentation.
163  std::string toString() const
164  {
165  return message_.toString();
166  }
167 
168 private:
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
182  typify(SbeMessage message)
183  {
184  return Messaging::typify<MessageT>(message);
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 
196 private:
197  MessageSize size_;
198  Container container_;
199  Message message_;
200 
201  template<class T> friend class MessagePtr;
202 };
203 
204 
205 template<>
207  : size_(size)
208  , container_(std::move(container))
209  , message_(container_.get(), size_)
210 {
211 }
212 
213 /// Tries to cast to another type.
214 template <typename AnotherType, typename BaseType>
216 {
217  return MessagePtr<AnotherType>(ptr);
218 }
219 
220 }}}}
221 
222 #endif
Message typify(const SbeMessage &message)
Casts SBE-encoded message to a given type.
Definition: Typification.h:36
MessagePtr(MessagePtr &&rhs) noexcept
Definition: MessagePtr.h:86
MessagePtr & operator=(MessagePtr &&rhs) noexcept
Definition: MessagePtr.h:93
void swap(MessagePtr &rhs) noexcept
Definition: MessagePtr.h:105
MessageHeader::TemplateId MessageTemplateId
Message type (template) identification.
#define ONIXS_CONFLATEDTCP_EXPORTED
Definition: Compiler.h:175
STL namespace.
const Message & message() const noexcept
Definition: MessagePtr.h:131
void throwWrongType(UInt16, UInt16)
MessagePtr(const MessagePtr< AnotherType > &rhs)
Create from another type.
Definition: MessagePtr.h:55
Definition: Defines.h:40
#define ONIXS_CONFLATEDTCP_NORETURN
Definition: Compiler.h:184
MessagePtr(MessagePtr< AnotherType > &&rhs)
Creates from the given type.
Definition: MessagePtr.h:66
UInt16 MessageSize
Message length type.
Definition: Aliases.h:29
const Message * operator->() const noexcept
Definition: MessagePtr.h:119
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition: MessagePtr.h:215
MessagePtr()
Creates an empty message container.
Definition: MessagePtr.h:75