OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and API documentation
Loading...
Searching...
No Matches
MbpBook.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 <cassert>
24#include <memory>
25#include <string>
26#include <stdexcept>
27#include <type_traits>
28
32
34
36template <typename Depth>
37inline void throwMbpBookRedepthFailure(Depth newDepth, Depth maxDepth)
38{
39 std::string error;
40
41 error += "Cannot re-depth order book "
42 "due to lack of capacity [newDepth=";
43
44 Messaging::toStr(error, newDepth);
45
46 error += ",maxDepth=";
47
48 Messaging::toStr(error, maxDepth);
49
50 error += "]. ";
51
52 throw std::runtime_error(error);
53}
54
59template <class EntryType, class DepthType>
61{
62 static_assert(std::is_trivially_destructible<EntryType>::value, "EntryType must be trivially destructible");
63
64public:
66 typedef EntryType Entry;
67
69 typedef DepthType Depth;
70
73
76
84 : depth_(0)
85 , state_(BookState::Constructed)
86 , bids_(bids, capacity)
87 , offers_(offers, capacity)
88 {
89 }
90
91 // Cleans everything up.
93
95 Depth depth() const
96 {
97 return depth_;
98 }
99
103 {
104 return bids_.capacity();
105 }
106
109 {
110 if (depth > capacity())
111 {
113 }
114
115 if (bids_.size() > depth)
116 bids_.resize(depth);
117
118 if (offers_.size() > depth)
119 offers_.resize(depth);
120
121 depth_ = depth;
122 }
123
126 {
127 return bids_;
128 }
129
132 {
133 return bids_;
134 }
135
137 const Entries& bids() const
138 {
139 return bids_;
140 }
141
143 const Entries& operator[](const Bids&) const
144 {
145 return bids_;
146 }
147
150 {
151 return offers_;
152 }
153
156 {
157 return offers_;
158 }
159
161 const Entries& offers() const
162 {
163 return offers_;
164 }
165
167 const Entries& operator[](const Offers&) const
168 {
169 return offers_;
170 }
171
174 {
175 return state_;
176 }
177
180 {
181 state_ = state;
182 }
183
187 void reset(BookState::Enum bookState)
188 {
189 state(bookState);
190
191 bids_.clear();
192 offers_.clear();
193 }
194
195private:
196 // Book depth.
197 Depth depth_;
198
199 // Book state.
200 BookState::Enum state_;
201
202 // Bids.
203 Entries bids_;
204
205 // Asks.
206 Entries offers_;
207
208 // Copying is done the other way.
209 MbpBook(const MbpBook&);
210
211 // Copying is done the other way.
212 MbpBook& operator=(const MbpBook&);
213};
214
223template <class TargetPriceLevel, class SourcePriceLevel, class Depth>
227 Depth maxDepth = static_cast<Depth>(-1)
228)
229{
230 if (maxDepth > source.depth())
231 {
232 maxDepth = source.depth();
233 }
234
235 target.depth(maxDepth);
236
237 copy(target.bids(), source.bids(), maxDepth);
238
239 copy(target.offers(), source.offers(), maxDepth);
240
241 target.state(source.state());
242
243 return target;
244}
245
247template <class PriceLevelType, class DepthType>
249{
250public:
253
255 typedef typename Book::Entry Entry;
256
258 typedef typename Book::Depth Depth;
259
262 static size_t calcSize(Depth maxDepth)
263 {
264 return (sizeof(Book) + 2 * maxDepth * sizeof(Entry));
265 }
266
270 static Book* construct(Depth maxDepth)
271 {
272 const size_t bookSize = calcSize(maxDepth);
273
274 return inplaceConstruct(maxDepth, new Messaging::Byte[bookSize]);
275 }
276
278 static void destruct(Book* book)
279 {
280 if (book)
281 {
282 book->~Book();
283
284 delete[] reinterpret_cast<Messaging::Byte*>(book);
285 }
286 }
287
289 static Book* inplaceConstruct(Depth maxDepth, void* block)
290 {
291 Entry* const entries = reinterpret_cast<Entry*>(reinterpret_cast<Messaging::Byte*>(block) + sizeof(Book));
292
293 inplaceConstruct(entries, entries + 2 * maxDepth);
294
295 return new (block) Book(maxDepth, entries, entries + maxDepth);
296 }
297
298private:
299 static void inplaceConstruct(Entry* first, Entry* last)
300 {
301 while (first != last)
302 new (first++) Entry();
303 }
304};
305
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:69
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:73
Serves as a factory for book of given type.
Definition MbpBook.h:249
static size_t calcSize(Depth maxDepth)
Definition MbpBook.h:262
static Book * inplaceConstruct(Depth maxDepth, void *block)
Definition MbpBook.h:289
static Book * construct(Depth maxDepth)
Definition MbpBook.h:270
MbpBook< ConsolidatedPriceLevel, MbpBookDepth > Book
Definition MbpBook.h:252
static void destruct(Book *book)
Destructs previously constructed book.
Definition MbpBook.h:278
const Entries & operator[](const Bids &) const
List of descending bid prices.
Definition MbpBook.h:143
const Entries & offers() const
List of ascending offer prices.
Definition MbpBook.h:161
Entries & operator[](const Offers &)
List of ascending offer prices.
Definition MbpBook.h:155
void reset(BookState::Enum bookState)
Definition MbpBook.h:187
void state(BookState::Enum state)
Updates state of book.
Definition MbpBook.h:179
Depth depth() const
Returns the maximal number of price levels.
Definition MbpBook.h:95
const Entries & bids() const
List of descending bid prices.
Definition MbpBook.h:137
MbpBook(Depth capacity, Entry *bids, Entry *offers)
Definition MbpBook.h:83
const Entries & operator[](const Offers &) const
List of ascending offer prices.
Definition MbpBook.h:167
Entries & operator[](const Bids &)
List of descending bid prices.
Definition MbpBook.h:131
void depth(Depth depth)
Updates the maximal number of price levels.
Definition MbpBook.h:108
BookState::Enum state() const
Indicates current state of book.
Definition MbpBook.h:173
Provides vector-like access to caller-owned, fixed-capacity storage.
void toStr(std::string &, Int8)
Serializes given integer into a string.
UInt8 Byte
Alias for Byte.
Definition Memory.h:36
MboBook & copy(MboBook &target, const MboBook &source)
Copies content of MBO book to the other one.
Definition MboBook.h:210
void throwMbpBookRedepthFailure(Depth newDepth, Depth maxDepth)
Raises exception on book re-depth failure.
Definition MbpBook.h:37
Tag type that selects the bid side of a book.
Describes the lifecycle state of an order book.
Enum
Known order book states.
Tag type that selects the offer side of a book.