From 4b441adff3856a7887d2698272083334c83e2a77 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Wed, 9 Oct 2024 16:08:41 +0200 Subject: [PATCH] some datamodel shit --- src/core/value.h | 20 ++++++++++++++++++ src/datamodel/instance.cpp | 11 ++++++++-- src/datamodel/instance.h | 20 ++++++++++++++++-- src/datamodel/instancetype.h | 39 ++++++++++++++++++++++++++++++++++++ src/datamodel/part.cpp | 15 ++++++++++++++ src/{ => datamodel}/part.h | 10 +++++++-- src/main.cpp | 2 +- src/physics/simulation.h | 2 +- src/rendering/renderer.cpp | 2 +- 9 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 src/core/value.h create mode 100644 src/datamodel/instancetype.h create mode 100644 src/datamodel/part.cpp rename src/{ => datamodel}/part.h (69%) diff --git a/src/core/value.h b/src/core/value.h new file mode 100644 index 0000000..63b9b09 --- /dev/null +++ b/src/core/value.h @@ -0,0 +1,20 @@ +#pragma once + +#include +enum ValueType { + OB_NIL = 0, + OB_BOOLEAN, + OB_INTEGER, + OB_FLOAT, + OB_STRING, +}; + +struct Value { + ValueType type; + union { + bool b; + int i; + float f; + std::string str; + }; +}; \ No newline at end of file diff --git a/src/datamodel/instance.cpp b/src/datamodel/instance.cpp index 755a79d..bf5def2 100644 --- a/src/datamodel/instance.cpp +++ b/src/datamodel/instance.cpp @@ -1,9 +1,16 @@ #include "instance.h" +#include "instancetype.h" #include #include #include #include +InstanceType TYPE_INSTANCE { + .superClass = nullptr, + .className = "Instance", + .flags = OB_INST_NOT_CREATABLE, +}; + std::optional> Instance::GetParent() { if (!this->Parent.has_value() || this->Parent.value().expired()) return std::nullopt; @@ -34,12 +41,12 @@ std::vector> Instance::GetChildren() { std::unique_ptr Instance::CloneInternal() { // TODO: Implement refs - std::unique_ptr clone = this->copy(); + std::unique_ptr clone = this->__copy(); clone->children = std::vector>(); clone->children.reserve(this->children.size()); for (std::shared_ptr child : this->children) { - clone->children.push_back(std::make_shared(child->CloneInternal())); + clone->children.push_back(child->CloneInternal()); } return clone; diff --git a/src/datamodel/instance.h b/src/datamodel/instance.h index 2ae4199..dd2c754 100644 --- a/src/datamodel/instance.h +++ b/src/datamodel/instance.h @@ -1,16 +1,28 @@ #pragma once +#include #include #include #include #include +#include "instancetype.h" -#define INSTANCE_COPY_IMPL(CLASS_NAME) std::unique_ptr CLASS_NAME::copy() { return std::make_unique(CLASS_NAME(*this)); } +#define INSTANCE_COPY_IMPL(CLASS_NAME) std::unique_ptr CLASS_NAME::__copy() { return std::make_unique(CLASS_NAME(*this)); } +#define INSTANCE_PRELUDE(CLASS_NAME) protected: virtual std::unique_ptr __copy() override; virtual InstanceType& GetType() const override; +// #define INSTANCE_TYPE(CLASS_NAME, TYPE_DEFINITION) InstanceType& CLASS_NAME::GetType() const { \ +// static InstanceType type = TYPE_DEFINITION; \ +// return type; \ +// }; +#define INSTANCE_TYPE(CLASS_NAME, TYPE_CONSTANT) InstanceType& CLASS_NAME::GetType() const { return TYPE_CONSTANT; }; + +extern InstanceType TYPE_INSTANCE; // The base class for all objects in the data model class Instance : std::enable_shared_from_this { private: std::optional> Parent; std::vector> children; +protected: + virtual std::unique_ptr __copy(); public: std::string Name; std::optional> GetParent(); @@ -19,5 +31,9 @@ public: virtual void Init(); std::unique_ptr CloneInternal(); - virtual std::unique_ptr copy() const = 0; + virtual InstanceType& GetType() const = 0; + + virtual Value GetProperty(std::string key); + virtual void SetProperty(std::string key, Value newValue); + virtual Value CallMethod(std::string key, std::vector arguments); }; \ No newline at end of file diff --git a/src/datamodel/instancetype.h b/src/datamodel/instancetype.h new file mode 100644 index 0000000..5f9537b --- /dev/null +++ b/src/datamodel/instancetype.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include "../core/value.h" + +enum PropertyFlags { + OB_PROP_READ_ONLY = 1, // The property cannot be set + // OB_PROP_CALLBACK = 2, // The proeprty is get/set through a function instead of directly to a field +}; + +enum MethodFlags { + // OB_METHOD_OVERLOADED = 1, // The method has multiple definitions with different parameters suffixed by __ followed by a number +}; + +enum InstanceFlags { + OB_INST_NOT_CREATABLE = 1, +}; + +struct PropertyInfo { + PropertyFlags flags; + void* fieldPointer; + ValueType valueType; +}; + +struct MethodInfo { + MethodFlags flags; + void* functionPointer; + ValueType returnType; + std::vector parameterTypes; +}; + +struct InstanceType { + InstanceType* superClass = nullptr; + std::string className; + InstanceFlags flags; + std::map properties; + std::map methods; +}; diff --git a/src/datamodel/part.cpp b/src/datamodel/part.cpp new file mode 100644 index 0000000..3699a6a --- /dev/null +++ b/src/datamodel/part.cpp @@ -0,0 +1,15 @@ +#include "part.h" +#include "instance.h" +#include "instancetype.h" +#include + +INSTANCE_COPY_IMPL(Part) + +InstanceType TYPE_PART { + .superClass = &TYPE_INSTANCE, + .className = "Part", + .properties = std::map(), + .methods = std::map(), +}; + +INSTANCE_TYPE(Part, TYPE_PART) \ No newline at end of file diff --git a/src/part.h b/src/datamodel/part.h similarity index 69% rename from src/part.h rename to src/datamodel/part.h index b1a349e..f5fd4bb 100644 --- a/src/part.h +++ b/src/datamodel/part.h @@ -3,11 +3,17 @@ #include #include #include -#include "rendering/material.h" +#include "../rendering/material.h" +#include "instance.h" +#include "instancetype.h" namespace rp = reactphysics3d; -struct Part { +extern InstanceType TYPE_PART; + +class Part : public Instance { + INSTANCE_PRELUDE(Part) +public: glm::vec3 position; glm::quat rotation = glm::identity(); glm::vec3 scale; diff --git a/src/main.cpp b/src/main.cpp index ed3ca5c..7282544 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ #include #include -#include "part.h" +#include "datamodel/part.h" #include "rendering/renderer.h" #include "physics/simulation.h" #include "camera.h" diff --git a/src/physics/simulation.h b/src/physics/simulation.h index d103cc3..dcb955a 100644 --- a/src/physics/simulation.h +++ b/src/physics/simulation.h @@ -1,6 +1,6 @@ #pragma once -#include "../part.h" +#include "../datamodel/part.h" void simulationInit(); void syncPartPhysics(Part& part); diff --git a/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index 6a5fba7..a99e50d 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -12,7 +12,7 @@ #include "mesh.h" #include "defaultmeshes.h" #include "../camera.h" -#include "../part.h" +#include "../datamodel/part.h" #include "renderer.h"