OnixS C++ CME iLink 3 Binary Order Entry Handler  1.18.11
API Documentation
MessageBatch.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 
23 #include <vector>
24 
28 
29 namespace OnixS
30 {
31  namespace CME
32  {
33  namespace iLink3
34  {
36  }
37  }
38 }
39 
41 
42 typedef std::vector<NetworkMessage> NetMessages;
43 typedef std::pair<NetworkMessage*, NetworkMessage*> NetMessagesRange;
45 
46 #if defined (ONIXS_ILINK3_CXX11)
47 
48 /**
49 * The message batch wrapper.
50 */
51 template <typename MessageTypeT,
52 size_t MaxMessageSize = GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
53 typename MessageInitializer = FieldsInitPolicy>
55 {
56 public:
57 
59  using MsgHolders = std::vector<MsgHolder>;
60 
61  using iterator = typename MsgHolders::iterator;
62  using const_iterator = typename MsgHolders::const_iterator;
63 
64  /**
65  * Creates an empty message batch.
66  * The `add` method should be used to add a message to the batch.
67  */
68  MessageBatch() = default;
69 
70  /**
71  * Creates a message batch with the given number of default constructed messages.
72  * A message in the batch can be updated via `operator[]` method.
73  */
74  explicit MessageBatch(size_t size) : batch_(size)
75  {
76  updateHeaders();
77  }
78 
79  /**
80  * Creates a message batch from the initializer list.
81  * A message in the batch can be updated via `operator[]` method.
82  */
83  MessageBatch(std::initializer_list<MsgHolder> list) : batch_(list)
84  {
85  updateHeaders();
86  }
87 
88  MessageBatch(const MessageBatch &) = delete;
89  MessageBatch & operator=(const MessageBatch &) = delete;
90 
91  /**
92  * Appends a message to the end of the batch.
93  */
94  void add(const MsgHolder & msg)
95  {
96  batch_.push_back(msg);
97  }
98 
99  /**
100  * Updates headers of SBE messages to be ready for sending.
101  *
102  * \note Should be called before sending after the completion of the batch filling.
103  *
104  * \note There is no need to call this function before sending in each case of a value updating of an existing field.
105  */
107  {
108  netMsgs_.resize(batch_.size());
109 
110  for (size_t counter = 0; counter < batch_.size(); ++counter)
111  netMsgs_[counter] = batch_[counter].toNetworkMessage();
112  }
113 
114  MsgHolder & operator[](size_t index) { return batch_[index]; }
115  const MsgHolder & operator[](size_t index) const { return batch_[index]; }
116 
117  size_t size() const { return batch_.size(); }
118 
119  const_iterator begin() const { return batch_.begin(); }
120  iterator begin() { return batch_.begin(); }
121 
122  const_iterator end() const { return batch_.end(); }
123  iterator end() { return batch_.end(); }
124 
125 private:
126 
129 
130  MsgHolders batch_;
131  NetMessages netMsgs_;
132 };
133 
134 /**
135 * The class can be used to combine messages with different types to the batch for sending.
136 *
137 * \warning The class does not copy combined messages and does not store them internally.
138 * Therefore, the lifetime of combined messages should be greater or equal to the lifetime of this class instance.
139 */
141 {
142 public:
143 
144  /**
145  * Creates an empty message batch combiner.
146  * The `add` method should be used to fill the batch.
147  */
148  MessageBatchCombiner() = default;
149 
150  /**
151  * Adds the message batch to the array of combined messages.
152  * Updates headers of SBE messages to be ready for sending.
153  */
154  template <typename MessageTypeT,
155  size_t MaxMessageSize = GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
156  typename MessageInitializer = FieldsInitPolicy>
158  {
159  netMsgs_.reserve(batch.size());
160  for (auto & holder : batch)
161  netMsgs_.push_back(holder.toNetworkMessage());
162  }
163 
164  template <typename MessageTypeT,
165  size_t MaxMessageSize = GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
166  typename MessageInitializer = FieldsInitPolicy>
168 
169  /**
170  * Adds the message to the array of combined messages.
171  * Updates headers of the SBE message to be ready for sending.
172  */
173  template <typename MessageTypeT,
174  size_t MaxMessageSize = GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
175  typename MessageInitializer = FieldsInitPolicy>
177  {
178  netMsgs_.push_back(holder.toNetworkMessage());
179  }
180 
181  template <typename MessageTypeT,
182  size_t MaxMessageSize = GetMaxMessageSize<typename HeldAdapter<MessageTypeT>::SbeType, DefaultMaxGroupItems>::Size,
183  typename MessageInitializer = FieldsInitPolicy>
185 
186  /**
187  * Removes all combined messages.
188  */
189  void clear()
190  {
191  netMsgs_.clear();
192  }
193 
194  /**
195  * Provides a range of messages
196  */
197  NetMessagesRange getRange(size_t index) noexcept
198  {
199  assert(!netMsgs_.empty());
200  return {&*netMsgs_.begin(), &*(netMsgs_.begin() + index)};
201  }
202 
203  MessageBatchCombiner(const MessageBatchCombiner &) = delete;
205 
206 private:
207 
210 
211  NetMessages netMsgs_;
212 };
213 
215 {
216 public:
217  /// \return The size of the message batch in bytes
218  template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
219  static size_t calculateBatchSize(
221  {
222  return calculateBatchSize(msgs.netMsgs_);
223  }
224 
225  /// \return true if the batch fits the size provided
226  template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
227  static size_t fitSize(
229  {
230  return calculateBatchSize(msgs) <= size;
231  }
232 
233  /// \return The size of the message batch in bytes
235  {
236  return calculateBatchSize(msgs.netMsgs_);
237  }
238 
239  /// \return true if the batch fits the size provided
240  static bool fitSize(MessageBatchCombiner & msgs, size_t size = defaultMaxPacketSize()) ONIXS_ILINK3_NOTHROW
241  {
242  return calculateBatchSize(msgs) <= size;
243  }
244 
245 private:
247  static size_t calculateBatchSize(const NetMessages& msgs) ONIXS_ILINK3_NOTHROW;
248 
250  static size_t defaultMaxPacketSize() ONIXS_ILINK3_NOTHROW;
251 };
252 
253 #endif
254 
void clear()
Removes all combined messages.
Definition: MessageBatch.h:189
An iLink 3 Session.
Definition: Session.h:47
std::vector< NetworkMessage > NetMessages
Definition: MessageBatch.h:42
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
std::pair< NetworkMessage *, NetworkMessage * > NetMessagesRange
Definition: MessageBatch.h:43
static bool fitSize(MessageBatchCombiner &msgs, size_t size=defaultMaxPacketSize()) noexcept
Definition: MessageBatch.h:240
static size_t fitSize(MessageBatch< SbeMessageType, MaxMessageSize, MessageInitializer > &msgs, size_t size=defaultMaxPacketSize()) noexcept
Definition: MessageBatch.h:227
static size_t calculateBatchSize(MessageBatchCombiner &msgs) noexcept
Definition: MessageBatch.h:234
MessageBatch(std::initializer_list< MsgHolder > list)
Creates a message batch from the initializer list.
Definition: MessageBatch.h:83
#define ONIXS_ILINK3_EXPORTED_CLASS
Definition: ABI.h:44
NetMessagesRange getRange(size_t index) noexcept
Provides a range of messages.
Definition: MessageBatch.h:197
MessageBatch & operator=(const MessageBatch &)=delete
Definition: Defines.h:40
static size_t calculateBatchSize(MessageBatch< SbeMessageType, MaxMessageSize, MessageInitializer > &msgs) noexcept
Definition: MessageBatch.h:219
void add(MessageBatch< MessageTypeT, MaxMessageSize, MessageInitializer > &batch)
Adds the message batch to the array of combined messages.
Definition: MessageBatch.h:157
const MsgHolder & operator[](size_t index) const
Definition: MessageBatch.h:115
void add(const MsgHolder &msg)
Appends a message to the end of the batch.
Definition: MessageBatch.h:94
typename MsgHolders::iterator iterator
Definition: MessageBatch.h:61
constexpr UInt8 DefaultMaxGroupItems
Default maximum number of repeating group items.
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
#define ONIXS_ILINK3_EXPORTED
Definition: Compiler.h:176
MessageBatch()=default
Creates an empty message batch.
typename MsgHolders::const_iterator const_iterator
Definition: MessageBatch.h:62
The class can be used to combine messages with different types to the batch for sending.
Definition: MessageBatch.h:140
The policy to create messages with null-initialized optional fields.
Definition: MessageHolder.h:48
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
MessageBatch(size_t size)
Creates a message batch with the given number of default constructed messages.
Definition: MessageBatch.h:74
void add(MessageHolder< MessageTypeT, MaxMessageSize, MessageInitializer > &holder)
Adds the message to the array of combined messages.
Definition: MessageBatch.h:176
void updateHeaders()
Updates headers of SBE messages to be ready for sending.
Definition: MessageBatch.h:106
#define ONIXS_ILINK3_NOTHROW
Definition: Compiler.h:177
NetworkMessage toNetworkMessage() noexcept