userver
C++ Async Framework
Loading...
Searching...
No Matches
string_types.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/postgres/io/string_types.hpp
4
/// @brief Strings I/O support
5
/// @ingroup userver_postgres_parse_and_format
6
7
#
include
<
cstring
>
8
#
include
<
string
>
9
#
include
<
string_view
>
10
11
#
include
<
userver
/
storages
/
postgres
/
exceptions
.
hpp
>
12
#
include
<
userver
/
storages
/
postgres
/
io
/
buffer_io_base
.
hpp
>
13
#
include
<
userver
/
storages
/
postgres
/
io
/
traits
.
hpp
>
14
#
include
<
userver
/
storages
/
postgres
/
io
/
type_mapping
.
hpp
>
15
16
USERVER_NAMESPACE_BEGIN
17
18
namespace
storages
::
postgres
::
io
{
19
20
//@{
21
/** @name const char* formatting */
22
template
<>
23
struct
BufferFormatter
<
const
char
*> {
24
const
char
*
value
;
25
26
explicit
BufferFormatter
(
const
char
*
val
) :
value
{
val
} {}
27
28
template
<
typename
Buffer
>
29
void
operator
()(
const
UserTypes
&,
Buffer
&
buf
)
const
{
30
auto
sz
=
std
::
strlen
(
value
);
31
WriteN
(
buf
,
value
,
sz
);
32
}
33
34
template
<
typename
Buffer
>
35
static
void
WriteN
(
Buffer
&
buf
,
const
char
*
c
,
std
::
size_t
n
) {
36
// Don't copy zero-terminator in binary mode
37
while
(
n
> 0 &&
c
[
n
- 1] ==
'\0'
) {
38
--
n
;
39
}
40
buf
.
reserve
(
buf
.
size
() +
n
);
41
std
::
copy
(
c
,
c
+
n
,
std
::
back_inserter
(
buf
));
42
}
43
};
44
//@}
45
46
//@{
47
/** @name char[N] formatting */
48
template
<
std
::
size_t
N
>
49
struct
BufferFormatter
<
char
[
N
]> {
50
using
CharFormatter
=
BufferFormatter
<
const
char
*>;
51
const
char
*
value
;
52
53
explicit
BufferFormatter
(
const
char
*
val
) :
value
{
val
} {}
54
55
template
<
typename
Buffer
>
56
void
operator
()(
const
UserTypes
&,
Buffer
&
buf
)
const
{
57
CharFormatter
::
WriteN
(
buf
,
value
,
N
);
58
}
59
};
60
61
//@}
62
63
//@{
64
/** @name std::string I/O */
65
template
<>
66
struct
BufferFormatter
<
std
::
string
> {
67
using
CharFormatter
=
BufferFormatter
<
const
char
*>;
68
const
std
::
string
&
value
;
69
70
explicit
BufferFormatter
(
const
std
::
string
&
val
) :
value
{
val
} {}
71
template
<
typename
Buffer
>
72
void
operator
()(
const
UserTypes
&,
Buffer
&
buf
)
const
{
73
CharFormatter
::
WriteN
(
buf
,
value
.
data
(),
value
.
size
());
74
}
75
};
76
77
template
<>
78
struct
BufferParser
<
std
::
string
> {
79
std
::
string
&
value
;
80
81
explicit
BufferParser
(
std
::
string
&
val
) :
value
{
val
} {}
82
83
void
operator
()(
const
FieldBuffer
&
buffer
);
84
};
85
//@}
86
87
//@{
88
/** @name string_view I/O */
89
template
<>
90
struct
BufferFormatter
<
std
::
string_view
>
91
:
detail
::
BufferFormatterBase
<
std
::
string_view
> {
92
using
BaseType
=
detail
::
BufferFormatterBase
<
std
::
string_view
>;
93
using
CharFormatter
=
BufferFormatter
<
const
char
*>;
94
95
using
BaseType
::
BaseType
;
96
97
template
<
typename
Buffer
>
98
void
operator
()(
const
UserTypes
&,
Buffer
&
buffer
)
const
{
99
CharFormatter
::
WriteN
(
buffer
,
value
.
data
(),
value
.
size
());
100
}
101
};
102
103
template
<>
104
struct
BufferParser
<
std
::
string_view
>
105
:
detail
::
BufferParserBase
<
std
::
string_view
> {
106
using
BaseType
=
detail
::
BufferParserBase
<
std
::
string_view
>;
107
using
BaseType
::
BaseType
;
108
109
void
operator
()(
const
FieldBuffer
&
buffer
) {
110
using
std
::
swap
;
111
ValueType
tmp
{
reinterpret_cast
<
const
char
*>(
buffer
.
buffer
),
buffer
.
length
};
112
swap
(
tmp
,
value
);
113
}
114
};
115
//@}
116
117
//@{
118
/** @name char I/O */
119
template
<>
120
struct
BufferFormatter
<
char
> {
121
char
value
;
122
123
explicit
BufferFormatter
(
char
val
) :
value
{
val
} {}
124
template
<
typename
Buffer
>
125
void
operator
()(
const
UserTypes
&,
Buffer
&
buf
)
const
{
126
buf
.
push_back
(
value
);
127
}
128
};
129
130
template
<>
131
struct
BufferParser
<
char
> {
132
char
&
value
;
133
134
explicit
BufferParser
(
char
&
val
) :
value
{
val
} {}
135
136
void
operator
()(
const
FieldBuffer
&
buffer
) {
137
if
(
buffer
.
length
!= 1) {
138
throw
InvalidInputBufferSize
{
buffer
.
length
,
"for type char"
};
139
}
140
value
= *
buffer
.
buffer
;
141
}
142
};
143
//@}
144
145
//@{
146
/** @name C++ to PostgreSQL mapping for string types */
147
template
<>
148
struct
CppToSystemPg<
const
char
*> : PredefinedOid<PredefinedOids::kText> {};
149
template
<std::size_t N>
150
struct
CppToSystemPg<
char
[N]> : PredefinedOid<PredefinedOids::kText> {};
151
template
<>
152
struct
CppToSystemPg<std::string> : PredefinedOid<PredefinedOids::kText> {};
153
template
<>
154
struct
CppToSystemPg
<
std
::
string_view
> :
PredefinedOid
<
PredefinedOids
::
kText
> {
155
};
156
template
<>
157
struct
CppToSystemPg<
char
> : PredefinedOid<PredefinedOids::kChar> {};
158
//@}
159
160
}
// namespace storages::postgres::io
161
162
USERVER_NAMESPACE_END
userver
storages
postgres
io
string_types.hpp
Generated on Wed May 15 2024 22:29:35 for userver by
Doxygen
1.10.0