OnixS C++ CME iLink 3 Binary Order Entry Handler 1.18.9
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
48<
49 typename Type
50>
53void*
54toOpaquePtr(Type* ptr)
56{
57 assert(ptr);
58 return static_cast<void*>(ptr);
59}
60
63template
64<
65 typename Type
66>
69const void*
70toOpaquePtr(const Type* ptr)
72{
73 assert(ptr);
74 return static_cast<const void*>(ptr);
75}
76
78template
79<
80 typename Type
81>
84void* toMutable(const Type* ptr) ONIXS_ILINK3_NOTHROW
85{
86 assert(ptr);
87 return const_cast<void*>(static_cast<const void*>(ptr));
88}
89
92template
93<
94 typename Type
95>
98Byte*
99toByteBlock(Type* ptr)
101{
102 assert(ptr);
103
104 return
105 static_cast
106 <Byte*>
107 (toOpaquePtr(ptr));
108}
109
112template
113<
114 typename Type
115>
118const Byte*
119toByteBlock(const Type* ptr)
121{
122 assert(ptr);
123
124 return
125 static_cast
126 <const Byte*>
127 (toOpaquePtr(ptr));
128}
129
132template
133<
134 typename Type
135>
138Type* advanceByBytes(Type* pointer, ptrdiff_t distance) ONIXS_ILINK3_NOTHROW
139{
140 assert(pointer);
141
142 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
143}
144
148template
149<
150 typename Type
151>
154Type*
155advanceBackByBytes(
156 Type* pointer,
157 ptrdiff_t distance)
159{
160 assert(pointer);
161
162 return
163 reinterpret_cast<Type*>
164 (
165 toByteBlock(pointer) - distance
166 );
167}
168
171template
172<
173 typename Left,
174 typename Right
175>
178ptrdiff_t
179byteDistance(
180 Left* left,
181 Right* right)
183{
184 assert(left);
185 assert(right);
186
187 return (
188 toByteBlock(left) -
189 toByteBlock(right)
190 );
191}
192
194template
195<
196 class Value
197>
199Value
200extractValue(const void* location) ONIXS_ILINK3_NOTHROW
201{
202 assert(location);
203
204 CHECK_TYPE_INTEGRAL(Value);
205
206 Value result;
207
208 std::memcpy(
209 &result,
210 location,
211 sizeof(Value));
212
213 return result;
214}
215
217template <unsigned ArgsCount>
218struct CompositeExtractor;
219
221template <>
222struct CompositeExtractor<1>
223{
224 template <class Value>
225 static
226 Value extract(const void* location) ONIXS_ILINK3_NOTHROW
227 {
228
229#if defined (ONIXS_ILINK3_CXX11)
230 static_assert(
231 noexcept(
232 Value(
233 std::declval<typename Value::MemberTraits::FirstArgType>()
234 )
235 ) ,
236 "must be noexcept");
237#endif
238
239 assert(location);
240
241 return
242 Value(
243 extractValue<typename Value::MemberTraits::FirstArgType>(location)
244 );
245 }
246};
247
249template <>
250struct CompositeExtractor<2>
251{
252 template <class Value>
253 static
254 Value extract(const void* location) ONIXS_ILINK3_NOTHROW
255 {
256 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
257 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
258
259#if defined (ONIXS_ILINK3_CXX11)
260 static_assert(
261 noexcept(
262 Value(
263 std::declval<FirstArgType>(),
264 std::declval<SecondArgType>()
265 )
266 ) ,
267 "must be noexcept");
268#endif
269
270 assert(location);
271
272 return
273 Value(
274 extractValue<FirstArgType>(location),
275 extractValue<SecondArgType>(
276 advanceByBytes(location, sizeof(FirstArgType)))
277 );
278 }
279};
280
282template <>
283struct CompositeExtractor<4>
284{
285 template <class Value>
286 static
287 Value extract(const void* location) ONIXS_ILINK3_NOTHROW
288 {
289 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
290 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
291 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
292 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
293
294#if defined (ONIXS_ILINK3_CXX11)
295 static_assert(
296 noexcept(
297 Value(
298 std::declval<FirstArgType>(),
299 std::declval<SecondArgType>(),
300 std::declval<ThirdArgType>(),
301 std::declval<FourthArgType>()
302 )
303 ) ,
304 "must be noexcept");
305#endif
306
307 assert(location);
308
309 return
310 Value(
311 extractValue<FirstArgType>(location),
312 extractValue<SecondArgType>(
313 advanceByBytes(location, sizeof(FirstArgType))),
314 extractValue<ThirdArgType>(
315 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
316 extractValue<FourthArgType>(
317 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType)))
318 );
319 }
320};
321
323template
324<
325 class Value
326>
328typename EnableIf<details::HasMemberTraits<Value>::value, Value>::type
329getValue(const void* location) ONIXS_ILINK3_NOTHROW
330{
331 assert(location);
332 return CompositeExtractor<Value::MemberTraits::Count>:: template extract<Value>(location);
333}
334
336template
337<
338 class Value
339>
341typename EnableIf<!details::HasMemberTraits<Value>::value, Value>::type
342getValue(const void* location) ONIXS_ILINK3_NOTHROW
343{
344 assert(location);
345 return extractValue<Value>(location);
346}
347
349template
350<
351 class Value
352>
354typename EnableIf<!details::HasMemberTraits<Value>::value, size_t>::type
356{
357 return sizeof(Value);
358}
359
361template
362<
363 class Value
364>
366typename EnableIf<details::HasMemberTraits<Value>::value, size_t>::type
368{
369 return Value::Size;
370}
371
373template
374<
375 class Value
376>
378typename EnableIf<!details::HasSerializeMember<Value>::value>::type
379commitValue(void* location, Value value)
381{
382 assert(location);
383 std::memcpy(
384 location,
385 &value,
386 sizeof(Value));
387}
388
390template
391<
392 class Value
393>
395typename EnableIf<details::HasSerializeMember<Value>::value>::type
396commitValue(void* location, Value value)
398{
399 assert(location);
400 value.serialize(location);
401}
402
404template
405<
406 class Value
407>
409typename EnableIf<!details::HasValueStaticMember<Value>::value>::type
410setValue(void* location, Value value)
412{
413 assert(location);
414 commitValue(location, value);
415}
416
418template
419<
420 class Value
421>
423typename EnableIf<details::HasValueStaticMember<Value>::value>::type
424setValue(void* location, Value)
426{
427 assert(location);
428 commitValue(location, Value::value());
429}
430
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_ILINK3_HOTPATH
Definition Compiler.h:187
#define ONIXS_ILINK3_CONSTEXPR
Definition Compiler.h:179
#define ONIXS_ILINK3_PURE
Definition Compiler.h:189
#define ONIXS_ILINK3_NOTHROW
Definition Compiler.h:176
#define CHECK_TYPE_INTEGRAL(Type)
Definition Utils.h:47
UInt16 Word
Alias for Word.
Definition Memory.h:37
UInt64 QWord
Alias for Quad Word.
Definition Memory.h:43
UInt32 DWord
Alias for Double Word.
Definition Memory.h:40
UInt8 Byte
Alias for Byte.
Definition Memory.h:34