VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::DtTreeClass< SELF_T, SUPER_T > Class Template Reference

Detailed Description

template<class SELF_T, class SUPER_T = DtSpObject>
class makVre::DtTreeClass< SELF_T, SUPER_T >
Template Parameters
SELF_TThe derived class type (CRTP pattern)
SUPER_TThe base class type, defaults to DtSpObject

This class provides a comprehensive implementation of a tree data structure with smart pointer ownership management, child lookup by name, and various traversal capabilities. It's designed to be used as a base class for objects that need to exist in hierarchical relationships.

#include <treeClass.h>

Inheritance diagram for makVre::DtTreeClass< SELF_T, SUPER_T >:
[legend]

Public Types

using Sptr = std::shared_ptr<SELF_T>
 
using ForChildFn = std::function<bool(Sptr child)>
 
using FindChildFn = std::function<bool(Sptr child)>
 
- Public Types inherited from makVre::DtSpSubClass< SELF_T, DtSpObject >
using Super
 
using Ptr
 
using Sptr
 
using Wptr
 
- Public Types inherited from makVre::DtSpObject
using Ptr = DtSpObject*
 
using Sptr = std::shared_ptr<DtSpObject>
 
using Wptr = std::weak_ptr<DtSpObject>
 

Public Member Functions

 DtTreeClass ()
 
virtual ~DtTreeClass () override
 
virtual void setOwnsChildren (bool owns)
 
virtual bool ownsChildren ()
 
virtual bool addChild (Sptr child)
 
virtual bool removeChild (Sptr child)
 
virtual void removeAllChildren ()
 
virtual bool forEachChild (ForChildFn fn)
 
virtual Sptr findChild (FindChildFn fn)
 
virtual bool isChild (Sptr child)
 
virtual Sptr findChild (const std::string &name)
 
virtual unsigned int numChildren ()
 
virtual bool hasChildren ()
 
virtual Sptr parent ()
 
- Public Member Functions inherited from makVre::DtSpSubClass< SELF_T, DtSpObject >
 DtSpSubClass ()
 
virtual ~DtSpSubClass ()
 
Sptr self ()
 
std::string type () const override
 
- Public Member Functions inherited from makVre::DtSpObject
 DtSpObject ()
 
virtual ~DtSpObject ()
 
Sptr self ()
 
virtual void setName (const std::string &name)
 
virtual std::string name () const
 

Protected Types

using Wptr = std::weak_ptr<SELF_T>
 
using OwnedChildList = std::list<Sptr>
 
using UnownedChildList = std::list<Wptr>
 
using ChildNameMap = std::map<std::string, Wptr>
 

Protected Member Functions

virtual void setParent (Sptr parent)
 
virtual void pruneChildren ()
 

Protected Attributes

OwnedChildList myOwnedChildren
 
UnownedChildList myUnownedChildren
 
ChildNameMap myChildrenByName
 
bool myOwnsChildren
 
Wptr myParent
 
- Protected Attributes inherited from makVre::DtSpObject
std::string myName
 

Additional Inherited Members

- Static Public Member Functions inherited from makVre::DtSpSubClass< SELF_T, DtSpObject >
static std::string Type ()
 
static std::shared_ptr< SELF_T > create ()
 
static void registerCreator ()
 
static std::shared_ptr< SELF_T > createVersion (unsigned int version)
 
static void registerCreatorVersion (unsigned int version)
 
- Static Public Member Functions inherited from makVre::DtSpObject
template<class AS_T>
static std::shared_ptr< AS_T > cast (Sptr p)
 
template<class AS_T>
static std::shared_ptr< AS_T > unsafe_cast (Sptr p)
 
- Static Protected Member Functions inherited from makVre::DtSpSubClass< SELF_T, DtSpObject >
static std::shared_ptr< DtBaseCreator< SELF_T > > & creator ()
 
static std::shared_ptr< DtBaseCreator< SELF_T > > & creatorVersion (unsigned int version)
 

Member Typedef Documentation

◆ Sptr

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::Sptr = std::shared_ptr<SELF_T>

Shared pointer type for this class.

◆ ForChildFn

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::ForChildFn = std::function<bool(Sptr child)>

Function type for child traversal callbacks.

◆ FindChildFn

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::FindChildFn = std::function<bool(Sptr child)>

Function type for child search predicates.

◆ Wptr

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::Wptr = std::weak_ptr<SELF_T>
protected

Weak pointer type for this class.

