50class TOAST_API DataValue {
52 using ObjectMap = std::vector<std::pair<std::string, DataValue>>;
53 using Array = std::vector<DataValue>;
55 DataValue() =
default;
57 explicit DataValue(
bool v) : m_type(DataType::bool_t), m_value(v) { }
59 explicit DataValue(int64_t v) : m_type(DataType::int_t), m_value(v) { }
61 explicit DataValue(
double v) : m_type(DataType::float_t), m_value(v) { }
63 explicit DataValue(std::string v) : m_type(DataType::string_t), m_value(std::move(v)) { }
65 DataValue(
toast::UID v, DataType kind) : m_type(kind), m_value(v) {
67 kind == DataType::node_t || kind == DataType::asset_t,
"DataValue",
"UID constructor requires Node or Asset DataType"
71 explicit DataValue(glm::vec2 v) : m_type(DataType::vec2_t), m_value(v) { }
73 DataValue(glm::vec3 v, DataType kind) : m_type(kind), m_value(v) {
75 kind == DataType::vec3_t || kind == DataType::color3_t,
"DataValue",
"vec3 constructor requires Vec3 or Color3 DataType"
79 explicit DataValue(glm::vec4 v) : m_type(DataType::color4_t), m_value(v) { }
81 static auto makeArray() -> DataValue;
82 static auto makeObject() -> DataValue;
85 auto type()
const noexcept -> DataType {
90 auto isNull()
const noexcept ->
bool {
91 return m_type == DataType::null;
95 auto isArray()
const noexcept ->
bool {
96 return m_type == DataType::array_t;
100 auto isObject()
const noexcept ->
bool {
101 return m_type == DataType::object_t;
106 auto as()
const -> T {
107 TOAST_ASSERT(m_value.has_value(),
"DataValue",
"as<T>() called on a Null DataValue");
108 if constexpr (std::is_same_v<T, std::string_view>) {
110 if (
const auto* str = std::any_cast<std::string>(&m_value)) {
113 }
else if constexpr (std::is_same_v<T, float>) {
114 if (
const auto* ptr = std::any_cast<float>(&m_value)) {
117 if (
const auto* ptr = std::any_cast<double>(&m_value)) {
118 return static_cast<float>(*ptr);
120 }
else if (
const T* ptr = std::any_cast<T>(&m_value)) {
126 "Type mismatch in DataValue::as<{}>(): requested '{}', but m_value contains '{}'",
129 m_value.type().name()
136 auto value()
const -> std::optional<T> {
137 if (!m_value.has_value()) {
140 if (
const T* ptr = std::any_cast<T>(&m_value)) {
143 TOAST_WARN(
"DataValue",
"Type mismatch in DataValue::value<T>()");
147 explicit operator bool()
const;
148 explicit operator int64_t()
const;
149 explicit operator int()
const;
150 explicit operator float()
const;
151 explicit operator double()
const;
152 explicit operator std::string()
const;
154 explicit operator glm::vec2()
const;
155 explicit operator glm::vec3()
const;
156 explicit operator glm::vec4()
const;
158 auto operator[](std::string_view key) -> DataValue&;
159 auto operator[](std::string_view key)
const ->
const DataValue&;
162 auto contains(std::string_view key)
const noexcept -> bool;
164 auto operator[](
size_t index) -> DataValue&;
165 auto operator[](
size_t index)
const ->
const DataValue&;
168 auto size()
const noexcept -> size_t;
171 auto empty()
const noexcept ->
bool {
175 void push(DataValue v);
176 void set(std::string key, DataValue v);
177 void remove(std::string_view key);
180 auto items() -> ObjectMap&;
182 auto items()
const ->
const ObjectMap&;
184 auto operator=(
bool v) -> DataValue&;
185 auto operator=(int64_t v) -> DataValue&;
186 auto operator=(
int v) -> DataValue&;
187 auto operator=(
double v) -> DataValue&;
188 auto operator=(
float v) -> DataValue&;
189 auto operator=(std::string v) -> DataValue&;
194 static auto fromToml(
const toml::node& n,
const SchemaField* field,
const toml::table* root =
nullptr) -> DataValue;
199 static auto fromObject(
const toml::table& t,
const std::vector<SchemaField>& fields,
const toml::table* root =
nullptr)
203 void appendTo(toml::table& tbl,
const std::string& key)
const;
206 void appendTo(toml::array& arr)
const;
214 auto toJson()
const -> json_t;
217 static auto fromJson(
const json_t& j, DataType hint = DataType::null) -> DataValue;
220 DataType m_type {DataType::null};
static auto fromObject(const toml::table &t, const std::vector< SchemaField > &fields, const toml::table *root=nullptr) -> DataValue
Build an Object DataValue from a TOML table, guided by a field list.
Definition data_value.cpp:422
static auto fromToml(const toml::node &n, const SchemaField *field, const toml::table *root=nullptr) -> DataValue
Parse a DataValue from a TOML node, using the schema field for type context.
Definition data_value.cpp:242