ocra-recipes
Doxygen documentation for the ocra-recipes repository
Public Member Functions | Friends | List of all members
ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo > Class Template Referenceabstract

Base class for the Component class of the Composite pattern. More...

#include <Composite.h>

Public Types

typedef ComponentDerived component_t
 Inherited typedefs. More...
 
typedef CompositeDerived parent_t
 
typedef Parenthood< ComponentDerived, CompositeDerived, ParenthoodInfo > parenthood_t
 
typedef std::vector< parenthood_t * >::const_iterator const_iterator
 
typedef std::vector< parenthood_t * >::iterator iterator
 

Public Member Functions

bool isChildOf (const CompositeDerived &node)
 
void printTree (std::ostream &os)
 
virtual int isAncestorOf (const ComponentDerived &node) const =0
 Returns the number of levels that separate the component from a potential child. More...
 
virtual void printSubTree (int depth, std::ostream &os) const =0
 Overload in ComponentDerived and CompositeDerived to simply call printTree_impl(). More...
 
virtual void printNode (int depth, std::ostream &os) const =0
 Overload in ComponentDerived and CompositeDerived to print information about the tree node. More...
 
size_t getNumParenthoods () const
 Basic access to the parents. More...
 
const parenthood_tgetParenthood (size_t i) const
 
const_iterator parents_begin () const
 Iterator range on the set of parents. More...
 
iterator parents_begin ()
 
const_iterator parents_end () const
 
iterator parents_end ()
 
int isDescendantOf (const CompositeDerived &node) const
 Returns the number of levels that separates the component from a potential parent. More...
 
int isDescendantOf (const ComponentDerived &node) const
 

Protected Member Functions

virtual void onAttachedParent (const parenthood_t &parent)
 Default implementation of the callbacks, to overload in class ComponentDerived. More...
 
virtual void onDetachedParent (const parenthood_t &parent)
 
 Component ()
 
 ~Component ()
 

Friends

class Parenthood< ComponentDerived, CompositeDerived, ParenthoodInfo >
 

Detailed Description

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
class ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >

Base class for the Component class of the Composite pattern.

Composite objects are built upon a tree of objects, whose nodes implement the type Component. Component is specialized in:

See the wikipedia article about the Composite design pattern for more information.

This class can be used as a base class to implement a Composite pattern. To use it, you have to take the following steps:

  1. Create an interface or a base class for your component. This interface will be used to manipulate your objects without knowing whether they are one-piece objects or composite objects.
  2. Derive two classes from it: one to implement the composite components, and the other one to implement the leaves (one-piece components).
  3. Assuming the aformentionned classes are named MyComponent, MyComposite and MyLeaf, make them derive from respectively: Component<MyComponent, MyComposite>, Composite<MyComponent, MyComposite>, and Leaf<MyLeaf>. An additional template parameter can be used for MyComponent and MyComposite to specify additional information about the bonds between a Composite object and its children (see class ocra::Parenthood).
  4. Implement isAncestorOf and other abstract methods in MyComposite and MyLeaf (see the doc of isAncestorOf or the example below); The methods to implement in your children classes are declared together in this class in a dedicated section so that you can easily spot them.
  5. Implement your stuff.
  6. Instantiate your objects and attach them to each other.

The following example illustrates the simpliest form of these steps. In your code, you will of course use classes instead of structs if necessary... #include "Composite.h"

struct GroupOfShapes;

struct Shape : public ocra::Component<Shape, GroupOfShapes> { virtual void resize() = 0; };

struct SingleShape : public Shape, public ocra::Leaf<SingleShape> { void resize() {} int isAncestorOf(const Shape& shape) const { return 0; } };

struct GroupOfShapes : public Shape, public ocra::Composite<Shape, GroupOfShapes> { void resize() {} int isAncestorOf(const Shape& shape) const { return isAncestorOf_impl(shape); } };

int main() { SingleShape leaf1; SingleShape leaf2; GroupOfShapes node; node.attach(leaf1); node.attach(leaf2); node.detach(leaf1); }

In this implementation, a node can be part of several trees and can therefore have several parents. However, cycles are forbidden.

