OnixS C++ CME Market Data Handler  4.8.0.10
API documentation
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 
28 #include <OnixS/CME/MDH/Memory.h>
31 
33 
34 /// Raises exception on book re-depth failure.
35 template
36 <
37  typename Depth
38 >
39 inline
40 void
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 
61 /// Base attributes of order book.
62 ///
63 /// A set of bids and offers data for the given security
64 /// (bids are descending and offers are ascending).
65 template
66 <
67  class EntryType,
68  class DepthType
69 >
70 class
71 MbpBook
72 {
73 public:
74  /// Indicates price level data type.
75  typedef
76  EntryType
78 
79  /// Aliases book dimension type.
80  typedef
81  DepthType
83 
84  /// Collection of price levels allowing modifications.
85  typedef
87  <
88  Entry,
89  Depth
90  >
92 
93  /// Iterator over bids and offers.
94  typedef
95  typename
98 
99  /// Initializes instance with given attributes.
100  ///
101  /// Bids and offers are stored in given segments of
102  /// plain arrays. Book constructs vectors over input
103  /// arrays of fixed size. That provides more flexibility
104  /// on memory layout for internally used data.
106  Depth capacity,
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 
126  /// Returns the maximal number of price levels.
127  Depth depth() const
128  {
129  return depth_;
130  }
131 
132  /// Indicates book capacity which is
133  /// maximal value of book depth allowed.
134  Depth capacity() const
135  {
136  return bids_.capacity();
137  }
138 
139  /// Updates the maximal number of price levels.
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 
160  /// List of descending bid prices.
162  {
163  return bids_;
164  }
165 
166  /// List of descending bid prices.
167  Entries&
168  operator [](
169  const Bids&)
170  {
171  return bids_;
172  }
173 
174  /// List of descending bid prices.
175  const
176  Entries&
177  bids() const
178  {
179  return bids_;
180  }
181 
182  /// List of descending bid prices.
183  const
184  Entries&
185  operator [](
186  const Bids&) const
187  {
188  return bids_;
189  }
190 
191  /// List of ascending offer prices.
193  {
194  return offers_;
195  }
196 
197  /// List of ascending offer prices.
198  Entries&
199  operator [](
200  const Offers&)
201  {
202  return offers_;
203  }
204 
205  /// List of ascending offer prices.
206  const
207  Entries&
208  offers() const
209  {
210  return offers_;
211  }
212 
213  /// List of ascending offer prices.
214  const
215  Entries&
216  operator [](
217  const Offers&) const
218  {
219  return offers_;
220  }
221 
222  /// Indicates current state of book.
224  {
225  return state_;
226  }
227 
228  /// Updates state of book.
229  void
231  BookState::Enum state)
232  {
233  state_ = state;
234  }
235 
236  /// Wipes out all bids and offers and other
237  /// data associated with book (like instance
238  /// of last applied FIX message).
239  void
241  BookState::Enum bookState)
242  {
243  state(bookState);
244 
245  bids_.resize(0);
246  offers_.resize(0);
247  }
248 
249 private:
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 
269 /// Copies content of one book to another.
270 ///
271 /// Source and destination books are not required
272 /// to be of same type, just compatible by price levels.
273 /// For example, content of direct book can be copied to implied book,
274 /// or content of consolidated book can be copied to implied book,
275 /// however, implied book can't be copied to direct book as far as
276 /// direct price level has more attributes rather than implied one.
277 template
278 <
279  class TargetPriceLevel,
280  class SourcePriceLevel,
281  class Depth
282 >
283 void
286  const MbpBook<SourcePriceLevel, Depth>& source,
287  Depth maxDepth = static_cast<Depth>(-1))
288 {
289  if (maxDepth > source.depth())
290  maxDepth = source.depth();
291 
292  target.depth(maxDepth);
293 
294  copy(
295  target.bids(),
296  source.bids(),
297  maxDepth);
298 
299  copy(
300  target.offers(),
301  source.offers(),
302  maxDepth);
303 
304  target.state(source.state());
305 }
306 
307 /// Serves as a factory for book of given type.
308 template
309 <
310  class PriceLevelType,
311  class DepthType
312 >
313 class
315 {
316 public:
317  /// Type of book constructed by given factory.
318  typedef
319  MbpBook
320  <
321  PriceLevelType,
322  DepthType
323  >
325 
326  /// Book entry type.
327  typedef
328  typename Book::Entry
330 
331  /// Book depth type.
332  typedef
333  typename Book::Depth
335 
336  /// Calculates size of memory required
337  /// to keep in memory book of given depth.
338  static
339  size_t
341  Depth maxDepth)
342  {
343  return (
344  sizeof(Book) +
345  2 *
346  maxDepth *
347  sizeof(Entry)
348  );
349  }
350 
351  /// Constructs book instance of given capacity.
352  ///
353  /// Book depth can be changed in bounds of specified capacity.
354  static
355  Book*
357  Depth maxDepth)
358  {
359  const
360  size_t
361  bookSize =
362  calcSize(maxDepth);
363 
364  return
365  inplaceConstruct(
366  maxDepth,
367  new Byte [bookSize]);
368  }
369 
370  /// Destructs previously constructed book.
371  static
372  void
374  Book* book)
375  {
376  if (book)
377  {
378  book->~Book();
379 
380  delete [] reinterpret_cast<Byte*>(book);
381  }
382  }
383 
384  /// Constructs book on a given memory block.
385  static
386  Book*
388  Depth maxDepth,
389  void* block)
390  {
391  Entry*
392  const
393  entries =
394  reinterpret_cast<Entry*>
395  (
396  reinterpret_cast<Byte*>(block) +
397  sizeof(Book)
398  );
399 
400  inplaceConstruct(
401  entries,
402  entries + 2 * maxDepth);
403 
404  return
405  new (block)
406  Book(
407  maxDepth,
408  entries,
409  entries + maxDepth);
410  }
411 
412 private:
413  static
414  void
415  inplaceConstruct(
416  Entry* first,
417  Entry* last)
418  {
419  while (first != last)
420  new (first++) Entry();
421  }
422 };
423 
void depth(Depth depth)
Updates the maximal number of price levels.
Definition: MbpBook.h:141
UInt8 Byte
Alias for Byte.
Definition: Memory.h:30
static size_t calcSize(Depth maxDepth)
Definition: MbpBook.h:340
void reset(BookState::Enum bookState)
Definition: MbpBook.h:240
Tag class for accessing bids in templates.
BookState::Enum state() const
Indicates current state of book.
Definition: MbpBook.h:223
static Book * inplaceConstruct(Depth maxDepth, void *block)
Constructs book on a given memory block.
Definition: MbpBook.h:387
Depth capacity() const
Definition: MbpBook.h:134
static Book * construct(Depth maxDepth)
Definition: MbpBook.h:356
EntryType Entry
Indicates price level data type.
Definition: MbpBook.h:77
Entries::ConstIterator EntryIterator
Iterator over bids and offers.
Definition: MbpBook.h:97
MbpBook< PriceLevelType, DepthType > Book
Type of book constructed by given factory.
Definition: MbpBook.h:324
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:168
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
void state(BookState::Enum state)
Updates state of book.
Definition: MbpBook.h:230
MbpBook(Depth capacity, Entry *bids, Entry *offers)
Definition: MbpBook.h:105
void copy(MbpBook< TargetPriceLevel, Depth > &target, const MbpBook< SourcePriceLevel, Depth > &source, Depth maxDepth=static_cast< Depth >(-1))
Definition: MbpBook.h:284
static void destruct(Book *book)
Destructs previously constructed book.
Definition: MbpBook.h:373
Book::Entry Entry
Book entry type.
Definition: MbpBook.h:329
DepthType Depth
Aliases book dimension type.
Definition: MbpBook.h:82
State of order book.
MDEntryType type.
Definition: Fields.h:279
Entries & bids()
List of descending bid prices.
Definition: MbpBook.h:161
Entries & offers()
List of ascending offer prices.
Definition: MbpBook.h:192
VectorOverArray< Entry, Depth > Entries
Collection of price levels allowing modifications.
Definition: MbpBook.h:91
const Item * ConstIterator
Aliases constant iterator type.
void throwMbpBookRedepthFailure(Depth newDepth, Depth maxDepth)
Raises exception on book re-depth failure.
Definition: MbpBook.h:41
Book::Depth Depth
Book depth type.
Definition: MbpBook.h:334
const Entries & offers() const
List of ascending offer prices.
Definition: MbpBook.h:208
Serves as a factory for book of given type.
Definition: MbpBook.h:313
Tag class for accessing offers in templates.
ONIXS_CMEMDH_EXPORTED void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:172