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