Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DefaultSerializer.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include <type_traits>
5
7
8 namespace Private_ {
9 template <typename TYPE_TO_COMPUTE_HASH_OF>
10 Memory::BLOB SerializeForHash_ (TYPE_TO_COMPUTE_HASH_OF data2Hash)
11 requires (is_trivially_copy_constructible_v<TYPE_TO_COMPUTE_HASH_OF>)
12 {
13 // From https://en.cppreference.com/w/cpp/types/is_trivially_copyable:
14 // Objects of trivially-copyable types that are not potentially-overlapping subobjects are the
15 // only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files
16 // with std::ofstream::write()/std::ifstream::read().
17 return Memory::BLOB{reinterpret_cast<const byte*> (&data2Hash), reinterpret_cast<const byte*> (&data2Hash + 1)};
18 }
19 template <typename TYPE_TO_COMPUTE_HASH_OF>
20 inline Memory::BLOB SerializeForHash_ (TYPE_TO_COMPUTE_HASH_OF data2Hash)
21 requires (same_as<remove_cv_t<TYPE_TO_COMPUTE_HASH_OF>, Memory::BLOB>)
22 {
23 return data2Hash;
24 }
25 inline Memory::BLOB SerializeForHash_ (const char* data2Hash)
26 {
27 return Memory::BLOB{reinterpret_cast<const byte*> (data2Hash), reinterpret_cast<const byte*> (data2Hash + ::strlen (data2Hash))};
28 }
29 inline Memory::BLOB SerializeForHash_ (const string& data2Hash)
30 {
31 return Memory::BLOB{reinterpret_cast<const byte*> (data2Hash.c_str ()),
32 reinterpret_cast<const byte*> (data2Hash.c_str () + data2Hash.length ())};
33 }
34 }
35
36 /*
37 ********************************************************************************
38 ************************ DataExchange::DefaultSerializer<T> ********************
39 ********************************************************************************
40 */
41 template <typename T>
42 inline Memory::BLOB DefaultSerializer<T>::operator() (const T& t) const
43 {
44 return Private_::SerializeForHash_ (t);
45 }
46
47}