pclass: Fix a potential future problem

This commit is contained in:
Joshua Scott 2018-12-13 00:57:56 +00:00
parent 9e5ca2816f
commit 1955e72bb9
3 changed files with 35 additions and 30 deletions

View File

@ -2,7 +2,7 @@
#include <utility>
#include <typeinfo>
#include <sstream>
#include <map>
#include <unordered_map>
#include "ki/util/exception.h"
namespace ki
@ -160,10 +160,10 @@ namespace pclass
* A static lookup used to find appropriate casters at runtime.
* Contains SrcT -> Caster elements.
*/
static std::map<std::size_t, ValueCaster *> s_caster_lookup;
static std::unordered_map<std::size_t, ValueCaster *> s_caster_lookup;
const std::type_info *m_src_type;
std::map<std::size_t, detail::value_caster_base *> m_casts;
std::unordered_map<std::size_t, detail::value_caster_base *> m_casts;
ValueCaster();
explicit ValueCaster(const std::type_info &src_type);
@ -394,7 +394,9 @@ namespace pclass
Value cast(const Value &value) const override
{
// By default, just attempt to static_cast from SrcT to DestT.
return static_cast<DestT>(value.get<SrcT>());
return Value::make_value<DestT>(
static_cast<DestT>(value.get<SrcT>())
);
}
};
}

View File

@ -9,20 +9,29 @@
define_primitive<st>("signed " n); \
define_primitive<ut>("unsigned " n)
#define DEFINE_BI_PRIMITIVE(n) \
define_primitive< bi<n> >("bi" #n)
#define DEFINE_BUI_PRIMITIVE(n) \
define_primitive< bui<n> >("bui" #n)
#define DEFINE_BIT_INTEGER_PRIMITIVE(n) \
DEFINE_BI_PRIMITIVE(n); \
DEFINE_BUI_PRIMITIVE(n)
namespace ki
{
namespace pclass
{
template <int N>
void define_bit_integer_primitive(pclass::TypeSystem &type_system)
{
define_bit_integer_primitive<N - 1>(type_system);
// Define the signed bit integer
std::ostringstream oss;
oss << "bi" << N;
type_system.define_primitive<bi<N>>(oss.str());
// Define the unsigned bit integer
oss = std::ostringstream();
oss << "bui" << N;
type_system.define_primitive<bui<N>>(oss.str());
}
template <>
void define_bit_integer_primitive<0>(TypeSystem &type_system) {}
TypeSystem::TypeSystem(HashCalculator *hash_calculator)
{
m_hash_calculator = hash_calculator;
@ -49,13 +58,7 @@ namespace pclass
define_primitive<uint64_t>("gid");
// Define bit-integer types
DEFINE_BIT_INTEGER_PRIMITIVE(1);
DEFINE_BIT_INTEGER_PRIMITIVE(2);
DEFINE_BIT_INTEGER_PRIMITIVE(3);
DEFINE_BIT_INTEGER_PRIMITIVE(4);
DEFINE_BIT_INTEGER_PRIMITIVE(5);
DEFINE_BIT_INTEGER_PRIMITIVE(6);
DEFINE_BIT_INTEGER_PRIMITIVE(7);
define_bit_integer_primitive<7>(*this);
define_primitive<bi<24>>("s24");
define_primitive<bui<24>>("u24");

View File

@ -111,6 +111,6 @@ namespace pclass
}
// Initialize the static lookup map
std::map<std::size_t, ValueCaster *> ValueCaster::s_caster_lookup = {};
std::unordered_map<std::size_t, ValueCaster *> ValueCaster::s_caster_lookup = {};
}
}