add src/ikarus subdir and make names unique for objects per scope
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
c65211e4ff
commit
5dce2ced94
51 changed files with 590 additions and 735 deletions
138
src/ikarus/values/value.cpp
Normal file
138
src/ikarus/values/value.cpp
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
#include "ikarus/values/value.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
// required for header-only inclusion
|
||||
#include "cppbase/templates.hpp"
|
||||
|
||||
#include <boost/json/src.hpp>
|
||||
|
||||
#include <ikarus/objects/properties/property_type.h>
|
||||
#include <ikarus/values/number_value.hpp>
|
||||
#include <ikarus/values/text_value.hpp>
|
||||
#include <ikarus/values/toggle_value.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusValue::IkarusValue(Data data):
|
||||
data(data) {}
|
||||
|
||||
cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_json(boost::json::value json) {
|
||||
if (auto const * obj = json.if_object(); obj == nullptr) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
} else {
|
||||
boost::int64_t const * type = nullptr;
|
||||
boost::json::value const * data = nullptr;
|
||||
|
||||
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 (data = obj->if_contains("data"); data == nullptr) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
}
|
||||
|
||||
auto create_value = [data]<typename T>() -> cppbase::Result<IkarusValue *, FromJsonError> {
|
||||
T * ret = nullptr;
|
||||
|
||||
if (data->is_null()) {
|
||||
ret = new T{};
|
||||
ret->data = boost::variant2::monostate{};
|
||||
} else {
|
||||
auto res =
|
||||
boost::json::try_value_to<boost::container::small_vector<typename T::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(*data
|
||||
);
|
||||
|
||||
if (res.has_error()) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
}
|
||||
|
||||
ret = new T{};
|
||||
ret->data = std::move(res.value());
|
||||
}
|
||||
|
||||
return cppbase::ok(ret);
|
||||
};
|
||||
|
||||
switch (*type) {
|
||||
case IkarusPropertyType_Toggle: {
|
||||
return create_value.operator()<IkarusToggleValue>();
|
||||
}
|
||||
case IkarusPropertyType_Number: {
|
||||
return create_value.operator()<IkarusNumberValue>();
|
||||
}
|
||||
case IkarusPropertyType_Text: {
|
||||
return create_value.operator()<IkarusTextValue>();
|
||||
}
|
||||
default: {
|
||||
return cppbase::err(FromJsonError{});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boost::json::value IkarusValue::to_json() const {
|
||||
auto type = boost::variant2::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
|
||||
);
|
||||
|
||||
auto data_json = boost::variant2::visit(
|
||||
[]<typename T>(T const * value) -> boost::json::value {
|
||||
return boost::variant2::visit(
|
||||
cppbase::overloaded{
|
||||
[]([[maybe_unused]] boost::variant2::monostate const & data) -> boost::json::value { return nullptr; },
|
||||
[](auto const & data) -> boost::json::value { return boost::json::value_from(data); }
|
||||
},
|
||||
value->data
|
||||
);
|
||||
},
|
||||
data
|
||||
);
|
||||
|
||||
return {
|
||||
{"type", type},
|
||||
{"data", data_json}
|
||||
};
|
||||
}
|
||||
|
||||
void ikarus_value_visit(
|
||||
IkarusValue * value,
|
||||
void (*toggle_visitor)(IkarusToggleValue *, void *),
|
||||
void (*number_visitor)(IkarusNumberValue *, void *),
|
||||
void (*text_visitor)(IkarusTextValue *, void *),
|
||||
void * data
|
||||
) {
|
||||
boost::variant2::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->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
|
||||
) {
|
||||
boost::variant2::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->data
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue