25 #ifndef SER4CPP_SINGLE_FLOAT_H 26 #define SER4CPP_SINGLE_FLOAT_H 28 #include "ser4cpp/util/Uncopyable.h" 29 #include "ser4cpp/container/SequenceTypes.h" 39 static_assert(
sizeof(
float) == 4,
"Unexpected length of single float");
41 static bool read_from(
rseq_t& input,
float& out)
43 if (input.length() < size)
return false;
50 static bool write_to(
wseq_t& dest,
float value)
52 if (dest.length() < size)
return false;
60 static constexpr std::size_t size =
sizeof(float);
61 static constexpr
float max_value = std::numeric_limits<float>::max();
62 static constexpr
float min_value = -std::numeric_limits<float>::max();
65 union SingleFloatUnion
71 static float read(
const uint8_t* data)
73 if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
75 SingleFloatUnion x = {{ data[0], data[1], data[2], data[3] }};
80 SingleFloatUnion x = {{ data[3], data[2], data[1], data[0] }};
85 static void write(uint8_t* dest,
float value)
87 if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
89 memcpy(dest, &value, size);
93 auto data = reinterpret_cast<uint8_t*>(&value);
94 uint8_t bytes[4] = { data[3], data[2], data[1], data[0] };
95 memcpy(dest, bytes, size);
ser4cpp header-only library namespace