Skip to content

Commit

Permalink
Renames; Improved Run methods; Proper detachment of units/things
Browse files Browse the repository at this point in the history
  • Loading branch information
Epixu committed Mar 4, 2024
1 parent c2f3575 commit 26633fe
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 88 deletions.
4 changes: 2 additions & 2 deletions source/External.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ namespace Langulus::A
virtual Offset Write(const Any&) = 0;
};

NOD() virtual Ptr<Reader> NewReader() = 0;
NOD() virtual Ptr<Writer> NewWriter(bool append) = 0;
NOD() virtual Ref<Reader> NewReader() = 0;
NOD() virtual Ref<Writer> NewWriter(bool append) = 0;
};

///
Expand Down
6 changes: 4 additions & 2 deletions source/Hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ namespace Langulus::Anyness
/// Execute a verb for all elements inside a type-erased constant block
/// @param verb - the verb to execute
template<CT::Data T>
void TAny<T>::Run(Flow::Verb& verb) const {
Flow::Verb& TAny<T>::Run(Flow::Verb& verb) const {
Flow::DispatchDeep(*this, verb);
return verb;
}

/// Execute a verb for all elements inside a type-erased block
/// @param verb - the verb to execute
template<CT::Data T>
void TAny<T>::Run(Flow::Verb& verb) {
Flow::Verb& TAny<T>::Run(Flow::Verb& verb) {
Flow::DispatchDeep(*this, verb);
return verb;
}

} // namespace Langulus::Anyness
Expand Down
4 changes: 2 additions & 2 deletions source/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ namespace Langulus::Entity
/// Get a file interface, relying on external modules to find it
/// @param path - the path for the file
/// @return the file interface, or nullptr if file doesn't exist
Ptr<A::File> Runtime::GetFile(const Path& path) {
Ref<A::File> Runtime::GetFile(const Path& path) {
auto& fileSystems = GetModules<A::FileSystem>();
LANGULUS_ASSERT(fileSystems, Module,
"Can't retrieve file `", path, "` - no file system module available");
Expand All @@ -507,7 +507,7 @@ namespace Langulus::Entity
/// Get a folder interface, relying on external modules to find it
/// @param path - the path for the folder
/// @return the folder interface, or nullptr if folder doesn't exist
Ptr<A::Folder> Runtime::GetFolder(const Path& path) {
Ref<A::Folder> Runtime::GetFolder(const Path& path) {
auto& fileSystems = GetModules<A::FileSystem>();
LANGULUS_ASSERT(fileSystems, Module,
"Can't retrieve folder `", path, "` - no file system module available");
Expand Down
4 changes: 2 additions & 2 deletions source/Runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ namespace Langulus::Entity
#endif

NOD() LANGULUS_API(ENTITY)
Ptr<A::File> GetFile(const Path&);
Ref<A::File> GetFile(const Path&);
NOD() LANGULUS_API(ENTITY)
Ptr<A::Folder> GetFolder(const Path&);
Ref<A::Folder> GetFolder(const Path&);
NOD() LANGULUS_API(ENTITY)
const Path& GetWorkingPath() const;
NOD() LANGULUS_API(ENTITY)
Expand Down
15 changes: 10 additions & 5 deletions source/Thing-Traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@

#if 0
#define ENTITY_VERBOSE_ENABLED() 1
#define ENTITY_VERBOSE_SELF(...) Logger::Verbose(Self(), __VA_ARGS__)
#define ENTITY_VERBOSE(...) Logger::Append(__VA_ARGS__)
#define ENTITY_VERBOSE_SELF(...) \
Logger::Verbose(this, ": ", __VA_ARGS__)
#define ENTITY_VERBOSE_SELF_TAB(...) \
const auto scoped = Logger::Verbose(this, ": ", __VA_ARGS__, Logger::Tabs {})
#define ENTITY_VERBOSE(...) \
Logger::Append(__VA_ARGS__)
#else
#define ENTITY_VERBOSE_ENABLED() 0
#define ENTITY_VERBOSE_SELF(...)
#define ENTITY_VERBOSE_SELF_TAB(...)
#define ENTITY_VERBOSE(...)
#endif

Expand Down Expand Up @@ -162,7 +167,7 @@ namespace Langulus::Entity

mTraits.Insert(tmeta, trait);
mRefreshRequired = true;
ENTITY_VERBOSE(trait << " added");
ENTITY_VERBOSE_SELF(trait, " added");
return &mTraits[tmeta].Last();
}

Expand All @@ -174,7 +179,7 @@ namespace Langulus::Entity
if (found) {
const auto removed = found.mValue->GetCount();
mTraits.RemoveIt(found);
ENTITY_VERBOSE_SELF(trait << " removed");
ENTITY_VERBOSE_SELF(trait, " removed");
mRefreshRequired = true;
return removed;
}
Expand All @@ -190,7 +195,7 @@ namespace Langulus::Entity
if (found) {
const auto removed = found.mValue->Remove(trait);
if (removed) {
ENTITY_VERBOSE_SELF(trait << " removed");
ENTITY_VERBOSE_SELF(trait, " removed");
mRefreshRequired = true;
return removed;
}
Expand Down
19 changes: 13 additions & 6 deletions source/Thing-Verbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@
#include "Runtime.hpp"
#include <Flow/Verbs/Interpret.hpp>


#if 0
#define ENTITY_VERBOSE_ENABLED() 1
#define ENTITY_VERBOSE_SELF(...) Logger::Verbose(Self(), __VA_ARGS__)
#define ENTITY_VERBOSE(...) Logger::Append(__VA_ARGS__)
#define ENTITY_CREATION_VERBOSE_SELF(...) Logger::Verbose(Self(), __VA_ARGS__)
#define ENTITY_SELECTION_VERBOSE_SELF(...) Logger::Verbose(Self(), __VA_ARGS__)
#define ENTITY_VERBOSE_SELF(...) \
Logger::Verbose(this, ": ", __VA_ARGS__)
#define ENTITY_VERBOSE_SELF_TAB(...) \
const auto scoped = Logger::Verbose(this, ": ", __VA_ARGS__, Logger::Tabs {})
#define ENTITY_VERBOSE(...) \
Logger::Append(__VA_ARGS__)
#define ENTITY_CREATION_VERBOSE_SELF(...) \
Logger::Verbose(Self(), __VA_ARGS__)
#define ENTITY_SELECTION_VERBOSE_SELF(...) \
Logger::Verbose(Self(), __VA_ARGS__)
#else
#define ENTITY_VERBOSE_ENABLED() 0
#define ENTITY_VERBOSE_SELF(...)
#define ENTITY_VERBOSE_SELF_TAB(...)
#define ENTITY_VERBOSE(...)
#define ENTITY_CREATION_VERBOSE_SELF(...)
#define ENTITY_SELECTION_VERBOSE_SELF(...)
Expand Down Expand Up @@ -68,8 +76,7 @@ namespace Langulus::Entity
// Dispatch to entity first, using reflected and default verbs,
// but disallowing custom dispatch, because we're currently in it
// and there's a potential for infinite regress
Resolvable::Run<false>(verb);
if (verb.IsDone())
if (Resolvable::Run<false>(verb).IsDone())
return;

// If verb is still not satisfied, dispatch to ALL units
Expand Down
44 changes: 29 additions & 15 deletions source/Thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Langulus::Entity
/// Default-constructor, always creates a parentless root thing
Thing::Thing()
: Resolvable {MetaOf<Thing>()} {
ENTITY_VERBOSE_SELF("Created (root, ", GetReferences(), " references)");
ENTITY_VERBOSE_SELF("Created (root, ", Reference(0), " references)");
}

/// Descriptor-constructor
Expand All @@ -47,12 +47,13 @@ namespace Langulus::Entity
if (mOwner) {
ENTITY_VERBOSE_SELF(
"Created as child to ", mOwner,
" (", GetReferences(), " references)"
" (", Reference(0), " references; parent now has ",
mOwner->Reference(0), " references)"
);
}
else {
ENTITY_VERBOSE_SELF(
"Created (root, ", GetReferences(), " references)"
"Created (root, ", Reference(0), " references)"
);
}
}
Expand All @@ -78,12 +79,13 @@ namespace Langulus::Entity
if (mOwner) {
ENTITY_VERBOSE_SELF(
"Created as child to ", mOwner,
" (", GetReferences(), " references)"
" (", Reference(0), " references; parent now has ",
mOwner->Reference(0), " references)"
);
}
else {
ENTITY_VERBOSE_SELF(
"Created (root, ", GetReferences(), " references)"
"Created (root, ", Reference(0), " references)"
);
}
}
Expand Down Expand Up @@ -156,30 +158,39 @@ namespace Langulus::Entity
, mChildren {Clone(other->mChildren)}
, mRefreshRequired {true} {
TODO();
// clone flow and runtime if pinned, recreate modules if new runtime,
//TODO clone flow and runtime if pinned, recreate modules if new runtime,
// recreate units and traits, then recreate children
ENTITY_VERBOSE_SELF("cloned from ", *other);
}

/// Destructor
Thing::~Thing() IF_UNSAFE(noexcept) {
Detach();
LANGULUS_ASSUME(DevAssumes, GetReferences() == 1, "Bad reference count");

/*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("Decoupling (", GetReferences(), " uses):");
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 GetReferences() > 1) {
/*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("...", GetReferences(), " uses ");
}
ENTITY_VERBOSE_SELF("...", Reference(0), " uses ");
}*/

// Decouple all children from this
for (auto& child : mChildren) {
Expand All @@ -188,19 +199,21 @@ namespace Langulus::Entity
LANGULUS_ASSUME(DevAssumes,
child->mOwner == this, "Parent mismatch");
child->mOwner.Reset();
ENTITY_VERBOSE_SELF("...", GetReferences(), " uses remain");
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);
ENTITY_VERBOSE_SELF("...", GetReferences(), " uses remain");
ENTITY_VERBOSE_SELF("...", Reference(0), " uses remain");
}

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

/// Compare two entities
Expand Down Expand Up @@ -521,7 +534,8 @@ namespace Langulus::Entity
/// @param descriptor - instructions for the entity's creation
/// @return the new child instance
Ref<Thing> Thing::CreateChild(const Neat& descriptor) {
ENTITY_VERBOSE_SELF_TAB("Producing child: ");
ENTITY_VERBOSE_SELF_TAB(
"Producing child (at ", Reference(0), " references): ");
Ref<Thing> newThing;
newThing.New(this, descriptor);
return Abandon(newThing);
Expand Down
11 changes: 5 additions & 6 deletions source/Thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ namespace Langulus::Entity
// Runtime should be destroyed last, hence it is the first member

// Runtime
Pin<Ptr<Runtime>> mRuntime;
Pin<Ref<Runtime>> mRuntime;
// Temporal flow
Pin<Ptr<Temporal>> mFlow;
Pin<Ref<Temporal>> mFlow;
// Hierarchy
Hierarchy mChildren;
// Units indexed by all their relevant reflected bases
Expand All @@ -94,7 +94,9 @@ namespace Langulus::Entity
LANGULUS_API(ENTITY) Thing(Thing&&) noexcept;
LANGULUS_API(ENTITY) Thing(Cloned<Thing>&&);
LANGULUS_API(ENTITY) Thing(Abandoned<Thing>&&);

LANGULUS_API(ENTITY) ~Thing() IF_UNSAFE(noexcept);
void Detach();

// Shallow copy is disabled, you should be able only to clone,
// move, or abandon
Expand All @@ -112,7 +114,7 @@ namespace Langulus::Entity
LANGULUS_API(ENTITY) void Create(Verb&);

template<Seek = Seek::HereAndAbove>
Any RunIn(CT::VerbBased auto&);
auto& RunIn(CT::VerbBased auto&);

LANGULUS_API(ENTITY) Any Run(const Lingua&);

Expand Down Expand Up @@ -165,9 +167,6 @@ namespace Langulus::Entity
LANGULUS_API(ENTITY)
void DumpHierarchy() const;

protected:
void Detach();

public:
///
/// Unit management
Expand Down
Loading

0 comments on commit 26633fe

Please sign in to comment.