OnixS C++ ICE Binary Order Entry Handler 1.0.0
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
46
47template<typename Type>
50ONIXS_ICEBOE_FORCEINLINE
51void* toOpaquePtr(Type* ptr) noexcept
52{
53 assert(ptr);
54 return static_cast<void*>(ptr);
55}
56
58
59template <typename Type>
62ONIXS_ICEBOE_FORCEINLINE
63const void* toOpaquePtr(const Type* ptr) noexcept
64{
65 assert(ptr);
66 return static_cast<const void*>(ptr);
67}
68
69
70template <typename Type>
73ONIXS_ICEBOE_FORCEINLINE
74void* toMutable(const Type* ptr) noexcept
75{
76 assert(ptr);
77 return const_cast<void*>(static_cast<const void*>(ptr));
78}
79
81
82template<typename Type>
85ONIXS_ICEBOE_FORCEINLINE
86Byte* toByteBlock(Type* ptr) noexcept
87{
88 assert(ptr);
89 return static_cast<Byte*>(toOpaquePtr(ptr));
90}
91
93
94template<typename Type>
97ONIXS_ICEBOE_FORCEINLINE
98const Byte* toByteBlock(const Type* ptr) noexcept
99{
100 assert(ptr);
101 return static_cast<const Byte*>(toOpaquePtr(ptr));
102}
103
105
106template <typename Type>
109ONIXS_ICEBOE_FORCEINLINE
110Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
111{
112 assert(pointer);
113 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
114}
115
118
119template<typename Type>
122ONIXS_ICEBOE_FORCEINLINE
123Type* advanceBackByBytes(Type* pointer, ptrdiff_t distance) noexcept
124{
125 assert(pointer);
126 return reinterpret_cast<Type*>(toByteBlock(pointer) - distance);
127}
128
130
131template<typename Left, typename Right>
134ONIXS_ICEBOE_FORCEINLINE
135ptrdiff_t byteDistance(Left* left, Right* right) noexcept
136{
137 assert(left);
138 assert(right);
139
140 return (toByteBlock(left) - toByteBlock(right));
141}
142
143
144template<class Value>
145ONIXS_ICEBOE_FORCEINLINE
146Value extractValue(const void* location) noexcept
147{
148 assert(location);
149
150 CHECK_TYPE_INTEGRAL(Value);
151
152 Value result;
153 std::memcpy(&result, location, sizeof(Value));
154 return result;
155}
156
157
158template <unsigned ArgsCount> struct CompositeExtractor;
159
160
161template <>
163{
164 template <class Value>
165 ONIXS_ICEBOE_FORCEINLINE
166 static std::tuple<typename Value::MemberTraits::FirstArgType>
167 extractAsTuple(const void* location) noexcept
168 {
169 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
170
171 static_assert(std::is_nothrow_constructible<Value, FirstArgType>::value,
172 "Value must be nothrow-constructible from its member types");
173
174 assert(location);
175
176 return std::tuple<FirstArgType>(extractValue<FirstArgType>(location));
177 }
178};
179
180
181template <>
183{
184 template <class Value>
185 static std::tuple
186 <
187 typename Value::MemberTraits::FirstArgType,
188 typename Value::MemberTraits::SecondArgType
189 >
190 ONIXS_ICEBOE_FORCEINLINE
191 extractAsTuple(const void* location) noexcept
192 {
193 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
194 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
195
196 static_assert(std::is_nothrow_constructible<
197 Value,
198 FirstArgType, SecondArgType>::value,
199 "Value must be nothrow-constructible from its member types");
200
201 assert(location);
202
203 return std::tuple<FirstArgType, SecondArgType>(
205 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType)))
206 );
207 }
208};
209
210
211template <>
213{
214 template <class Value>
215 ONIXS_ICEBOE_FORCEINLINE
216 static std::tuple
217 <
218 typename Value::MemberTraits::FirstArgType,
219 typename Value::MemberTraits::SecondArgType,
220 typename Value::MemberTraits::ThirdArgType
221 >
222 extractAsTuple(const void* location) noexcept
223 {
224 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
225 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
226 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
227
228 static_assert(std::is_nothrow_constructible<
229 Value,
230 FirstArgType, SecondArgType, ThirdArgType>::value,
231 "Value must be nothrow-constructible from its member types");
232
233 assert(location);
234
235 return std::tuple<FirstArgType, SecondArgType, ThirdArgType>(
237 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
238 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType)))
239 );
240 }
241};
242
243
244
245template <>
247{
248 template <class Value>
249 ONIXS_ICEBOE_FORCEINLINE
250 static std::tuple
251 <
252 typename Value::MemberTraits::FirstArgType,
253 typename Value::MemberTraits::SecondArgType,
254 typename Value::MemberTraits::ThirdArgType,
255 typename Value::MemberTraits::FourthArgType
256 >
257 extractAsTuple(const void* location) noexcept
258 {
259 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
260 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
261 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
262 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
263
264 static_assert(std::is_nothrow_constructible<
265 Value,
266 FirstArgType, SecondArgType, ThirdArgType, FourthArgType>::value,
267 "Value must be nothrow-constructible from its member types");
268
269 assert(location);
270
271 return std::tuple<FirstArgType, SecondArgType, ThirdArgType, FourthArgType>(
273 extractValue<SecondArgType>(advanceByBytes(location, sizeof(FirstArgType))),
274 extractValue<ThirdArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
275 extractValue<FourthArgType>(advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType)))
276 );
277 }
278};
279
280
281template <class Value>
282ONIXS_ICEBOE_FORCEINLINE
283typename std::enable_if<details::HasMemberTraits<Value>::value, Value>::type
284getValue(const void* location) noexcept(noexcept(makeFromTuple<Value>(CompositeExtractor<Value::MemberTraits::Count>:: template extractAsTuple<Value>(location))))
285{
286 assert(location);
287 return makeFromTuple<Value>(CompositeExtractor<Value::MemberTraits::Count>:: template extractAsTuple<Value>(location));
288}
289
290
291template <class Value>
292ONIXS_ICEBOE_FORCEINLINE
293typename std::enable_if<!details::HasMemberTraits<Value>::value, Value>::type
294getValue(const void* location) noexcept
295{
296 assert(location);
297 return extractValue<Value>(location);
298}
299
300
301template <class Value>
302constexpr typename std::enable_if<!details::HasMemberTraits<Value>::value, size_t>::type
303size() noexcept
304{
305 return sizeof(cleanType<Value>);
306}
307
308
309template <class Value>
310constexpr typename std::enable_if<details::HasMemberTraits<Value>::value, size_t>::type
311size() noexcept
312{
313 return Value::Size;
314}
315
316
317template <class Value>
318ONIXS_ICEBOE_FORCEINLINE
319typename std::enable_if<!details::HasSerializeMember<Value>::value>::type
320commitValue(void* location, Value value) noexcept
321{
322 assert(location);
323 static_assert(details::smallType<Value>::value, ONIXS_ICEBOE_TO_STR(Value));
324 std::memcpy(location, &value, sizeof(Value));
325}
326
327
328template <class Value>
330typename std::enable_if<details::HasSerializeMember<Value>::value>::type
331ONIXS_ICEBOE_FORCEINLINE
332commitValue(void* location, Value value) noexcept
333{
334 assert(location);
335 value.serialize(location);
336}
337
338
339template <class Value>
340ONIXS_ICEBOE_FORCEINLINE
341typename std::enable_if<!details::HasValueStaticMember<Value>::value>::type
342setValue(void* location, Value value) noexcept
343{
344 assert(location);
345 commitValue(location, value);
346}
347
348
349template <class Value>
350ONIXS_ICEBOE_FORCEINLINE
351typename std::enable_if<details::HasValueStaticMember<Value>::value>::type
352setValue(void* location, Value) noexcept
353{
354 assert(location);
355 commitValue(location, Value::value());
356}
357
#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:155
#define ONIXS_ICEBOE_PURE
Definition Compiler.h:157
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:284
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
ONIXS_ICEBOE_FORCEINLINE std::enable_if<!details::HasValueStaticMember< Value >::value >::type setValue(void *location, Value value) noexcept
Definition Memory.h:342
UInt16 Word
Alias for Word.
Definition Memory.h:37
UInt64 QWord
Alias for Quad Word.
Definition Memory.h:43
ONIXS_ICEBOE_FORCEINLINE void * toOpaquePtr(Type *ptr) noexcept
Makes the pointer an opaque one.
Definition Memory.h:51
ONIXS_ICEBOE_FORCEINLINE void * toMutable(const Type *ptr) noexcept
Definition Memory.h:74
ONIXS_ICEBOE_FORCEINLINE Value extractValue(const void *location) noexcept
Definition Memory.h:146
ONIXS_ICEBOE_FORCEINLINE Type * advanceByBytes(Type *pointer, ptrdiff_t distance) noexcept
Advances the pointer to a given offset (distance) in bytes.
Definition Memory.h:110
UInt32 DWord
Alias for Double Word.
Definition Memory.h:40
ONIXS_ICEBOE_FORCEINLINE Byte * toByteBlock(Type *ptr) noexcept
Reinterprets the pointer as a byte block one.
Definition Memory.h:86
ONIXS_ICEBOE_FORCEINLINE ptrdiff_t byteDistance(Left *left, Right *right) noexcept
Definition Memory.h:135
ONIXS_ICEBOE_FORCEINLINE Type * advanceBackByBytes(Type *pointer, ptrdiff_t distance) noexcept
Definition Memory.h:123
ONIXS_ICEBOE_FORCEINLINE std::enable_if<!details::HasSerializeMember< Value >::value >::type commitValue(void *location, Value value) noexcept
Definition Memory.h:320
UInt8 Byte
Alias for Byte.
Definition Memory.h:34
static ONIXS_ICEBOE_FORCEINLINE std::tuple< typename Value::MemberTraits::FirstArgType > extractAsTuple(const void *location) noexcept
Definition Memory.h:167
static std::tuple< typename Value::MemberTraits::FirstArgType, typename Value::MemberTraits::SecondArgType > ONIXS_ICEBOE_FORCEINLINE extractAsTuple(const void *location) noexcept
Definition Memory.h:191
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:222
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:257