From f9ab6560d9f1bd3f8bd9c613c6ed6e1cafd705d3 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Sat, 15 Dec 2018 22:03:08 +0000 Subject: [PATCH] pclass: Fix CI build errors + small change to value_caster --- include/ki/pclass/Casters.h | 76 ++++++++++++++------------------- include/ki/pclass/TypeSystem.h | 2 +- include/ki/pclass/Value.h | 46 +++++++++++--------- test/src/unit-serialization.cpp | 29 ++++++------- 4 files changed, 72 insertions(+), 81 deletions(-) diff --git a/include/ki/pclass/Casters.h b/include/ki/pclass/Casters.h index 543c6c0..e1222ce 100644 --- a/include/ki/pclass/Casters.h +++ b/include/ki/pclass/Casters.h @@ -2,8 +2,8 @@ #include #include #include -#include "ki/util/BitTypes.h" #include "ki/pclass/Value.h" +#include "ki/util/BitTypes.h" #include "ki/pclass/types/EnumType.h" namespace ki @@ -45,18 +45,17 @@ namespace detail > : value_caster_impl { - Value cast(const Value &value) const override + nlohmann::json cast_value(const SrcT &value) const override { - const nlohmann::json j = value.get(); - return Value::make_value(j); + return value; } }; /** - * value_caster specialization for casting bi and bui + * value_caster specialization for casting bit integers (bi and bui) * to a json object. */ - template + template struct value_caster< BitInteger, nlohmann::json > @@ -68,12 +67,29 @@ namespace detail typename bits::int_type >::type; - Value cast(const Value &value) const override + nlohmann::json cast_value( + const BitInteger &value) const override { - const nlohmann::json j = static_cast( - value.get>() - ); - return Value::make_value(j); + return static_cast(value); + } + }; + + /** + * value_caster specialization for casting enum to bit integer types + * (bi and bui). + */ + template + struct value_caster< + SrcT, BitInteger, + typename std::enable_if::value>::type + > + : value_caster_impl> + { + using underlying_type = typename std::underlying_type::type; + + BitInteger cast_value(const SrcT &value) const override + { + return static_cast(value); } }; @@ -89,36 +105,9 @@ namespace detail { using underlying_type = typename std::underlying_type::type; - Value cast(const Value &value) const override + nlohmann::json cast_value(const SrcT &value) const override { - const auto underlying_value = - static_cast(value.get()); - const nlohmann::json j = underlying_value; - return Value::make_value(j); - } - }; - - /** - * value_caster specialization for casting enums to bit integer types. - */ - template < - typename SrcT, - int N, bool Unsigned - > - struct value_caster< - SrcT, BitInteger, - typename std::enable_if::value>::type - > - : value_caster_impl - { - using underlying_type = typename std::underlying_type::type; - - Value cast(const Value &value) const override - { - const auto underlying_value = - static_cast(value.get()); - const auto bit_value = BitInteger(underlying_value); - return Value::make_value>(bit_value); + return static_cast(value); } }; @@ -164,14 +153,13 @@ namespace detail struct value_caster : value_caster_impl { - Value cast(const Value &value) const override + std::string cast_value(const SrcT &value) const override { std::ostringstream oss; auto casted_value = static_cast< - typename string_cast_t::type - >(value.get()); + typename string_cast_t::type>(value); oss << casted_value; - return Value::make_value(oss.str()); + return oss.str(); } }; diff --git a/include/ki/pclass/TypeSystem.h b/include/ki/pclass/TypeSystem.h index 5608335..c7c07cb 100644 --- a/include/ki/pclass/TypeSystem.h +++ b/include/ki/pclass/TypeSystem.h @@ -2,11 +2,11 @@ #include #include #include "ki/pclass/HashCalculator.h" +#include "ki/pclass/Casters.h" #include "ki/pclass/types/Type.h" #include "ki/pclass/types/PrimitiveType.h" #include "ki/pclass/types/ClassType.h" #include "ki/pclass/types/EnumType.h" -#include "ki/pclass/Casters.h" namespace ki { diff --git a/include/ki/pclass/Value.h b/include/ki/pclass/Value.h index 05b63a8..0c41d94 100644 --- a/include/ki/pclass/Value.h +++ b/include/ki/pclass/Value.h @@ -1,7 +1,6 @@ #pragma once #include #include -#include #include #include "ki/util/exception.h" @@ -44,8 +43,27 @@ namespace pclass template struct value_caster_impl : value_caster_base { + Value cast(const Value& value) const override; + virtual DestT cast_value(const SrcT &value) const = 0; + protected: - static Value bad_cast(); + static DestT bad_cast(); + }; + + /** + * TODO: Documentation + */ + template < + typename SrcT, typename DestT, + typename SrcEnable, typename DestEnable + > + struct value_caster : value_caster_impl + { + DestT cast_value(const SrcT &value) const override + { + // By default, just attempt to static_cast from SrcT to DestT. + return static_cast(value); + } }; /** @@ -386,28 +404,18 @@ namespace pclass namespace detail { template - Value value_caster_impl::bad_cast() + DestT value_caster_impl::bad_cast() { throw cast_error(typeid(SrcT), typeid(DestT)); } - /** - * TODO: Documentation - */ - template < - typename SrcT, typename DestT, - typename SrcEnable, typename DestEnable - > - struct value_caster : value_caster_impl + template + Value value_caster_impl::cast(const Value& value) const { - Value cast(const Value &value) const override - { - // By default, just attempt to static_cast from SrcT to DestT. - return Value::make_value( - static_cast(value.get()) - ); - } - }; + return Value::make_value( + cast_value(value.get()) + ); + } } } } diff --git a/test/src/unit-serialization.cpp b/test/src/unit-serialization.cpp index 542c687..f3bff89 100644 --- a/test/src/unit-serialization.cpp +++ b/test/src/unit-serialization.cpp @@ -3,13 +3,13 @@ #include #include #include +#include #include #include #include #include #include -#include "ki/util/unique.h" -#include "ki/serialization/JsonSerializer.h" +#include using namespace ki; @@ -108,15 +108,13 @@ namespace detail struct value_caster : value_caster_impl { - Value cast(const Value &value) const override + nlohmann::json cast_value(const Vector3D &value) const { - auto &vector = value.get(); - const nlohmann::json j = { - { "x", vector.m_x }, - { "y", vector.m_y }, - { "z", vector.m_z } + return { + { "x", value.m_x }, + { "y", value.m_y }, + { "z", value.m_z } }; - return Value::make_value(j); } }; @@ -127,15 +125,12 @@ namespace detail struct value_caster : value_caster_impl { - Value cast(const Value &value) const override + Vector3D cast_value(const nlohmann::json &value) const { - auto &j = value.get(); - return Value::make_value( - Vector3D( - j["x"].get(), - j["y"].get(), - j["z"].get() - ) + return Vector3D( + value["x"].get(), + value["y"].get(), + value["z"].get() ); } };