From 7903e23d90ebca87b245f455f106d36a60639ec0 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 30 Mar 2018 16:54:02 +0100 Subject: [PATCH] dml: Specialize floating point types (also, fixed a typo in the tests for DBL fields) --- src/dml/types/DblField.cpp | 18 ++++++++++++++---- src/dml/types/FltField.cpp | 18 ++++++++++++++---- test/src/unit-dml.cpp | 4 ++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/dml/types/DblField.cpp b/src/dml/types/DblField.cpp index 7eaf895..9d1c2e8 100644 --- a/src/dml/types/DblField.cpp +++ b/src/dml/types/DblField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,28 @@ namespace dml template <> void DblField::write_to(std::ostream &ostream) const { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data = { m_value }; + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[7]); + ostream.write(data.buff, sizeof(DBL)); } template <> void DblField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(DBL)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[3]); + m_value = data.value; } template <> size_t DblField::get_size() const { - return 0; + return sizeof(DBL); } } -} \ No newline at end of file +} diff --git a/src/dml/types/FltField.cpp b/src/dml/types/FltField.cpp index 13a1e46..bfa3fd6 100644 --- a/src/dml/types/FltField.cpp +++ b/src/dml/types/FltField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,28 @@ namespace dml template <> void FltField::write_to(std::ostream &ostream) const { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data = { m_value }; + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[3]); + ostream.write(data.buff, sizeof(FLT)); } template <> void FltField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(FLT)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[3]); + m_value = data.value; } template <> size_t FltField::get_size() const { - return 0; + return sizeof(FLT); } } -} \ No newline at end of file +} diff --git a/test/src/unit-dml.cpp b/test/src/unit-dml.cpp index de77e31..7c5fc5d 100644 --- a/test/src/unit-dml.cpp +++ b/test/src/unit-dml.cpp @@ -135,7 +135,7 @@ TEST_CASE("Field Serialization", "[dml]") { record->add_field("TestDbl")->set_value(152.4); record->write_to(ss); - REQUIRE(ss.str() == "\xCD\xCC\xCC\xCC\xCC\x0C\x64\x40"); + REQUIRE(ss.str() == "\xCD\xCC\xCC\xCC\xCC\x0C\x63\x40"); } SECTION("GID Fields") @@ -245,7 +245,7 @@ TEST_CASE("Field Deserialization", "[dml]") SECTION("DBL Fields") { - ss.write("\xCD\xCC\xCC\xCC\xCC\x0C\x64\x40", 8); + ss.write("\xCD\xCC\xCC\xCC\xCC\x0C\x63\x40", 8); ss.seekg(std::stringstream::beg); auto *field = record->add_field("TestDbl");