ocra-recipes
Doxygen documentation for the ocra-recipes repository
|
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_t & | getParenthood (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 > |
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:
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.
ComponentDerived | is the abstract type used to manipulate both nodes and leaves without knowledge of whether they are composite or not. |
CompositeDerived | is the type of the composite objects, i.e. objects that are composed of several subobjects. |
ParenthoodInfo | is 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). |
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.
typedef ComponentDerived ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::component_t |
Inherited typedefs.
Definition at line 456 of file Composite.h.
typedef std::vector<parenthood_t*>::const_iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::const_iterator |
Definition at line 459 of file Composite.h.
typedef std::vector<parenthood_t*>::iterator ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::iterator |
Definition at line 460 of file Composite.h.
typedef CompositeDerived ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parent_t |
Definition at line 457 of file Composite.h.
typedef Parenthood<ComponentDerived, CompositeDerived, ParenthoodInfo> ocra::Component< ComponentDerived, CompositeDerived, ParenthoodInfo >::parenthood_t |
Definition at line 458 of file Composite.h.
|
inlineprotected |
Definition at line 548 of file Composite.h.
|
inlineprotected |
Definition at line 551 of file Composite.h.
|
inline |
Basic access to the parents.
Definition at line 466 of file Composite.h.
|
inline |
Definition at line 467 of file Composite.h.
|
pure virtual |
Returns the number of levels that separate the component from a potential child.
Implemented in ocra::CompositeVariable, and ocra::BaseVariable.
|
inline |
Definition at line 489 of file Composite.h.
|
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.
|
inline |
Definition at line 486 of file Composite.h.
|
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.
|
inlineprotectedvirtual |
Definition at line 539 of file Composite.h.
|
inline |
Iterator range on the set of parents.
Definition at line 472 of file Composite.h.
|
inline |
Definition at line 473 of file Composite.h.
|
inline |
Definition at line 475 of file Composite.h.
|
inline |
Definition at line 476 of file Composite.h.
|
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!).
depth | is the current depth of the node. |
Implemented in ocra::Variable.
|
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.
depth | is the current depth of the node. |
Implemented in ocra::CompositeVariable, and ocra::BaseVariable.
|
inline |
Definition at line 497 of file Composite.h.
|
friend |
Definition at line 604 of file Composite.h.