OnixS C++ CME MDP Premium Market Data Handler 5.10.3
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
72template <typename Type>
73ONIXS_CMEMDH_FORCEINLINE Type* toMutableType(const Type* ptr) noexcept
74{
75 assert(ptr);
76 return const_cast<Type*>(ptr);
77}
78
81template <typename Type>
82ONIXS_CMEMDH_FORCEINLINE Byte* toByteBlock(Type* ptr) noexcept
83{
84 assert(ptr);
85
86 return static_cast<Byte*>(toOpaquePtr(ptr));
87}
88
91template <typename Type>
92ONIXS_CMEMDH_FORCEINLINE const Byte* toByteBlock(const Type* ptr) noexcept
93{
94 assert(ptr);
95
96 return static_cast<const Byte*>(toOpaquePtr(ptr));
97}
98
101template <typename Type>
102ONIXS_CMEMDH_FORCEINLINE Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
103{
104 assert(pointer);
105
106 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
107}
108
111template <typename Type>
112ONIXS_CMEMDH_FORCEINLINE const Type* advanceByBytes(const Type* pointer, ptrdiff_t distance) noexcept
113{
114 assert(pointer);
115
116 return reinterpret_cast<const Type*>(toByteBlock(pointer) + distance);
117}
118
122template <typename Type>
123ONIXS_CMEMDH_FORCEINLINE Type* advanceBackByBytes(Type* pointer, ptrdiff_t distance) noexcept
124{
125 assert(pointer);
126
127 return reinterpret_cast<Type*>(toByteBlock(pointer) - distance);
128}
129
132template <typename Left, typename Right>
133ONIXS_CMEMDH_FORCEINLINE ptrdiff_t byteDistance(Left* left, Right* right) noexcept
134{
135 assert(left);
136 assert(right);
137
138 return (toByteBlock(left) - toByteBlock(right));
139}
140
142template <class Value>
143ONIXS_CMEMDH_FORCEINLINE Value extractValue(const void* location) noexcept
144{
145 assert(location);
146
148
149 Value result;
150
151 std::memcpy(&result, location, sizeof(Value));
152
153 return result;
154}
155
157template <unsigned ArgsCount>
158struct CompositeExtractor;
159
161template <>
162struct CompositeExtractor<1>
163{
164 template <class Value>
166 static Value extract(const void* location) noexcept
167 {
168 static_assert(noexcept(Value(std::declval<typename Value::MemberTraits::FirstArgType>())), "must be noexcept");
169
170 assert(location);
171
172 return Value(extractValue<typename Value::MemberTraits::FirstArgType>(location));
173 }
174};
175
177template <>
178struct CompositeExtractor<2>
179{
180 template <class Value>
182 static Value extract(const void* location) noexcept
183 {
184 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
185 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
186
187 static_assert(noexcept(Value(std::declval<FirstArgType>(), std::declval<SecondArgType>())), "must be noexcept");
188
189 assert(location);
190
191 return Value(
192 extractValue<FirstArgType>(location),
193 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType)))
194 );
195 }
196};
197
199template <>
200struct CompositeExtractor<4>
201{
202 template <class Value>
204 static Value extract(const void* location) noexcept
205 {
206 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
207 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
208 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
209 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
210
211 static_assert(
212 noexcept(Value(
213 std::declval<FirstArgType>(),
214 std::declval<SecondArgType>(),
215 std::declval<ThirdArgType>(),
216 std::declval<FourthArgType>()
217 )),
218 "must be noexcept"
219 );
220
221 assert(location);
222
223 return Value(
224 extractValue<FirstArgType>(location),
225 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
226 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
227 extractValue<FourthArgType>(
228 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType))
229 )
230 );
231 }
232};
233
235template <class Value>
236ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasMemberTraits<Value>::value, Value>::type
237getValue(const void* location) noexcept
238{
239 assert(location);
240 return CompositeExtractor<Value::MemberTraits::Count>::template extract<Value>(location);
241}
242
244template <class Value>
245ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasMemberTraits<Value>::value, Value>::type
246getValue(const void* location) noexcept
247{
248 assert(location);
249 return extractValue<Value>(location);
250}
251
253template <class Value>
255constexpr typename std::enable_if<!details::HasMemberTraits<Value>::value, size_t>::type size() noexcept
256{
257 return sizeof(Value);
258}
259
261template <class Value>
263constexpr typename std::enable_if<details::HasMemberTraits<Value>::value, size_t>::type size() noexcept
264{
265 return Value::Size;
266}
267
269template <class Value>
270ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasSerializeMember<Value>::value>::type
271commitValue(void* location, Value value) noexcept
272{
273 assert(location);
274 std::memcpy(location, &value, sizeof(Value));
275}
276
278template <class Value>
279ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasSerializeMember<Value>::value>::type
280commitValue(void* location, Value value) noexcept
281{
282 assert(location);
283 value.serialize(location);
284}
285
287template <class Value>
288ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<!details::HasValueStaticMember<Value>::value>::type
289setValue(void* location, Value value) noexcept
290{
291 assert(location);
292 commitValue(location, value);
293}
294
296template <class Value>
297ONIXS_CMEMDH_FORCEINLINE typename std::enable_if<details::HasValueStaticMember<Value>::value>::type
298setValue(void* location, Value) noexcept
299{
300 assert(location);
301 commitValue(location, Value::value());
302}
303
#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