167 lines
5.9 KiB
C++
167 lines
5.9 KiB
C++
#include "ikarus/values/value.h"
|
|
|
|
#include <string>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <cppbase/templates.hpp>
|
|
|
|
#include <ikarus/objects/properties/property_type.h>
|
|
|
|
#include <values/number_value.hpp>
|
|
#include <values/text_value.hpp>
|
|
#include <values/toggle_value.hpp>
|
|
#include <values/value.hpp>
|
|
|
|
IkarusValue::IkarusValue(Data data):
|
|
_data(data) {}
|
|
|
|
cppbase::Result<IkarusValue, IkarusValue::FromJsonError> IkarusValue::from_json(boost::json::value const& json) {
|
|
bool const * intermediate = nullptr;
|
|
boost::int64_t const * type = nullptr;
|
|
boost::json::array const * data = nullptr;
|
|
|
|
if (auto const * obj = json.if_object(); obj == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
} else {
|
|
if (auto const * type_value = obj->if_contains("type"); type_value == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
} else if (type = type_value->if_int64(); type == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
}
|
|
|
|
if (auto const * intermediate_value = obj->if_contains("intermediate"); intermediate_value == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
} else if (intermediate = intermediate_value->if_bool(); intermediate == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
}
|
|
|
|
if (auto const * data = obj->if_contains("data"); data == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
} else if (auto * array = data->if_array(); array == nullptr) {
|
|
return cppbase::err(FromJsonError{});
|
|
}
|
|
|
|
auto assign_value =
|
|
[]<typename T>(
|
|
auto const& value, boost::json::array const& array, auto convert = [](auto const& val) { return val; }
|
|
) {
|
|
for (auto const& data : array) {
|
|
if (auto res = boost::json::try_value_to<T>(data); res.has_error()) {
|
|
return cppbase::err(FromJsonError{});
|
|
} else {
|
|
value->get_data().push_back(convert(res.value()));
|
|
return cppbase::ok();
|
|
}
|
|
}
|
|
};
|
|
|
|
switch (*type) {
|
|
case IkarusPropertyType_Toggle: {
|
|
auto * ret = new IkarusToggleValue{};
|
|
ret->set_intermediate(*intermediate);
|
|
|
|
assign_value.operator()<bool, bool>(ret, *data);
|
|
}
|
|
case IkarusPropertyType_Number: {
|
|
auto * ret = new IkarusToggleValue{};
|
|
ret->set_intermediate(*intermediate);
|
|
|
|
assign_value.operator()<long double>(ret, *data);
|
|
}
|
|
case IkarusPropertyType_Text: {
|
|
auto * ret = new IkarusToggleValue{};
|
|
ret->set_intermediate(*intermediate);
|
|
|
|
assign_value.operator()<std::string>(ret, *data);
|
|
}
|
|
default: return cppbase::err(FromJsonError{});
|
|
}
|
|
}
|
|
}
|
|
|
|
boost::json::value IkarusValue::to_json() const {
|
|
return {
|
|
{"indeterminate", _indeterminate},
|
|
{"type",
|
|
std::visit(
|
|
cppbase::overloaded{
|
|
[]([[maybe_unused]] IkarusToggleValue const * value) { return IkarusPropertyType_Toggle; },
|
|
[]([[maybe_unused]] IkarusNumberValue const * value) { return IkarusPropertyType_Number; },
|
|
[]([[maybe_unused]] IkarusTextValue const * value) { return IkarusPropertyType_Text; },
|
|
}, _data
|
|
)},
|
|
{"data", std::visit([](auto const * value) { return boost::json::value_from(value->get_data()); }, _data)}
|
|
};
|
|
}
|
|
|
|
bool IkarusValue::is_indeterminate() const {
|
|
return _indeterminate;
|
|
}
|
|
|
|
void IkarusValue::set_indeterminate(bool value) {
|
|
_indeterminate = value;
|
|
}
|
|
|
|
IkarusValue::Data& IkarusValue::get_data() {
|
|
return _data;
|
|
}
|
|
|
|
IkarusValue::Data const& IkarusValue::get_data() const {
|
|
return _data;
|
|
}
|
|
|
|
bool ikarus_value_is_indeterminate(IkarusValue const * value) {
|
|
return value->is_indeterminate();
|
|
}
|
|
|
|
void ikarus_value_set_indeterminate(IkarusValue * value, bool indeterminate) {
|
|
value->set_intermediate(indeterminate);
|
|
}
|
|
|
|
char const * ikarus_value_to_string(IkarusValue const * value) {
|
|
auto const str = std::visit(
|
|
cppbase::overloaded{
|
|
[](IkarusToggleValue const * toggle_value) -> std::string { return toggle_value->get_data() ? "true" : "false"; },
|
|
[](IkarusNumberValue const * number_value) -> std::string { return fmt::format("{}", number_value->get_value()); },
|
|
[](IkarusTextValue const * text_value) -> std::string { return fmt::format("{}", text_value->get_value()); },
|
|
},
|
|
value->get_data()
|
|
);
|
|
|
|
return strdup(str.data());
|
|
}
|
|
|
|
void ikarus_value_visit(
|
|
IkarusValue * value,
|
|
void (*toggle_visitor)(IkarusToggleValue *, void *),
|
|
void (*number_visitor)(IkarusNumberValue *, void *),
|
|
void (*text_visitor)(IkarusTextValue *, void *),
|
|
void * data
|
|
) {
|
|
std::visit(
|
|
cppbase::overloaded{
|
|
[toggle_visitor, data](IkarusToggleValue * toggle_value) { toggle_visitor(toggle_value, data); },
|
|
[number_visitor, data](IkarusNumberValue * number_value) { number_visitor(number_value, data); },
|
|
[text_visitor, data](IkarusTextValue * text_value) { text_visitor(text_value, data); },
|
|
},
|
|
value->get_data()
|
|
);
|
|
}
|
|
|
|
void ikarus_value_visit_const(
|
|
IkarusValue const * value,
|
|
void (*toggle_visitor)(IkarusToggleValue const *, void *),
|
|
void (*number_visitor)(IkarusNumberValue const *, void *),
|
|
void (*text_visitor)(IkarusTextValue const *, void *),
|
|
void * data
|
|
) {
|
|
std::visit(
|
|
cppbase::overloaded{
|
|
[toggle_visitor, data](IkarusToggleValue const * toggle_value) { toggle_visitor(toggle_value, data); },
|
|
[number_visitor, data](IkarusNumberValue const * number_value) { number_visitor(number_value, data); },
|
|
[text_visitor, data](IkarusTextValue const * text_value) { text_visitor(text_value, data); },
|
|
},
|
|
value->get_data()
|
|
);
|
|
}
|