OnixS C++ CME MDP Conflated UDP Handler  1.0.2
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 
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 template
274 <
275  class TargetPriceLevel,
276  class SourcePriceLevel,
277  class Depth
278 >
279 void
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 
322 /// Serves as a factory for book of given type.
323 template
324 <
325  class PriceLevelType,
326  class DepthType
327 >
328 class
330 {
331 public:
332  /// Type of book constructed by given factory.
333  typedef
334  MbpBook
335  <
336  PriceLevelType,
337  DepthType
338  >
340 
341  /// Book entry type.
342  typedef
343  typename Book::Entry
345 
346  /// Book depth type.
347  typedef
348  typename Book::Depth
350 
351  /// Calculates size of memory required
352  /// to keep in memory book of given depth.
353  static
354  size_t
356  Depth maxDepth)
357  {
358  return (
359  sizeof(Book) +
360  2 *
361  maxDepth *
362  sizeof(Entry)
363  );
364  }
365 
366  /// Constructs book instance of given capacity.
367  ///
368  /// Book depth can be changed in bounds of specified capacity.
369  static
370  Book*
372  Depth maxDepth)
373  {
374  const
375  size_t
376  bookSize =
377  calcSize(maxDepth);
378 
379  return
380  inplaceConstruct(
381  maxDepth,
382  new Byte [bookSize]);
383  }
384 
385  /// Destructs previously constructed book.
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 
399  /// Constructs book on a given memory block.
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 
415  inplaceConstruct(
416  entries,
417  entries + 2 * maxDepth);
418 
419  return
420  new (block)
421  Book(
422  maxDepth,
423  entries,
424  entries + maxDepth);
425  }
426 
427 private:
428  static
429  void
430  inplaceConstruct(
431  Entry* first,
432  Entry* last)
433  {
434  while (first != last)
435  new (first++) Entry();
436  }
437 };
438 
void depth(Depth depth)
Updates the maximal number of price levels.
Definition: MbpBook.h:141
Entries::ConstIterator EntryIterator
Iterator over bids and offers.
Definition: MbpBook.h:97
const Entries & bids() const
List of descending bid prices.
Definition: MbpBook.h:177
Tag class for accessing offers in templates.
static Book * inplaceConstruct(Depth maxDepth, void *block)
Constructs book on a given memory block.
Definition: MbpBook.h:402
Entries & offers()
List of ascending offer prices.
Definition: MbpBook.h:192
DepthType Depth
Aliases book dimension type.
Definition: MbpBook.h:82
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:379
EntryType Entry
Indicates price level data type.
Definition: MbpBook.h:77
static Book * construct(Depth maxDepth)
Definition: MbpBook.h:371
static void destruct(Book *book)
Destructs previously constructed book.
Definition: MbpBook.h:388
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
const Entries & offers() const
List of ascending offer prices.
Definition: MbpBook.h:208
Tag class for accessing bids in templates.
Book::Entry Entry
Book entry type.
Definition: MbpBook.h:344
void state(BookState::Enum state)
Updates state of book.
Definition: MbpBook.h:230
BookState::Enum state() const
Indicates current state of book.
Definition: MbpBook.h:223
Serves as a factory for book of given type.
Definition: MbpBook.h:328
static size_t calcSize(Depth maxDepth)
Definition: MbpBook.h:355
void copy(MbpBook< TargetPriceLevel, Depth > &target, const MbpBook< SourcePriceLevel, Depth > &source, Depth maxDepth=static_cast< Depth >(-1))
Definition: MbpBook.h:280
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition: Bootstrap.h:156
Entries & bids()
List of descending bid prices.
Definition: MbpBook.h:161
MbpBook< PriceLevelType, DepthType > Book
Type of book constructed by given factory.
Definition: MbpBook.h:339
Book::Depth Depth
Book depth type.
Definition: MbpBook.h:349
Depth depth() const
Returns the maximal number of price levels.
Definition: MbpBook.h:127
void reset(BookState::Enum bookState)
Definition: MbpBook.h:240
const Item * ConstIterator
Aliases constant iterator type.
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
VectorOverArray< Entry, Depth > Entries
Collection of price levels allowing modifications.
Definition: MbpBook.h:91
MbpBook(Depth capacity, Entry *bids, Entry *offers)
Definition: MbpBook.h:105