implement IkarusPropertySource and switch to cppbase::overloaded

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2023-12-11 16:16:34 +01:00 committed by Folling
parent 700d9a92f9
commit 8fa56decd0
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
10 changed files with 90 additions and 50 deletions

View file

@ -2,7 +2,7 @@
#include <objects/object.hpp>
struct IkarusBlueprint final : IkarusObject {
struct IkarusBlueprint : IkarusObject {
IkarusBlueprint(struct IkarusProject * project, IkarusId id);
IkarusBlueprint(IkarusBlueprint const&) = default;

View file

@ -2,7 +2,7 @@
#include <objects/object.hpp>
struct IkarusEntity final : IkarusObject {
struct IkarusEntity : IkarusObject {
inline IkarusEntity(struct IkarusProject * project, IkarusId id):
IkarusObject{project, id} {}

View file

@ -0,0 +1,46 @@
#include "property_source.hpp"
#include <boost/functional/overloaded_function.hpp>
#include <objects/blueprint.hpp>
IkarusPropertySource::IkarusPropertySource(Data data):
data{data} {}
IkarusPropertySource * ikarus_property_source_create_blueprint(IkarusBlueprint * blueprint) {
return new IkarusPropertySource{blueprint};
}
IkarusPropertySource * ikarus_property_source_create_entity(IkarusEntity * entity) {
return new IkarusPropertySource{entity};
}
void ikarus_property_source_visit(
struct IkarusPropertySource * property_source,
void (*blueprint_visitor)(struct IkarusBlueprint *, void *),
void (*entity_visitor)(struct IkarusEntity *, void *),
void * user_data
) {
boost::variant2::visit(
boost::make_overloaded_function(
[blueprint_visitor, user_data](IkarusBlueprint * blueprint) { blueprint_visitor(blueprint, user_data); },
[entity_visitor, user_data](IkarusEntity * entity) { entity_visitor(entity, user_data); }
),
property_source->data
);
}
void ikarus_property_source_visit_const(
struct IkarusPropertySource const * property_source,
void (*blueprint_visitor)(struct IkarusBlueprint const *, void *),
void (*entity_visitor)(struct IkarusEntity const *, void *),
void * user_data
) {
boost::variant2::visit(
boost::make_overloaded_function(
[blueprint_visitor, user_data](IkarusBlueprint const * blueprint) { blueprint_visitor(blueprint, user_data); },
[entity_visitor, user_data](IkarusEntity const * entity) { entity_visitor(entity, user_data); }
),
property_source->data
);
}

View file

@ -1,16 +1,15 @@
#pragma once
#include <variant>
#include <boost/variant2.hpp>
#include <ikarus/objects/properties/property_source.h>
struct IkarusPropertySource {
public:
using Data = std::variant<IkarusBlueprint *, IkarusEntity *>;
using Data = boost::variant2::variant<IkarusBlueprint *, IkarusEntity *>;
public:
inline explicit IkarusPropertySource(Data data):
_data{data} {}
explicit IkarusPropertySource(Data data);
IkarusPropertySource(IkarusPropertySource const&) = default;
IkarusPropertySource(IkarusPropertySource&&) = default;
@ -18,13 +17,8 @@ public:
IkarusPropertySource& operator=(IkarusPropertySource const&) = default;
IkarusPropertySource& operator=(IkarusPropertySource&&) = default;
~IkarusPropertySource() = default;
virtual ~IkarusPropertySource() = default;
public:
[[nodiscard]] inline Data const& get_data() const {
return _data;
}
private:
std::variant<IkarusBlueprint *, IkarusEntity *> _data;
Data data;
};

View file

@ -2,7 +2,7 @@
#include <objects/properties/property.hpp>
struct IkarusToggleProperty final : IkarusProperty {
struct IkarusToggleProperty : IkarusProperty {
public:
IkarusToggleProperty(struct IkarusProject * project, IkarusId id);
};

View file

@ -5,9 +5,9 @@
#include <values/value.hpp>
struct IkarusNumberValue final : IkarusValue {
struct IkarusNumberValue : IkarusValue {
public:
using data_type = double;
using DataType = double;
public:
explicit IkarusNumberValue();
@ -21,6 +21,6 @@ public:
~IkarusNumberValue() override = default;
public:
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<data_type, SMALL_VEC_VALUE_SIZE>> data{
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<DataType, SMALL_VEC_VALUE_SIZE>> data{
};
};

View file

@ -4,9 +4,9 @@
#include <values/value.hpp>
struct IkarusTextValue final : IkarusValue {
struct IkarusTextValue : IkarusValue {
public:
using data_type = std::string;
using DataType = std::string;
public:
explicit IkarusTextValue();
@ -20,6 +20,6 @@ public:
~IkarusTextValue() override = default;
public:
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<data_type, SMALL_VEC_VALUE_SIZE>> data{
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<DataType, SMALL_VEC_VALUE_SIZE>> data{
};
};

View file

@ -4,9 +4,9 @@
#include <values/value.hpp>
struct IkarusToggleValue final : IkarusValue {
struct IkarusToggleValue : IkarusValue {
public:
using data_type = bool;
using DataType = bool;
public:
explicit IkarusToggleValue();
@ -20,6 +20,6 @@ public:
~IkarusToggleValue() override = default;
public:
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<data_type, SMALL_VEC_VALUE_SIZE>> data{
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<DataType, SMALL_VEC_VALUE_SIZE>> data{
};
};

View file

@ -3,9 +3,10 @@
#include <string>
#include <boost/container/small_vector.hpp>
#include <boost/functional/overloaded_function.hpp>
// required for header-only inclusion
#include "cppbase/templates.hpp"
#include <boost/json/src.hpp>
#include <ikarus/objects/properties/property_type.h>
@ -43,7 +44,7 @@ cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_jso
ret->data = boost::variant2::monostate{};
} else {
auto res = boost::json::try_value_to<
boost::container::small_vector<typename T::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(*data);
boost::container::small_vector<typename T::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(*data);
if (res.has_error()) {
return cppbase::err(FromJsonError{});
@ -75,21 +76,21 @@ cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_jso
boost::json::value IkarusValue::to_json() const {
auto type = boost::variant2::visit(
boost::make_overloaded_function(
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(
boost::make_overloaded_function(
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
);
},
@ -110,11 +111,11 @@ void ikarus_value_visit(
void * data
) {
boost::variant2::visit(
boost::make_overloaded_function(
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
);
}
@ -127,11 +128,11 @@ void ikarus_value_visit_const(
void * data
) {
boost::variant2::visit(
boost::make_overloaded_function(
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
);
}

View file

@ -1,16 +1,15 @@
#pragma once
#include <concepts>
#include <ranges>
#include <boost/bind/bind.hpp>
#include <cppbase/templates.hpp>
#include <boost/container/small_vector.hpp>
#include <boost/functional/overloaded_function.hpp>
template<typename V>
typename V::data_type const * ikarus_value_base_get(V * value, size_t idx) {
typename V::DataType const * ikarus_value_base_get(V * value, size_t idx) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
@ -23,7 +22,7 @@ typename V::data_type const * ikarus_value_base_get(V * value, size_t idx) {
template<typename V>
size_t ikarus_value_base_get_size(V const * value) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
@ -34,9 +33,9 @@ size_t ikarus_value_base_get_size(V const * value) {
}
template<typename V>
void ikarus_value_base_set(V * value, size_t idx, typename V::data_type new_data) {
void ikarus_value_base_set(V * value, size_t idx, typename V::DataType new_data) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
@ -47,7 +46,7 @@ void ikarus_value_base_set(V * value, size_t idx, typename V::data_type new_data
template<typename V>
void ikarus_value_base_remove(V * value, size_t idx) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
@ -56,9 +55,9 @@ void ikarus_value_base_remove(V * value, size_t idx) {
}
template<typename V>
void ikarus_value_base_insert(V * value, size_t idx, typename V::data_type new_data) {
void ikarus_value_base_insert(V * value, size_t idx, typename V::DataType new_data) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
@ -69,7 +68,7 @@ void ikarus_value_base_insert(V * value, size_t idx, typename V::data_type new_d
template<typename V>
void ikarus_value_base_clear(V * value) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
@ -87,16 +86,16 @@ void ikarus_value_base_set_undefined(V * value, bool undefined) {
if (undefined) {
value->data = boost::variant2::monostate{};
} else {
value->data = boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE>{};
value->data = boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>{};
}
}
template<typename V, std::invocable<typename V::data_type> F>
template<typename V, std::invocable<typename V::DataType> F>
char const * ikarus_value_base_to_string(V const * value, F transformer) {
return boost::variant2::visit(
boost::make_overloaded_function(
cppbase::overloaded {
[](boost::variant2::monostate const&) -> char const * { return nullptr; },
[&transformer](boost::container::small_vector<typename V::data_type, IkarusValue::SMALL_VEC_VALUE_SIZE> const& data
[&transformer](auto const& data
) -> char const * {
auto buffer = fmt::memory_buffer{};
@ -108,7 +107,7 @@ char const * ikarus_value_base_to_string(V const * value, F transformer) {
return buffer.data();
}
),
},
value->data
);
}