#pragma once #include #include namespace nall { struct any { any() = default; any(const any& source) { operator=(source); } any(any&& source) { operator=(move(source)); } template any(const T& value) { operator=(value); } ~any() { reset(); } explicit operator bool() const { return container; } auto reset() -> void { if(container) { delete container; container = nullptr; } } auto type() const -> const std::type_info& { return container ? container->type() : typeid(void); } template auto is() const -> bool { return type() == typeid(typename remove_reference::type); } template auto get() -> T& { if(!is()) throw; return static_cast::type>*>(container)->value; } template auto get() const -> const T& { if(!is()) throw; return static_cast::type>*>(container)->value; } template auto get(const T& fallback) const -> const T& { if(!is()) return fallback; return static_cast::type>*>(container)->value; } template auto operator=(const T& value) -> any& { using auto_t = type_if, typename remove_extent::type>::type*, T>; if(type() == typeid(auto_t)) { static_cast*>(container)->value = (auto_t)value; } else { if(container) delete container; container = new holder((auto_t)value); } return *this; } auto operator=(const any& source) -> any& { if(container) { delete container; container = nullptr; } if(source.container) container = source.container->copy(); return *this; } auto operator=(any&& source) -> any& { if(container) delete container; container = source.container; source.container = nullptr; return *this; } private: struct placeholder { virtual ~placeholder() = default; virtual auto type() const -> const std::type_info& = 0; virtual auto copy() const -> placeholder* = 0; }; placeholder* container = nullptr; template struct holder : placeholder { holder(const T& value) : value(value) {} auto type() const -> const std::type_info& { return typeid(T); } auto copy() const -> placeholder* { return new holder(value); } T value; }; }; }