libpsynth 0.2.1
Public Types | Public Member Functions | Protected Member Functions
psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy > Class Template Reference

A tree node. More...

#include <tree.hpp>

Inheritance diagram for psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >:
Inheritance graph
[legend]

List of all members.

Public Types

typedef std::unique_ptr< Node > node_ptr
 Pointer to the node type.
typedef ptr_iterator
< map_iterator< Key, node_ptr > > 
iterator
 Iterator to check the childs of this node.
typedef ptr_const_iterator
< map_const_iterator< Key,
std::unique_ptr< Node > > > 
const_iterator
 Iterator to check the childs of this node, const version.
typedef Traits traits
 Traits being used in this instanciation.

Public Member Functions

 tree_node ()
 Constuctor.
 ~tree_node ()
 Destructor.
iterator begin ()
 Returns an iterator to the first child of this node.
const_iterator begin () const
 Returns an iterator to the first child of this node.
iterator end ()
 Returns an iterator to the end of this node childs.
const_iterator end () const
 Returns an iterator to the end of this node childs.
const Node * parent () const
 Returns a pointer to the parent of this node or null if this is a root node.
Node * parent ()
 Returns a pointer to the parent of this node or null if this is a root node.
const Key & name () const
 Returns the name of this node.
iterator find_child (const Key &name)
 Returns an iterator to a child given by its key or end () otherwise.
const_iterator find_child (const Key &name) const
 Returns an iterator to a child given by its key or end () otherwise.
node_ptr detach (const Key &name)
 Detaches a node from the tree.
iterator remove_child (const Key &name)
 Deletes a child of this node.
bool attach (const Key &name, node_ptr node)
 Attaches a node to a tree.
node_ptr detach (iterator iter)
 Detaches a node from the tree.
Key path_name () const
 Returns the path of this node from the root to the node.
iterator remove_child (iterator iter)
 Deletes a child of this node.
void clear_childs ()
 Removes all childs.
Node & child (const Key &name)
 Returns a reference to the child of this node mathing a name.
Node & existing_child (const Key &name)
 Returns a reference to the child of this node mathing a name, throwing an exception if the node does not exist.
const Node & existing_child (const Key &name) const
 Returns a reference to the child of this node mathing a name, throwing an exception if the node does not exist.
Node & path (const Key &name)
 Returns a reference to the child matching a path.
Node & existing_path (const Key &name)
 Returns a reference to the child matching a path, an exception is thrown if the path does not exist.
const Node & existing_path (const Key &name) const
 Returns a reference to the child matching a path, an exception is thrown if the path does not exist.

Protected Member Functions

virtual void on_new_child (Node &node)
virtual void on_remove_child (Node &node)
virtual void on_init ()
virtual void on_uninit ()

Detailed Description

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
class psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >

A tree node.

This class in intended to be used by inheriting from it, so you directly gain a tree interface for a composite type. You should pass it the derived class as type parameter.

Note that the void sequence is a valid name, so the key string "..." denotes the node "" -> "" -> "" -> "".

Parameters:
NodeThe type of the class that is inheriting from us.
KeyThey type of the class that identifies tree nodes. It must be some kind of compound type and in the current implementation it must have std::string semantics.
TraitsAdditional information about the key type that is used to compose the paths.
ThreadingPolicyWether you want your tree type to be thread-safe. Note that to avoid problems when creating mix-ins tree_node won't inherit from ThreadingPolicy but it will expect you to do so (it uses Node::lock).
Todo:

Fix copy operations.

Fix string operations to be more generic.

Add value_type and other container typedefs.


Member Typedef Documentation

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
typedef ptr_const_iterator<map_const_iterator<Key, std::unique_ptr<Node> > > psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::const_iterator

Iterator to check the childs of this node, const version.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
typedef ptr_iterator<map_iterator<Key, node_ptr > > psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::iterator

Iterator to check the childs of this node.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
typedef std::unique_ptr<Node> psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::node_ptr

Pointer to the node type.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
typedef Traits psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::traits

