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;
52 using ValueType =
typename BaseType::ValueType;
57 this->value = NumericCast<ValueType>(FloatingPointBySizeParser<4>::ParseBuffer(buf));
60 this->value = NumericCast<ValueType>(FloatingPointBySizeParser<8>::ParseBuffer(buf));
64 fmt::format(
"Buffer size {} is invalid for a floating point value type", buf.length)
71struct FloatingPointBinaryFormatter {
72 static constexpr std::size_t size =
sizeof(T);
75 explicit FloatingPointBinaryFormatter(T val)
79 template <
typename Buffer>
80 void operator()(
const UserTypes& types, Buffer& buf)
const {
81 using IntType =
typename IntegralType<size>::type;
83 std::memcpy(&int_value, &value, size);
84 IntegralBinaryFormatter<IntType>{int_value}(types, buf);
92struct BufferParser<
float> : detail::FloatingPointBinaryParser<
float> {
93 explicit BufferParser(
float& val)
94 : FloatingPointBinaryParser(val)
98struct BufferFormatter<
float> : detail::FloatingPointBinaryFormatter<
float> {
99 explicit BufferFormatter(
float val)
100 : FloatingPointBinaryFormatter(val)
108struct BufferParser<
double> : detail::FloatingPointBinaryParser<
double> {
109 explicit BufferParser(
double& val)
110 : FloatingPointBinaryParser(val)
114struct BufferFormatter<
double> : detail::FloatingPointBinaryFormatter<
double> {
115 explicit BufferFormatter(
double val)
116 : FloatingPointBinaryFormatter(val)