Template Parameters
ComponentDerivedis the abstract type used to manipulate both nodes and leaves without knowledge of whether they are composite or not.
CompositeDerivedis the type of the composite objects, i.e. objects that are composed of several subobjects.
ParenthoodInfois an additional type to associate additional information with a parenthood relationship. It must be default constructible with a default constructor (it can be the one defined by the compiler).
Precondition
CompositeDerived is a publicly derived class of ComponentDerived.
ParenthoodInfo is default constructible with a no-throw default constructor.
ComponentDerived publicly derives Component<ComponentDerived, CompositeDerived, ParenthoodInfo>.
CompositeDerived publicly derives Composite<ComponentDerived, CompositeDerived, ParenthoodInfo>.

The preconditions on the template parameters will be enforced at compile-time by the Parenthood class. If a BOOST_STATIC_ASSERT is fired when compiling your composite classes, please have a look at the name of the parameter of the failing assertion: it will give you a hint about how to correct your code.

Definition at line 37 of file Composite.h.

Member Typedef Documentation

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
typedef ComponentDerived ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::component_t

Inherited typedefs.

Definition at line 456 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
typedef std::vector<parenthood_t*>::const_iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::const_iterator

Definition at line 459 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
typedef std::vector<parenthood_t*>::iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::iterator

Definition at line 460 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
typedef CompositeDerived ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parent_t

Definition at line 457 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
typedef Parenthood<ComponentDerived, CompositeDerived, ParenthoodInfo> ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parenthood_t

Definition at line 458 of file Composite.h.

Constructor & Destructor Documentation

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::Component ( )
inlineprotected

Definition at line 548 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::~Component ( )
inlineprotected

Definition at line 551 of file Composite.h.

Member Function Documentation

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
size_t ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::getNumParenthoods ( ) const
inline

Basic access to the parents.

Definition at line 466 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
const parenthood_t& ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::getParenthood ( size_t  i) const
inline

Definition at line 467 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
virtual int ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::isAncestorOf ( const ComponentDerived &  node) const
pure virtual

Returns the number of levels that separate the component from a potential child.

Implemented in ocra::CompositeVariable, and ocra::BaseVariable.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
bool ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::isChildOf ( const CompositeDerived &  node)
inline

Definition at line 489 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
int ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::isDescendantOf ( const CompositeDerived &  node) const
inline

Returns the number of levels that separates the component from a potential parent.

If the object is not a descendant of node, returns 0. If it is a child of node, returns 1. If it is a grand-child, returns 2, and so on...

Definition at line 485 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
int ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::isDescendantOf ( const ComponentDerived &  node) const
inline

Definition at line 486 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
virtual void ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::onAttachedParent ( const parenthood_t parent)
inlineprotectedvirtual

Default implementation of the callbacks, to overload in class ComponentDerived.

These callbacks are called once a parent and a child have been attached/detached. When the callback is called, the parenthood has already been registered/unregistered by the child and parent (this is guaranteed by the ocra::attach/ detach function).

Definition at line 538 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
virtual void ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::onDetachedParent ( const parenthood_t parent)
inlineprotectedvirtual

Definition at line 539 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
const_iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parents_begin ( ) const
inline

Iterator range on the set of parents.

Definition at line 472 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parents_begin ( )
inline

Definition at line 473 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
const_iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parents_end ( ) const
inline

Definition at line 475 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parents_end ( )
inline

Definition at line 476 of file Composite.h.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
virtual void ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::printNode ( int  depth,
std::ostream &  os 
) const
pure virtual

Overload in ComponentDerived and CompositeDerived to print information about the tree node.

The method must not attempt to print the tree; it must only print the current node and ignore parent and children. Printing the tree is the role of printTree (a surprise, to be sure!).

Parameters
depthis the current depth of the node.

Implemented in ocra::Variable.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
virtual void ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::printSubTree ( int  depth,
std::ostream &  os 
) const
pure virtual

Overload in ComponentDerived and CompositeDerived to simply call printTree_impl().

You can also choose to reimplement the while function rather than calling the proposed printTree_impl method.

Parameters
depthis the current depth of the node.

Implemented in ocra::CompositeVariable, and ocra::BaseVariable.

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
void ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::printTree ( std::ostream &  os)
inline

Definition at line 497 of file Composite.h.

Friends And Related Function Documentation

template<class ComponentDerived, class CompositeDerived, class ParenthoodInfo = NoInfo>
friend class Parenthood< ComponentDerived, CompositeDerived, ParenthoodInfo >
friend

Definition at line 604 of file Composite.h.


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