initial commit
This commit is contained in:
commit
de042a580c
22 changed files with 221 additions and 0 deletions
26
Crafter.Component-Component.cpp
Normal file
26
Crafter.Component-Component.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
module Crafter.Component;
|
||||||
|
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
Component::Component() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Component::~Component() {
|
||||||
|
onDelete.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Component::AddOwner()
|
||||||
|
{
|
||||||
|
refCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Component::RemoveOwner()
|
||||||
|
{
|
||||||
|
refCount--;
|
||||||
|
if (refCount == 0) {
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
Crafter.Component-Component.cppm
Normal file
19
Crafter.Component-Component.cppm
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
export module Crafter.Component:Component;
|
||||||
|
|
||||||
|
import Crafter.Event;
|
||||||
|
|
||||||
|
namespace Crafter {
|
||||||
|
export class Component {
|
||||||
|
public:
|
||||||
|
Event<void> onDelete;
|
||||||
|
std::uint32_t refCount = 0;
|
||||||
|
Component();
|
||||||
|
virtual ~Component();
|
||||||
|
void AddOwner();
|
||||||
|
void RemoveOwner();
|
||||||
|
};
|
||||||
|
}
|
||||||
134
Crafter.Component-ComponentRef.cppm
Normal file
134
Crafter.Component-ComponentRef.cppm
Normal file
|
|
@ -0,0 +1,134 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
export module Crafter.Component:ComponentRef;
|
||||||
|
|
||||||
|
import :Component;
|
||||||
|
import Crafter.Event;
|
||||||
|
|
||||||
|
namespace Crafter {
|
||||||
|
export template <typename T>
|
||||||
|
class ComponentRef {
|
||||||
|
public:
|
||||||
|
Event<T*> refChanged;
|
||||||
|
Event<T*> componentDeleted;
|
||||||
|
T* component;
|
||||||
|
ComponentRef() : component(nullptr) {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
ComponentRef(T* component) {
|
||||||
|
SetRef(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void SetRef(T* component) {
|
||||||
|
if (component != nullptr) {
|
||||||
|
onDeleteListener.SetEvent(&component->onDelete, [this, component]() {
|
||||||
|
this->componentDeleted.Invoke(component);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
refChanged.Invoke(component);
|
||||||
|
this->component = component;
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
EventListener<void> onDeleteListener;
|
||||||
|
};
|
||||||
|
|
||||||
|
export template <typename T>
|
||||||
|
class ComponentRefOwning : public ComponentRef<T> {
|
||||||
|
public:
|
||||||
|
ComponentRefOwning() : ComponentRef<T>() {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
ComponentRefOwning(T* component) : ComponentRef<T>(component) {
|
||||||
|
component->AddOwner();
|
||||||
|
}
|
||||||
|
~ComponentRefOwning() {
|
||||||
|
if (ComponentRef<T>::component) {
|
||||||
|
ComponentRef<T>::component->RemoveOwner();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetRef(T* component) override {
|
||||||
|
if (this->component != nullptr) {
|
||||||
|
this->component->RemoveOwner();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (component != nullptr) {
|
||||||
|
ComponentRef<T>::onDeleteListener.SetEvent(&component->onDelete, [this, component]() {
|
||||||
|
this->componentDeleted.Invoke(component);
|
||||||
|
});
|
||||||
|
component->AddOwner();
|
||||||
|
}
|
||||||
|
this->component = component;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export template <typename T>
|
||||||
|
class ComponentRefVector {
|
||||||
|
public:
|
||||||
|
Event<T*> componentRemoved;
|
||||||
|
Event<T*> componentDeleted;
|
||||||
|
Event<T*> componentAdded;
|
||||||
|
std::vector<T*> components;
|
||||||
|
|
||||||
|
ComponentRefVector() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentRefVector(std::vector<T*> components) : components(components) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void AddComponent(T* component) {
|
||||||
|
componentAdded.Invoke(component);
|
||||||
|
components.push_back(component);
|
||||||
|
listeners.emplace_back(&component->onDelete, [this, component]() {
|
||||||
|
this->componentDeleted.Invoke(component);
|
||||||
|
components.erase(std::remove(components.begin(), components.end(), component), components.end());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void RemoveComponent(T* component) {
|
||||||
|
componentRemoved.Invoke(component);
|
||||||
|
auto it = find(components.begin(), components.end(), component);
|
||||||
|
int index = it - components.begin();
|
||||||
|
components.erase(components.begin() + index);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::vector<EventListener<void>> listeners;
|
||||||
|
};
|
||||||
|
|
||||||
|
export template <typename T>
|
||||||
|
class ComponentRefVectorOwning : public ComponentRefVector<T> {
|
||||||
|
public:
|
||||||
|
ComponentRefVectorOwning() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentRefVectorOwning(std::vector<T*> components) : ComponentRefVector<T>(components) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
~ComponentRefVectorOwning() {
|
||||||
|
for (size_t i = 0; i < ComponentRefVector<T>::components.size(); i++)
|
||||||
|
{
|
||||||
|
ComponentRefVector<T>::components[i]->RemoveOwner();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddComponent(T* component) override {
|
||||||
|
ComponentRefVector<T>::AddComponent(component);
|
||||||
|
component->AddOwner();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void RemoveComponent(T* component) override {
|
||||||
|
ComponentRefVector<T>::RemoveComponent(component);
|
||||||
|
component->RemoveOwner();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
4
Crafter.Component.cppm
Normal file
4
Crafter.Component.cppm
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export module Crafter.Component;
|
||||||
|
|
||||||
|
export import :Component;
|
||||||
|
export import :ComponentRef;
|
||||||
BIN
bin/Crafter.Component-Component.pcm
Normal file
BIN
bin/Crafter.Component-Component.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Component-ComponentRef.pcm
Normal file
BIN
bin/Crafter.Component-ComponentRef.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Component.pcm
Normal file
BIN
bin/Crafter.Component.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Event.pcm
Normal file
BIN
bin/Crafter.Event.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Thread-ThreadPool.pcm
Normal file
BIN
bin/Crafter.Thread-ThreadPool.pcm
Normal file
Binary file not shown.
BIN
bin/Crafter.Thread.pcm
Normal file
BIN
bin/Crafter.Thread.pcm
Normal file
Binary file not shown.
BIN
bin/libcrafter-component.a
Normal file
BIN
bin/libcrafter-component.a
Normal file
Binary file not shown.
BIN
bin/libcrafter-event.a
Normal file
BIN
bin/libcrafter-event.a
Normal file
Binary file not shown.
BIN
bin/libcrafter-thread.a
Normal file
BIN
bin/libcrafter-thread.a
Normal file
Binary file not shown.
BIN
build/Crafter.Component-Component.o
Normal file
BIN
build/Crafter.Component-Component.o
Normal file
Binary file not shown.
BIN
build/Crafter.Component-ComponentRef.o
Normal file
BIN
build/Crafter.Component-ComponentRef.o
Normal file
Binary file not shown.
BIN
build/Crafter.Component-Component_source.o
Normal file
BIN
build/Crafter.Component-Component_source.o
Normal file
Binary file not shown.
BIN
build/Crafter.Component.o
Normal file
BIN
build/Crafter.Component.o
Normal file
Binary file not shown.
BIN
build/debug/Crafter.Component-Component.o
Normal file
BIN
build/debug/Crafter.Component-Component.o
Normal file
Binary file not shown.
BIN
build/debug/Crafter.Component-ComponentRef.o
Normal file
BIN
build/debug/Crafter.Component-ComponentRef.o
Normal file
Binary file not shown.
BIN
build/debug/Crafter.Component-Component_source.o
Normal file
BIN
build/debug/Crafter.Component-Component_source.o
Normal file
Binary file not shown.
BIN
build/debug/Crafter.Component.o
Normal file
BIN
build/debug/Crafter.Component.o
Normal file
Binary file not shown.
38
project.json
Normal file
38
project.json
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
{
|
||||||
|
"name": "crafter-component",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "base",
|
||||||
|
"standard": "c++26",
|
||||||
|
"source_files": ["Crafter.Component-Component"],
|
||||||
|
"module_files": ["Crafter.Component","Crafter.Component-ComponentRef","Crafter.Component-Component"],
|
||||||
|
"additional_files": [],
|
||||||
|
"build_dir": "./build",
|
||||||
|
"output_dir": "./bin",
|
||||||
|
"type":"library"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "debug",
|
||||||
|
"extends": ["base"],
|
||||||
|
"optimization_level": "0",
|
||||||
|
"debug":true,
|
||||||
|
"dependencies": [
|
||||||
|
{
|
||||||
|
"path":"/home/jorijn/repos/Crafter/Crafter.Event/project.json",
|
||||||
|
"configuration":"debug"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "release",
|
||||||
|
"extends": ["base"],
|
||||||
|
"optimization_level": "3",
|
||||||
|
"dependencies": [
|
||||||
|
{
|
||||||
|
"path":"/home/jorijn/repos/Crafter/Crafter.Event/project.json",
|
||||||
|
"configuration":"debug"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue