Skip to content

Commit

Permalink
Code cleanup; Units now rely on user to Couple them; Unit::Couple now…
Browse files Browse the repository at this point in the history
… scans descriptor
  • Loading branch information
Epixu committed Mar 7, 2024
1 parent a8469e3 commit 426e88d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 48 deletions.
55 changes: 11 additions & 44 deletions source/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Traits::Parent>(owner))
descriptor.ExtractData(owner);
Couple(owner);
}
: Resolvable {classid} {}

/// Move unit
/// @param other - the unit to move
Expand All @@ -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<false>(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) {
Expand Down Expand Up @@ -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<Traits::Parent>(owner))
descriptor.ExtractData(owner);

if (mOwners.Merge(IndexBack, const_cast<Thing*>(entity)))
const_cast<Thing*>(entity)->AddUnit<false>(this);
if (owner and mOwners.Merge(IndexBack, const_cast<Thing*>(owner)))
const_cast<Thing*>(owner)->AddUnit<false>(this);
}

/// Decouple the component from an entity (always two-sided)
Expand Down
3 changes: 1 addition & 2 deletions source/Unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -116,7 +115,7 @@ namespace Langulus::Entity
NOD() TAny<D> GatherValues() const;

protected:
void Couple(const Thing*);
void Couple(const Neat&);
void Decouple(const Thing*);
void ReplaceOwner(const Thing*, const Thing*);
};
Expand Down
8 changes: 6 additions & 2 deletions test/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class TestUnit1 final : public Unit {
: Unit {MetaOf<TestUnit1>()} {}

TestUnit1(Describe&& describe)
: Unit {MetaOf<TestUnit1>(), *describe} {}
: Unit {MetaOf<TestUnit1>(), *describe} {
Couple(*describe);
}

~TestUnit1() {
Logger::Verbose(this, ": destroying...");
Expand All @@ -42,7 +44,9 @@ class TestUnit2 final : public Unit {
: Unit {MetaOf<TestUnit2>()} {}

TestUnit2(Describe&& describe)
: Unit {MetaOf<TestUnit2>(), *describe} {}
: Unit {MetaOf<TestUnit2>(), *describe} {
Couple(*describe);
}

~TestUnit2() {
Logger::Verbose(this, ": destroying...");
Expand Down

0 comments on commit 426e88d

Please sign in to comment.