OnixS C++ Euronext Optiq MDG Handler 1.5.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#include <utility>
31
33
35typedef UInt8 Byte;
36
38typedef UInt16 Word;
39
41typedef UInt32 DWord;
42
44typedef UInt64 QWord;
45
48template
49<
50 typename Type
51>
54void*
55toOpaquePtr(Type* ptr)
56 noexcept
57{
58 assert(ptr);
59 return static_cast<void*>(ptr);
60}
61
64template
65<
66 typename Type
67>
70const void*
71toOpaquePtr(const Type* ptr)
72 noexcept
73{
74 assert(ptr);
75 return static_cast<const void*>(ptr);
76}
77
80template
81<
82 typename Type
83>
86Byte*
87toByteBlock(Type* ptr)
88 noexcept
89{
90 assert(ptr);
91
92 return
93 static_cast
94 <Byte*>
95 (toOpaquePtr(ptr));
96}
97
100template
101<
102 typename Type
103>
106const Byte*
107toByteBlock(const Type* ptr)
108 noexcept
109{
110 assert(ptr);
111
112 return
113 static_cast
114 <const Byte*>
115 (toOpaquePtr(ptr));
116}
117
120template
121<
122 typename Type
123>
126Type* advanceByBytes(Type* pointer, ptrdiff_t distance) noexcept
127{
128 assert(pointer);
129
130 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
131}
132
136template
137<
138 typename Type
139>
142Type*
143advanceBackByBytes(
144 Type* pointer,
145 ptrdiff_t distance)
146 noexcept
147{
148 assert(pointer);
149
150 return
151 reinterpret_cast<Type*>
152 (
153 toByteBlock(pointer) - distance
154 );
155}
156
159template
160<
161 typename Left,
162 typename Right
163>
166ptrdiff_t
167byteDistance(
168 Left* left,
169 Right* right)
170 noexcept
171{
172 assert(left);
173 assert(right);
174
175 return (
176 toByteBlock(left) -
177 toByteBlock(right)
178 );
179}
180
182template
183<
184 class Value
185>
187Value
188extractValue(const void* location) noexcept
189{
190 assert(location);
191
192 CHECK_TYPE_INTEGRAL(Value);
193
194 Value result;
195
196 std::memcpy(
197 &result,
198 location,
199 sizeof(Value));
200
201 return result;
202}
203
205template <unsigned ArgsCount>
206struct CompositeExtractor;
207
209template <>
210struct CompositeExtractor<1>
211{
212 template <class Value>
213 static
214 Value extract(const void* location) noexcept
215 {
216
217 static_assert(
218 noexcept(
219 Value(
220 std::declval<typename Value::MemberTraits::FirstArgType>()
221 )
222 ) ,
223 "must be noexcept");
224
225 assert(location);
226
227 return
228 Value(
229 extractValue<typename Value::MemberTraits::FirstArgType>(location)
230 );
231 }
232};
233
235template <>
236struct CompositeExtractor<2>
237{
238 template <class Value>
239 static
240 Value extract(const void* location) noexcept
241 {
242 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
243 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
244
245 static_assert(
246 noexcept(
247 Value(
248 std::declval<FirstArgType>(),
249 std::declval<SecondArgType>()
250 )
251 ) ,
252 "must be noexcept");
253
254 assert(location);
255
256 return
257 Value(
258 extractValue<FirstArgType>(location),
259 extractValue<SecondArgType>(
260 advanceByBytes(location, sizeof(FirstArgType)))
261 );
262 }
263};
264
266template <>
267struct CompositeExtractor<4>
268{
269 template <class Value>
270 static
271 Value extract(const void* location) noexcept
272 {
273 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
274 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
275 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
276 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
277
278 static_assert(
279 noexcept(
280 Value(
281 std::declval<FirstArgType>(),
282 std::declval<SecondArgType>(),
283 std::declval<ThirdArgType>(),
284 std::declval<FourthArgType>()
285 )
286 ) ,
287 "must be noexcept");
288
289 assert(location);
290
291 return
292 Value(
293 extractValue<FirstArgType>(location),
294 extractValue<SecondArgType>(
295 advanceByBytes(location, sizeof(FirstArgType))),
296 extractValue<ThirdArgType>(
297 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
298 extractValue<FourthArgType>(
299 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType)))
300 );
301 }
302};
303
305template
306<
307 class Value
308>
310typename EnableIf<details::HasMemberTraits<Value>::value, Value>::type
311getValue(const void* location) noexcept
312{
313 assert(location);
314 return CompositeExtractor<Value::MemberTraits::Count>:: template extract<Value>(location);
315}
316
318template
319<
320 class Value
321>
323typename EnableIf<!details::HasMemberTraits<Value>::value, Value>::type
324getValue(const void* location) noexcept
325{
326 assert(location);
327 return extractValue<Value>(location);
328}
329
331template
332<
333 class Value
334>
335constexpr
336typename EnableIf<!details::HasMemberTraits<Value>::value, size_t>::type
337size() noexcept
338{
339 return sizeof(Value);
340}
341
343template
344<
345 class Value
346>
347constexpr
348typename EnableIf<details::HasMemberTraits<Value>::value, size_t>::type
349size() noexcept
350{
351 return Value::Size;
352}
353
355template
356<
357 class Value
358>
360typename EnableIf<!details::HasSerializeMember<Value>::value>::type
361commitValue(void* location, Value value)
362 noexcept
363{
364 assert(location);
365 std::memcpy(
366 location,
367 &value,
368 sizeof(Value));
369}
370
372template
373<
374 class Value
375>
377typename EnableIf<details::HasSerializeMember<Value>::value>::type
378commitValue(void* location, Value value)
379 noexcept
380{
381 assert(location);
382 value.serialize(location);
383}
384
386template
387<
388 class Value
389>
391typename EnableIf<!details::HasValueStaticMember<Value>::value>::type
392setValue(void* location, Value value)
393 noexcept
394{
395 assert(location);
396 commitValue(location, value);
397}
398
400template
401<
402 class Value
403>
405typename EnableIf<details::HasValueStaticMember<Value>::value>::type
406setValue(void* location, Value)
407 noexcept
408{
409 assert(location);
410 commitValue(location, Value::value());
411}
412
#define ONIXS_EURONEXT_OPTIQMDG_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:146
#define ONIXS_EURONEXT_OPTIQMDG_MESSAGING_NAMESPACE_END
Definition ABI.h:151
#define ONIXS_EURONEXT_OPTIQMDG_PURE
Definition Compiler.h:123
#define ONIXS_EURONEXT_OPTIQMDG_HOTPATH
Definition Compiler.h:121
#define CHECK_TYPE_INTEGRAL(Type)
Definition Utils.h:42
UInt32 DWord
Alias for Double Word.
Definition Memory.h:41