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