libki/include/ki/pclass/Value.h

168 lines
3.5 KiB
C
Raw Normal View History

2018-10-25 10:39:16 +00:00
#pragma once
#include <utility>
#include <typeinfo>
2018-12-04 22:24:26 +00:00
#include <sstream>
#include "ki/util/exception.h"
2018-10-25 10:39:16 +00:00
namespace ki
{
namespace pclass
{
2018-12-04 22:24:26 +00:00
class Value;
namespace detail
{
template <typename SrcT>
struct value_caster_helper;
/**
* TODO: Documentation
*/
struct value_caster_base
{
virtual ~value_caster_base() {}
2018-12-05 00:45:01 +00:00
virtual Value cast(const std::type_info &dst_type, Value &v) const = 0;
2018-12-04 22:24:26 +00:00
protected:
/**
* Provides a nice way for specialized casters to throw with a
* consistent error when casting is not possible.
*/
2018-12-05 00:45:01 +00:00
Value bad_cast(const std::type_info &src_type, const std::type_info &dst_type) const;
2018-12-04 22:24:26 +00:00
};
/**
* TODO: Documentation
*/
struct value_caster
{
// Allow Value to call the default constructor and make
friend Value;
/**
* @tparam DstT The cast destination type.
* @param[in] value The Value that is being casted to the destination type.
* @returns A Value with a reference that has been casted to the destination type.
*/
template <typename DstT>
2018-12-05 00:45:01 +00:00
Value cast(const Value &value) const;
2018-12-04 22:24:26 +00:00
private:
value_caster_base *m_caster;
explicit value_caster(value_caster_base *caster = nullptr)
{
m_caster = caster;
}
/**
* @tparam SrcT
*/
template <typename SrcT>
static value_caster make()
{
return value_caster(new value_caster_helper<SrcT>());
}
};
}
2018-10-25 10:39:16 +00:00
/**
2018-12-04 22:24:26 +00:00
* A wrapper around a void pointer that ensures type safety.
*/
2018-10-25 10:39:16 +00:00
class Value
{
public:
template <typename T>
Value(T &value)
{
m_value_ptr = static_cast<void *>(&value);
m_type_hash = typeid(value).hash_code();
2018-12-04 22:24:26 +00:00
m_caster = detail::value_caster::make<T>();
}
template <typename T>
Value(const T &value)
{
m_value_ptr = const_cast<void *>(static_cast<const void *>(&value));
2018-10-25 10:39:16 +00:00
m_type_hash = typeid(value).hash_code();
2018-12-04 22:24:26 +00:00
m_caster = detail::value_caster::make<T>();
2018-10-25 10:39:16 +00:00
}
Value(Value &&o) noexcept
2018-12-04 22:24:26 +00:00
: m_value_ptr(std::move(o.m_value_ptr))
2018-10-25 10:39:16 +00:00
, m_type_hash(std::move(o.m_type_hash))
2018-12-04 22:24:26 +00:00
, m_caster(std::move(o.m_caster))
2018-10-25 10:39:16 +00:00
{}
/**
2018-12-04 22:24:26 +00:00
* @return Whether or not the value being held is of type T.
*/
2018-10-25 10:39:16 +00:00
template <typename T>
bool is() const
{
// Do the type hashes match?
return m_type_hash == typeid(T).hash_code();
}
/**
2018-12-04 22:24:26 +00:00
* @return A reference to the value being held as the specified type.
*/
2018-10-25 10:39:16 +00:00
template <typename T>
const T &get() const
{
2018-12-04 22:24:26 +00:00
// Do we need to attempt casting?
2018-10-25 10:39:16 +00:00
if (!is<T>())
2018-12-04 22:24:26 +00:00
return m_caster.cast<T>(*this).get<T>();
return *static_cast<T *>(m_value_ptr);
2018-10-25 10:39:16 +00:00
}
/**
2018-12-04 22:24:26 +00:00
* @return A reference to the value being held as the specified type.
*/
2018-10-25 10:39:16 +00:00
template <typename T>
T &get()
{
2018-12-04 22:24:26 +00:00
// Do we need to attempt casting?
2018-10-25 10:39:16 +00:00
if (!is<T>())
2018-12-04 22:24:26 +00:00
return m_caster.cast<T>(*this).get<T>();
return *static_cast<T *>(m_value_ptr);
2018-10-25 10:39:16 +00:00
}
private:
void *m_value_ptr;
std::size_t m_type_hash;
2018-12-04 22:24:26 +00:00
detail::value_caster m_caster;
2018-10-25 10:39:16 +00:00
};
2018-12-04 22:24:26 +00:00
namespace detail
{
inline Value value_caster_base::bad_cast(
2018-12-05 00:45:01 +00:00
const std::type_info& src_type, const std::type_info& dst_type) const
2018-12-04 22:24:26 +00:00
{
std::ostringstream oss;
oss << "Cannot cast Value from " << src_type.name()
<< " to " << dst_type.name() << ".";
throw runtime_error(oss.str());
}
template <typename DstT>
2018-12-05 00:45:01 +00:00
Value value_caster::cast(const Value &value) const
2018-12-04 22:24:26 +00:00
{
2018-12-05 00:45:01 +00:00
return m_caster->cast(typeid(DstT), const_cast<Value &>(value));
2018-12-04 22:24:26 +00:00
}
/**
* TODO: Documentation
*/
template <typename SrcT>
struct value_caster_helper : value_caster_base
{
2018-12-05 00:45:01 +00:00
Value cast(const std::type_info &dst_type, Value& value) const override
2018-12-04 22:24:26 +00:00
{
return bad_cast(typeid(SrcT), dst_type);
}
};
}
}
2018-10-25 10:39:16 +00:00
}