adjust values api to vector-esque interface
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
2e41c36d84
commit
8ad9869d0d
13 changed files with 297 additions and 220 deletions
|
|
@ -1,9 +1,13 @@
|
|||
#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>
|
||||
|
|
@ -12,11 +16,90 @@
|
|||
IkarusValue::IkarusValue(Data data):
|
||||
_data(data) {}
|
||||
|
||||
bool IkarusValue::is_interminate() const {
|
||||
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_intermediate(bool value) {
|
||||
void IkarusValue::set_indeterminate(bool value) {
|
||||
_indeterminate = value;
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +112,7 @@ IkarusValue::Data const& IkarusValue::get_data() const {
|
|||
}
|
||||
|
||||
bool ikarus_value_is_indeterminate(IkarusValue const * value) {
|
||||
return value->is_interminate();
|
||||
return value->is_indeterminate();
|
||||
}
|
||||
|
||||
void ikarus_value_set_indeterminate(IkarusValue * value, bool indeterminate) {
|
||||
|
|
@ -39,18 +122,14 @@ void ikarus_value_set_indeterminate(IkarusValue * value, bool 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_value() ? "true" : "false"; },
|
||||
[](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()
|
||||
);
|
||||
|
||||
auto * const ret = new char[str.size() + 1];
|
||||
|
||||
std::strncpy(ret, str.data(), str.size() + 1);
|
||||
|
||||
return ret;
|
||||
return strdup(str.data());
|
||||
}
|
||||
|
||||
void ikarus_value_visit(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue