SSP21-CPP
SingleFloat.h
1 /*
2  * Copyright (c) 2018, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef SER4CPP_SINGLE_FLOAT_H
26 #define SER4CPP_SINGLE_FLOAT_H
27 
28 #include "ser4cpp/util/Uncopyable.h"
29 #include "ser4cpp/container/SequenceTypes.h"
30 
31 #include <cstddef>
32 
33 namespace ser4cpp
34 {
35 
36 class SingleFloat : private StaticOnly
37 {
38 public:
39  static_assert(sizeof(float) == 4, "Unexpected length of single float");
40 
41  static bool read_from(rseq_t& input, float& out)
42  {
43  if (input.length() < size) return false;
44 
45  out = read(input);
46  input.advance(size);
47  return true;
48  }
49 
50  static bool write_to(wseq_t& dest, float value)
51  {
52  if (dest.length() < size) return false;
53 
54  write(dest, value);
55  dest.advance(size);
56  return true;
57  }
58 
59  typedef float type_t;
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();
63 
64 private:
65  union SingleFloatUnion
66  {
67  uint8_t bytes[4];
68  float value;
69  };
70 
71  static float read(const uint8_t* data)
72  {
73  if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
74  {
75  SingleFloatUnion x = {{ data[0], data[1], data[2], data[3] }};
76  return x.value;
77  }
78  else
79  {
80  SingleFloatUnion x = {{ data[3], data[2], data[1], data[0] }};
81  return x.value;
82  }
83  }
84 
85  static void write(uint8_t* dest, float value)
86  {
87  if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
88  {
89  memcpy(dest, &value, size);
90  }
91  else
92  {
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);
96  }
97  }
98 };
99 
100 }
101 
102 #endif
ser4cpp header-only library namespace
Definition: Buffer.h:33