dml: Specialize floating point types

(also, fixed a typo in the tests for DBL fields)
This commit is contained in:
Joshua Scott 2018-03-30 16:54:02 +01:00
parent 2dad97aded
commit 7903e23d90
3 changed files with 30 additions and 10 deletions

View File

@ -1,4 +1,5 @@
#include "ki/dml/Field.h" #include "ki/dml/Field.h"
#include "ki/util/ValueBytes.h"
namespace ki namespace ki
{ {
@ -7,19 +8,28 @@ namespace dml
template <> template <>
void DblField::write_to(std::ostream &ostream) const void DblField::write_to(std::ostream &ostream) const
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<DBL> data = { m_value };
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[7]);
ostream.write(data.buff, sizeof(DBL));
} }
template <> template <>
void DblField::read_from(std::istream &istream) void DblField::read_from(std::istream &istream)
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<DBL> 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 <> template <>
size_t DblField::get_size() const size_t DblField::get_size() const
{ {
return 0; return sizeof(DBL);
} }
} }
} }

View File

@ -1,4 +1,5 @@
#include "ki/dml/Field.h" #include "ki/dml/Field.h"
#include "ki/util/ValueBytes.h"
namespace ki namespace ki
{ {
@ -7,19 +8,28 @@ namespace dml
template <> template <>
void FltField::write_to(std::ostream &ostream) const void FltField::write_to(std::ostream &ostream) const
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<FLT> data = { m_value };
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[3]);
ostream.write(data.buff, sizeof(FLT));
} }
template <> template <>
void FltField::read_from(std::istream &istream) void FltField::read_from(std::istream &istream)
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<FLT> 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 <> template <>
size_t FltField::get_size() const size_t FltField::get_size() const
{ {
return 0; return sizeof(FLT);
} }
} }
} }

View File

@ -135,7 +135,7 @@ TEST_CASE("Field Serialization", "[dml]")
{ {
record->add_field<DBL>("TestDbl")->set_value(152.4); record->add_field<DBL>("TestDbl")->set_value(152.4);
record->write_to(ss); 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") SECTION("GID Fields")
@ -245,7 +245,7 @@ TEST_CASE("Field Deserialization", "[dml]")
SECTION("DBL Fields") 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); ss.seekg(std::stringstream::beg);
auto *field = record->add_field<DBL>("TestDbl"); auto *field = record->add_field<DBL>("TestDbl");