Traits being used in this instanciation.


Constructor & Destructor Documentation

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::tree_node ( ) [inline]

Constuctor.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::~tree_node ( )

Destructor.


Member Function Documentation

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
bool psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::attach ( const Key &  name,
node_ptr  node 
)

Attaches a node to a tree.

Note that this wil fail if the node is already attached to another tree or if the selected key is already used by another child of this tree.

Returns:
true If the node was successfully attached and false otherwise.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const_iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::begin ( ) const [inline]

Returns an iterator to the first child of this node.

Const version.

Note:
Using iterators is not thread safe with any threading model.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::begin ( ) [inline]

Returns an iterator to the first child of this node.

Note:
Using iterators is not thread safe with any threading model.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
Node& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::child ( const Key &  name)

Returns a reference to the child of this node mathing a name.

The child is created if it does not exist yet.

Parameters:
nameThe name of the child.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
void psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::clear_childs ( )

Removes all childs.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
node_ptr psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::detach ( const Key &  name) [inline]

Detaches a node from the tree.

Once detached you are responsible of freeing the node memory. If the key does not refer to a node it throws an exception.

Returns:
A pointer to the detached node.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
node_ptr psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::detach ( iterator  iter)

Detaches a node from the tree.

Once detached you are responsible of freeing the node memory. If the iterator points to end () it throws an exception.

Returns:
A pointer to the detached node.
Note:
This version of detach is not thread-safe.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const_iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::end ( ) const [inline]

Returns an iterator to the end of this node childs.

Const version.

Note:
Using iterators is not thread safe with any threading model.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::end ( ) [inline]

Returns an iterator to the end of this node childs.

Note:
Using iterators is not thread safe with any threading model.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
Node& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::existing_child ( const Key &  name)

Returns a reference to the child of this node mathing a name, throwing an exception if the node does not exist.

Parameters:
nameThe name of the child.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const Node& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::existing_child ( const Key &  name) const

Returns a reference to the child of this node mathing a name, throwing an exception if the node does not exist.

Parameters:
nameThe name of the child.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
Node& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::existing_path ( const Key &  name)

Returns a reference to the child matching a path, an exception is thrown if the path does not exist.

Parameters:
nameThe path to find.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const Node& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::existing_path ( const Key &  name) const

Returns a reference to the child matching a path, an exception is thrown if the path does not exist.

Parameters:
nameThe path to find.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::find_child ( const Key &  name) [inline]

Returns an iterator to a child given by its key or end () otherwise.

Parameters:
nameThe key name of the child.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const_iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::find_child ( const Key &  name) const [inline]

Returns an iterator to a child given by its key or end () otherwise.

Parameters:
nameThe key name of the child.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const Key& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::name ( ) const [inline]

Returns the name of this node.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
virtual void psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::on_init ( ) [inline, protected, virtual]
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
virtual void psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::on_new_child ( Node &  node) [inline, protected, virtual]
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
virtual void psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::on_remove_child ( Node &  node) [inline, protected, virtual]
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
virtual void psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::on_uninit ( ) [inline, protected, virtual]
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
Node* psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::parent ( ) [inline]

Returns a pointer to the parent of this node or null if this is a root node.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
const Node* psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::parent ( ) const [inline]

Returns a pointer to the parent of this node or null if this is a root node.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
Node& psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::path ( const Key &  name)

Returns a reference to the child matching a path.

All nodes in the path are created if they do not exist.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
Key psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::path_name ( ) const

Returns the path of this node from the root to the node.

template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::remove_child ( const Key &  name) [inline]

Deletes a child of this node.

Returns:
An iterator to the next child.
template<class Node, class Key = std::string, class Traits = tree_node_traits<Key>, class ThreadingPolicy = PSYNTH_DEFAULT_THREADING <Node>>
iterator psynth::base::tree_node< Node, Key, Traits, ThreadingPolicy >::remove_child ( iterator  iter)

Deletes a child of this node.

Returns:
An iterator to the next child.
This version of remove child is not thread-safe.

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