◆ OwnedChildList

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::OwnedChildList = std::list<Sptr>
protected

Type for the list of owned children.

◆ UnownedChildList

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::UnownedChildList = std::list<Wptr>
protected

Type for the list of unowned children.

◆ ChildNameMap

template<class SELF_T, class SUPER_T = DtSpObject>
using makVre::DtTreeClass< SELF_T, SUPER_T >::ChildNameMap = std::map<std::string, Wptr>
protected

Type for the map of child names to child pointers.

Constructor & Destructor Documentation

◆ DtTreeClass()

template<class SELF_T, class SUPER_T = DtSpObject>
makVre::DtTreeClass< SELF_T, SUPER_T >::DtTreeClass ( )
inline

Default constructor.

Creates a new tree node with ownership of children enabled by default

References myOwnsChildren.

◆ ~DtTreeClass()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual makVre::DtTreeClass< SELF_T, SUPER_T >::~DtTreeClass ( )
inlineoverridevirtual

Virtual destructor.

Removes all children when the node is destroyed

References removeAllChildren().

Member Function Documentation

◆ setOwnsChildren()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual void makVre::DtTreeClass< SELF_T, SUPER_T >::setOwnsChildren ( bool owns)
inlinevirtual

Sets whether this node owns its children.

Parameters
ownsTrue if this node should own its children, false otherwise

When a node owns its children, it maintains strong references to them, preventing their destruction as long as the parent exists

References myOwnsChildren.

◆ ownsChildren()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual bool makVre::DtTreeClass< SELF_T, SUPER_T >::ownsChildren ( )
inlinevirtual

Checks if this node owns its children.

Returns
True if this node owns its children, false otherwise

References myOwnsChildren.

Referenced by addChild().

◆ addChild()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual bool makVre::DtTreeClass< SELF_T, SUPER_T >::addChild ( Sptr child)
inlinevirtual

Adds a child to this node.

Parameters
childShared pointer to the child node to add
Returns
True if the child was added successfully, false otherwise

This method adds a child to this node if:

  • The child is not null
  • The child is not already a child of this node
  • The child does not already have a parent

The child is added to either the owned or unowned list based on the current ownership setting of this node.

Reimplemented in makVre::DtCigiPublisherBase.

References isChild(), LOG_WARN, myOwnedChildren, myUnownedChildren, ownsChildren(), pruneChildren(), and makVre::DtSpSubClass< SELF_T, DtSpObject >::self().

◆ removeChild()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual bool makVre::DtTreeClass< SELF_T, SUPER_T >::removeChild ( Sptr child)
inlinevirtual

Removes a child from this node.

Parameters
childShared pointer to the child node to remove
Returns
True if the child was removed successfully, false if not found

This method searches for the child in both the owned and unowned children lists and removes it if found. It also clears the parent reference from the child.

References LOG_WARN, myOwnedChildren, myUnownedChildren, and pruneChildren().

◆ removeAllChildren()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual void makVre::DtTreeClass< SELF_T, SUPER_T >::removeAllChildren ( )
inlinevirtual

Removes all children from this node.

This method removes all children from this node, converting owned children to unowned first to prevent their destruction during the process. It then clears the parent reference from all children and clears internal lists.

References myChildrenByName, myOwnedChildren, myUnownedChildren, and pruneChildren().

Referenced by ~DtTreeClass().

◆ forEachChild()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual bool makVre::DtTreeClass< SELF_T, SUPER_T >::forEachChild ( ForChildFn fn)
inlinevirtual

Iterates over all children and applies a function to each.

Parameters
fnFunction to apply to each child
Returns
True if the function was applied to all children, false if interrupted

This method iterates over all children (both owned and unowned) and applies the provided function to each. If the function returns false for any child, iteration stops and the method returns false. Otherwise, it returns true.

Note that it creates copies of the child lists before iteration to allow safe modification of the lists during iteration.

References myOwnedChildren, myUnownedChildren, and pruneChildren().

◆ findChild() [1/2]

template<class SELF_T, class SUPER_T = DtSpObject>
virtual Sptr makVre::DtTreeClass< SELF_T, SUPER_T >::findChild ( FindChildFn fn)
inlinevirtual

Finds a child that matches a given predicate.

Parameters
fnPredicate function to test each child
Returns
Shared pointer to the first matching child, or null if none found

This method searches through all children (both owned and unowned) and returns the first child for which the predicate function returns true. If no child matches, it returns a null pointer.

