OnixS C++ eSpeed ITCH Market Data Handler  1.7.3
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 #pragma once
21 
22 #include <cassert>
23 
27 
29 
30 
31 /// Exposes base services to access fields stored in
32 /// little Endian block of memory. Class represents an
33 /// abstraction, instance construction available through
34 /// descendants implementing actual access to encoded data.
35 
36 template <template<class> class Block, class FieldAccessor>
38 {
39  typedef MessageSize BlockSize;
40 
41  /// Instance of block providing
42  /// actual access to encoded data.
43  const Block<FieldAccessor>& block() const
45  {
46  return *static_cast<const Block<FieldAccessor>*>(this);
47  }
48 
49 protected:
50  /// Returns value of a field by its offset.
51  template <class FieldValue>
52  FieldValue ordinary(BlockSize offset) const
54  {
55  assert(block().binarySize() >= (offset + sizeof(FieldValue)));
56 
57  return FieldAccessor::get(FieldValue(), advanceByBytes(block().binary(), offset));
58  }
59 
60  /// Returns value of a field by its offset.
61  template <class FieldValue>
62  const FieldValue& ordinaryRef(BlockSize offset) const
64  {
65  assert(block().binarySize() >= (offset + sizeof(FieldValue)));
66 
67  return *static_cast <const FieldValue*>(advanceByBytes(block().binary(), offset));
68  }
69 
70  /// Returns value of a field by its offset.
71  ///
72  /// Given function serves as specialization of
73  /// general case for fields of enumeration type.
74  template <class Enumeration>
75  typename Enumeration::Enum enumeration(BlockSize offset) const
77  {
78  typedef typename Enumeration::Base Base;
79 
80  typedef typename Enumeration::Enum Enum;
81 
82  return static_cast<Enum>(ordinaryRef<Base>(offset));
83  }
84 
85  /// Provides access to string field by its offset.
86  ///
87  /// Given function serves as specialization
88  /// of general case for fields of string type.
89  template <BlockSize Size>
90  StrRef fixedStr(BlockSize offset) const
92  {
93  typedef Char Str[Size];
94 
95  assert(block().binarySize() >= (offset + Size));
96 
97  return
98  StrRef(ordinaryRef<Str>(offset), strnlen(ordinaryRef<Str>(offset), Size));
99  }
100 };
101 
102 /// Encapsulates services for manipulating encoded messages.
103 template <class Accessor>
105  : public BinaryFields<BinaryMessage, Accessor>
106 {
107  const void* data_;
108  MessageSize size_;
109 
110 public:
111  /// Length of message binary data.
113 
114  /// Initializes instance over given memory block.
116  const void* data,
117  MessageSize size)
119  : data_(data)
120  , size_(size)
121  {
122  }
123 
124  /// Initializes instance as copy of the other one.
125  ONIXS_ESPEED_ITCH_CONSTEXPR
127  const BinaryMessage& other)
129  : data_(other.data_)
130  , size_(other.size_)
131  {
132  }
133 
134  /// Message content.
135  const void* binary() const
137  {
138  return data_;
139  }
140 
141  /// Size of message.
144  {
145  return size_;
146  }
147 
148  /// Re-initializes instance as a copy of the other one.
150  operator =(
151  const BinaryMessage& other)
153  {
154  data_ = other.data_;
155  size_ = other.size_;
156 
157  return *this;
158  }
159 
160 #if defined(ONIXS_ESPEED_ITCH_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_ESPEED_ITCH_COMPILER_CXX_RVALUE_REFERENCES
161 
164  : data_(std::move(other.data_))
165  , size_(std::move(other.size_))
166  {
167  }
168 
170  operator=(
171  BinaryMessage&& other)
173  {
174  if (this == &other)
175  return *this;
176 
177  data_ = std::move(other.data_);
178  size_ = std::move(other.size_);
179 
180  return *this;
181  }
182 
183 #endif
184 };
185 
186 ONIXS_ESPEED_ITCH_API
187 void throwIncorrectSize(const std::string& messageName, MessageSize receivedSize, MessageSize expectedSize);
188 
189 
#define ONIXS_ESPEED_ITCH_NAMESPACE_END
Definition: Bootstrap.h:31
#define ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN
Definition: Bootstrap.h:27
ONIXS_ESPEED_ITCH_API void throwIncorrectSize(const std::string &messageName, MessageSize receivedSize, MessageSize expectedSize)
Encapsulates services for manipulating encoded messages.
MessageSize BinarySize
Length of message binary data.
MessageSize binarySize() const ONIXS_ESPEED_ITCH_NOTHROW
Size of message.
const FieldValue & ordinaryRef(BlockSize offset) const ONIXS_ESPEED_ITCH_NOTHROW
Returns value of a field by its offset.
Definition: BinaryMessage.h:62
ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN typedef char Char
Character type alias.
Definition: String.h:37
ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN typedef UInt16 MessageSize
Aliases message length type.
Definition: Defines.h:34
Type * advanceByBytes(Type *pointer, ptrdiff_t distance)
Advances given pointer to a given offset (distance) in bytes.
Definition: Memory.h:98
const void * binary() const ONIXS_ESPEED_ITCH_NOTHROW
Message content.
BinaryMessage(const void *data, MessageSize size) ONIXS_ESPEED_ITCH_NOTHROW
Initializes instance over given memory block.
StrRef fixedStr(BlockSize offset) const ONIXS_ESPEED_ITCH_NOTHROW
Definition: BinaryMessage.h:90
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:40
ONIXS_ESPEED_ITCH_CONSTEXPR BinaryMessage(const BinaryMessage &other) ONIXS_ESPEED_ITCH_NOTHROW
Initializes instance as copy of the other one.
#define ONIXS_ESPEED_ITCH_NOTHROW
Definition: Compiler.h:27
Enumeration::Enum enumeration(BlockSize offset) const ONIXS_ESPEED_ITCH_NOTHROW
Definition: BinaryMessage.h:75
FieldValue ordinary(BlockSize offset) const ONIXS_ESPEED_ITCH_NOTHROW
Returns value of a field by its offset.
Definition: BinaryMessage.h:52