#include "Value.hpp" #include "Type.hpp" namespace sk { /*static*/ std::shared_ptr Value::make_int(int value) { auto res = std::make_shared(std::make_shared(TYPE_INT)); res->m_int_val = value; return res; } /*static*/ std::shared_ptr Value::make_float(float value) { auto res = std::make_shared(std::make_shared(TYPE_FLOAT)); res->m_float_val = value; return res; } /*static*/ std::shared_ptr Value::make_bool(bool value) { auto res = std::make_shared(std::make_shared(TYPE_BOOL)); res->m_bool_val = value; return res; } /*static*/ std::shared_ptr Value::make_string(std::string const& value) { auto res = std::make_shared(std::make_shared(TYPE_STRING)); res->m_string_val = value; return res; } /*explicit*/ Value::Value(std::shared_ptr type) : m_type { type } { } /*virtual*/ Value::~Value() { } bool Value::equals(Value const& rhs) const { return m_int_val == rhs.m_int_val && m_float_val == rhs.m_float_val && m_bool_val == rhs.m_bool_val && m_string_val == rhs.m_string_val; } std::string Value::string() const { switch (m_type->type()) { case TYPE_INT: return std::to_string(as_int()); case TYPE_FLOAT: return std::to_string(as_float()); case TYPE_BOOL: return as_bool() ? "true" : "false"; case TYPE_STRING: return as_string().substr(1, as_string().size()-2); default: throw std::runtime_error {std::string() + "cannot stringify unknown type '" + TypeTypeStr[m_type->type()] + "'"}; } } std::weak_ptr Value::type() const { return m_type; } }