dml: Specialize integer types

(BYT, UBYT, SHRT, USHRT, INT, UINT, and GID)
This commit is contained in:
Joshua Scott 2018-03-30 16:34:59 +01:00
parent 029acce2d0
commit ade0822747
8 changed files with 87 additions and 22 deletions

View File

@ -0,0 +1,10 @@
#pragma once
namespace ki
{
template <typename ValueT>
union ValueBytes {
ValueT value = 0;
char buff[sizeof(ValueT)];
};
}

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,23 @@ namespace dml
template <> template <>
void BytField::write_to(std::ostream &ostream) const void BytField::write_to(std::ostream &ostream) const
{ {
ValueBytes<BYT> data;
data.value = m_value;
ostream.write(data.buff, sizeof(BYT));
} }
template <> template <>
void BytField::read_from(std::istream &istream) void BytField::read_from(std::istream &istream)
{ {
ValueBytes<BYT> data;
istream.read(data.buff, sizeof(BYT));
m_value = data.value;
} }
template <> template <>
size_t BytField::get_size() const size_t BytField::get_size() const
{ {
return 0; return sizeof(BYT);
} }
} }
} }

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,27 @@ namespace dml
template <> template <>
void GidField::write_to(std::ostream &ostream) const void GidField::write_to(std::ostream &ostream) const
{ {
ValueBytes<GID> data = { m_value };
if (data.buff[0] == ((m_value & 0xFF00000000000000) >> 56))
std::reverse(&data.buff[0], &data.buff[7]);
ostream.write(data.buff, sizeof(GID));
} }
template <> template <>
void GidField::read_from(std::istream &istream) void GidField::read_from(std::istream &istream)
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<GID> data;
istream.read(data.buff, sizeof(GID));
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[7]);
m_value = data.value;
} }
template <> template <>
size_t GidField::get_size() const size_t GidField::get_size() const
{ {
return 0; return sizeof(GID);
} }
} }
} }

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,27 @@ namespace dml
template <> template <>
void IntField::write_to(std::ostream &ostream) const void IntField::write_to(std::ostream &ostream) const
{ {
ValueBytes<INT> data = { m_value };
if (data.buff[0] == ((m_value & 0xFF000000) >> 24))
std::reverse(&data.buff[0], &data.buff[3]);
ostream.write(data.buff, sizeof(INT));
} }
template <> template <>
void IntField::read_from(std::istream &istream) void IntField::read_from(std::istream &istream)
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<INT> data;
istream.read(data.buff, sizeof(INT));
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[3]);
m_value = data.value;
} }
template <> template <>
size_t IntField::get_size() const size_t IntField::get_size() const
{ {
return 0; return sizeof(INT);
} }
} }
} }

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,27 @@ namespace dml
template <> template <>
void ShrtField::write_to(std::ostream &ostream) const void ShrtField::write_to(std::ostream &ostream) const
{ {
ValueBytes<SHRT> data = { m_value };
if (data.buff[0] == ((m_value & 0xFF00) >> 8))
std::reverse(&data.buff[0], &data.buff[1]);
ostream.write(data.buff, sizeof(SHRT));
} }
template <> template <>
void ShrtField::read_from(std::istream &istream) void ShrtField::read_from(std::istream &istream)
{ {
const ValueBytes<SHRT> endianness_check = { 0x0102 };
ValueBytes<SHRT> data;
istream.read(data.buff, sizeof(SHRT));
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[1]);
m_value = data.value;
} }
template <> template <>
size_t ShrtField::get_size() const size_t ShrtField::get_size() const
{ {
return 0; return sizeof(USHRT);
} }
} }
} }

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,23 @@ namespace dml
template <> template <>
void UBytField::write_to(std::ostream &ostream) const void UBytField::write_to(std::ostream &ostream) const
{ {
ValueBytes<UBYT> data;
data.value = m_value;
ostream.write(data.buff, sizeof(UBYT));
} }
template <> template <>
void UBytField::read_from(std::istream &istream) void UBytField::read_from(std::istream &istream)
{ {
ValueBytes<UBYT> data;
istream.read(data.buff, sizeof(UBYT));
m_value = data.value;
} }
template <> template <>
size_t UBytField::get_size() const size_t UBytField::get_size() const
{ {
return 0; return sizeof(BYT);
} }
} }
} }

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,27 @@ namespace dml
template <> template <>
void UIntField::write_to(std::ostream &ostream) const void UIntField::write_to(std::ostream &ostream) const
{ {
ValueBytes<UINT> data = { m_value };
if (data.buff[0] == ((m_value & 0xFF000000) >> 24))
std::reverse(&data.buff[0], &data.buff[3]);
ostream.write(data.buff, sizeof(UINT));
} }
template <> template <>
void UIntField::read_from(std::istream &istream) void UIntField::read_from(std::istream &istream)
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<UINT> data;
istream.read(data.buff, sizeof(UINT));
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[3]);
m_value = data.value;
} }
template <> template <>
size_t UIntField::get_size() const size_t UIntField::get_size() const
{ {
return 0; return sizeof(UINT);
} }
} }
} }

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,27 @@ namespace dml
template <> template <>
void UShrtField::write_to(std::ostream &ostream) const void UShrtField::write_to(std::ostream &ostream) const
{ {
ValueBytes<USHRT> data = { m_value };
if (data.buff[0] == ((m_value & 0xFF00) >> 8))
std::reverse(&data.buff[0], &data.buff[1]);
ostream.write(data.buff, sizeof(USHRT));
} }
template <> template <>
void UShrtField::read_from(std::istream &istream) void UShrtField::read_from(std::istream &istream)
{ {
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<USHRT> data;
istream.read(data.buff, sizeof(USHRT));
if (endianness_check.buff[0] == 0x01)
std::reverse(&data.buff[0], &data.buff[1]);
m_value = data.value;
} }
template <> template <>
size_t UShrtField::get_size() const size_t UShrtField::get_size() const
{ {
return 0; return sizeof(USHRT);
} }
} }
} }