OnixS C++ B3 Binary UMDF Market Data Handler 1.10.0
Users' manual and API documentation
Loading...
Searching...
No Matches
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
34typedef UInt8 Byte;
35
37typedef UInt16 Word;
38
40typedef UInt32 DWord;
41
43typedef UInt64 QWord;
44
47template
48<
49 typename Type
50>
51ONIXS_B3_UMDF_MD_HOTPATH
52ONIXS_B3_UMDF_MD_PURE
53void*
54toOpaquePtr(Type* ptr)
55 noexcept
56{
57 assert(ptr);
58 return static_cast<void*>(ptr);
59}
60
63template
64<
65 typename Type
66>
67ONIXS_B3_UMDF_MD_HOTPATH
68ONIXS_B3_UMDF_MD_PURE
69const void*
70toOpaquePtr(const Type* ptr)
71 noexcept
72{
73 assert(ptr);
74 return static_cast<const void*>(ptr);
75}
76
79template
80<
81 typename Type
82>
83ONIXS_B3_UMDF_MD_HOTPATH
84ONIXS_B3_UMDF_MD_PURE
85Byte*
86toByteBlock(Type* ptr)
87 noexcept
88{
89 assert(ptr);
90
91 return
92 static_cast
93 <Byte*>
94 (toOpaquePtr(ptr));
95}
96
99template
100<
101 typename Type
102>
103ONIXS_B3_UMDF_MD_HOTPATH
104ONIXS_B3_UMDF_MD_PURE
105const Byte*
106toByteBlock(const Type* ptr)
107 noexcept
108{
109 assert(ptr);
110
111 return
112 static_cast
113 <const Byte*>
114 (toOpaquePtr(ptr));
115}
116
119template
120<
121 typename Type
122>
123ONIXS_B3_UMDF_MD_HOTPATH
124ONIXS_B3_UMDF_MD_PURE
125Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
126{
127 assert(pointer);
128
129 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
130}
131
135template
136<
137 typename Type
138>
139ONIXS_B3_UMDF_MD_HOTPATH
140ONIXS_B3_UMDF_MD_PURE
141Type*
142advanceBackByBytes(
143 Type* pointer,
144 ptrdiff_t distance)
145 noexcept
146{
147 assert(pointer);
148
149 return
150 reinterpret_cast<Type*>
151 (
152 toByteBlock(pointer) - distance
153 );
154}
155
158template
159<
160 typename Left,
161 typename Right
162>
163ONIXS_B3_UMDF_MD_HOTPATH
164ONIXS_B3_UMDF_MD_PURE
165ptrdiff_t
166byteDistance(
167 Left* left,
168 Right* right)
169 noexcept
170{
171 assert(left);
172 assert(right);
173
174 return (
175 toByteBlock(left) -
176 toByteBlock(right)
177 );
178}
179
181template
182<
183 class Value
184>
185ONIXS_B3_UMDF_MD_HOTPATH
186Value
187extractValue(const void* location) noexcept
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
204template <unsigned ArgsCount>
205struct CompositeExtractor;
206
208template <>
209struct CompositeExtractor<1>
210{
211 template <class Value>
212 static
213 Value extract(const void* location) noexcept
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
236template <>
237struct CompositeExtractor<2>
238{
239 template <class Value>
240 static
241 Value extract(const void* location) noexcept
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
269template <>
270struct CompositeExtractor<4>
271{
272 template <class Value>
273 static
274 Value extract(const void* location) noexcept
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
310template
311<
312 class Value
313>
314ONIXS_B3_UMDF_MD_HOTPATH
315typename std::enable_if<details::HasMemberTraits<Value>::value, Value>::type
316getValue(const void* location) noexcept
317{
318 assert(location);
319 return CompositeExtractor<Value::MemberTraits::Count>:: template extract<Value>(location);
320}
321
323template
324<
325 class Value
326>
327ONIXS_B3_UMDF_MD_HOTPATH
328typename std::enable_if<!details::HasMemberTraits<Value>::value, Value>::type
329getValue(const void* location) noexcept
330{
331 assert(location);
332 return extractValue<Value>(location);
333}
334
336template
337<
338 class Value
339>
340constexpr
341typename std::enable_if<!details::HasMemberTraits<Value>::value, size_t>::type
342size() noexcept
343{
344 return sizeof(Value);
345}
346
348template
349<
350 class Value
351>
352constexpr
353typename std::enable_if<details::HasMemberTraits<Value>::value, size_t>::type
354size() noexcept
355{
356 return Value::Size;
357}
358
360template
361<
362 class Value
363>
364ONIXS_B3_UMDF_MD_HOTPATH
365typename std::enable_if<!details::HasSerializeMember<Value>::value>::type
366commitValue(void* location, Value value)
367 noexcept
368{
369 assert(location);
370 std::memcpy(
371 location,
372 &value,
373 sizeof(Value));
374}
375
377template
378<
379 class Value
380>
381ONIXS_B3_UMDF_MD_HOTPATH
382typename std::enable_if<details::HasSerializeMember<Value>::value>::type
383commitValue(void* location, Value value)
384 noexcept
385{
386 assert(location);
387 value.serialize(location);
388}
389
391template
392<
393 class Value
394>
395ONIXS_B3_UMDF_MD_HOTPATH
396typename std::enable_if<!details::HasValueStaticMember<Value>::value>::type
397setValue(void* location, Value value)
398 noexcept
399{
400 assert(location);
401 commitValue(location, value);
402}
403
405template
406<
407 class Value
408>
409ONIXS_B3_UMDF_MD_HOTPATH
410typename std::enable_if<details::HasValueStaticMember<Value>::value>::type
411setValue(void* location, Value)
412 noexcept
413{
414 assert(location);
415 commitValue(location, Value::value());
416}
417
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_END
Definition ABI.h:158
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:153
#define CHECK_TYPE_INTEGRAL(Type)
Definition Utils.h:30
UInt64 UInt64
8-byte unsigned integer, from 0 to 18446744073709551615 (2^64-1).
Definition Fields.h:62
UInt16 Word
Alias for Word.
Definition Memory.h:37
UInt64 QWord
Alias for Quad Word.
Definition Memory.h:43
UInt32 DWord
Alias for Double Word.
Definition Memory.h:40
UInt32 UInt32
4-byte unsigned integer, from 0 to 4294967295 (2^32-1).
Definition Fields.h:58
UInt8 UInt8
1-byte unsigned integer, from 0 to 255.
Definition Fields.h:50
UInt16 UInt16
2-byte unsigned integer, from 0 to 65535.
Definition Fields.h:54