Skip to content

Commit

Permalink
Code cleanuo
Browse files Browse the repository at this point in the history
  • Loading branch information
Epixu committed Mar 11, 2024
1 parent 70b9ae1 commit 26a28a8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion source/External.inl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace Langulus::A
/// @param desc - messy descriptor for the content
LANGULUS(INLINED)
Asset::Asset(RTTI::DMeta type, AssetModule* producer, const Neat& desc)
: Unit {type, desc}
: Unit {type}
, ProducedFrom<AssetModule> {producer, desc} {}

/// Get the entire content data map
Expand Down
7 changes: 5 additions & 2 deletions source/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace Langulus::Entity

IF_LANGULUS_MANAGED_MEMORY(Allocator::CollectGarbage());
IF_LANGULUS_MEMORY_STATISTICS(Allocator::DumpPools());
LANGULUS_THROW(Destruct, "Can't unload last module(s)");
LANGULUS_OOPS(Destruct, "Can't unload last module(s)");
}
}

Expand All @@ -123,7 +123,10 @@ namespace Langulus::Entity
// A module instance doesn't exist yet, so instantiate it
VERBOSE(this, ": Module `", name, "` is not instantiated yet"
", so attempting to create it...");
return InstantiateModule(library, descriptor);
const auto instance = InstantiateModule(library, descriptor);
if (not instance)
UnloadSharedLibrary(library);
return instance;
}

/// Register by all bases in mModulesByType
Expand Down
45 changes: 18 additions & 27 deletions source/Thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ namespace Langulus::Entity
/// @param other - move that entity
Thing::Thing(Thing&& other) noexcept
: Resolvable {Forward<Resolvable>(other)}
, mChildren {Move(other.mChildren)}
, mRuntime {Move(other.mRuntime)}
, mFlow {Move(other.mFlow)}
, mChildren {Move(other.mChildren)}
, mUnitsAmbiguous {Move(other.mUnitsAmbiguous)}
, mUnitsList {Move(other.mUnitsList)}
, mTraits {Move(other.mTraits)}
Expand All @@ -121,8 +121,8 @@ namespace Langulus::Entity

/// Abandon constructor
/// @attention owner is never cloned, you're moving only the hierarchy
/// below the parent, however other's parent is notified of
/// the clone, because 'other' is duplicated in its children
/// below the parent, however other's parent is notified of the clone,
/// because 'other' is duplicated in its children
/// @param other - clone that entity
Thing::Thing(Abandoned<Thing>&& other)
: Resolvable {*other}
Expand Down Expand Up @@ -150,8 +150,8 @@ namespace Langulus::Entity

/// Clone constructor
/// @attention owner is never cloned, you're moving only the hierarchy
/// below the parent, however other's parent is notified of
/// the clone, because 'other' is duplicated in its children
/// below the parent, however other's parent is notified of the clone,
/// because 'other' is duplicated in its children
/// @param other - clone that entity
Thing::Thing(Cloned<Thing>&& other)
: Resolvable {*other}
Expand All @@ -166,31 +166,18 @@ namespace Langulus::Entity
/// Destructor
Thing::~Thing() IF_UNSAFE(noexcept) {
Detach();

/*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);
}*/
}

/// A nested call to detach all parents of all children
void Thing::Detach() {
ENTITY_VERBOSE_SELF_TAB("Destroying (", Reference(0), " uses):");

// The thing might be on the stack, make sure we decouple it from
// its owner, if that's the case
/*if (mOwner and Reference(0)) {
ENTITY_VERBOSE_SELF("Decoupling from owner: ", *mOwner);
IF_SAFE(Count removed = )
mOwner->RemoveChild<false>(this);
LANGULUS_ASSUME(DevAssumes, removed, "Parent is missing");
ENTITY_VERBOSE_SELF("...", Reference(0), " uses ");
}*/
if (not mRuntime.IsLocked())
mRuntime->Reset();
if (not mFlow.IsLocked())
mFlow->Reset();

mTraits.Reset();

// Decouple all children from this
for (auto& child : mChildren) {
Expand All @@ -199,21 +186,25 @@ namespace Langulus::Entity
LANGULUS_ASSUME(DevAssumes,
child->mOwner == this, "Parent mismatch");
child->mOwner.Reset();
child->Detach();
ENTITY_VERBOSE_SELF("...", Reference(0), " uses remain");
}
}

// Decouple all units from this owner
for (auto& unit : mUnitsList) {
ENTITY_VERBOSE_SELF("Decoupling unit: ", unit);
unit->mOwners.Remove(this);
unit->mOwners.Reset();// .Remove(this);
ENTITY_VERBOSE_SELF("...", Reference(0), " uses remain");
}

for (auto& child : mChildren) {
mUnitsAmbiguous.Reset();
mUnitsList.Reset();

/*for (auto& child : mChildren) {
child->Detach();
ENTITY_VERBOSE_SELF("...", Reference(0), " uses remain");
}
}*/
}

/// Compare two entities
Expand Down
7 changes: 3 additions & 4 deletions source/Thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ namespace Langulus::Entity
LANGULUS_API(ENTITY) void ResetFlow(Temporal*);

// The order of members is critical!
// Runtime should be destroyed last, hence it is the first member

// Hierarchy should be destroyed last, hence it is the first
// member
Hierarchy mChildren;
// Runtime
Pin<Ref<Runtime>> mRuntime;
// Temporal flow
Pin<Ref<Temporal>> mFlow;
// Hierarchy
Hierarchy mChildren;
// Units indexed by all their relevant reflected bases
UnitMap mUnitsAmbiguous;
// Units indexed by concrete type, in order of addition
Expand Down
3 changes: 1 addition & 2 deletions source/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ using namespace Langulus::Entity;

/// Manual construction
/// @param classid - type of the unit
/// @param descriptor - the unit descriptor, used to extract unit owner
Unit::Unit(DMeta classid, const Neat& descriptor) noexcept
Unit::Unit(DMeta classid) noexcept
: Resolvable {classid} {}

/// Move unit
Expand Down
2 changes: 1 addition & 1 deletion source/Unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Langulus::Entity
Unit& operator = (const Unit&) = delete;

/// A unit can only be moved or created with type and owner
Unit(DMeta, const Neat& = {}) noexcept;
Unit(DMeta) noexcept;
Unit(Unit&&) noexcept;

Unit& operator = (Unit&&) noexcept;
Expand Down
7 changes: 5 additions & 2 deletions test/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using namespace Langulus;

#define CATCH_CONFIG_ENABLE_BENCHMARKING


/// A unit implementation for testing
class TestUnit1 final : public Unit {
public:
LANGULUS(ABSTRACT) false;
Expand All @@ -23,7 +25,7 @@ class TestUnit1 final : public Unit {
: Unit {MetaOf<TestUnit1>()} {}

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

Expand All @@ -34,6 +36,7 @@ class TestUnit1 final : public Unit {
void Refresh() {}
};

/// A unit implementation for testing
class TestUnit2 final : public Unit {
public:
LANGULUS(ABSTRACT) false;
Expand All @@ -44,7 +47,7 @@ class TestUnit2 final : public Unit {
: Unit {MetaOf<TestUnit2>()} {}

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

Expand Down

0 comments on commit 26a28a8

Please sign in to comment.