From fd8461445024eca3a81b482321c2a7a069ebb97e Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 26 Oct 2018 18:40:11 +0100 Subject: [PATCH] pclass: Implement floating point primitives --- include/ki/pclass/PrimitiveType.h | 3 +- .../pclass/types/FloatingPointPrimitiveType.h | 51 +++++++++++++++++++ .../ki/pclass/types/IntegralPrimitiveType.h | 1 + src/pclass/TypeSystem.cpp | 6 ++- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 include/ki/pclass/types/FloatingPointPrimitiveType.h diff --git a/include/ki/pclass/PrimitiveType.h b/include/ki/pclass/PrimitiveType.h index 306c1b1..825ca3d 100644 --- a/include/ki/pclass/PrimitiveType.h +++ b/include/ki/pclass/PrimitiveType.h @@ -68,4 +68,5 @@ namespace pclass } // Include all template specializations -#include "ki/pclass/types/IntegralPrimitiveType.h" \ No newline at end of file +#include "ki/pclass/types/IntegralPrimitiveType.h" +#include "ki/pclass/types/FloatingPointPrimitiveType.h" \ No newline at end of file diff --git a/include/ki/pclass/types/FloatingPointPrimitiveType.h b/include/ki/pclass/types/FloatingPointPrimitiveType.h new file mode 100644 index 0000000..2ea84c3 --- /dev/null +++ b/include/ki/pclass/types/FloatingPointPrimitiveType.h @@ -0,0 +1,51 @@ +#pragma once +#include +#include "ki/util/BitTypes.h" + +namespace ki +{ + namespace pclass + { + template + struct PrimitiveTypeWriter< + ValueT, + typename std::enable_if::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(v, bitsizeof::value); + } + + private: + /** + * An unsigned integer type with the same size as the floating point type + * ValueT. + */ + using uint_type = typename bits::value>::uint_type; + }; + + template + struct PrimitiveTypeReader< + ValueT, + typename std::enable_if::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(bitsizeof::value); + } + + private: + /** + * An unsigned integer type with the same size as the floating point type + * ValueT. + */ + using uint_type = typename bits::value>::uint_type; + }; + } +} \ No newline at end of file diff --git a/include/ki/pclass/types/IntegralPrimitiveType.h b/include/ki/pclass/types/IntegralPrimitiveType.h index 833153d..f332205 100644 --- a/include/ki/pclass/types/IntegralPrimitiveType.h +++ b/include/ki/pclass/types/IntegralPrimitiveType.h @@ -1,5 +1,6 @@ #pragma once #include +#include "ki/util/BitTypes.h" namespace ki { diff --git a/src/pclass/TypeSystem.cpp b/src/pclass/TypeSystem.cpp index 066e74b..718213b 100644 --- a/src/pclass/TypeSystem.cpp +++ b/src/pclass/TypeSystem.cpp @@ -65,8 +65,10 @@ namespace pclass define_primitive>("s24"); define_primitive>("u24"); - // TODO: Define bit integer types - // TODO: Define floating point types + // Define floating point types + define_primitive("float"); + define_primitive("double"); + // TODO: Define bit floating point types // TODO: Define string types }