lintel 0.2.0
Loading...
Searching...
No Matches
Serialization.h
Go to the documentation of this file.
1#ifndef LINTEL_SQLITE_SERIALIZATION_H
2#define LINTEL_SQLITE_SERIALIZATION_H
3
4#include <chrono>
5#include <exception>
6#include <memory>
7#include <type_traits>
8
13
21#define IMPLEMENT_SERIALIZE(type, convertToString, convertToValue) \
22 template<> class Serialization<type> \
23 { \
24 public: \
25 static std::string serialize(const type &value) { return convertToString(value); } \
26 static type deserialize(const std::string &value) { return convertToValue(value); } \
27 };
28
29namespace lintel {
30namespace sqlite {
37 template<typename T, typename Enable = void> class Serialization
38 {
39 };
40
41 IMPLEMENT_SERIALIZE(int8_t, std::to_string, std::stoi)
42
43 IMPLEMENT_SERIALIZE(int16_t, std::to_string, std::stoi)
44
45 IMPLEMENT_SERIALIZE(int32_t, std::to_string, std::stoi)
46
47 IMPLEMENT_SERIALIZE(int64_t, std::to_string, std::stol)
48
49 IMPLEMENT_SERIALIZE(uint8_t, std::to_string, std::stoul)
50
51 IMPLEMENT_SERIALIZE(uint16_t, std::to_string, std::stoul)
52
53 IMPLEMENT_SERIALIZE(uint32_t, std::to_string, std::stoul)
54
55 IMPLEMENT_SERIALIZE(uint64_t, std::to_string, std::stoul)
56
57 IMPLEMENT_SERIALIZE(float, std::to_string, std::stof)
58
59 IMPLEMENT_SERIALIZE(double, std::to_string, std::stod)
60
62 std::string,
63 [](const std::string &string) -> std::string { return string; },
64 [](const std::string &string) -> std::string { return string; })
65
74 {
75 public:
76 static std::string serialize(const bool &value) { return value ? "t" : "0"; }
77 static bool deserialize(const std::string &value) { return value == "t"; }
78 };
79
81 std::chrono::sys_time<std::chrono::microseconds>,
82 [](const std::chrono::sys_time<std::chrono::microseconds> &time) -> std::string {
83 return datetime::formatSystemTime(time);
84 },
85 [](const std::string &timeString) -> std::chrono::sys_time<std::chrono::microseconds> {
87 })
88
89 template<typename T> class Serialization<T, std::enable_if_t<std::is_same_v<T, std::size_t>>>
90 {
91 public:
92 static std::string serialize(const T &value) { return std::to_string(value); }
93 static T deserialize(const std::string &value) { return static_cast<T>(std::stoul(value)); }
94 };
95}// namespace sqlite
96
97}// namespace lintel
98#endif// LINTEL_SQLITE_SERIALIZATION_H
Definition Serialization.h:38
SystemTime parseSystemTime(const std::string &value)
Definition DateTimeFormat.h:126
std::string formatSystemTime(const SystemTime &value, char dateSeparator='-')
Definition DateTimeFormat.h:91
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9
#define IMPLEMENT_SERIALIZE(type, convertToString, convertToValue)
Definition Serialization.h:17