OnixS C++ CME MDP Premium Market Data Handler 5.10.2
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 <typename Type>
48ONIXS_CMEMDH_FORCEINLINE void* toOpaquePtr(Type* ptr) noexcept
49{
50 assert(ptr);
51 return static_cast<void*>(ptr);
52}
53
56template <typename Type>
57ONIXS_CMEMDH_FORCEINLINE const void* toOpaquePtr(const Type* ptr) noexcept
58{
59 assert(ptr);
60 return static_cast<const void*>(ptr);
61}
62
64template <typename Type>
65ONIXS_CMEMDH_FORCEINLINE void* toMutable(const Type* ptr) noexcept
66{
67 assert(ptr);
68 return const_cast<void*>(static_cast<const void*>(ptr));
69}
70
73template <typename Type>
74ONIXS_CMEMDH_FORCEINLINE Byte* toByteBlock(Type* ptr) noexcept
75{
76 assert(ptr);
77
78 return static_cast<Byte*>(toOpaquePtr(ptr));
79}
80
83template <typename Type>
84ONIXS_CMEMDH_FORCEINLINE const Byte* toByteBlock(const Type* ptr) noexcept
85{
86 assert(ptr);
87
88 return static_cast<const Byte*>(toOpaquePtr(ptr));
89}
90
93template <typename Type>
94ONIXS_CMEMDH_FORCEINLINE Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
95{
96 assert(pointer);
97
98 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
99}
100
103template <typename Type>
104ONIXS_CMEMDH_FORCEINLINE const Type* advanceByBytes(const Type* pointer, ptrdiff_t distance) noexcept
105{
106 assert(pointer);
107
108 return reinterpret_cast<const Type*>(toByteBlock(pointer) + distance);
109}
110
114template <typename Type>
115ONIXS_CMEMDH_FORCEINLINE Type* advanceBackByBytes(Type* pointer, ptrdiff_t distance) noexcept
116{
117 assert(pointer);
118
119 return reinterpret_cast<Type*>(toByteBlock(pointer) - distance);
120}
121
124template <typename Left, typename Right>
125ONIXS_CMEMDH_FORCEINLINE ptrdiff_t byteDistance(Left* left, Right* right) noexcept
126{
127 assert(left);
128 assert(right);
129
130 return (toByteBlock(left) - toByteBlock(right));
131}
132
134template <class Value>
135ONIXS_CMEMDH_FORCEINLINE Value extractValue(const void* location) noexcept
136{
137 assert(location);
138
140
141 Value result;
142
143 std::memcpy(&result, location, sizeof(Value));
144
145 return result;
146}
147
149template <unsigned ArgsCount>
150struct CompositeExtractor;
151
153template <>
154struct CompositeExtractor<1>
155{
156 template <class Value>
158 static Value extract(const void* location) noexcept
159 {
160 static_assert(noexcept(Value(std::declval<typename Value::MemberTraits::FirstArgType>())), "must be noexcept");
161
162 assert(location);
163
164 return Value(extractValue<typename Value::MemberTraits::FirstArgType>(location));
165 }
166};
167
169template <>
170struct CompositeExtractor<2>
171{
172 template <class Value>
174 static Value extract(const void* location) noexcept
175 {
176 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
177 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
178
179 static_assert(noexcept(Value(std::declval<FirstArgType>(), std::declval<SecondArgType>())), "must be noexcept");
180
181 assert(location);
182
183 return Value(
184 extractValue<FirstArgType>(location),
185 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType)))
186 );
187 }
188};
189
191template <>
192struct CompositeExtractor<4>
193{
194 template <class Value>
196 static Value extract(const void* location) noexcept
197 {
198 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
199 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
200 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
201 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
202
203 static_assert(
204 noexcept(Value(
205 std::declval<FirstArgType>(),
206 std::declval<SecondArgType>(),
207 std::declval<ThirdArgType>(),
208 std::declval<FourthArgType>()
209 )),
210 "must be noexcept"
211 );
212
213 assert(location);
214
215 return Value(
216 extractValue<FirstArgType>(location),
217 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
218 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
219 extractValue<FourthArgType>(
220 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType))
221 )
222 );
223 }
224};
225
227template <class Value>
228ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasMemberTraits<Value>::value, Value>::type
229getValue(const void* location) noexcept
230{
231 assert(location);
232 return CompositeExtractor<Value::MemberTraits::Count>::template extract<Value>(location);
233}
234
236template <class Value>
237ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasMemberTraits<Value>::value, Value>::type
238getValue(const void* location) noexcept
239{
240 assert(location);
241 return extractValue<Value>(location);
242}
243
245template <class Value>
247constexpr typename std::enable_if<!details::HasMemberTraits<Value>::value, size_t>::type size() noexcept
248{
249 return sizeof(Value);
250}
251
253template <class Value>
255constexpr typename std::enable_if<details::HasMemberTraits<Value>::value, size_t>::type size() noexcept
256{
257 return Value::Size;
258}
259
261template <class Value>
262ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasSerializeMember<Value>::value>::type
263commitValue(void* location, Value value) noexcept
264{
265 assert(location);
266 std::memcpy(location, &value, sizeof(Value));
267}
268
270template <class Value>
271ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasSerializeMember<Value>::value>::type
272commitValue(void* location, Value value) noexcept
273{
274 assert(location);
275 value.serialize(location);
276}
277
279template <class Value>
280ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasValueStaticMember<Value>::value>::type
281setValue(void* location, Value value) noexcept
282{
283 assert(location);
284 commitValue(location, value);
285}
286
288template <class Value>
289ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasValueStaticMember<Value>::value>::type
290setValue(void* location, Value) noexcept
291{
292 assert(location);
293 commitValue(location, Value::value());
294}
295
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:57
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:58
#define ONIXS_CMEMDH_FORCEINLINE
Definition Compiler.h:161
#define ONIXS_CMEMDH_CHECK_TYPE_INTEGRAL(Type)
Definition Utils.h:29
bool value(Number &number, const MultiContainer &container, Tag tag)
UInt16 Word
Alias for Word.
Definition Memory.h:37
UInt64 QWord
Alias for Quad Word.
Definition Memory.h:43
std::uint32_t UInt32
uInt32.
Definition Integral.h:35
std::uint16_t UInt16
uInt16.
Definition Integral.h:33
UInt32 DWord
Alias for Double Word.
Definition Memory.h:40
std::uint64_t UInt64
uInt64.
Definition Integral.h:37
std::uint8_t UInt8
uInt8.
Definition Integral.h:31
UInt8 Byte
Alias for Byte.
Definition Memory.h:34