userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
type_mapping.hpp
1
#
pragma
once
2
3
#
include
<
string
>
4
#
include
<
type_traits
>
5
6
#
include
<
userver
/
compiler
/
demangle
.
hpp
>
7
#
include
<
userver
/
storages
/
postgres
/
detail
/
is_decl_complete
.
hpp
>
8
#
include
<
userver
/
storages
/
postgres
/
io
/
pg_types
.
hpp
>
9
#
include
<
userver
/
storages
/
postgres
/
io
/
traits
.
hpp
>
10
#
include
<
userver
/
storages
/
postgres
/
io
/
type_traits
.
hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
storages::
postgres
::
io
{
15
16
/// @brief Detect mapping of a C++ type to Postgres type.
17
template
<
typename
T>
18
struct
CppToPg
;
19
20
template
<
PredefinedOids
Oid,
typename
T>
21
struct
PgToCpp
;
22
23
/// Find out if a parser for a predefined Postgres type was registered.
24
bool
HasParser
(
PredefinedOids
);
25
/// Find out if predefined Postgres types are mapped to the same cpp type
26
bool
MappedToSameType
(
PredefinedOids
,
PredefinedOids
);
27
/// Get array element oid for a predefined type
28
PredefinedOids
GetArrayElementOid
(
PredefinedOids
);
29
/// Get the buffer category for a predefined type
30
BufferCategory
GetBufferCategory
(
PredefinedOids
);
31
32
bool
HasParser(
DBTypeName
);
33
34
namespace
detail {
35
36
/// Works as boost::ignore_unused, but returning a value.
37
template
<
typename
... T>
38
inline
bool
ForceReference(
const
T&...) {
39
return
true
;
40
}
41
42
struct
RegisterPredefinedOidParser {
43
static
RegisterPredefinedOidParser Register(
44
PredefinedOids
type_oid,
45
PredefinedOids
array_oid,
46
BufferCategory
category,
47
std::string cpp_name
48
);
49
};
50
51
// note: implementation is in user_types.cpp
52
struct
RegisterUserTypeParser {
53
static
RegisterUserTypeParser Register(
const
DBTypeName
&, std::string cpp_name);
54
const
DBTypeName
postgres_name;
55
};
56
57
/// @brief Declare a mapping of a C++ type to a PostgreSQL type oid.
58
/// Also mark available parsers
59
template
<
typename
T>
60
struct
CppToSystemPgImpl {
61
using
Type = T;
62
using
Mapping =
CppToSystemPg
<T>;
63
static
constexpr
auto
type_oid = Mapping::value;
64
static_assert
(type_oid !=
PredefinedOids
::kInvalid,
"Type oid is invalid"
);
65
66
using
ArrayMapping =
ArrayType
<type_oid>;
67
static
constexpr
auto
array_oid = ArrayMapping::value;
68
static_assert
(array_oid !=
PredefinedOids
::kInvalid,
"Array type oid is invalid"
);
69
70
// We cannot perform a static check for parser presence here as there are
71
// types that should not have one, e.g. char[N]. All parser checks will be
72
// performed at runtime.
73
74
static
const
inline
RegisterPredefinedOidParser init = RegisterPredefinedOidParser::Register(
75
type_oid,
76
array_oid,
77
io
::
traits
::kTypeBufferCategory<T>,
78
std::string{
compiler
::GetTypeName<T>()}
79
);
80
81
static
constexpr
Oid
GetOid(
const
UserTypes
&) {
82
ForceReference(init);
83
return
static_cast
<
Oid
>(type_oid);
84
}
85
static
constexpr
Oid
GetArrayOid(
const
UserTypes
&) {
86
ForceReference(init);
87
return
static_cast
<
Oid
>(array_oid);
88
}
89
};
90
91
template
<
typename
T>
92
struct
CppToUserPgImpl;
93
94
/// @brief Declare a mapping of a PostgreSQL predefined type oid to a C++ type.
95
/// Available parsers will be marked after referencing the init somewhere in the code.
96
template
<
PredefinedOids
TypeOid,
typename
T>
97
struct
PgToCppPredefined {
98
static
constexpr
auto
type_oid = TypeOid;
99
static_assert
(type_oid !=
PredefinedOids
::kInvalid,
"Type oid is invalid"
);
100
101
using
ArrayMapping =
ArrayType
<type_oid>;
102
static
constexpr
auto
array_oid = ArrayMapping::value;
103
static_assert
(array_oid !=
PredefinedOids
::kInvalid,
"Array type oid is invalid"
);
104
105
// See above on parser presence checks.
106
107
static
const
inline
RegisterPredefinedOidParser init = RegisterPredefinedOidParser::Register(
108
type_oid,
109
array_oid,
110
io
::
traits
::kTypeBufferCategory<T>,
111
std::string{
compiler
::GetTypeName<T>()}
112
);
113
};
114
115
}
// namespace detail
116
117
template
<
typename
T>
118
requires
(!
traits
::
IsSpecialMapping
<T>::value &&
traits
::
kIsMappedToPg
<T> && !
traits
::
IsMappedToArray
<T>)
119
struct
CppToPg
<T>
120
: std::conditional_t<
traits
::
IsMappedToSystemType
<T>, detail::CppToSystemPgImpl<T>, detail::CppToUserPgImpl<T>> {};
121
122
template
<
typename
T>
123
constexpr
bool
IsTypeMappedToSystem() {
124
return
traits
::
kIsMappedToPg
<T> &&
125
std::is_same<
typename
CppToPg
<T>::Mapping,
CppToSystemPg
<
typename
CppToPg
<T>::Type>>::value;
126
}
127
128
void
LogRegisteredTypes();
129
130
}
// namespace storages::postgres::io
131
132
USERVER_NAMESPACE_END
userver
storages
postgres
io
type_mapping.hpp
Generated on
for userver by
Doxygen
1.17.0