2014-01-10 01:01:29 +04:00
|
|
|
#ifndef value_hh_INCLUDED
|
|
|
|
#define value_hh_INCLUDED
|
|
|
|
|
2014-12-16 21:57:19 +03:00
|
|
|
#include "unordered_map.hh"
|
2014-01-10 01:01:29 +04:00
|
|
|
#include "units.hh"
|
|
|
|
|
2014-12-16 21:57:19 +03:00
|
|
|
#include <memory>
|
|
|
|
|
2014-01-10 01:01:29 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
struct bad_value_cast {};
|
|
|
|
|
|
|
|
struct Value
|
|
|
|
{
|
|
|
|
Value() = default;
|
|
|
|
|
|
|
|
template<typename T>
|
2014-10-28 22:17:54 +03:00
|
|
|
Value(T&& val) : m_value{new Model<T>{std::forward<T>(val)}} {}
|
2014-01-10 01:01:29 +04:00
|
|
|
|
2014-11-05 01:18:19 +03:00
|
|
|
Value(const Value& val) = delete;
|
2014-01-10 01:01:29 +04:00
|
|
|
Value(Value&&) = default;
|
|
|
|
|
2014-11-05 01:18:19 +03:00
|
|
|
Value& operator=(const Value& val) = delete;
|
2014-01-10 01:01:29 +04:00
|
|
|
Value& operator=(Value&& val) = default;
|
|
|
|
|
|
|
|
explicit operator bool() const { return (bool)m_value; }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool is_a() const
|
|
|
|
{
|
|
|
|
return m_value and m_value->type() == typeid(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& as()
|
|
|
|
{
|
|
|
|
if (not is_a<T>())
|
|
|
|
throw bad_value_cast{};
|
|
|
|
return static_cast<Model<T>*>(m_value.get())->m_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T& as() const
|
|
|
|
{
|
|
|
|
return const_cast<Value*>(this)->as<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Concept
|
|
|
|
{
|
|
|
|
virtual ~Concept() {}
|
|
|
|
virtual const std::type_info& type() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Model : public Concept
|
|
|
|
{
|
|
|
|
Model(T&& val) : m_content(std::move(val)) {}
|
|
|
|
const std::type_info& type() const override { return typeid(T); }
|
|
|
|
|
|
|
|
T m_content;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Concept> m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ValueId : public StronglyTypedNumber<ValueId, int>
|
|
|
|
{
|
2014-11-13 00:27:07 +03:00
|
|
|
constexpr ValueId(int value = 0) : StronglyTypedNumber(value) {}
|
2014-01-10 01:01:29 +04:00
|
|
|
|
|
|
|
static ValueId get_free_id()
|
|
|
|
{
|
|
|
|
static ValueId next;
|
|
|
|
return next++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-16 21:57:19 +03:00
|
|
|
inline size_t hash_value(ValueId val) { return hash_value((int)val); }
|
2014-01-10 01:01:29 +04:00
|
|
|
|
2014-12-16 21:57:19 +03:00
|
|
|
using ValueMap = UnorderedMap<ValueId, Value>;
|
2014-01-10 01:01:29 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // value_hh_INCLUDED
|