BitStream
float_trait.h
Go to the documentation of this file.
1 #pragma once
2 #include "../utility/assert.h"
3 #include "../utility/meta.h"
4 #include "../utility/parameter.h"
5 
6 #include "../stream/serialize_traits.h"
7 
8 #include <cstdint>
9 #include <cstring>
10 
11 namespace bitstream
12 {
16  template<>
17  struct serialize_traits<float>
18  {
25  template<typename Stream>
27  static serialize(Stream& writer, in<float> value) noexcept
28  {
29  uint32_t tmp;
30  std::memcpy(&tmp, &value, sizeof(float));
31 
32  BS_ASSERT(writer.serialize_bits(tmp, 32));
33 
34  return true;
35  }
36 
43  template<typename Stream>
45  static serialize(Stream& reader, float& value) noexcept
46  {
47  uint32_t tmp;
48 
49  BS_ASSERT(reader.serialize_bits(tmp, 32));
50 
51  std::memcpy(&value, &tmp, sizeof(float));
52 
53  return true;
54  }
55  };
56 
60  template<>
61  struct serialize_traits<double>
62  {
69  template<typename Stream>
71  static serialize(Stream& writer, in<double> value) noexcept
72  {
73  uint32_t tmp[2];
74  std::memcpy(tmp, &value, sizeof(double));
75 
76  BS_ASSERT(writer.serialize_bits(tmp[0], 32));
77  BS_ASSERT(writer.serialize_bits(tmp[1], 32));
78 
79  return true;
80  }
81 
88  template<typename Stream>
90  static serialize(Stream& reader, double& value) noexcept
91  {
92  uint32_t tmp[2];
93 
94  BS_ASSERT(reader.serialize_bits(tmp[0], 32));
95  BS_ASSERT(reader.serialize_bits(tmp[1], 32));
96 
97  std::memcpy(&value, tmp, sizeof(double));
98 
99  return true;
100  }
101  };
102 }
#define BS_ASSERT(x)
Definition: assert.h:15
std::enable_if_t< T::writing, R > is_writing_t
Definition: meta.h:25
std::enable_if_t< T::reading, R > is_reading_t
Definition: meta.h:28
Definition: bounded_range.h:28
std::conditional_t<(sizeof(T)<=16 &&std::is_trivially_copy_constructible_v< T >), std::add_const_t< T >, std::add_lvalue_reference_t< std::add_const_t< T > >> in
Passes by const or const reference depending on size.
Definition: parameter.h:94
static utility::is_reading_t< Stream > serialize(Stream &reader, double &value) noexcept
Serializes a whole double from the reader.
Definition: float_trait.h:90
static utility::is_writing_t< Stream > serialize(Stream &writer, in< double > value) noexcept
Serializes a whole double into the writer.
Definition: float_trait.h:71
static utility::is_writing_t< Stream > serialize(Stream &writer, in< float > value) noexcept
Serializes a whole float into the writer.
Definition: float_trait.h:27
static utility::is_reading_t< Stream > serialize(Stream &reader, float &value) noexcept
Serializes a whole float from the reader.
Definition: float_trait.h:45
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11