split property & values into separate classes and files

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
folling 2023-11-21 15:10:43 +01:00 committed by Folling
parent f38ebeab14
commit e377340781
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
28 changed files with 700 additions and 269 deletions

View file

@ -5,10 +5,7 @@
#include <cppbase/strings.hpp>
#include <ikarus/objects/blueprint.h>
#include <ikarus/objects/entity.h>
#include <ikarus/objects/object_type.h>
#include <id.hpp>
#include <objects/entity.hpp>
#include <objects/property.hpp>
#include <persistence/project.hpp>

87
src/objects/property.cpp Normal file
View file

@ -0,0 +1,87 @@
#include "property.hpp"
#include <cppbase/logger.hpp>
#include <cppbase/result.hpp>
#include <cppbase/strings.hpp>
#include <ikarus/values/value.h>
#include <objects/blueprint.hpp>
#include <objects/entity.hpp>
#include <objects/property.hpp>
#include <objects/property_info.hpp>
#include <objects/property_source.hpp>
#include <persistence/project.hpp>
IkarusProperty * ikarus_property_create(
struct IkarusProject * project,
char const * name,
struct IkarusPropertySource * property_source,
struct IkarusPropertyInfo * property_info
) {
LOG_INFO("creating new property");
if (project == nullptr) {
LOG_ERROR("project is nullptr");
return nullptr;
}
auto ctx = project->function_context();
if (property_source == nullptr) {
ctx->set_error("property_source is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
if (name == nullptr) {
ctx->set_error("name is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
if (cppbase::is_empty_or_blank(name)) {
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
if (property_info == nullptr) {
ctx->set_error("property_info is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
LOG_VERBOSE(
"project={}; name={}; property_source={}; property_info={}",
project->path().c_str(),
name,
std::visit(
cppbase::overload{
[](IkarusBlueprint * blueprint) { return fmt::format("Blueprint({})", blueprint->id); },
[](IkarusEntity * entity) { return fmt::format("Entity({})", entity->id); }},
property_source->data
),
std::visit(
cppbase::overload{
[](IkarusTogglePropertyInfo * info) {
return fmt::format(
"Toggle(default_value={})",
cppbase::OwningString{ikarus_value_to_string(ikarus_toggle_value_to_entity_value(info->default_value))}
.data
);
},
[](IkarusNumberPropertyInfo * info) {
return fmt::format(
"Number(default_value={})",
cppbase::OwningString{ikarus_value_to_string(ikarus_number_value_to_entity_value(info->default_value))}
.data
);
},
[](IkarusTextPropertyInfo * info) {
return fmt::format(
"Text(default_value={})",
cppbase::OwningString{ikarus_value_to_string(ikarus_text_value_to_entity_value(info->default_value))}
.data
);
}},
property_info->data
)
);
}

View file

@ -0,0 +1,74 @@
#pragma once
#include <variant>
#include <ikarus/objects/property_info.h>
#include <values/number_value.hpp>
#include <values/text_value.hpp>
#include <values/toggle_value.hpp>
// this looks a bit cursed, but there's reason to my madness:
// Let's go over the facts:
// 1. The client uses the concrete types (e.g. Ikarus"Toggle"PropertyInfo)
// 2. The API needs to accept a common type (i.e. IkarusPropertyInfo)
// 3. Casting between a concrete subtype and a common type is only defined behaviour if we use inheritance, otherwise an
// allocation is unavoidable
// 4. On the implementation side, we need to be able to distinguish between the concrete types
//
// There's a few ways to model this (using dynamic_casts, using an enum, a union, the visitor pattern, ...) but std::variant
// gives us the most type safety without any performance overhead
/// \private
struct IkarusPropertyInfo {
public:
using IkarusPropertyInfoData =
std::variant<struct IkarusTogglePropertyInfo *, struct IkarusNumberPropertyInfo *, struct IkarusTextPropertyInfo *>;
public:
inline explicit IkarusPropertyInfo(
std::variant<struct IkarusTogglePropertyInfo *, struct IkarusNumberPropertyInfo *, struct IkarusTextPropertyInfo *> data
):
data{data} {}
public:
[[nodiscard]] inline IkarusPropertyInfoData const& get_data() const {
return data;
}
private:
IkarusPropertyInfoData data;
};
/// \private
struct IkarusTogglePropertyInfo : public IkarusPropertyInfo {
public:
inline IkarusTogglePropertyInfo():
IkarusPropertyInfo{this} {}
public:
[[nodiscard]] IkarusToggleValue * get_default_value() const {}
private:
IkarusToggleValue * default_value{nullptr};
};
/// \private
struct IkarusNumberPropertyInfo : public IkarusPropertyInfo {
public:
inline IkarusNumberPropertyInfo():
IkarusPropertyInfo{this} {}
private:
IkarusNumberValue * default_value{nullptr};
};
/// \private
struct IkarusTextPropertyInfo : public IkarusPropertyInfo {
public:
inline IkarusTextPropertyInfo():
IkarusPropertyInfo{this} {}
private:
IkarusTextValue * default_value{nullptr};
};

View file

@ -0,0 +1,10 @@
#pragma once
#include <variant>
#include <ikarus/objects/property_source.h>
/// \private
struct IkarusPropertySource {
std::variant<IkarusBlueprint *, IkarusEntity *> data;
};