Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Composite< V > Class Template Reference

A visitable node class that can have multiple children and parents. More...

#include <Composite.h>

Inheritance diagram for Composite< V >:

Visitable< V > CompositeBase< Visitable< V > > Referenced Referenced List of all members.

Public Types

typedef Composite Node
typedef Ref< NodeNodeRef
typedef Ref< V > VisitorRef
typedef std::vector< NodeRefChildList
 A list of child node references.

typedef std::vector< Node * > ParentList
 A list of parent node pointers (which avoids circular references).


Public Methods

virtual VisitorRef accept (VisitorRef v)=0
 Accept a visitor.

virtual bool addChild (Node *node)
 Add a child to this node.

virtual bool removeChild (Node *node)
 Remove a child from this node.

virtual bool removeChild (unsigned int pos, unsigned int count=1)
 Remove one or more children.

unsigned int getNumChildren () const
 Get the number of immediate children of this node.

virtual bool setChild (unsigned int i, Node *node)
 Replace an existing child node with a new one.

NodegetChild (unsigned int i)
 Get a child by index number.

const NodegetChild (unsigned int i) const
 Get a child by index number.

ChildList const & getChildren () const
 Get a list of immediate children of this node.

bool containsNode (Node const *node) const
 Test if this node contains a given node.

unsigned int getChildIndex (Node const *node) const
 Get the index of a child node.

const ParentListgetParents () const
 Get a list of immediate parents of this node.

NodegetParent (unsigned int i)
 Get a parent by index number.

const NodegetParent (unsigned int i) const
 Get a parent by index number.

unsigned int getNumParents () const
 Get the number of immediate parents of this node.

virtual bool isContainer () const
virtual bool canBeAdded () const
virtual Ref< V > accept (Ref< V > v)=0

Protected Methods

ParentList getParents ()
 Get a copy of this node's parent list.

void descend (VisitorBase *visitor)
 Propagate a visitor through all immediate children.

void ascend (VisitorBase *visitor)
 Propagate a visitor through all immediate parents.


Detailed Description

template<class V>
class Composite< V >

A visitable node class that can have multiple children and parents.

Composite should be used as a base class for nodes in a Composite/Visitor design pattern. First declare a context specific visitor class (see Visitor), then create use Composite<> as a base class for the primary node type. Starting from a context specific visitor (see Visitor for details) named MyNodeVisitor, one would declare MyNode as:

class MyNode: public Composite<MyNodeVisitor> { public: SIMDATA_VISITABLE(MyNodeVisitor); // the rest of MyNode's declaration... };

MyNode and each of its subclasses must define an accept() method, which is just a couple lines of boilerplate code handled by the SIMDATA_VISITABLE macro (feel free to write it out by hand if you despise macros). Defining accept() in each subclass allows the overloaded apply() method in MyNodeVisitor to be correctly selected based on the type of each node.

Notes:

Although Composite supports multiple parents, this capability can easily be disabled within a given context by overriding the addChild() method of the primary node class and testing that getNumOfParents() equals zero for each child to be added.

The current implementation does not support non- composite leaf nodes. Any node can contain other nodes, although again this feature can be disabled for specific node classes simply by overriding addChild().

Parameters:
V  is a context specific visitor class.


Member Typedef Documentation

template<class V>
typedef std::vector< NodeRef > Composite< V >::ChildList
 

A list of child node references.

template<class V>
typedef Composite Composite< V >::Node
 

template<class V>
typedef Ref<Node> Composite< V >::NodeRef
 

template<class V>
typedef std::vector< Node* > Composite< V >::ParentList
 

A list of parent node pointers (which avoids circular references).

template<class V>
typedef Ref<V> Composite< V >::VisitorRef
 


Member Function Documentation

template<class V>
virtual Ref<V> Visitable< V >::accept Ref< V >    v [pure virtual, inherited]
 

template<class V>
virtual VisitorRef Composite< V >::accept VisitorRef    v [pure virtual]
 

Accept a visitor.

This method must be implemented in each subclasses to ensure proper dispatch of the visitor's apply methods. See the Composite class documentation for details.

template<class V>
virtual bool Composite< V >::addChild Node   node [inline, virtual]
 

Add a child to this node.

Extend this method to inforce constraints on the graph structure, such as preventing nodes from having multiple parents.

Returns:
True if the child was added, false if it already exists.

template<class V>
void Composite< V >::ascend VisitorBase   visitor [inline, protected, virtual]
 

Propagate a visitor through all immediate parents.

Implements CompositeBase< Visitable< V > >.

template<class V>
virtual bool Composite< V >::canBeAdded   const [inline, virtual]
 

template<class V>
bool Composite< V >::containsNode Node const *    node const [inline]
 

Test if this node contains a given node.

Returns:
True if the node is an immediate child, false otherwise.

template<class V>
void Composite< V >::descend VisitorBase   visitor [inline, protected, virtual]
 

Propagate a visitor through all immediate children.

Implements CompositeBase< Visitable< V > >.

template<class V>
const Node* Composite< V >::getChild unsigned int    i const [inline]
 

Get a child by index number.

Returns:
0 if the index is out of range.

template<class V>
Node* Composite< V >::getChild unsigned int    i [inline]
 

Get a child by index number.

Returns:
0 if the index is out of range.

template<class V>
unsigned int Composite< V >::getChildIndex Node const *    node const [inline]
 

Get the index of a child node.

Returns:
The index if found, otherwise the number of children.

template<class V>
ChildList const& Composite< V >::getChildren   const [inline]
 

Get a list of immediate children of this node.

template<class V>
unsigned int Composite< V >::getNumChildren   const [inline]
 

Get the number of immediate children of this node.

template<class V>
unsigned int Composite< V >::getNumParents   const [inline]
 

Get the number of immediate parents of this node.

template<class V>
const Node* Composite< V >::getParent unsigned int    i const [inline]
 

Get a parent by index number.

Returns:
0 if the index is out of range.

template<class V>
Node* Composite< V >::getParent unsigned int    i [inline]
 

Get a parent by index number.

Returns:
0 if the index is out of range.

template<class V>
ParentList Composite< V >::getParents   [inline, protected]
 

Get a copy of this node's parent list.

template<class V>
const ParentList& Composite< V >::getParents   const [inline]
 

Get a list of immediate parents of this node.

template<class V>
virtual bool Composite< V >::isContainer   const [inline, virtual]
 

template<class V>
virtual bool Composite< V >::removeChild unsigned int    pos,
unsigned int    count = 1
[inline, virtual]
 

Remove one or more children.

Parameters:
pos  The index of the first child to remove.
count  The number of children to remove (default 1).
Returns:
True if one or more children were removed, false otherwise.

template<class V>
virtual bool Composite< V >::removeChild Node   node [inline, virtual]
 

Remove a child from this node.

Returns:
True if the child was removed, false if it was not found.

template<class V>
virtual bool Composite< V >::setChild unsigned int    i,
Node   node
[inline, virtual]
 

Replace an existing child node with a new one.

The original node will be destroyed if it is not already referenced elsewhere.

Returns:
True if the child was replaced, false otherwise.


The documentation for this class was generated from the following file:
SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:40 2003, using Doxygen 1.2.18.

[SF.net]