OnixS C++ CME MDP Premium Market Data Handler 5.11.1
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#include <memory>
31#include <new>
32
34
36typedef UInt8 Byte;
37
39typedef UInt16 Word;
40
42typedef UInt32 DWord;
43
45typedef UInt64 QWord;
46
48template<class T>
49ONIXS_CMEMDH_FORCEINLINE T* fromExistingBytes(void* storage) noexcept
50{
51 static_assert(IsTriviallyCopyable<T>::value, "T must be trivially copiable");
52 static_assert(IsTriviallyDefaultConstructible<T>::value, "T must be trivially default-constructible");
53
54 assert(storage);
55 assert(reinterpret_cast<std::uintptr_t>(storage) % alignof(T) == 0);
56
57#if defined(ONIXS_CMEMDH_HAS_START_LIFETIME_AS)
58 return std::start_lifetime_as<T>(storage);
59#else
60 return static_cast<T*>(storage);
61#endif
62}
63
65template<class T>
66ONIXS_CMEMDH_FORCEINLINE T* fromEmptyStorage(void* storage) noexcept
67{
68 static_assert(IsTriviallyCopyable<T>::value, "T must be trivially copiable");
69 static_assert(IsTriviallyDefaultConstructible<T>::value, "T must be trivially default-constructible");
70
71 assert(storage);
72 assert(reinterpret_cast<std::uintptr_t>(storage) % alignof(T) == 0);
73
74 return ::new (storage) T;
75}
76
79template <typename Type>
80ONIXS_CMEMDH_FORCEINLINE void* toOpaquePtr(Type* ptr) noexcept
81{
82 assert(ptr);
83 return static_cast<void*>(ptr);
84}
85
88template <typename Type>
89ONIXS_CMEMDH_FORCEINLINE const void* toOpaquePtr(const Type* ptr) noexcept
90{
91 assert(ptr);
92 return static_cast<const void*>(ptr);
93}
94
96template <typename Type>
97ONIXS_CMEMDH_FORCEINLINE void* toMutable(const Type* ptr) noexcept
98{
99 assert(ptr);
100 return const_cast<void*>(static_cast<const void*>(ptr));
101}
102
104template <typename Type>
105ONIXS_CMEMDH_FORCEINLINE Type* toMutableType(const Type* ptr) noexcept
106{
107 assert(ptr);
108 return const_cast<Type*>(ptr);
109}
110
113template <typename Type>
114ONIXS_CMEMDH_FORCEINLINE Byte* toByteBlock(Type* ptr) noexcept
115{
116 assert(ptr);
117
118 return static_cast<Byte*>(toOpaquePtr(ptr));
119}
120
123template <typename Type>
124ONIXS_CMEMDH_FORCEINLINE const Byte* toByteBlock(const Type* ptr) noexcept
125{
126 assert(ptr);
127
128 return static_cast<const Byte*>(toOpaquePtr(ptr));
129}
130
133template <typename Type>
134ONIXS_CMEMDH_FORCEINLINE Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
135{
136 assert(pointer);
137
138 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
139}
140
143template <typename Type>
144ONIXS_CMEMDH_FORCEINLINE const Type* advanceByBytes(const Type* pointer, ptrdiff_t distance) noexcept
145{
146 assert(pointer);
147
148 return reinterpret_cast<const Type*>(toByteBlock(pointer) + distance);
149}
150
154template <typename Type>
155ONIXS_CMEMDH_FORCEINLINE Type* advanceBackByBytes(Type* pointer, ptrdiff_t distance) noexcept
156{
157 assert(pointer);
158
159 return reinterpret_cast<Type*>(toByteBlock(pointer) - distance);
160}
161
164template <typename Left, typename Right>
165ONIXS_CMEMDH_FORCEINLINE ptrdiff_t byteDistance(Left* left, Right* right) noexcept
166{
167 assert(left);
168 assert(right);
169
170 return (toByteBlock(left) - toByteBlock(right));
171}
172
174template <class Value>
175ONIXS_CMEMDH_FORCEINLINE Value extractValue(const void* location) noexcept
176{
177 assert(location);
178
180
181 Value result;
182
183 std::memcpy(&result, location, sizeof(Value));
184
185 return result;
186}
187
189template <unsigned ArgsCount>
190struct CompositeExtractor;
191
193template <>
194struct CompositeExtractor<1>
195{
196 template <class Value>
198 static Value extract(const void* location) noexcept
199 {
200 static_assert(noexcept(Value(std::declval<typename Value::MemberTraits::FirstArgType>())), "must be noexcept");
201
202 assert(location);
203
204 return Value(extractValue<typename Value::MemberTraits::FirstArgType>(location));
205 }
206};
207
209template <>
210struct CompositeExtractor<2>
211{
212 template <class Value>
214 static Value extract(const void* location) noexcept
215 {
216 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
217 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
218
219 static_assert(noexcept(Value(std::declval<FirstArgType>(), std::declval<SecondArgType>())), "must be noexcept");
220
221 assert(location);
222
223 return Value(
224 extractValue<FirstArgType>(location),
225 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType)))
226 );
227 }
228};
229
231template <>
232struct CompositeExtractor<4>
233{
234 template <class Value>
236 static Value extract(const void* location) noexcept
237 {
238 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
239 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
240 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
241 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
242
243 static_assert(
244 noexcept(Value(
245 std::declval<FirstArgType>(),
246 std::declval<SecondArgType>(),
247 std::declval<ThirdArgType>(),
248 std::declval<FourthArgType>()
249 )),
250 "must be noexcept"
251 );
252
253 assert(location);
254
255 return Value(
256 extractValue<FirstArgType>(location),
257 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
258 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
259 extractValue<FourthArgType>(
260 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType))
261 )
262 );
263 }
264};
265
267template <class Value>
268ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasMemberTraits<Value>::value, Value>::type
269getValue(const void* location) noexcept
270{
271 assert(location);
272 return CompositeExtractor<Value::MemberTraits::Count>::template extract<Value>(location);
273}
274
276template <class Value>
277ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasMemberTraits<Value>::value, Value>::type
278getValue(const void* location) noexcept
279{
280 assert(location);
281 return extractValue<Value>(location);
282}
283
285template <class Value>
287constexpr typename std::enable_if<!details::HasMemberTraits<Value>::value, size_t>::type size() noexcept
288{
289 return sizeof(Value);
290}
291
293template <class Value>
295constexpr typename std::enable_if<details::HasMemberTraits<Value>::value, size_t>::type size() noexcept
296{
297 return Value::Size;
298}
299
301template <class Value>
302ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasSerializeMember<Value>::value>::type
303commitValue(void* location, Value value) noexcept
304{
305 assert(location);
306 std::memcpy(location, &value, sizeof(Value));
307}
308
310template <class Value>
311ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasSerializeMember<Value>::value>::type
312commitValue(void* location, Value value) noexcept
313{
314 assert(location);
315 value.serialize(location);
316}
317
319template <class Value>
320ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasValueStaticMember<Value>::value>::type
321setValue(void* location, Value value) noexcept
322{
323 assert(location);
324 commitValue(location, value);
325}
326
328template <class Value>
329ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasValueStaticMember<Value>::value>::type
330setValue(void* location, Value) noexcept
331{
332 assert(location);
333 commitValue(location, Value::value());
334}
335
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:77
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:78
#define ONIXS_CMEMDH_FORCEINLINE
Definition Compiler.h:176
#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:39
UInt64 QWord
Alias for Quad Word.
Definition Memory.h:45
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:42
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:36