OnixS C++ CME MDP Conflated TCP Handler 1.3.6
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
79template
80<
81 typename Type
82>
85Byte*
86toByteBlock(Type* ptr)
88{
89 assert(ptr);
90
91 return
92 static_cast
93 <Byte*>
94 (toOpaquePtr(ptr));
95}
96
99template
100<
101 typename Type
102>
105const Byte*
106toByteBlock(const Type* ptr)
108{
109 assert(ptr);
110
111 return
112 static_cast
113 <const Byte*>
114 (toOpaquePtr(ptr));
115}
116
119template
120<
121 typename Type
122>
125Type* advanceByBytes(Type* pointer, ptrdiff_t distance) ONIXS_CONFLATEDTCP_NOTHROW
126{
127 assert(pointer);
128
129 return reinterpret_cast<Type*>(toByteBlock(pointer) + distance);
130}
131
135template
136<
137 typename Type
138>
141Type*
142advanceBackByBytes(
143 Type* pointer,
144 ptrdiff_t distance)
146{
147 assert(pointer);
148
149 return
150 reinterpret_cast<Type*>
151 (
152 toByteBlock(pointer) - distance
153 );
154}
155
158template
159<
160 typename Left,
161 typename Right
162>
165ptrdiff_t
166byteDistance(
167 Left* left,
168 Right* right)
170{
171 assert(left);
172 assert(right);
173
174 return (
175 toByteBlock(left) -
176 toByteBlock(right)
177 );
178}
179
181template
182<
183 class Value
184>
186Value
187extractValue(const void* location) ONIXS_CONFLATEDTCP_NOTHROW
188{
189 assert(location);
190
191 //CHECK_TYPE_INTEGRAL(Value);
192
193 Value result;
194
195 std::memcpy(
196 &result,
197 location,
198 sizeof(Value));
199
200 return result;
201}
202
204template <unsigned ArgsCount>
205struct CompositeExtractor;
206
208template <>
209struct CompositeExtractor<1>
210{
211 template <class Value>
212 static
213 Value extract(const void* location)
214 {
215
216#if defined (ONIXS_CONFLATEDTCP_CXX11)
217 static_assert(
218 noexcept(
219 Value(
220 std::declval<typename Value::MemberTraits::FirstArgType>()
221 )
222 ) ,
223 "must be noexcept");
224#endif
225
226 assert(location);
227
228 return
229 Value(
230 extractValue<typename Value::MemberTraits::FirstArgType>(location)
231 );
232 }
233};
234
236template <>
237struct CompositeExtractor<2>
238{
239 template <class Value>
240 static
241 Value extract(const void* location) ONIXS_CONFLATEDTCP_NOTHROW
242 {
243 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
244 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
245
246#if defined (ONIXS_CONFLATEDTCP_CXX11)
247 static_assert(
248 noexcept(
249 Value(
250 std::declval<FirstArgType>(),
251 std::declval<SecondArgType>()
252 )
253 ) ,
254 "must be noexcept");
255#endif
256
257 assert(location);
258
259 return
260 Value(
261 extractValue<FirstArgType>(location),
262 extractValue<SecondArgType>(
263 advanceByBytes(location, sizeof(FirstArgType)))
264 );
265 }
266};
267
269template <>
270struct CompositeExtractor<4>
271{
272 template <class Value>
273 static
274 Value extract(const void* location) ONIXS_CONFLATEDTCP_NOTHROW
275 {
276 typedef typename Value::MemberTraits::FirstArgType FirstArgType;
277 typedef typename Value::MemberTraits::SecondArgType SecondArgType;
278 typedef typename Value::MemberTraits::ThirdArgType ThirdArgType;
279 typedef typename Value::MemberTraits::FourthArgType FourthArgType;
280
281#if defined (ONIXS_CONFLATEDTCP_CXX11)
282 static_assert(
283 noexcept(
284 Value(
285 std::declval<FirstArgType>(),
286 std::declval<SecondArgType>(),
287 std::declval<ThirdArgType>(),
288 std::declval<FourthArgType>()
289 )
290 ) ,
291 "must be noexcept");
292#endif
293
294 assert(location);
295
296 return
297 Value(
298 extractValue<FirstArgType>(location),
299 extractValue<SecondArgType>(
300 advanceByBytes(location, sizeof(FirstArgType))),
301 extractValue<ThirdArgType>(
302 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType))),
303 extractValue<FourthArgType>(
304 advanceByBytes(location, sizeof(FirstArgType) + sizeof(SecondArgType) + sizeof(ThirdArgType)))
305 );
306 }
307};
308
310template
311<
312 class Value
313>
315typename EnableIf<details::HasMemberTraits<Value>::value, Value>::type
316getValue(const void* location) ONIXS_CONFLATEDTCP_NOTHROW
317{
318 assert(location);
319 return CompositeExtractor<Value::MemberTraits::Count>:: template extract<Value>(location);
320}
321
323template
324<
325 class Value
326>
328typename EnableIf<!details::HasMemberTraits<Value>::value, Value>::type
329getValue(const void* location) ONIXS_CONFLATEDTCP_NOTHROW
330{
331 assert(location);
332 return extractValue<Value>(location);
333}
334
336template
337<
338 class Value
339>
341typename EnableIf<!details::HasMemberTraits<Value>::value, size_t>::type
343{
344 return sizeof(Value);
345}
346
348template
349<
350 class Value
351>
353typename EnableIf<details::HasMemberTraits<Value>::value, size_t>::type
355{
356 return Value::Size;
357}
358
360template
361<
362 class Value
363>
365typename EnableIf<!details::HasSerializeMember<Value>::value>::type
366commitValue(void* location, Value value)
368{
369 assert(location);
370 std::memcpy(
371 location,
372 &value,
373 sizeof(Value));
374}
375
377template
378<
379 class Value
380>
382typename EnableIf<details::HasSerializeMember<Value>::value>::type
383commitValue(void* location, Value value)
385{
386 assert(location);
387 value.serialize(location);
388}
389
391template
392<
393 class Value
394>
396typename EnableIf<!details::HasValueStaticMember<Value>::value>::type
397setValue(void* location, Value value)
399{
400 assert(location);
401 commitValue(location, value);
402}
403
405template
406<
407 class Value
408>
410typename EnableIf<details::HasValueStaticMember<Value>::value>::type
411setValue(void* location, Value)
413{
414 assert(location);
415 commitValue(location, Value::value());
416}
417
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_END
Definition ABI.h:143
#define ONIXS_CONFLATEDTCP_PURE
Definition Compiler.h:189
#define ONIXS_CONFLATEDTCP_CONSTEXPR
Definition Compiler.h:179
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition Compiler.h:176
#define ONIXS_CONFLATEDTCP_HOTPATH
Definition Compiler.h:187
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