OnixS C++ CME Market Data Handler  5.4.0
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
285  MbpBook
286  <
287  TargetPriceLevel,
288  Depth
289  >& target,
290  const
291  MbpBook
292  <
293  SourcePriceLevel,
294  Depth
295  >& source,
296  Depth maxDepth =
297  static_cast<Depth>(-1))
298 {
299  if (maxDepth >
300  source.depth())
301  {
302  maxDepth =
303  source.depth();
304  }
305 
306  target.depth(maxDepth);
307 
308  copy
309  (
310  target.bids(),
311  source.bids(),
312  maxDepth
313  );
314 
315  copy
316  (
317  target.offers(),
318  source.offers(),
319  maxDepth
320  );
321 
322  target.state(
323  source.state());
324 }
325 
326 /// Serves as a factory for book of given type.
327 template
328 <
329  class PriceLevelType,
330  class DepthType
331 >
332 class
334 {
335 public:
336  /// Type of book constructed by given factory.
337  typedef
338  MbpBook
339  <
340  PriceLevelType,
341  DepthType
342  >
344 
345  /// Book entry type.
346  typedef
347  typename Book::Entry
349 
350  /// Book depth type.
351  typedef
352  typename Book::Depth
354 
355  /// Calculates size of memory required
356  /// to keep in memory book of given depth.
357  static
358  size_t
360  Depth maxDepth)
361  {
362  return (
363  sizeof(Book) +
364  2 *
365  maxDepth *
366  sizeof(Entry)
367  );
368  }
369 
370  /// Constructs book instance of given capacity.
371  ///
372  /// Book depth can be changed in bounds of specified capacity.
373  static
374  Book*
376  Depth maxDepth)
377  {
378  const
379  size_t
380  bookSize =
381  calcSize(maxDepth);
382 
383  return
384  inplaceConstruct(
385  maxDepth,
386  new Byte [bookSize]);
387  }
388 
389  /// Destructs previously constructed book.
390  static
391  void
393  Book* book)
394  {
395  if (book)
396  {
397  book->~Book();
398 
399  delete [] reinterpret_cast<Byte*>(book);
400  }
401  }
402 
403  /// Constructs book on a given memory block.
404  static
405  Book*
407  Depth maxDepth,
408  void* block)
409  {
410  Entry*
411  const
412  entries =
413  reinterpret_cast<Entry*>
414  (
415  reinterpret_cast<Byte*>(block) +
416  sizeof(Book)
417  );
418 
419  inplaceConstruct(
420  entries,
421  entries + 2 * maxDepth);
422 
423  return
424  new (block)
425  Book(
426  maxDepth,
427  entries,
428  entries + maxDepth);
429  }
430 
431 private:
432  static
433  void
434  inplaceConstruct(
435  Entry* first,
436  Entry* last)
437  {
438  while (first != last)
439  new (first++) Entry();
440  }
441 };
442 
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:359
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:406
Depth capacity() const
Definition: MbpBook.h:134
static Book * construct(Depth maxDepth)
Definition: MbpBook.h:375
EntryType Entry
Indicates price level data type.
Definition: MbpBook.h:77
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:379
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:343
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
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:392
Book::Entry Entry
Book entry type.
Definition: MbpBook.h:348
DepthType Depth
Aliases book dimension type.
Definition: MbpBook.h:82
State of order book.
MDEntryType type.
Definition: Fields.h:352
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:353
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:332
Tag class for accessing offers in templates.
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:156