OnixS C++ CME MDP Conflated UDP Handler 1.1.2
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
36<
37 typename Depth
38>
39inline
40void
42 Depth newDepth, Depth maxDepth)
43{
44 std::string error;
45
46 error +=
47 "Cannot re-depth order book "
48 "due to lack of capacity [newDepth=";
49
50 toStr(error, newDepth);
51
52 error += ",maxDepth=";
53
54 toStr(error, maxDepth);
55
56 error += "]. ";
57
58 throw std::runtime_error(error);
59}
60
65template
66<
67 class EntryType,
68 class DepthType
69>
70class
72{
73public:
75 typedef
78
80 typedef
81 DepthType
83
85 typedef
87 <
88 Entry,
89 Depth
90 >
92
94 typedef
95 typename
98
107 Entry* bids,
108 Entry* offers)
109 : depth_(0)
110 , state_(
111 BookState::Constructed)
112 , bids_(
113 bids,
114 capacity)
115 , offers_(
116 offers,
117 capacity)
118 {
119 }
120
121 // Cleans everything up.
123 {
124 }
125
127 Depth depth() const
128 {
129 return depth_;
130 }
131
135 {
136 return bids_.capacity();
137 }
138
140 void
142 Depth depth)
143 {
144 if (depth > capacity())
145 {
147 depth,
148 capacity());
149 }
150
151 if (bids_.size() > depth)
152 bids_.resize(depth);
153
154 if (offers_.size() > depth)
155 offers_.resize(depth);
156
157 depth_ = depth;
158 }
159
162 {
163 return bids_;
164 }
165
167 Entries&
168 operator [](
169 const Bids&)
170 {
171 return bids_;
172 }
173
175 const
176 Entries&
177 bids() const
178 {
179 return bids_;
180 }
181
183 const
184 Entries&
185 operator [](
186 const Bids&) const
187 {
188 return bids_;
189 }
190
193 {
194 return offers_;
195 }
196
198 Entries&
199 operator [](
200 const Offers&)
201 {
202 return offers_;
203 }
204
206 const
207 Entries&
208 offers() const
209 {
210 return offers_;
211 }
212
214 const
215 Entries&
216 operator [](
217 const Offers&) const
218 {
219 return offers_;
220 }
221
224 {
225 return state_;
226 }
227
229 void
232 {
233 state_ = state;
234 }
235
239 void
241 BookState::Enum bookState)
242 {
243 state(bookState);
244
245 bids_.resize(0);
246 offers_.resize(0);
247 }
248
249private:
250 // Book depth.
251 Depth depth_;
252
253 // Book state.
254 BookState::Enum state_;
255
256 // Bids.
257 Entries bids_;
258
259 // Asks.
260 Entries offers_;
261
262 // Copying is done the other way.
263 MbpBook(const MbpBook&);
264
265 // Copying is done the other way.
266 MbpBook& operator=(const MbpBook&);
267};
268
273template
274<
275 class TargetPriceLevel,
276 class SourcePriceLevel,
277 class Depth
278>
279void
281 MbpBook
282 <
283 TargetPriceLevel,
284 Depth
285 >& target,
286 const
287 MbpBook
288 <
289 SourcePriceLevel,
290 Depth
291 >& source,
292 Depth maxDepth =
293 static_cast<Depth>(-1))
294{
295 if (maxDepth >
296 source.depth())
297 {
298 maxDepth =
299 source.depth();
300 }
301
302 target.depth(maxDepth);
303
304 copy
305 (
306 target.bids(),
307 source.bids(),
308 maxDepth
309 );
310
311 copy
312 (
313 target.offers(),
314 source.offers(),
315 maxDepth
316 );
317
318 target.state(
319 source.state());
320}
321
323template
324<
325 class PriceLevelType,
326 class DepthType
327>
328class
330{
331public:
333 typedef
334 MbpBook
335 <
336 PriceLevelType,
337 DepthType
338 >
340
342 typedef
343 typename Book::Entry
345
347 typedef
348 typename Book::Depth
350
353 static
354 size_t
356 Depth maxDepth)
357 {
358 return (
359 sizeof(Book) +
360 2 *
361 maxDepth *
362 sizeof(Entry)
363 );
364 }
365
369 static
370 Book*
372 Depth maxDepth)
373 {
374 const
375 size_t
376 bookSize =
377 calcSize(maxDepth);
378
379 return
381 maxDepth,
382 new Byte [bookSize]);
383 }
384
386 static
387 void
389 Book* book)
390 {
391 if (book)
392 {
393 book->~Book();
394
395 delete [] reinterpret_cast<Byte*>(book);
396 }
397 }
398
400 static
401 Book*
403 Depth maxDepth,
404 void* block)
405 {
406 Entry*
407 const
408 entries =
409 reinterpret_cast<Entry*>
410 (
411 reinterpret_cast<Byte*>(block) +
412 sizeof(Book)
413 );
414
416 entries,
417 entries + 2 * maxDepth);
418
419 return
420 new (block)
421 Book(
422 maxDepth,
423 entries,
424 entries + maxDepth);
425 }
426
427private:
428 static
429 void
430 inplaceConstruct(
431 Entry* first,
432 Entry* last)
433 {
434 while (first != last)
435 new (first++) Entry();
436 }
437};
438
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition Bootstrap.h:157
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition Bootstrap.h:153
Serves as a factory for book of given type.
Definition MbpBook.h:330
static size_t calcSize(Depth maxDepth)
Definition MbpBook.h:355
static Book * inplaceConstruct(Depth maxDepth, void *block)
Definition MbpBook.h:402
static Book * construct(Depth maxDepth)
Definition MbpBook.h:371
static void destruct(Book *book)
Destructs previously constructed book.
Definition MbpBook.h:388
const Entries & offers() const
List of ascending offer prices.
Definition MbpBook.h:208
void reset(BookState::Enum bookState)
Definition MbpBook.h:240
void state(BookState::Enum state)
Updates state of book.
Definition MbpBook.h:230
Depth depth() const
Returns the maximal number of price levels.
Definition MbpBook.h:127
const Entries & bids() const
List of descending bid prices.
Definition MbpBook.h:177
MbpBook(Depth capacity, Entry *bids, Entry *offers)
Definition MbpBook.h:105
void depth(Depth depth)
Updates the maximal number of price levels.
Definition MbpBook.h:141
BookState::Enum state() const
Indicates current state of book.
Definition MbpBook.h:223
void copy(MbpBook< TargetPriceLevel, Depth > &target, const MbpBook< SourcePriceLevel, Depth > &source, Depth maxDepth=static_cast< Depth >(-1))
Definition MbpBook.h:280
ONIXS_CONFLATEDUDP_EXPORTED 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:41
UInt8 Byte
Alias for Byte.
Definition Memory.h:30
Tag class for accessing bids in templates.
Tag class for accessing offers in templates.