OnixS C++ CBOE CFE Binary Order Entry (BOE) Handler  1.11.0
API documentation
BinaryMessage.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 
26 
27 #include <cassert>
28 
29 namespace OnixS
30 {
31  namespace CboeCFE
32  {
33  namespace Trading
34  {
35  namespace BOE
36  {
37  /// Value extractor helper
38  template <class FieldValue>
40  {
41  static FieldValue extract(const void* p)
42  {
43  return unalignedCopy<FieldValue>(p);
44  }
45  };
46 
47  template<class MantissaType, class ExponentType>
48  struct OrdinaryExtractor<FixedPointDecimal<MantissaType, ExponentType> >
49  {
51  {
53  unalignedCopy<typename FixedPointDecimal<MantissaType, ExponentType>::Mantissa>(p));
54  }
55  };
56 
57  /// Exposes base services to access fields stored in
58  /// little Endian block of memory. Class represents an
59  /// abstraction, instance construction available through
60  /// descendants implementing actual access to encoded data.
61  template <class Block, class BlockSize>
63  {
64  /// Instance of block providing
65  /// actual access to encoded data.
66  const Block& block() const
67  {
68  return *static_cast<const Block*>(this);
69  }
70 
71  protected:
72  /// Returns sub message.
73  template <class SubMessage>
74  const SubMessage submessage(BlockSize offset, BlockSize size) const
75  {
76  assert(block().binarySize() >= (offset + size));
77 
78  const void* const data = advanceByBytes(block().binary(), offset);
79 
80  return SubMessage(data, size);
81  }
82 
83  /// Returns value of a field by its offset.
84  template <class FieldValue>
85  const FieldValue& ordinaryRef(BlockSize offset) const
86  {
87  assert(block().binarySize() >= (offset + sizeof(FieldValue)));
88 
89  const void* const fieldValue = advanceByBytes(block().binary(), offset);
90  return *static_cast <const FieldValue*>(fieldValue);
91  }
92 
93  /// Returns value of a field by its offset.
94  template <class FieldValue>
95  FieldValue ordinary(BlockSize offset) const
96  {
97  assert(block().binarySize() >= (offset + sizeof(FieldValue)));
98 
99  return OrdinaryExtractor<FieldValue>::extract(advanceByBytes(block().binary(), offset));
100  }
101 
102  /// Returns value of a field by its offset.
103  ///
104  /// Given function serves as specialization of
105  /// general case for fields of enumeration type.
106  template <class Enumeration>
107  typename Enumeration::Enum enumeration(BlockSize offset) const
108  {
109  typedef typename Enumeration::Base Base;
110 
111  typedef typename Enumeration::Enum Enum;
112 
113  return static_cast<Enum>( ordinary<Base>(offset));
114  }
115 
116  /// Provides access to string field by its offset.
117  ///
118  /// Given function serves as specialization
119  /// of general case for fields of string type.
120  template <BlockSize Size>
121  StrRef fixedStr(BlockSize offset) const
122  {
123  typedef Char Str[Size];
124 
125  const Str& str = ordinaryRef<Str>(offset);
126 
127  return StrRef(str, strnlen(str, Size));
128  }
129  };
130 
131  /// Aliases message length type.
133 
134  /// Encapsulates services for manipulating little endian encoded messages.
135  class ONIXS_CBOE_CFE_BOE_API BinaryMessage : public BinaryFields<BinaryMessage, MessageSize>
136  {
137  const void* data_;
138  MessageSize size_;
139 
140  public:
141  /// Length of message binary data.
142  typedef MessageSize BinarySize;
143 
144  /// Initializes blank instance referencing to nothing.
146  : data_(ONIXS_BATS_BOE_NULLPTR)
147  , size_(0)
148  {
149  }
150 
151  /// Initializes instance over given memory block.
153  const void* data,
154  MessageSize size)
155  : data_(data)
156  , size_(size)
157  {
158  assert(ONIXS_BATS_BOE_NULLPTR != data);
159  }
160 
161  /// Initializes instance as copy of the other one.
163  const BinaryMessage& other)
164  : data_(other.data_)
165  , size_(other.size_)
166  {
167  }
168 
169  /// Indicates whether instance refers to a valid message.
170  operator bool() const
171  {
172  return (ONIXS_BATS_BOE_NULLPTR != data_);
173  }
174 
175  /// Message content.
176  const void* binary() const
177  {
178  return data_;
179  }
180 
181  /// Size of message.
182  MessageSize binarySize() const
183  {
184  return size_;
185  }
186 
187  /// Re-initializes instance as a copy of the other one.
189  operator =(
190  const BinaryMessage& other)
191  {
192  data_ = other.data_;
193  size_ = other.size_;
194 
195  return *this;
196  }
197  };
198  }
199  }
200  }
201 }
Enumeration::Enum enumeration(BlockSize offset) const
Type * advanceByBytes(Type *pointer, ptrdiff_t distance)
Advances given pointer to a given offset (distance) in bytes.
Definition: Memory.h:126
static FieldValue extract(const void *p)
Definition: BinaryMessage.h:41
BinaryMessage()
Initializes blank instance referencing to nothing.
Provides efficient way of accessing text-based field values.
Definition: String.h:45
StrRef fixedStr(BlockSize offset) const
Encapsulates services for manipulating little endian encoded messages.
const SubMessage submessage(BlockSize offset, BlockSize size) const
Returns sub message.
Definition: BinaryMessage.h:74
MessageSize binarySize() const
Size of message.
static FixedPointDecimal< MantissaType, ExponentType > extract(const void *p)
Definition: BinaryMessage.h:50
Binary2 MessageSize
Aliases message length type.
const FieldValue & ordinaryRef(BlockSize offset) const
Returns value of a field by its offset.
Definition: BinaryMessage.h:85
BinaryMessage(const BinaryMessage &other)
Initializes instance as copy of the other one.
BinaryMessage(const void *data, MessageSize size)
Initializes instance over given memory block.
MessageSize BinarySize
Length of message binary data.
const void * binary() const
Message content.
FieldValue ordinary(BlockSize offset) const
Returns value of a field by its offset.
Definition: BinaryMessage.h:95
char Char
Character type alias.
Definition: String.h:42