pclass: Implement floating point primitives

This commit is contained in:
Joshua Scott 2018-10-26 18:40:11 +01:00
parent 1fbfaee8cb
commit fd84614450
4 changed files with 58 additions and 3 deletions

View File

@ -68,4 +68,5 @@ namespace pclass
}
// Include all template specializations
#include "ki/pclass/types/IntegralPrimitiveType.h"
#include "ki/pclass/types/IntegralPrimitiveType.h"
#include "ki/pclass/types/FloatingPointPrimitiveType.h"

View File

@ -0,0 +1,51 @@
#pragma once
#include <type_traits>
#include "ki/util/BitTypes.h"
namespace ki
{
namespace pclass
{
template <typename ValueT>
struct PrimitiveTypeWriter<
ValueT,
typename std::enable_if<std::is_floating_point<ValueT>::value>::type
>
{
static void write_to(BitStream &stream, const ValueT &value)
{
// Reinterpret the reference as a reference to an integer
const uint_type &v = *((const uint_type *)&value);
stream.write<uint_type>(v, bitsizeof<ValueT>::value);
}
private:
/**
* An unsigned integer type with the same size as the floating point type
* ValueT.
*/
using uint_type = typename bits<bitsizeof<ValueT>::value>::uint_type;
};
template <typename ValueT>
struct PrimitiveTypeReader<
ValueT,
typename std::enable_if<std::is_floating_point<ValueT>::value>::type
>
{
static void read_from(BitStream &stream, ValueT &value)
{
// Reinterpret the reference as a reference to an integer
uint_type &v = *((uint_type *)&value);
v = stream.read<uint_type>(bitsizeof<ValueT>::value);
}
private:
/**
* An unsigned integer type with the same size as the floating point type
* ValueT.
*/
using uint_type = typename bits<bitsizeof<ValueT>::value>::uint_type;
};
}
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <type_traits>
#include "ki/util/BitTypes.h"
namespace ki
{

View File

@ -65,8 +65,10 @@ namespace pclass
define_primitive<bi<24>>("s24");
define_primitive<bui<24>>("u24");
// TODO: Define bit integer types
// TODO: Define floating point types
// Define floating point types
define_primitive<float>("float");
define_primitive<double>("double");
// TODO: Define bit floating point types
// TODO: Define string types
}