diff --git a/include/ki/pclass/Value.h b/include/ki/pclass/Value.h index bb8805d..be1c2ff 100644 --- a/include/ki/pclass/Value.h +++ b/include/ki/pclass/Value.h @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include "ki/util/exception.h" namespace ki @@ -160,18 +160,18 @@ namespace pclass * A static lookup used to find appropriate casters at runtime. * Contains SrcT -> Caster elements. */ - static std::map s_caster_lookup; + static std::unordered_map s_caster_lookup; const std::type_info *m_src_type; - std::map m_casts; + std::unordered_map m_casts; ValueCaster(); explicit ValueCaster(const std::type_info &src_type); /** - * @tparam SrcT The cast source type. - * @returns The ValueCaster instance responsible for casting values of type SrcT. - */ + * @tparam SrcT The cast source type. + * @returns The ValueCaster instance responsible for casting values of type SrcT. + */ template static ValueCaster &get() { @@ -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(value.get()); + return Value::make_value( + static_cast(value.get()) + ); } }; } diff --git a/src/pclass/TypeSystem.cpp b/src/pclass/TypeSystem.cpp index 7feca83..ad6e521 100644 --- a/src/pclass/TypeSystem.cpp +++ b/src/pclass/TypeSystem.cpp @@ -9,20 +9,29 @@ define_primitive("signed " n); \ define_primitive("unsigned " n) -#define DEFINE_BI_PRIMITIVE(n) \ - define_primitive< bi >("bi" #n) - -#define DEFINE_BUI_PRIMITIVE(n) \ - define_primitive< bui >("bui" #n) - -#define DEFINE_BIT_INTEGER_PRIMITIVE(n) \ - DEFINE_BI_PRIMITIVE(n); \ - DEFINE_BUI_PRIMITIVE(n) - namespace ki { namespace pclass { + template + void define_bit_integer_primitive(pclass::TypeSystem &type_system) + { + define_bit_integer_primitive(type_system); + + // Define the signed bit integer + std::ostringstream oss; + oss << "bi" << N; + type_system.define_primitive>(oss.str()); + + // Define the unsigned bit integer + oss = std::ostringstream(); + oss << "bui" << N; + type_system.define_primitive>(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("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>("s24"); define_primitive>("u24"); diff --git a/src/pclass/Value.cpp b/src/pclass/Value.cpp index ca770d9..4171986 100644 --- a/src/pclass/Value.cpp +++ b/src/pclass/Value.cpp @@ -6,7 +6,7 @@ namespace pclass { namespace detail { - Value value_caster_base::cast(const Value& v) const + Value value_caster_base::cast(const Value &v) const { throw runtime_error("Unimplemented cast."); } @@ -39,7 +39,7 @@ namespace pclass delete m_deallocator; } - void ValueDeallocator::deallocate(void* ptr) const + void ValueDeallocator::deallocate(void *ptr) const { m_deallocator->deallocate(ptr); } @@ -59,7 +59,7 @@ namespace pclass } } - ValueCaster::ValueCaster(const std::type_info& src_type) + ValueCaster::ValueCaster(const std::type_info &src_type) { m_src_type = &src_type; } @@ -73,7 +73,7 @@ namespace pclass m_deallocator = detail::ValueDeallocator(); } - Value::Value(Value&& that) noexcept + Value::Value(Value &&that) noexcept { // Move pointer to this Value object, and take ownership if // the other Value previously owned it. @@ -85,7 +85,7 @@ namespace pclass m_deallocator = std::move(that.m_deallocator); } - Value &Value::operator=(Value&& that) noexcept + Value &Value::operator=(Value &&that) noexcept { // If the current pointer is owned, deallocate it if (m_ptr_is_owned) @@ -111,6 +111,6 @@ namespace pclass } // Initialize the static lookup map - std::map ValueCaster::s_caster_lookup = {}; + std::unordered_map ValueCaster::s_caster_lookup = {}; } }