OnixS C++ B3 Binary UMDF Market Data Handler  1.3.3
API documentation
Memory.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 <cstddef>
28 #include <cstring>
29 #include <cassert>
30 
32 
33 /// Alias for Byte.
34 typedef UInt8 Byte;
35 
36 /// Alias for Word.
37 typedef UInt16 Word;
38 
39 /// Alias for Double Word.
40 typedef UInt32 DWord;
41 
42 /// Alias for Quad Word.
43 typedef UInt64 QWord;
44 
45 /// Makes the pointer an opaque one.
46 /// \private
47 template
48 <
49  typename Type
50 >
51 ONIXS_B3_UMDF_MD_HOTPATH
52 ONIXS_B3_UMDF_MD_PURE
53 void*
54 toOpaquePtr(Type* ptr)
56 {
57  assert(ptr);
58  return static_cast<void*>(ptr);
59 }
60 
61 /// Makes the pointer an opaque one.
62 /// \private
63 template
64 <
65  typename Type
66 >
67 ONIXS_B3_UMDF_MD_HOTPATH
68 ONIXS_B3_UMDF_MD_PURE
69 const void*
70 toOpaquePtr(const Type* ptr)
72 {
73  assert(ptr);
74  return static_cast<const void*>(ptr);
75 }
76 
77 /// Reinterprets the pointer as a byte block one.
78 /// \private
79 template
80 <
81  typename Type
82 >
83 ONIXS_B3_UMDF_MD_HOTPATH
84 ONIXS_B3_UMDF_MD_PURE
85 Byte*
86 toByteBlock(Type* ptr)
88 {
89  assert(ptr);
90 
91  return
92  static_cast
93  <Byte*>
94  (toOpaquePtr(ptr));
95 }
96 
97 /// Reinterprets the pointer as a byte block one.
98 /// \private
99 template
100 <
101  typename Type
102 >
103 ONIXS_B3_UMDF_MD_HOTPATH
104 ONIXS_B3_UMDF_MD_PURE
105 const Byte*
106 toByteBlock(const Type* ptr)
108 {
109  assert(ptr);
110 
111  return
112  static_cast
113  <const Byte*>
114  (toOpaquePtr(ptr));
115 }
116 
117 /// Advances the pointer to a given offset (distance) in bytes.
118 /// \private
119 template
120 <
121  typename Type
122 >
123 ONIXS_B3_UMDF_MD_HOTPATH
124 ONIXS_B3_UMDF_MD_PURE
125 Type* advanceByBytes(Type* pointer, ptrdiff_t distance) ONIXS_B3_UMDF_MD_NOTHROW
126 {
127  assert(pointer);
128 
129  return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
130 }
131 
132 /// \return a pointer that is lower than
133 /// the given one by a given number of bytes.
134 /// \private
135 template
136 <
137  typename Type
138 >
139 ONIXS_B3_UMDF_MD_HOTPATH
140 ONIXS_B3_UMDF_MD_PURE
141 Type*
142 advanceBackByBytes(
143  Type* pointer,
144  ptrdiff_t distance)
146 {
147  assert(pointer);
148 
149  return
150  reinterpret_cast<Type*>
151  (
152  toByteBlock(pointer) - distance
153  );
154 }
155 
156 /// \return the distance in bytes between two pointers.
157 /// \private
158 template
159 <
160  typename Left,
161  typename Right
162 >
163 ONIXS_B3_UMDF_MD_HOTPATH
164 ONIXS_B3_UMDF_MD_PURE
165 ptrdiff_t
166 byteDistance(
167  Left* left,
168  Right* right)
170 {
171  assert(left);
172  assert(right);
173 
174  return (
175  toByteBlock(left) -
176  toByteBlock(right)
177  );
178 }
179 
180 /// \private
181 template
182 <
183  class Value
184 >
185 ONIXS_B3_UMDF_MD_HOTPATH
186 Value
187 extractValue(const void* location) ONIXS_B3_UMDF_MD_NOTHROW
188 {
189  assert(location);
190 
191  CHECK_TYPE_INTEGRAL(Value);
192 
193  Value result;
194 
195  std::memcpy(
196  &result,
197  location,
198  sizeof(Value));
199 
200  return result;
201 }
202 
203 /// \private
204 template <unsigned ArgsCount>
205 struct CompositeExtractor;
206 
207 /// \private
208 template <>
209 struct CompositeExtractor<1>
210 {
211  template <class Value>
212  static
213  Value extract(const void* location) ONIXS_B3_UMDF_MD_NOTHROW
214  {
215 
216 #if defined (ONIXS_B3_UMDF_MD_CXX11)
217  static_assert(
218  noexcept(
219  Value(
220  std::declval<typename Value::MemberTraits::FirstArgType>()
221  )
222  ) ,
223  "must be noexcept");
224 #endif
225 
226  assert(location);
227 
228  return
229  Value(
230  extractValue<typename Value::MemberTraits::FirstArgType>(location)
231  );
232  }
233 };
234 
235 /// \private
236 template <>
237 struct CompositeExtractor<2>
238 {
239  template <class Value>
240  static
241  Value extract(const void* location) ONIXS_B3_UMDF_MD_NOTHROW
242  {
243  typedef typename Value::MemberTraits::FirstArgType FirstArgType;
244  typedef typename Value::MemberTraits::SecondArgType SecondArgType;
245 
246 #if defined (ONIXS_B3_UMDF_MD_CXX11)
247  static_assert(
248  noexcept(
249  Value(
250  std::declval<FirstArgType>(),
251  std::declval<SecondArgType>()
252  )
253  ) ,
254  "must be noexcept");
255 #endif
256 
257  assert(location);
258 
259  return
260  Value(
261  extractValue<FirstArgType>(location),
262  extractValue<SecondArgType>(
263  advanceByBytes(location, sizeof(FirstArgType)))
264  );
265  }
266 };
267 
268 /// \private
269 template <>
270 struct CompositeExtractor<4>
271 {
272  template <class Value>
273  static
274  Value extract(const void* location) ONIXS_B3_UMDF_MD_NOTHROW
275  {
276  typedef typename Value::MemberTraits::FirstArgType FirstArgType;
277  typedef typename Value::MemberTraits::SecondArgType SecondArgType;
278  typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
279  typedef typename Value::MemberTraits::FourthArgType FourthArgType;
280 
281 #if defined (ONIXS_B3_UMDF_MD_CXX11)
282  static_assert(
283  noexcept(
284  Value(
285  std::declval<FirstArgType>(),
286  std::declval<SecondArgType>(),
287  std::declval<ThirdArgType>(),
288  std::declval<FourthArgType>()
289  )
290  ) ,
291  "must be noexcept");
292 #endif
293 
294  assert(location);
295 
296  return
297  Value(
298  extractValue<FirstArgType>(location),
299  extractValue<SecondArgType>(
300  advanceByBytes(location, sizeof(FirstArgType))),
301  extractValue<ThirdArgType>(
302  advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
303  extractValue<FourthArgType>(
304  advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType)))
305  );
306  }
307 };
308 
309 /// \private
310 template
311 <
312  class Value
313 >
314 ONIXS_B3_UMDF_MD_HOTPATH
315 typename EnableIf<details::HasMemberTraits<Value>::value, Value>::type
316 getValue(const void* location) ONIXS_B3_UMDF_MD_NOTHROW
317 {
318  assert(location);
319  return CompositeExtractor<Value::MemberTraits::Count>:: template extract<Value>(location);
320 }
321 
322 /// \private
323 template
324 <
325  class Value
326 >
327 ONIXS_B3_UMDF_MD_HOTPATH
328 typename EnableIf<!details::HasMemberTraits<Value>::value, Value>::type
329 getValue(const void* location) ONIXS_B3_UMDF_MD_NOTHROW
330 {
331  assert(location);
332  return extractValue<Value>(location);
333 }
334 
335 /// \private
336 template
337 <
338  class Value
339 >
341 typename EnableIf<!details::HasMemberTraits<Value>::value, size_t>::type
343 {
344  return sizeof(Value);
345 }
346 
347 /// \private
348 template
349 <
350  class Value
351 >
353 typename EnableIf<details::HasMemberTraits<Value>::value, size_t>::type
355 {
356  return Value::Size;
357 }
358 
359 /// \private
360 template
361 <
362  class Value
363 >
364 ONIXS_B3_UMDF_MD_HOTPATH
365 typename EnableIf<!details::HasSerializeMember<Value>::value>::type
366 commitValue(void* location, Value value)
368 {
369  assert(location);
370  std::memcpy(
371  location,
372  &value,
373  sizeof(Value));
374 }
375 
376 /// \private
377 template
378 <
379  class Value
380 >
381 ONIXS_B3_UMDF_MD_HOTPATH
382 typename EnableIf<details::HasSerializeMember<Value>::value>::type
383 commitValue(void* location, Value value)
385 {
386  assert(location);
387  value.serialize(location);
388 }
389 
390 /// \private
391 template
392 <
393  class Value
394 >
395 ONIXS_B3_UMDF_MD_HOTPATH
396 typename EnableIf<!details::HasValueStaticMember<Value>::value>::type
397 setValue(void* location, Value value)
399 {
400  assert(location);
401  commitValue(location, value);
402 }
403 
404 /// \private
405 template
406 <
407  class Value
408 >
409 ONIXS_B3_UMDF_MD_HOTPATH
410 typename EnableIf<details::HasValueStaticMember<Value>::value>::type
411 setValue(void* location, Value)
413 {
414  assert(location);
415  commitValue(location, Value::value());
416 }
417 
#define ONIXS_B3_UMDF_MD_NOTHROW
Definition: Compiler.h:114
#define ONIXS_B3_UMDF_MD_CONSTEXPR
Definition: Compiler.h:120
UInt8 Byte
Alias for Byte.
Definition: Memory.h:34
UInt16 UInt16
2-byte unsigned integer, from 0 to 65535..
Definition: Fields.h:54
UInt8 UInt8
1-byte unsigned integer, from 0 to 255..
Definition: Fields.h:50
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_END
Definition: ABI.h:151
UInt64 QWord
Alias for Quad Word.
Definition: Memory.h:43
UInt64 UInt64
8-byte unsigned integer, from 0 to 18446744073709551615 (2^64-1)..
Definition: Fields.h:62
UInt32 UInt32
4-byte unsigned integer, from 0 to 4294967295 (2^32-1)..
Definition: Fields.h:58
UInt16 Word
Alias for Word.
Definition: Memory.h:37
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:146
UInt32 DWord
Alias for Double Word.
Definition: Memory.h:40
#define CHECK_TYPE_INTEGRAL(Type)
Definition: Utils.h:79