-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserialization.hpp
110 lines (99 loc) · 4.3 KB
/
serialization.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#define __gnu_linux__
#include "alpaca/alpaca.h"
namespace espp {
/**
* @brief Serialization convenience wrapper for espp, providing methods for
* serializaing structures / classes into containers (such as c-sytle arrays,
* std::arrays, std::vectors, etc.). and for deserializing such containers into
* structures / classes.
*
* @note When defining types to be serialized, you _MUST_ use strictly sized
* types, such as uint8_t / int8_t or uint32_t or int8_t instead of just int or
* even size_t.
*
* @note This class does not really exist or do anything, but it's the only
* way I could figure out how to get this documentation built into the system
* :(
*
* \section serialization_ex1 (De-)Serialization Example
* \snippet serialization_example.cpp serialization example
* \section serialization_ex2 Complex Structure (De-)Serialization Example
* \snippet serialization_example.cpp complex serialization example
*/
class __serialization_documentation__ {};
/**
* @brief Default serialization options for espp.
*/
static constexpr auto SERIALIZATION_DEFAULT_OPTIONS =
alpaca::options::fixed_length_encoding | alpaca::options::with_version;
/**
* @brief Serialize data using the default options into the container.
* @param data Structure to be serialized.
* @param container Container class to serialize into.
* @return Number of bytes that were serialized.
*/
template <class T, class Container> auto serialize(const T &data, Container &container) -> size_t {
return alpaca::serialize<SERIALIZATION_DEFAULT_OPTIONS>(data, container);
}
/**
* @brief Serialize data using custom options into the container.
* @param data Structure to be serialized.
* @param container Container class to serialize into.
* @return Number of bytes that were serialized.
*/
template <alpaca::options O, class T, class Container>
auto serialize(const T &data, Container &container) -> size_t {
return alpaca::serialize<O>(data, container);
}
/**
* @brief Deserialize from container into new object of type T using default
* serialization options.
* @param container The container of serialized data representing an object of
* type T.
* @param ec The error code that was generated during deserialization, if any.
* @return The object that was deserialized. Only valid if !ec.
*/
template <class T, class Container>
auto deserialize(Container &container, std::error_code &ec) -> T {
return alpaca::deserialize<SERIALIZATION_DEFAULT_OPTIONS, T>(container, ec);
}
/**
* @brief Deserialize from container into new object of type T using custom
* serialization options.
* @param container The container of serialized data representing an object of
* type T.
* @param ec The error code that was generated during deserialization, if any.
* @return The object that was deserialized. Only valid if !ec.
*/
template <alpaca::options O, class T, class Container>
auto deserialize(Container &container, std::error_code &ec) -> T {
return alpaca::deserialize<O, T>(container, ec);
}
/**
* @brief Deserialize num_bytes from container into new object of type T using
* default serialization options.
* @param container The container of serialized data representing an object of
* type T.
* @param num_bytes The number of bytes to deserialize from the container.
* @param ec The error code that was generated during deserialization, if any.
* @return The object that was deserialized. Only valid if !ec.
*/
template <class T, class Container>
auto deserialize(Container &container, const std::size_t num_bytes, std::error_code &ec) -> T {
return alpaca::deserialize<SERIALIZATION_DEFAULT_OPTIONS, T>(container, num_bytes, ec);
}
/**
* @brief Deserialize num_bytes from container into new object of type T using
* custom serialization options.
* @param container The container of serialized data representing an object of
* type T.
* @param num_bytes The number of bytes to deserialize from the container.
* @param ec The error code that was generated during deserialization, if any.
* @return The object that was deserialized. Only valid if !ec.
*/
template <alpaca::options O, class T, class Container>
auto deserialize(Container &container, const std::size_t num_bytes, std::error_code &ec) -> T {
return alpaca::deserialize<O, T>(container, num_bytes, ec);
}
} // namespace espp