Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DefaultSerializer.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_DefaultSerializer_h_
5#define _Stroika_Foundation_DataExchange_DefaultSerializer_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <cstdint>
10
12
13/*
14 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
15 *
16 */
18
19 /**
20 * \brief function object which serializes type T to a BLOB (or BLOB like) object
21 *
22 * This is (at least originally) intended to be used by Cryptography::Hash<T>, but will probably be useful for many
23 * operations that need to treat a type T as a sequence of bytes
24 *
25 * NOTE - not always defined - since we dont know how to serialze everthing - callers may need to specialize this
26 *
27 * For now, this works with T:
28 * o is_trivially_copy_constructible_v (e.g. int, float, uint32_t, etc)
29 * o const char*
30 * o std::string
31 * o Memory::BLOB (just passed throgh, not adpated)
32 *
33 * ALSO, MANY other Stroika classes support this by providing their own specialization of DefaultSerializer<>
34 * For example:
35 * o Characters::String
36 * o IO::Network::InternetAddress
37 * ...
38 *
39 * \par Example Usage (specializing and USING DefaultSerializer)
40 * \code
41 * namespace Stroika::Foundation::DataExchange
42 * template <>
43 * struct DefaultSerializer<Stroika::Foundation::IO::Network::InternetAddress> {
44 * Memory::BLOB operator() (const Stroika::Foundation::IO::Network::InternetAddress& arg) const
45 * {
46 * return DefaultSerializer<Characters::String>{}(arg.As<Characters::String> ());
47 * }
48 * };
49 * }
50 * \endcode
51 *
52 * \note \em Design Note
53 * We chose to have this return Memory::BLOB instead of an abstract template type which was often
54 * Memory::BLOB but could be something more efficient, because I'm still supporting C++17 (no concepts)
55 * and I think Memory::BLOB can be made VERY NEARLY as efficient (by having a constructor which uses
56 * block allocation and pre-storages (InlineBuffer) the space for small objects (small strings and ints etc)
57 * (maybe do this performance tweak for Stroika 2.1b6?)
58 *
59 * @todo Consider using other tricks like serialize to JSON if the converters for that are defined, or with operator<< if those are defined.
60 */
61 template <typename T>
63 /**
64 * Convert the given t (of type T) to a Memory::BLOB; This should be as quick and efficient as practical.
65 */
66 Memory::BLOB operator() (const T& t) const;
67 };
68
69}
70
71/*
72 ********************************************************************************
73 ***************************** Implementation Details ***************************
74 ********************************************************************************
75 */
76#include "DefaultSerializer.inl"
77
78#endif /*_Stroika_Foundation_DataExchange_DefaultSerializer_h_*/
function object which serializes type T to a BLOB (or BLOB like) object