9#include <boost/endian/arithmetic.hpp>
10#include <userver/storages/postgres/io/buffer_io_base.hpp>
11#include <userver/storages/postgres/io/integral_types.hpp>
12#include <userver/storages/postgres/io/traits.hpp>
13#include <userver/storages/postgres/io/type_mapping.hpp>
15USERVER_NAMESPACE_BEGIN
22struct FloatingPointType;
25struct FloatingPointType<4> {
30struct FloatingPointType<8> {
34template <std::size_t Size>
35struct FloatingPointBySizeParser {
36 using FloatType =
typename FloatingPointType<Size>::type;
37 using IntType =
typename IntegralType<Size>::type;
38 using IntParser = IntegralBySizeParser<Size>;
40 static FloatType ParseBuffer(
const FieldBuffer& buf) {
41 const IntType tmp = IntParser::ParseBuffer(buf);
42 FloatType float_value{};
43 std::memcpy(&float_value, &tmp, Size);
49struct FloatingPointBinaryParser : BufferParserBase<T> {
50 using BaseType = BufferParserBase<T>;
51 using BaseType::BaseType;
56 this->value = FloatingPointBySizeParser<4>::ParseBuffer(buf);
59 this->value = FloatingPointBySizeParser<8>::ParseBuffer(buf);
63 fmt::format(
"Buffer size {} is invalid for a floating point value type", buf.length)};
69struct FloatingPointBinaryFormatter {
70 static constexpr std::size_t size =
sizeof(T);
73 explicit FloatingPointBinaryFormatter(T val) : value{val} {}
75 template <
typename Buffer>
76 void operator()(
const UserTypes& types, Buffer& buf)
const {
77 using IntType =
typename IntegralType<size>::type;
79 std::memcpy(&int_value, &value, size);
80 IntegralBinaryFormatter<IntType>{int_value}(types, buf);
88struct BufferParser<
float> : detail::FloatingPointBinaryParser<
float> {
89 explicit BufferParser(
float& val) : FloatingPointBinaryParser(val) {}
92struct BufferFormatter<
float> : detail::FloatingPointBinaryFormatter<
float> {
93 explicit BufferFormatter(
float val) : FloatingPointBinaryFormatter(val) {}
100struct BufferParser<
double> : detail::FloatingPointBinaryParser<
double> {
101 explicit BufferParser(
double& val) : FloatingPointBinaryParser(val) {}
104struct BufferFormatter<
double> : detail::FloatingPointBinaryFormatter<
double> {
105 explicit BufferFormatter(
double val) : FloatingPointBinaryFormatter(val) {}
112struct CppToSystemPg<
float> : PredefinedOid<PredefinedOids::kFloat4> {};
114struct CppToSystemPg<
double> : PredefinedOid<PredefinedOids::kFloat8> {};