SSP21-CPP
DoubleFloat.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_DOUBLE_FLOAT_H
26 #define SER4CPP_DOUBLE_FLOAT_H
27 
28 #include "ser4cpp/container/SequenceTypes.h"
29 #include "ser4cpp/serialization/DoubleFloat.h"
30 #include "ser4cpp/serialization/FloatByteOrder.h"
31 #include "ser4cpp/util/Uncopyable.h"
32 
33 #include <cstddef>
34 #include <cstring>
35 #include <limits>
36 
37 namespace ser4cpp
38 {
39 
40 class DoubleFloat : private StaticOnly
41 {
42 public:
43  static_assert(sizeof(double) == 8, "Unexpected length of double float");
44 
45  static bool read_from(rseq_t& input, double& out)
46  {
47  if (input.length() < size) return false;
48 
49  out = read(input);
50  input.advance(size);
51  return true;
52  }
53 
54  static bool write_to(wseq_t& dest, double value)
55  {
56  if (dest.length() < size) return false;
57 
58  write(dest, value);
59  dest.advance(size);
60  return true;
61  }
62 
63  typedef double type_t;
64  static constexpr std::size_t size = sizeof(double);
65  static constexpr double max_value = std::numeric_limits<double>::max();
66  static constexpr double min_value = -std::numeric_limits<double>::max();
67 
68 private:
69  union DoubleFloatUnion
70  {
71  uint8_t bytes[8];
72  double value;
73  };
74 
75  static double read(const uint8_t* data)
76  {
77  if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
78  {
79  DoubleFloatUnion x = {{ data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7] }};
80  return x.value;
81  }
82  else
83  {
84  DoubleFloatUnion x = {{ data[7], data[6], data[5], data[4], data[3], data[2], data[1], data[0] }};
85  return x.value;
86  }
87  }
88 
89  static void write(uint8_t* dest, double value)
90  {
91  if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
92  {
93  memcpy(dest, &value, size);
94  }
95  else
96  {
97  auto data = reinterpret_cast<uint8_t*>(&value);
98  uint8_t bytes[8] = { data[7], data[6], data[5], data[4], data[3], data[2], data[1], data[0] };
99  memcpy(dest, bytes, size);
100  }
101  }
102 };
103 
104 }
105 
106 #endif
ser4cpp header-only library namespace
Definition: Buffer.h:33