pclass: Fix CI build errors + small change to value_caster

This commit is contained in:
Joshua Scott 2018-12-15 22:03:08 +00:00
parent c512185115
commit f9ab6560d9
4 changed files with 72 additions and 81 deletions

View File

@ -2,8 +2,8 @@
#include <type_traits>
#include <sstream>
#include <json.hpp>
#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<SrcT, nlohmann::json>
{
Value cast(const Value &value) const override
nlohmann::json cast_value(const SrcT &value) const override
{
const nlohmann::json j = value.get<SrcT>();
return Value::make_value<nlohmann::json>(j);
return value;
}
};
/**
* value_caster specialization for casting bi<N> and bui<N>
* value_caster specialization for casting bit integers (bi<N> and bui<N>)
* to a json object.
*/
template <int N, bool Unsigned>
template <uint8_t N, bool Unsigned>
struct value_caster<
BitInteger<N, Unsigned>, nlohmann::json
>
@ -68,12 +67,29 @@ namespace detail
typename bits<N>::int_type
>::type;
Value cast(const Value &value) const override
nlohmann::json cast_value(
const BitInteger<N, Unsigned> &value) const override
{
const nlohmann::json j = static_cast<type>(
value.get<BitInteger<N, Unsigned>>()
);
return Value::make_value<nlohmann::json>(j);
return static_cast<type>(value);
}
};
/**
* value_caster specialization for casting enum to bit integer types
* (bi<N> and bui<N>).
*/
template <typename SrcT, uint8_t N, bool Unsigned>
struct value_caster<
SrcT, BitInteger<N, Unsigned>,
typename std::enable_if<std::is_enum<SrcT>::value>::type
>
: value_caster_impl<SrcT, BitInteger<N, Unsigned>>
{
using underlying_type = typename std::underlying_type<SrcT>::type;
BitInteger<N, Unsigned> cast_value(const SrcT &value) const override
{
return static_cast<underlying_type>(value);
}
};
@ -89,36 +105,9 @@ namespace detail
{
using underlying_type = typename std::underlying_type<SrcT>::type;
Value cast(const Value &value) const override
nlohmann::json cast_value(const SrcT &value) const override
{
const auto underlying_value =
static_cast<underlying_type>(value.get<SrcT>());
const nlohmann::json j = underlying_value;
return Value::make_value<nlohmann::json>(j);
}
};
/**
* value_caster specialization for casting enums to bit integer types.
*/
template <
typename SrcT,
int N, bool Unsigned
>
struct value_caster<
SrcT, BitInteger<N, Unsigned>,
typename std::enable_if<std::is_enum<SrcT>::value>::type
>
: value_caster_impl<SrcT, nlohmann::json>
{
using underlying_type = typename std::underlying_type<SrcT>::type;
Value cast(const Value &value) const override
{
const auto underlying_value =
static_cast<underlying_type>(value.get<SrcT>());
const auto bit_value = BitInteger<N, Unsigned>(underlying_value);
return Value::make_value<BitInteger<N, Unsigned>>(bit_value);
return static_cast<underlying_type>(value);
}
};
@ -164,14 +153,13 @@ namespace detail
struct value_caster<SrcT, std::string>
: value_caster_impl<SrcT, std::string>
{
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<SrcT>::type
>(value.get<SrcT>());
typename string_cast_t<SrcT>::type>(value);
oss << casted_value;
return Value::make_value<std::string>(oss.str());
return oss.str();
}
};

View File

@ -2,11 +2,11 @@
#include <vector>
#include <unordered_map>
#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
{

View File

@ -1,7 +1,6 @@
#pragma once
#include <utility>
#include <typeinfo>
#include <sstream>
#include <unordered_map>
#include "ki/util/exception.h"
@ -44,8 +43,27 @@ namespace pclass
template <typename SrcT, typename DestT>
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<SrcT, DestT>
{
DestT cast_value(const SrcT &value) const override
{
// By default, just attempt to static_cast from SrcT to DestT.
return static_cast<DestT>(value);
}
};
/**
@ -386,28 +404,18 @@ namespace pclass
namespace detail
{
template <typename SrcT, typename DestT>
Value value_caster_impl<SrcT, DestT>::bad_cast()
DestT value_caster_impl<SrcT, DestT>::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<SrcT, DestT>
template <typename SrcT, typename DestT>
Value value_caster_impl<SrcT, DestT>::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<DestT>(
static_cast<DestT>(value.get<SrcT>())
cast_value(value.get<SrcT>())
);
}
};
}
}
}

View File

@ -3,13 +3,13 @@
#include <cstdint>
#include <string>
#include <catch.hpp>
#include <ki/util/unique.h>
#include <ki/pclass/TypeSystem.h>
#include <ki/pclass/PropertyClass.h>
#include <ki/pclass/StaticProperty.h>
#include <ki/pclass/VectorProperty.h>
#include <ki/serialization/BinarySerializer.h>
#include "ki/util/unique.h"
#include "ki/serialization/JsonSerializer.h"
#include <ki/serialization/JsonSerializer.h>
using namespace ki;
@ -108,15 +108,13 @@ namespace detail
struct value_caster<Vector3D, nlohmann::json>
: value_caster_impl<Vector3D, nlohmann::json>
{
Value cast(const Value &value) const override
nlohmann::json cast_value(const Vector3D &value) const
{
auto &vector = value.get<Vector3D>();
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<nlohmann::json>(j);
}
};
@ -127,15 +125,12 @@ namespace detail
struct value_caster<nlohmann::json, Vector3D>
: value_caster_impl<nlohmann::json, Vector3D>
{
Value cast(const Value &value) const override
Vector3D cast_value(const nlohmann::json &value) const
{
auto &j = value.get<nlohmann::json>();
return Value::make_value<Vector3D>(
Vector3D(
j["x"].get<float>(),
j["y"].get<float>(),
j["z"].get<float>()
)
return Vector3D(
value["x"].get<float>(),
value["y"].get<float>(),
value["z"].get<float>()
);
}
};