OnixS C++ ICE Binary Order Entry Handler 1.2.3
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 <iterator>
31#include <memory>
32#include <type_traits>
33
35
37typedef UInt8 Byte;
38
40typedef UInt16 Word;
41
43typedef UInt32 DWord;
44
46typedef UInt64 QWord;
47
49
50template<typename Type>
53ONIXS_ICEBOE_FORCEINLINE
54void* toOpaquePtr(Type* ptr) noexcept
55{
56 assert(ptr);
57 return static_cast<void*>(ptr);
58}
59
61
62template <typename Type>
65ONIXS_ICEBOE_FORCEINLINE
66const void* toOpaquePtr(const Type* ptr) noexcept
67{
68 assert(ptr);
69 return static_cast<const void*>(ptr);
70}
71
72#if defined(__cpp_lib_to_address) && defined(__cpp_lib_concepts)
76
77template <std::contiguous_iterator Iterator>
78 requires (!std::is_pointer_v<Iterator>)
81ONIXS_ICEBOE_FORCEINLINE
82const void* toOpaquePtr(Iterator it) noexcept
83{
84 return toOpaquePtr(std::to_address(it));
85}
86#endif
87
88
89template <typename Type>
92ONIXS_ICEBOE_FORCEINLINE
93void* toMutable(const Type* ptr) noexcept
94{
95 assert(ptr);
96 return const_cast<void*>(static_cast<const void*>(ptr));
97}
98
100
101template<typename Type>
104ONIXS_ICEBOE_FORCEINLINE
105Byte* toByteBlock(Type* ptr) noexcept
106{
107 assert(ptr);
108 return static_cast<Byte*>(toOpaquePtr(ptr));
109}
110
112
113template<typename Type>
116ONIXS_ICEBOE_FORCEINLINE
117const Byte* toByteBlock(const Type* ptr) noexcept
118{
119 assert(ptr);
120 return static_cast<const Byte*>(toOpaquePtr(ptr));
121}
122
124
125template <typename Type>
128ONIXS_ICEBOE_FORCEINLINE
129Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
130{
131 assert(pointer);
132 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
133}
134
137
138template<typename Type>
141ONIXS_ICEBOE_FORCEINLINE
142Type* advanceBackByBytes(Type* pointer, ptrdiff_t distance) noexcept
143{
144 assert(pointer);
145 return reinterpret_cast<Type*>(toByteBlock(pointer) - distance);
146}
147
149
150template<typename Left, typename Right>
153ONIXS_ICEBOE_FORCEINLINE
154ptrdiff_t byteDistance(Left* left, Right* right) noexcept
155{
156 assert(left);
157 assert(right);
158
159 return (toByteBlock(left) - toByteBlock(right));
160}
161
162
163template<class Value>
164ONIXS_ICEBOE_FORCEINLINE
165Value extractValue(const void* location) noexcept
166{
167 assert(location);
168
169 CHECK_TYPE_INTEGRAL(Value);
170
171 Value result;
172 std::memcpy(&result, location, sizeof(Value));
173 return result;
174}
175
176
177template <unsigned ArgsCount> struct CompositeExtractor;
178
179
180template <>
182{
183 template <class Value>
184 ONIXS_ICEBOE_FORCEINLINE
185 static std::tuple<typename Value::MemberTraits::FirstArgType>
186 extractAsTuple(const void* location) noexcept
187 {
188 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
189
190 static_assert(std::is_nothrow_constructible<Value, FirstArgType>::value,
191 "Value must be nothrow-constructible from its member types");
192
193 assert(location);
194
195 return std::tuple<FirstArgType>(extractValue<FirstArgType>(location));
196 }
197};
198
199
200template <>
202{
203 template <class Value>
204 static std::tuple
205 <
206 typename Value::MemberTraits::FirstArgType,
207 typename Value::MemberTraits::SecondArgType
208 >
209 ONIXS_ICEBOE_FORCEINLINE
210 extractAsTuple(const void* location) noexcept
211 {
212 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
213 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
214
215 static_assert(std::is_nothrow_constructible<
216 Value,
217 FirstArgType, SecondArgType>::value,
218 "Value must be nothrow-constructible from its member types");
219
220 assert(location);
221
222 return std::tuple<FirstArgType, SecondArgType>(
224 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType)))
225 );
226 }
227};
228
229
230template <>
232{
233 template <class Value>
234 ONIXS_ICEBOE_FORCEINLINE
235 static std::tuple
236 <
237 typename Value::MemberTraits::FirstArgType,
238 typename Value::MemberTraits::SecondArgType,
239 typename Value::MemberTraits::ThirdArgType
240 >
241 extractAsTuple(const void* location) noexcept
242 {
243 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
244 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
245 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
246
247 static_assert(std::is_nothrow_constructible<
248 Value,
249 FirstArgType, SecondArgType, ThirdArgType>::value,
250 "Value must be nothrow-constructible from its member types");
251
252 assert(location);
253
254 return std::tuple<FirstArgType, SecondArgType, ThirdArgType>(
256 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
257 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType)))
258 );
259 }
260};
261
262
263
264template <>
266{
267 template <class Value>
268 ONIXS_ICEBOE_FORCEINLINE
269 static std::tuple
270 <
271 typename Value::MemberTraits::FirstArgType,
272 typename Value::MemberTraits::SecondArgType,
273 typename Value::MemberTraits::ThirdArgType,
274 typename Value::MemberTraits::FourthArgType
275 >
276 extractAsTuple(const void* location) noexcept
277 {
278 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
279 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
280 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
281 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
282
283 static_assert(std::is_nothrow_constructible<
284 Value,
285 FirstArgType, SecondArgType, ThirdArgType, FourthArgType>::value,
286 "Value must be nothrow-constructible from its member types");
287
288 assert(location);
289
290 return std::tuple<FirstArgType, SecondArgType, ThirdArgType, FourthArgType>(
292 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
293 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
294 extractValue<FourthArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType)))
295 );
296 }
297};
298
299
300template <class Value>
301ONIXS_ICEBOE_FORCEINLINE
302typename std::enable_if<details::HasMemberTraits<Value>::value, Value>::type
303getValue(const void* location) noexcept(noexcept(makeFromTuple<Value>(CompositeExtractor<Value::MemberTraits::Count>:: template extractAsTuple<Value>(location))))
304{
305 assert(location);
306 return makeFromTuple<Value>(CompositeExtractor<Value::MemberTraits::Count>:: template extractAsTuple<Value>(location));
307}
308
309
310template <class Value>
311ONIXS_ICEBOE_FORCEINLINE
312typename std::enable_if<!details::HasMemberTraits<Value>::value, Value>::type
313getValue(const void* location) noexcept
314{
315 assert(location);
316 return extractValue<Value>(location);
317}
318
319
320template <class Value>
321constexpr typename std::enable_if<!details::HasMemberTraits<Value>::value, size_t>::type
322size() noexcept
323{
324 return sizeof(cleanType<Value>);
325}
326
327
328template <class Value>
329constexpr typename std::enable_if<details::HasMemberTraits<Value>::value, size_t>::type
330size() noexcept
331{
332 return Value::Size;
333}
334
335
336template <class Value>
337ONIXS_ICEBOE_FORCEINLINE
338typename std::enable_if<!details::HasSerializeMember<Value>::value>::type
339commitValue(void* location, Value value) noexcept
340{
341 assert(location);
342 static_assert(details::smallType<Value>::value, ONIXS_ICEBOE_TO_STR(Value));
343 std::memcpy(location, &value, sizeof(Value));
344}
345
346
347template <class Value>
349typename std::enable_if<details::HasSerializeMember<Value>::value>::type
350ONIXS_ICEBOE_FORCEINLINE
351commitValue(void* location, Value value) noexcept
352{
353 assert(location);
354 value.serialize(location);
355}
356
357
358template <class Value>
359ONIXS_ICEBOE_FORCEINLINE
360typename std::enable_if<!details::HasValueStaticMember<Value>::value>::type
361setValue(void* location, Value value) noexcept
362{
363 assert(location);
364 commitValue(location, value);
365}
366
367
368template <class Value>
369ONIXS_ICEBOE_FORCEINLINE
370typename std::enable_if<details::HasValueStaticMember<Value>::value>::type
371setValue(void* location, Value) noexcept
372{
373 assert(location);
374 commitValue(location, Value::value());
375}
376
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:102
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_END
Definition ABI.h:106
#define ONIXS_ICEBOE_HOTPATH
Definition Compiler.h:156
#define ONIXS_ICEBOE_PURE
Definition Compiler.h:158
ONIXS_ICEBOE_FORCEINLINE std::enable_if< details::HasMemberTraits< Value >::value, Value >::type getValue(const void *location) noexcept(noexcept(makeFromTuple< Value >(CompositeExtractor< Value::MemberTraits::Count >::template extractAsTuple< Value >(location))))
Definition Memory.h:303
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:322
ONIXS_ICEBOE_FORCEINLINE std::enable_if<!details::HasValueStaticMember< Value >::value >::type setValue(void *location, Value value) noexcept
Definition Memory.h:361
UInt16 Word
Alias for Word.
Definition Memory.h:40
UInt64 QWord
Alias for Quad Word.
Definition Memory.h:46
ONIXS_ICEBOE_FORCEINLINE void * toOpaquePtr(Type *ptr) noexcept
Makes the pointer an opaque one.
Definition Memory.h:54
ONIXS_ICEBOE_FORCEINLINE void * toMutable(const Type *ptr) noexcept
Definition Memory.h:93
ONIXS_ICEBOE_FORCEINLINE Value extractValue(const void *location) noexcept
Definition Memory.h:165
ONIXS_ICEBOE_FORCEINLINE Type * advanceByBytes(Type *pointer, ptrdiff_t distance) noexcept
Advances the pointer to a given offset (distance) in bytes.
Definition Memory.h:129
UInt32 DWord
Alias for Double Word.
Definition Memory.h:43
ONIXS_ICEBOE_FORCEINLINE Byte * toByteBlock(Type *ptr) noexcept
Reinterprets the pointer as a byte block one.
Definition Memory.h:105
ONIXS_ICEBOE_FORCEINLINE ptrdiff_t byteDistance(Left *left, Right *right) noexcept
Definition Memory.h:154
ONIXS_ICEBOE_FORCEINLINE Type * advanceBackByBytes(Type *pointer, ptrdiff_t distance) noexcept
Definition Memory.h:142
ONIXS_ICEBOE_FORCEINLINE std::enable_if<!details::HasSerializeMember< Value >::value >::type commitValue(void *location, Value value) noexcept
Definition Memory.h:339
UInt8 Byte
Alias for Byte.
Definition Memory.h:37
static ONIXS_ICEBOE_FORCEINLINE std::tuple< typename Value::MemberTraits::FirstArgType > extractAsTuple(const void *location) noexcept
Definition Memory.h:186
static std::tuple< typename Value::MemberTraits::FirstArgType, typename Value::MemberTraits::SecondArgType > ONIXS_ICEBOE_FORCEINLINE extractAsTuple(const void *location) noexcept
Definition Memory.h:210
static ONIXS_ICEBOE_FORCEINLINE std::tuple< typename Value::MemberTraits::FirstArgType, typename Value::MemberTraits::SecondArgType, typename Value::MemberTraits::ThirdArgType > extractAsTuple(const void *location) noexcept
Definition Memory.h:241
static ONIXS_ICEBOE_FORCEINLINE std::tuple< typename Value::MemberTraits::FirstArgType, typename Value::MemberTraits::SecondArgType, typename Value::MemberTraits::ThirdArgType, typename Value::MemberTraits::FourthArgType > extractAsTuple(const void *location) noexcept
Definition Memory.h:276