Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit b8649f5

Browse files
committed
Lazy-initialize byte order in FloatByteOrder.
1 parent 2a69b0c commit b8649f5

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

src/ser4cpp/serialization/DoubleFloat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class DoubleFloat : private StaticOnly
7474

7575
static double read(const uint8_t* data)
7676
{
77-
if (FloatByteOrder::get_byte_order() == FloatByteOrder::Value::normal)
77+
if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
7878
{
7979
DoubleFloatUnion x = {{ data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7] }};
8080
return x.value;
@@ -88,7 +88,7 @@ class DoubleFloat : private StaticOnly
8888

8989
static void write(uint8_t* dest, double value)
9090
{
91-
if (FloatByteOrder::get_byte_order() == FloatByteOrder::Value::normal)
91+
if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
9292
{
9393
memcpy(dest, &value, size);
9494
}

src/ser4cpp/serialization/FloatByteOrder.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,20 @@ struct FloatByteOrder : private StaticOnly
4141
reverse,
4242
unsupported
4343
};
44-
44+
45+
static Value order()
46+
{
47+
static Value order = get_byte_order();
48+
return order;
49+
}
50+
51+
private:
52+
union FloatUnion
53+
{
54+
uint8_t bytes[4];
55+
float f;
56+
};
57+
4558
static Value get_byte_order()
4659
{
4760
if (is_normal_byte_order())
@@ -58,13 +71,6 @@ struct FloatByteOrder : private StaticOnly
5871
}
5972
}
6073

61-
private:
62-
union FloatUnion
63-
{
64-
uint8_t bytes[4];
65-
float f;
66-
};
67-
6874
static bool is_normal_byte_order()
6975
{
7076
FloatUnion value = {{ 0x00, 0x00, 0xA0, 0xC1 }};

src/ser4cpp/serialization/SingleFloat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SingleFloat : private StaticOnly
7070

7171
static float read(const uint8_t* data)
7272
{
73-
if (FloatByteOrder::get_byte_order() == FloatByteOrder::Value::normal)
73+
if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
7474
{
7575
SingleFloatUnion x = {{ data[0], data[1], data[2], data[3] }};
7676
return x.value;
@@ -84,7 +84,7 @@ class SingleFloat : private StaticOnly
8484

8585
static void write(uint8_t* dest, float value)
8686
{
87-
if (FloatByteOrder::get_byte_order() == FloatByteOrder::Value::normal)
87+
if (FloatByteOrder::order() == FloatByteOrder::Value::normal)
8888
{
8989
memcpy(dest, &value, size);
9090
}

test/TestFloatSerialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool TestFloatParsing(std::string expected_hex, typename T::type_t value)
5858

5959
TEST_CASE(SUITE("Float memory byte order is IEEE 754"))
6060
{
61-
REQUIRE(ser4cpp::FloatByteOrder::get_byte_order() != FloatByteOrder::Value::unsupported);
61+
REQUIRE(ser4cpp::FloatByteOrder::order() != FloatByteOrder::Value::unsupported);
6262
}
6363

6464
TEST_CASE(SUITE("DoubleFloatSerialization"))

0 commit comments

Comments
 (0)