From 426e88d6a85d6a058bcf4ef3b69acad2c00ba9e6 Mon Sep 17 00:00:00 2001 From: Epixu Date: Thu, 7 Mar 2024 20:07:57 +0200 Subject: [PATCH] Code cleanup; Units now rely on user to Couple them; Unit::Couple now scans descriptor --- source/Unit.cpp | 55 ++++++++++--------------------------------------- source/Unit.hpp | 3 +-- test/Main.hpp | 8 +++++-- 3 files changed, 18 insertions(+), 48 deletions(-) diff --git a/source/Unit.cpp b/source/Unit.cpp index 552474c..207deae 100644 --- a/source/Unit.cpp +++ b/source/Unit.cpp @@ -15,13 +15,7 @@ using namespace Langulus::Entity; /// @param classid - type of the unit /// @param descriptor - the unit descriptor, used to extract unit owner Unit::Unit(DMeta classid, const Neat& descriptor) noexcept - : Resolvable {classid} { - // Couple any Thing provided in the descriptor - const Thing* owner = nullptr; - if (not descriptor.ExtractTrait(owner)) - descriptor.ExtractData(owner); - Couple(owner); -} + : Resolvable {classid} {} /// Move unit /// @param other - the unit to move @@ -34,36 +28,6 @@ Unit::Unit(Unit&& other) noexcept owner->ReplaceUnit(&other, this);*/ } -/// Unit destruction -Unit::~Unit() { - // The unit might be on the stack, make sure we decouple it from - // all its owners, if that's the case - //if (Reference(0) == mOwners.GetCount() + 1) { - // After removing - //} - - - //if (Reference(-1)) { - /*for (auto owner : mOwners) - owner->RemoveUnit(this); - //} - - if (Reference(0) == 1) { - // If after detaching the entire hierarchy, there's still one - // reference remaining, then we're sure that this reference is - // because the Thing is on the stack - //TODO requires the destruction of the hierarchy, too? othwise some member could still hold a ref to this thing? - //TODO alternatively, items without jurisdiction are never referenced, so we could detect things on the stack, by comparing references before and after Detach() - //TODO eventually we can just do Fractalloc::CheckAuthority(this) - Reference(-1); - }*/ - - /*auto heap = Fractalloc::Instance.Find(GetType(), this); - if (not heap) { - // The unit is on the stack, or outside jurisdiction - }*/ -} - /// Default unit selection simply relays to the owner /// @param verb - the selection verb void Unit::Select(Verb& verb) { @@ -137,15 +101,18 @@ Runtime* Unit::GetRuntime() const noexcept { return mOwners[0]->GetRuntime(); } -/// Couple the component with an entity (always two-sided) +/// Couple the component with an entity, extracted from a descriptor's +/// Traits::Parent, if any was defined (always two-sided) /// This will call refresh to all units in that entity on next tick -/// @param entity - the entity to couple with -void Unit::Couple(const Thing* entity) { - if (not entity) - return; +/// @param descriptor - the descriptor to scan for parents +void Unit::Couple(const Neat& descriptor) { + // Couple any Thing provided in the descriptor + const Thing* owner = nullptr; + if (not descriptor.ExtractTrait(owner)) + descriptor.ExtractData(owner); - if (mOwners.Merge(IndexBack, const_cast(entity))) - const_cast(entity)->AddUnit(this); + if (owner and mOwners.Merge(IndexBack, const_cast(owner))) + const_cast(owner)->AddUnit(this); } /// Decouple the component from an entity (always two-sided) diff --git a/source/Unit.hpp b/source/Unit.hpp index 39d872c..9f6df26 100644 --- a/source/Unit.hpp +++ b/source/Unit.hpp @@ -47,7 +47,6 @@ namespace Langulus::Entity /// A unit can only be moved or created with type and owner Unit(DMeta, const Neat& = {}) noexcept; Unit(Unit&&) noexcept; - ~Unit() override; Unit& operator = (Unit&&) noexcept; @@ -116,7 +115,7 @@ namespace Langulus::Entity NOD() TAny GatherValues() const; protected: - void Couple(const Thing*); + void Couple(const Neat&); void Decouple(const Thing*); void ReplaceOwner(const Thing*, const Thing*); }; diff --git a/test/Main.hpp b/test/Main.hpp index 21518f6..4aee735 100644 --- a/test/Main.hpp +++ b/test/Main.hpp @@ -23,7 +23,9 @@ class TestUnit1 final : public Unit { : Unit {MetaOf()} {} TestUnit1(Describe&& describe) - : Unit {MetaOf(), *describe} {} + : Unit {MetaOf(), *describe} { + Couple(*describe); + } ~TestUnit1() { Logger::Verbose(this, ": destroying..."); @@ -42,7 +44,9 @@ class TestUnit2 final : public Unit { : Unit {MetaOf()} {} TestUnit2(Describe&& describe) - : Unit {MetaOf(), *describe} {} + : Unit {MetaOf(), *describe} { + Couple(*describe); + } ~TestUnit2() { Logger::Verbose(this, ": destroying...");