lintel 0.2.0
Loading...
Searching...
No Matches
Serialization.h
Go to the documentation of this file.
1#ifndef LINTEL_POSTGRES_SERIALIZATION_H
2#define LINTEL_POSTGRES_SERIALIZATION_H
3
4#include <chrono>
5#include <string>
6#include <type_traits>
7
9
17#define IMPLEMENT_SERIALIZE(type, convertToString, convertToValue) \
18 template<> class Serialization<type> \
19 { \
20 public: \
21 static std::string serialize(const type &value) { return convertToString(value); } \
22 static type deserialize(const std::string &value) { return convertToValue(value); } \
23 };
24
25namespace lintel {
26namespace postgresql {
33 template<typename T, typename Enable = void> class Serialization
34 {
35 };
36
37 IMPLEMENT_SERIALIZE(int8_t, std::to_string, std::stoi)
38
39 IMPLEMENT_SERIALIZE(int16_t, std::to_string, std::stoi)
40
41 IMPLEMENT_SERIALIZE(int32_t, std::to_string, std::stoi)
42
43 IMPLEMENT_SERIALIZE(int64_t, std::to_string, std::stol)
44
45 IMPLEMENT_SERIALIZE(uint8_t, std::to_string, std::stoul)
46
47 IMPLEMENT_SERIALIZE(uint16_t, std::to_string, std::stoul)
48
49 IMPLEMENT_SERIALIZE(uint32_t, std::to_string, std::stoul)
50
51 IMPLEMENT_SERIALIZE(uint64_t, std::to_string, std::stoul)
52
53 IMPLEMENT_SERIALIZE(float, std::to_string, std::stof)
54
55 IMPLEMENT_SERIALIZE(double, std::to_string, std::stod)
56
58 std::string,
59 [](const std::string &string) -> std::string { return string; },
60 [](const std::string &string) -> std::string { return string; })
61
70 {
71 public:
72 static std::string serialize(const bool &value) { return value ? "t" : "f"; }
73 static bool deserialize(const std::string &value) { return value == "t"; }
74 };
75
77 std::chrono::sys_time<std::chrono::microseconds>,
78 [](const std::chrono::sys_time<std::chrono::microseconds> &time) -> std::string {
79 return datetime::formatSystemTime(time);
80 },
81 [](const std::string &timeString) -> std::chrono::sys_time<std::chrono::microseconds> {
83 })
84
85 // Only activated when std::size_t is distinct from all fixed-width integer types
86 // (full specializations above take precedence over this partial specialization)
87 template<typename T> class Serialization<T, std::enable_if_t<std::is_same_v<T, std::size_t>>>
88 {
89 public:
90 static std::string serialize(const T &value) { return std::to_string(value); }
91 static T deserialize(const std::string &value) { return static_cast<T>(std::stoul(value)); }
92 };
93}// namespace postgresql
94
95}// namespace lintel
96#endif// LINTEL_POSTGRES_SERIALIZATION_H
Definition Serialization.h:34
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