OnixS C++ Cboe CFE Multicast PITCH Market Data Handler  1.12.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 
23 #include <cassert>
24 
25 #include <OnixS/CboeCFE/MarketData/Pitch/Export.h>
30 
31 namespace OnixS
32 {
33  namespace CboeCFE
34  {
35  namespace MarketData
36  {
37  namespace Pitch
38  {
39  /// Value extractor helper
40  template <class FieldValue>
42  {
43  static FieldValue extract(const void* p) ONIXS_BATS_PITCH_NOTHROW
44  {
45  return unalignedCopy<FieldValue>(p);
46  }
47  };
48 
49  template<class MantissaType, class ExponentType>
50  struct OrdinaryExtractor<FixedPointDecimal<MantissaType, ExponentType> >
51  {
53  {
55  unalignedCopy<typename FixedPointDecimal<MantissaType, ExponentType>::Mantissa>(p));
56  }
57  };
58 
59  /// Exposes base services to access fields stored in
60  /// little Endian block of memory. Class represents an
61  /// abstraction, instance construction available through
62  /// descendants implementing actual access to encoded data.
63  template <class Block, class BlockSize>
65  {
66  /// Instance of block providing
67  /// actual access to encoded data.
68  const Block& block() const
70  {
71  return *static_cast<const Block*>(this);
72  }
73 
74  protected:
75  /// Returns sub message.
76  template <class SubMessage>
77  const SubMessage submessage(BlockSize offset, BlockSize size) const
79  {
80  assert(block().binarySize() >= (offset + size));
81 
82  return
83  SubMessage(advanceByBytes(block().binary(), offset), size);
84  }
85 
86  /// Returns value of a field by its offset.
87  template <class FieldValue>
88  FieldValue ordinary(BlockSize offset) const
90  {
91  assert(block().binarySize() >= (offset + sizeof(FieldValue)));
92 
93  return OrdinaryExtractor<FieldValue>::extract(advanceByBytes(block().binary(), offset));
94  }
95 
96  /// Returns value of a field by its offset.
97  template <class FieldValue>
98  const FieldValue& ordinaryRef(BlockSize offset) const
100  {
101  assert(block().binarySize() >= (offset + sizeof(FieldValue)));
102 
103  return
104  *static_cast <const FieldValue*>(advanceByBytes(block().binary(), offset));
105  }
106 
107  /// Returns value of a field by its offset.
108  ///
109  /// Given function serves as specialization of
110  /// general case for fields of enumeration type.
111  template <class Enumeration>
112  typename Enumeration::Enum enumeration(BlockSize offset) const
114  {
115  typedef typename Enumeration::Base Base;
116 
117  typedef typename Enumeration::Enum Enum;
118 
119  return static_cast<Enum>(ordinary<Base>(offset));
120  }
121 
122  /// Provides access to string field by its offset.
123  ///
124  /// Given function serves as specialization
125  /// of general case for fields of string type.
126  template <BlockSize Size>
127  StrRef fixedStr(BlockSize offset) const
129  {
130  typedef Char Str[Size];
131 
132  const Str& str = ordinaryRef<Str>(offset);
133 
134  return StrRef(str, strnlen(str, Size));
135  }
136  };
137 
138  /// Encapsulates services for manipulating little endian encoded messages.
139  class ONIXS_CBOECFE_PITCH_API BinaryMessage : public BinaryFields<BinaryMessage, MessageSize>
140  {
141  const void* data_;
142  MessageSize size_;
143 
144  public:
145  /// Length of message binary data.
147 
148  /// Initializes blank instance referencing to nothing.
151  : data_(ONIXS_BATS_PITCH_NULLPTR)
152  , size_(0)
153  {
154  }
155 
156  /// Initializes instance over given memory block.
158  const void* data,
159  MessageSize size)
161  : data_(data)
162  , size_(size)
163  {
164  }
165 
166  /// Initializes instance as copy of the other one.
168  const BinaryMessage& other)
170  : data_(other.data_)
171  , size_(other.size_)
172  {
173  }
174 
175  /// Indicates whether instance refers to a valid message.
177  operator bool() const
179  {
180  return (ONIXS_BATS_PITCH_NULLPTR != data_);
181  }
182 
183  /// Message content.
184  const void* binary() const
186  {
187  return data_;
188  }
189 
190  /// Size of message.
193  {
194  return size_;
195  }
196 
197  /// Re-initializes instance as a copy of the other one.
199  operator =(
200  const BinaryMessage& other)
202  {
203  if (this == &other)
204  return *this;
205 
206  data_ = other.data_;
207  size_ = other.size_;
208 
209  return *this;
210  }
211 
212 #if defined(ONIXS_BATS_PITCH_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_BATS_PITCH_COMPILER_CXX_RVALUE_REFERENCES
213 
216  : data_(std::move(other.data_))
217  , size_(std::move(other.size_))
218  {
219  }
220 
222  operator=(
223  BinaryMessage&& other)
225  {
226  if (this == &other)
227  return *this;
228 
229  data_ = std::move(other.data_);
230  size_ = std::move(other.size_);
231 
232  return *this;
233  }
234 #endif
235 
236  };
237 
238  ONIXS_CBOECFE_PITCH_API
241  void throwIncorrectSize(const std::string& messageName, MessageSize receivedSize, MessageSize expectedSize);
242 
243  }
244  }
245  }
246 }
ONIXS_CBOECFE_PITCH_API ONIXS_BATS_PITCH_COLD_PATH ONIXS_BATS_PITCH_NORETURN void throwIncorrectSize(const std::string &messageName, MessageSize receivedSize, MessageSize expectedSize)
const FieldValue & ordinaryRef(BlockSize offset) const ONIXS_BATS_PITCH_NOTHROW
Returns value of a field by its offset.
Definition: BinaryMessage.h:98
MessageSize BinarySize
Length of message binary data.
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:45
#define ONIXS_BATS_PITCH_NORETURN
Definition: Compiler.h:77
Encapsulates services for manipulating little endian encoded messages.
BinaryMessage() ONIXS_BATS_PITCH_NOTHROW
Initializes blank instance referencing to nothing.
static FieldValue extract(const void *p) ONIXS_BATS_PITCH_NOTHROW
Definition: BinaryMessage.h:43
Enumeration::Enum enumeration(BlockSize offset) const ONIXS_BATS_PITCH_NOTHROW
char Char
Character type alias.
Definition: String.h:42
Binary2 MessageSize
Aliases message length type.
Definition: Defines.h:90
const SubMessage submessage(BlockSize offset, BlockSize size) const ONIXS_BATS_PITCH_NOTHROW
Returns sub message.
Definition: BinaryMessage.h:77
const void * binary() const ONIXS_BATS_PITCH_NOTHROW
Message content.
BinaryMessage(const BinaryMessage &other) ONIXS_BATS_PITCH_NOTHROW
Initializes instance as copy of the other one.
Type * advanceByBytes(Type *pointer, ptrdiff_t distance) ONIXS_BATS_PITCH_NOTHROW
Advances given pointer to a given offset (distance) in bytes.
Definition: Memory.h:109
#define ONIXS_BATS_PITCH_NOTHROW
Definition: Compiler.h:27
static FixedPointDecimal< MantissaType, ExponentType > extract(const void *p) ONIXS_BATS_PITCH_NOTHROW
Definition: BinaryMessage.h:52
#define ONIXS_BATS_PITCH_COLD_PATH
Definition: Compiler.h:60
#define ONIXS_BATS_PITCH_EXPLICIT
Definition: Compiler.h:33
FieldValue ordinary(BlockSize offset) const ONIXS_BATS_PITCH_NOTHROW
Returns value of a field by its offset.
Definition: BinaryMessage.h:88
MessageSize binarySize() const ONIXS_BATS_PITCH_NOTHROW
Size of message.
StrRef fixedStr(BlockSize offset) const ONIXS_BATS_PITCH_NOTHROW
BinaryMessage(const void *data, MessageSize size) ONIXS_BATS_PITCH_NOTHROW
Initializes instance over given memory block.