References myOwnedChildren, myUnownedChildren, and pruneChildren().

Referenced by findChild().

◆ isChild()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual bool makVre::DtTreeClass< SELF_T, SUPER_T >::isChild ( Sptr child)
inlinevirtual

Checks if a given node is a child of this node.

Parameters
childShared pointer to the node to check
Returns
True if the node is a child of this node, false otherwise

This method checks both the owned and unowned children lists to determine if the specified node is a direct child of this node.

References myOwnedChildren, and myUnownedChildren.

Referenced by addChild().

◆ findChild() [2/2]

template<class SELF_T, class SUPER_T = DtSpObject>
virtual Sptr makVre::DtTreeClass< SELF_T, SUPER_T >::findChild ( const std::string & name)
inlinevirtual

Finds a child by name.

Parameters
nameThe name of the child to find
Returns
Shared pointer to the child with the given name, or null if none found

This method first checks a name-to-child map for fast lookup. If the child is not found in the map, it searches all children for one with the given name. If found, it adds the child to the name map for faster subsequent lookups.

The method also performs cleanup of expired references in the name map.

References findChild(), myChildrenByName, and makVre::DtSpObject::name().

◆ numChildren()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual unsigned int makVre::DtTreeClass< SELF_T, SUPER_T >::numChildren ( )
inlinevirtual

Gets the total number of children.

Returns
The total number of children (both owned and unowned)

References myOwnedChildren, and myUnownedChildren.

Referenced by hasChildren().

◆ hasChildren()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual bool makVre::DtTreeClass< SELF_T, SUPER_T >::hasChildren ( )
inlinevirtual

Checks if this node has any children.

Returns
True if this node has at least one child, false otherwise

References numChildren().

◆ parent()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual Sptr makVre::DtTreeClass< SELF_T, SUPER_T >::parent ( )
inlinevirtual

Gets the parent of this node.

Returns
Shared pointer to the parent node, or null if no parent

References myParent.

Referenced by setParent().

◆ setParent()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual void makVre::DtTreeClass< SELF_T, SUPER_T >::setParent ( Sptr parent)
inlineprotectedvirtual

Sets the parent of this node.

Parameters
parentShared pointer to the new parent node

This protected method is called by the addChild and removeChild methods to update the parent reference of this node.

References myParent, and parent().

◆ pruneChildren()

template<class SELF_T, class SUPER_T = DtSpObject>
virtual void makVre::DtTreeClass< SELF_T, SUPER_T >::pruneChildren ( )
inlineprotectedvirtual

Removes expired weak references from the unowned children list.

This protected method is called internally to clean up any weak references to children that have been destroyed. It iterates through the unowned children list and removes any expired references.

References myUnownedChildren.

Referenced by addChild(), findChild(), forEachChild(), removeAllChildren(), and removeChild().

Member Data Documentation

◆ myOwnedChildren

template<class SELF_T, class SUPER_T = DtSpObject>
OwnedChildList makVre::DtTreeClass< SELF_T, SUPER_T >::myOwnedChildren
protected

List of children owned by this node (strong references)

Referenced by addChild(), findChild(), forEachChild(), isChild(), numChildren(), removeAllChildren(), and removeChild().

◆ myUnownedChildren

template<class SELF_T, class SUPER_T = DtSpObject>
UnownedChildList makVre::DtTreeClass< SELF_T, SUPER_T >::myUnownedChildren
protected

List of children not owned by this node (weak references)

Referenced by addChild(), findChild(), forEachChild(), isChild(), numChildren(), pruneChildren(), removeAllChildren(), and removeChild().

◆ myChildrenByName

template<class SELF_T, class SUPER_T = DtSpObject>
ChildNameMap makVre::DtTreeClass< SELF_T, SUPER_T >::myChildrenByName
protected

Map of child names to child pointers for fast lookup.

Referenced by findChild(), and removeAllChildren().

◆ myOwnsChildren

template<class SELF_T, class SUPER_T = DtSpObject>
bool makVre::DtTreeClass< SELF_T, SUPER_T >::myOwnsChildren
protected

Flag indicating whether this node owns its children.

Referenced by DtTreeClass(), ownsChildren(), and setOwnsChildren().

◆ myParent

template<class SELF_T, class SUPER_T = DtSpObject>
Wptr makVre::DtTreeClass< SELF_T, SUPER_T >::myParent
protected

Weak reference to the parent node.

Referenced by parent(), and setParent().


The documentation for this class was generated from the following file: