OnixS C++ CME MDP Premium Market Data Handler 5.9.0
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
31
33
35template <typename Depth>
36inline void throwMbpBookRedepthFailure(Depth newDepth, Depth maxDepth)
37{
38 std::string error;
39
40 error += "Cannot re-depth order book "
41 "due to lack of capacity [newDepth=";
42
43 toStr(error, newDepth);
44
45 error += ",maxDepth=";
46
47 toStr(error, maxDepth);
48
49 error += "]. ";
50
51 throw std::runtime_error(error);
52}
53
58template <class EntryType, class DepthType>
60{
61public:
64
66 typedef DepthType Depth;
67
70
73
81 : depth_(0)
82 , state_(BookState::Constructed)
83 , bids_(bids, capacity)
84 , offers_(offers, capacity)
85 {
86 }
87
88 // Cleans everything up.
90
92 Depth depth() const
93 {
94 return depth_;
95 }
96
100 {
101 return bids_.capacity();
102 }
103
106 {
107 if (depth > capacity())
108 {
110 }
111
112 if (bids_.size() > depth)
113 bids_.resize(depth);
114
115 if (offers_.size() > depth)
116 offers_.resize(depth);
117
118 depth_ = depth;
119 }
120
123 {
124 return bids_;
125 }
126
129 {
130 return bids_;
131 }
132
134 const Entries& bids() const
135 {
136 return bids_;
137 }
138
140 const Entries& operator[](const Bids&) const
141 {
142 return bids_;
143 }
144
147 {
148 return offers_;
149 }
150
153 {
154 return offers_;
155 }
156
158 const Entries& offers() const
159 {
160 return offers_;
161 }
162
164 const Entries& operator[](const Offers&) const
165 {
166 return offers_;
167 }
168
171 {
172 return state_;
173 }
174
177 {
178 state_ = state;
179 }
180
184 void reset(BookState::Enum bookState)
185 {
186 state(bookState);
187
188 bids_.resize(0);
189 offers_.resize(0);
190 }
191
192private:
193 // Book depth.
194 Depth depth_;
195
196 // Book state.
197 BookState::Enum state_;
198
199 // Bids.
200 Entries bids_;
201
202 // Asks.
203 Entries offers_;
204
205 // Copying is done the other way.
206 MbpBook(const MbpBook&);
207
208 // Copying is done the other way.
209 MbpBook& operator=(const MbpBook&);
210};
211
220template <class TargetPriceLevel, class SourcePriceLevel, class Depth>
224 Depth maxDepth = static_cast<Depth>(-1)
225)
226{
227 if (maxDepth > source.depth())
228 {
229 maxDepth = source.depth();
230 }
231
232 target.depth(maxDepth);
233
234 copy(target.bids(), source.bids(), maxDepth);
235
236 copy(target.offers(), source.offers(), maxDepth);
237
238 target.state(source.state());
239
240 return target;
241}
242
244template <class PriceLevelType, class DepthType>
246{
247public:
250
252 typedef typename Book::Entry Entry;
253
255 typedef typename Book::Depth Depth;
256
259 static size_t calcSize(Depth maxDepth)
260 {
261 return (sizeof(Book) + 2 * maxDepth * sizeof(Entry));
262 }
263
267 static Book* construct(Depth maxDepth)
268 {
269 const size_t bookSize = calcSize(maxDepth);
270
271 return inplaceConstruct(maxDepth, new Byte[bookSize]);
272 }
273
275 static void destruct(Book* book)
276 {
277 if (book)
278 {
279 book->~Book();
280
281 delete[] reinterpret_cast<Byte*>(book);
282 }
283 }
284
286 static Book* inplaceConstruct(Depth maxDepth, void* block)
287 {
288 Entry* const entries = reinterpret_cast<Entry*>(reinterpret_cast<Byte*>(block) + sizeof(Book));
289
290 inplaceConstruct(entries, entries + 2 * maxDepth);
291
292 return new (block) Book(maxDepth, entries, entries + maxDepth);
293 }
294
295private:
296 static void inplaceConstruct(Entry* first, Entry* last)
297 {
298 while (first != last)
299 new (first++) Entry();
300 }
301};
302
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:67
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:68
Serves as a factory for book of given type.
Definition MbpBook.h:246
static size_t calcSize(Depth maxDepth)
Calculates size of memory required to keep in memory book of given depth.
Definition MbpBook.h:259
static Book * inplaceConstruct(Depth maxDepth, void *block)
Definition MbpBook.h:286
static Book * construct(Depth maxDepth)
Constructs book instance of given capacity.
Definition MbpBook.h:267
MbpBook< ConsolidatedPriceLevel, MbpBookDepth > Book
Definition MbpBook.h:249
static void destruct(Book *book)
Destructs previously constructed book.
Definition MbpBook.h:275
Base attributes of order book.
Definition MbpBook.h:60
const Entries & operator[](const Bids &) const
List of descending bid prices.
Definition MbpBook.h:140
const Entries & offers() const
List of ascending offer prices.
Definition MbpBook.h:158
Entries & operator[](const Offers &)
List of ascending offer prices.
Definition MbpBook.h:152
void reset(BookState::Enum bookState)
Wipes out all bids and offers and other data associated with book (like instance of last applied FIX ...
Definition MbpBook.h:184
void state(BookState::Enum state)
Updates state of book.
Definition MbpBook.h:176
Depth depth() const
Returns the maximal number of price levels.
Definition MbpBook.h:92
const Entries & bids() const
List of descending bid prices.
Definition MbpBook.h:134
MbpBook(Depth capacity, Entry *bids, Entry *offers)
Initializes instance with given attributes.
Definition MbpBook.h:80
const Entries & operator[](const Offers &) const
List of ascending offer prices.
Definition MbpBook.h:164
Entries & operator[](const Bids &)
List of descending bid prices.
Definition MbpBook.h:128
void depth(Depth depth)
Updates the maximal number of price levels.
Definition MbpBook.h:105
BookState::Enum state() const
Indicates current state of book.
Definition MbpBook.h:170
Implements vector-like container over built-in array of fixed size being referenced.
MboBook & copy(MboBook &target, const MboBook &source)
Copies content of MBO book to the other one.
Definition MboBook.h:210
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
void throwMbpBookRedepthFailure(Depth newDepth, Depth maxDepth)
Raises exception on book re-depth failure.
Definition MbpBook.h:36
UInt8 Byte
Alias for Byte.
Definition Memory.h:30
Tag class for accessing bids in templates.
State of order book.
MDEntryType type.
Definition Fields.h:308
Tag class for accessing offers in templates.