This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_printer.h
159 lines (145 loc) · 4.99 KB
/
field_printer.h
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include <locale> // std::wstring_convert
#include <string> // std::string
#include <sstream> // std::stringstream
#include <vector> // std::vector
#include <memory> // std::shared_ptr, std::unique_ptr
#ifndef SQLITE_ORM_OMITS_CODECVT
#include <codecvt> // std::codecvt_utf8_utf16
#endif // SQLITE_ORM_OMITS_CODECVT
#include "functional/cxx_optional.h"
#include "functional/cxx_universal.h"
#include "functional/cxx_type_traits_polyfill.h"
#include "is_std_ptr.h"
namespace sqlite_orm {
/**
* Is used to print members mapped to objects in storage_t::dump member function.
* Other developers can create own specialization to map custom types
*/
template<class T, typename SFINAE = void>
struct field_printer;
namespace internal {
template<class T, class SFINAE = void>
SQLITE_ORM_INLINE_VAR constexpr bool is_printable_v = false;
template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_printable_v<T, polyfill::void_t<decltype(field_printer<T>{})>> = true
// Also see implementation note for `is_bindable_v`
;
template<class T>
using is_printable = polyfill::bool_constant<is_printable_v<T>>;
}
template<class T>
struct field_printer<T, std::enable_if_t<std::is_arithmetic<T>::value>> {
std::string operator()(const T& t) const {
std::stringstream ss;
ss << t;
return ss.str();
}
};
/**
* Upgrade to integer is required when using unsigned char(uint8_t)
*/
template<>
struct field_printer<unsigned char, void> {
std::string operator()(const unsigned char& t) const {
std::stringstream ss;
ss << +t;
return ss.str();
}
};
/**
* Upgrade to integer is required when using signed char(int8_t)
*/
template<>
struct field_printer<signed char, void> {
std::string operator()(const signed char& t) const {
std::stringstream ss;
ss << +t;
return ss.str();
}
};
/**
* char is neither signed char nor unsigned char so it has its own specialization
*/
template<>
struct field_printer<char, void> {
std::string operator()(const char& t) const {
std::stringstream ss;
ss << +t;
return ss.str();
}
};
template<class T>
struct field_printer<T, std::enable_if_t<std::is_base_of<std::string, T>::value>> {
std::string operator()(std::string string) const {
return string;
}
};
template<>
struct field_printer<std::vector<char>, void> {
std::string operator()(const std::vector<char>& t) const {
std::stringstream ss;
ss << std::hex;
for(auto c: t) {
ss << c;
}
return ss.str();
}
};
#ifndef SQLITE_ORM_OMITS_CODECVT
/**
* Specialization for std::wstring (UTF-16 assumed).
*/
template<class T>
struct field_printer<T, std::enable_if_t<std::is_base_of<std::wstring, T>::value>> {
std::string operator()(const std::wstring& wideString) const {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(wideString);
}
};
#endif // SQLITE_ORM_OMITS_CODECVT
template<>
struct field_printer<nullptr_t, void> {
std::string operator()(const nullptr_t&) const {
return "null";
}
};
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
template<>
struct field_printer<std::nullopt_t, void> {
std::string operator()(const std::nullopt_t&) const {
return "null";
}
};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
struct field_printer<
T,
std::enable_if_t<polyfill::conjunction_v<is_std_ptr<T>,
internal::is_printable<std::remove_cv_t<typename T::element_type>>>>> {
using unqualified_type = std::remove_cv_t<typename T::element_type>;
std::string operator()(const T& t) const {
if(t) {
return field_printer<unqualified_type>()(*t);
} else {
return field_printer<nullptr_t>{}(nullptr);
}
}
};
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
struct field_printer<
T,
std::enable_if_t<polyfill::conjunction_v<polyfill::is_specialization_of<T, std::optional>,
internal::is_printable<std::remove_cv_t<typename T::value_type>>>>> {
using unqualified_type = std::remove_cv_t<typename T::value_type>;
std::string operator()(const T& t) const {
if(t.has_value()) {
return field_printer<unqualified_type>()(*t);
} else {
return field_printer<std::nullopt_t>{}(std::nullopt);
}
}
};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
}