OnixS C++ CME MDP Premium Market Data Handler  5.8.9
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 <typename Depth>
36 inline 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 
54 /// Base attributes of order book.
55 ///
56 /// A set of bids and offers data for the given security
57 /// (bids are descending and offers are ascending).
58 template <class EntryType, class DepthType>
59 class MbpBook
60 {
61 public:
62  /// Indicates price level data type.
63  typedef EntryType Entry;
64 
65  /// Aliases book dimension type.
66  typedef DepthType Depth;
67 
68  /// Collection of price levels allowing modifications.
70 
71  /// Iterator over bids and offers.
73 
74  /// Initializes instance with given attributes.
75  ///
76  /// Bids and offers are stored in given segments of
77  /// plain arrays. Book constructs vectors over input
78  /// arrays of fixed size. That provides more flexibility
79  /// on memory layout for internally used data.
80  MbpBook(Depth capacity, Entry* bids, Entry* offers)
81  : depth_(0)
82  , state_(BookState::Constructed)
83  , bids_(bids, capacity)
84  , offers_(offers, capacity)
85  {
86  }
87 
88  // Cleans everything up.
89  ~MbpBook() {}
90 
91  /// Returns the maximal number of price levels.
92  Depth depth() const
93  {
94  return depth_;
95  }
96 
97  /// Indicates book capacity which is
98  /// maximal value of book depth allowed.
99  Depth capacity() const
100  {
101  return bids_.capacity();
102  }
103 
104  /// Updates the maximal number of price levels.
105  void depth(Depth depth)
106  {
107  if (depth > capacity())
108  {
109  throwMbpBookRedepthFailure(depth, capacity());
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 
121  /// List of descending bid prices.
122  Entries& bids()
123  {
124  return bids_;
125  }
126 
127  /// List of descending bid prices.
128  Entries& operator[](const Bids&)
129  {
130  return bids_;
131  }
132 
133  /// List of descending bid prices.
134  const Entries& bids() const
135  {
136  return bids_;
137  }
138 
139  /// List of descending bid prices.
140  const Entries& operator[](const Bids&) const
141  {
142  return bids_;
143  }
144 
145  /// List of ascending offer prices.
146  Entries& offers()
147  {
148  return offers_;
149  }
150 
151  /// List of ascending offer prices.
152  Entries& operator[](const Offers&)
153  {
154  return offers_;
155  }
156 
157  /// List of ascending offer prices.
158  const Entries& offers() const
159  {
160  return offers_;
161  }
162 
163  /// List of ascending offer prices.
164  const Entries& operator[](const Offers&) const
165  {
166  return offers_;
167  }
168 
169  /// Indicates current state of book.
171  {
172  return state_;
173  }
174 
175  /// Updates state of book.
176  void state(BookState::Enum state)
177  {
178  state_ = state;
179  }
180 
181  /// Wipes out all bids and offers and other
182  /// data associated with book (like instance
183  /// of last applied FIX message).
184  void reset(BookState::Enum bookState)
185  {
186  state(bookState);
187 
188  bids_.resize(0);
189  offers_.resize(0);
190  }
191 
192 private:
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 
212 /// Copies content of one book to another.
213 ///
214 /// Source and destination books are not required
215 /// to be of same type, just compatible by price levels.
216 /// For example, content of direct book can be copied to implied book,
217 /// or content of consolidated book can be copied to implied book,
218 /// however, implied book can't be copied to direct book as far as
219 /// direct price level has more attributes rather than implied one.
220 template <class TargetPriceLevel, class SourcePriceLevel, class Depth>
223  const MbpBook<SourcePriceLevel, Depth>& source,
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 
243 /// Serves as a factory for book of given type.
244 template <class PriceLevelType, class DepthType>
246 {
247 public:
248  /// Type of book constructed by given factory.
250 
251  /// Book entry type.
252  typedef typename Book::Entry Entry;
253 
254  /// Book depth type.
255  typedef typename Book::Depth Depth;
256 
257  /// Calculates size of memory required
258  /// to keep in memory book of given depth.
259  static size_t calcSize(Depth maxDepth)
260  {
261  return (sizeof(Book) + 2 * maxDepth * sizeof(Entry));
262  }
263 
264  /// Constructs book instance of given capacity.
265  ///
266  /// Book depth can be changed in bounds of specified capacity.
267  static Book* construct(Depth maxDepth)
268  {
269  const size_t bookSize = calcSize(maxDepth);
270 
271  return inplaceConstruct(maxDepth, new Byte[bookSize]);
272  }
273 
274  /// Destructs previously constructed book.
275  static void destruct(Book* book)
276  {
277  if (book)
278  {
279  book->~Book();
280 
281  delete[] reinterpret_cast<Byte*>(book);
282  }
283  }
284 
285  /// Constructs book on a given memory block.
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 
295 private:
296  static void inplaceConstruct(Entry* first, Entry* last)
297  {
298  while (first != last)
299  new (first++) Entry();
300  }
301 };
302 
Base attributes of order book.
Definition: MbpBook.h:59
void depth(Depth depth)
Updates the maximal number of price levels.
Definition: MbpBook.h:105
UInt8 Byte
Alias for Byte.
Definition: Memory.h:30
static size_t calcSize(Depth maxDepth)
Calculates size of memory required to keep in memory book of given depth.
Definition: MbpBook.h:259
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
Tag class for accessing bids in templates.
BookState::Enum state() const
Indicates current state of book.
Definition: MbpBook.h:170
MbpBook< TargetPriceLevel, Depth > & copy(MbpBook< TargetPriceLevel, Depth > &target, const MbpBook< SourcePriceLevel, Depth > &source, Depth maxDepth=static_cast< Depth >(-1))
Copies content of one book to another.
Definition: MbpBook.h:221
static Book * inplaceConstruct(Depth maxDepth, void *block)
Constructs book on a given memory block.
Definition: MbpBook.h:286
Depth capacity() const
Indicates book capacity which is maximal value of book depth allowed.
Definition: MbpBook.h:99
static Book * construct(Depth maxDepth)
Constructs book instance of given capacity.
Definition: MbpBook.h:267
EntryType Entry
Indicates price level data type.
Definition: MbpBook.h:63
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:318
Entries::ConstIterator EntryIterator
Iterator over bids and offers.
Definition: MbpBook.h:72
Entries & operator[](const Bids &)
List of descending bid prices.
Definition: MbpBook.h:128
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
Depth depth() const
Returns the maximal number of price levels.
Definition: MbpBook.h:92
MbpBook< PriceLevelType, DepthType > Book
Type of book constructed by given factory.
Definition: MbpBook.h:249
const Entries & bids() const
List of descending bid prices.
Definition: MbpBook.h:134
VectorOverArray< Entry, Depth > Entries
Collection of price levels allowing modifications.
Definition: MbpBook.h:69
void state(BookState::Enum state)
Updates state of book.
Definition: MbpBook.h:176
MbpBook(Depth capacity, Entry *bids, Entry *offers)
Initializes instance with given attributes.
Definition: MbpBook.h:80
static void destruct(Book *book)
Destructs previously constructed book.
Definition: MbpBook.h:275
Book::Entry Entry
Book entry type.
Definition: MbpBook.h:252
DepthType Depth
Aliases book dimension type.
Definition: MbpBook.h:66
State of order book.
Entries & operator[](const Offers &)
List of ascending offer prices.
Definition: MbpBook.h:152
MDEntryType type.
Definition: Fields.h:294
const Entries & operator[](const Bids &) const
List of descending bid prices.
Definition: MbpBook.h:140
Entries & bids()
List of descending bid prices.
Definition: MbpBook.h:122
Entries & offers()
List of ascending offer prices.
Definition: MbpBook.h:146
const Entries & operator[](const Offers &) const
List of ascending offer prices.
Definition: MbpBook.h:164
const Item * ConstIterator
Aliases constant iterator type.
void throwMbpBookRedepthFailure(Depth newDepth, Depth maxDepth)
Raises exception on book re-depth failure.
Definition: MbpBook.h:36
Book::Depth Depth
Book depth type.
Definition: MbpBook.h:255
const Entries & offers() const
List of ascending offer prices.
Definition: MbpBook.h:158
Serves as a factory for book of given type.
Definition: MbpBook.h:245
Tag class for accessing offers in templates.
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68