Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
DRAIN_TREE_NAME< T, EXCLUSIVE, P > Class Template Reference

A templated class for directed rooted trees. More...

#include <Tree.h>

Collaboration diagram for DRAIN_TREE_NAME< T, EXCLUSIVE, P >:
Collaboration graph
[legend]

Public Types

typedef drain::DRAIN_TREE_NAME< T, EXCLUSIVE, P > tree_t
 
typedef T node_data_t
 
typedef P path_t
 
typedef path_t::elem_t key_t
 
typedef path_t::elem_t path_elem_t
 
typedef std::pair< key_t, tree_tpair_t
 
typedef container_t::iterator iterator
 
typedef container_t::const_iterator const_iterator
 
typedef std::pair< key_t, node_data_t > node_pair_t
 Experimental. Given pair<elem, data> assigns child[elem] = data;.
 

Public Member Functions

typedef DRAIN_TREE_CONTAINER (key_t, tree_t) container_t
 
 DRAIN_TREE_NAME ()
 Default constructor.
 
 DRAIN_TREE_NAME (const node_data_t &data)
 Copy constructor; copy only node data at the root.
 
 DRAIN_TREE_NAME (const DRAIN_TREE_NAME &t)
 Copy constructor; copy only node data at the root.
 
virtual bool hasMultipleData () const
 
virtual const node_data_t & getData () const
 
virtual node_data_t & getData ()
 
virtual const node_data_t & getData (const key_t &key) const
 
virtual node_data_t & getData (const key_t &key)
 
container_t::const_iterator begin () const
 Child iterator pointing to the first child.
 
container_t::const_iterator end () const
 Child iterator pointing beyond the last child.
 
container_t::iterator begin ()
 Child iterator pointing to the first child.
 
container_t::iterator end ()
 Child iterator end.
 
tree_toperator= (const tree_t &t)
 Copies the data of another node. Does not copy the children.
 
template<class T2 >
tree_toperator= (const T2 &v)
 Assigns value to contents.
 
tree_toperator= (const char *str)
 
template<typename K , typename V >
tree_toperator= (std::initializer_list< std::pair< K, V > > l)
 Assign tree structure (of depth one).
 
tree_toperator= (std::initializer_list< std::pair< const char *, const Variable > > l)
 Assign tree structure (of depth one).
 
template<typename V >
tree_toperator= (const std::initializer_list< V > &l)
 Assign data.
 
tree_toperator<< (const node_pair_t &entry)
 Experimental. Given pair(elem, data) assigns child[elem] = data;.
 
 operator const node_data_t & () const
 
 operator node_data_t & ()
 
template<class K >
tree_toperator[] (const K &key)
 NEW 2025 templated child addressing operator.
 
template<class K >
const tree_toperator[] (const K &key) const
 NEW 2025 templated child addressing operator.
 
tree_toperator() (const path_t &path)
 Returns a descendant. Creates one if not existing already.
 
template<class S >
tree_toperator() (const S &arg)
 Returns a descendant. Creates one if not existing already.
 
tree_toperator() (const char *arg)
 Redirects the call to operator()(const std::string & arg) .
 
const tree_toperator() (const path_t &path) const
 Returns a descendant if that exists, else returns an empty node. Otherwise like non-const counterpart.
 
template<class S >
const tree_toperator() (const S &arg) const
 Returns a descendant if that exists, else returns an empty node. Otherwise like non-const counterpart.
 
const tree_toperator() (const char *arg) const
 Redirects the call to operator()(const std::string & arg) .
 
void clear ()
 Clear children and node data.
 
void clearData ()
 
void clearChildren ()
 Clears the children of this node. Does not clear data.
 
void erase (const path_t &path)
 Deletes a descendant node and hence its subtrees.
 
virtual const tree_tgetEmpty () const
 
virtual bool empty () const
 Check if the tree node has empty data and no children.
 
bool hasChildren () const
 
virtual int hasChildren (const key_t &key) const
 The number of children with name key.
 
virtual bool hasChild (const key_t &key) const
 Check if the tree node has a direct descendant with name key.
 
template<typename K >
bool hasChild (const K &key) const
 
bool hasPath (const path_t &path) const
 
void initChild (tree_t &child) const
 Means for automatically setting something, for example the type of a node.
 
tree_taddChild ()
 Add a child with an automatically generated key (path element).
 
tree_taddChild (const key_t &key)
 Add a child node. If UNORDERED and not MULTIPLE, reuse existing nodes.
 
tree_tprependChild ()
 Push in the front. This is only available for unordered trees.
 
virtual tree_tprependChild (const key_t &key)
 
virtual tree_tretrieveChild (const key_t &key)
 
virtual const tree_tretrieveChild (const key_t &key) const
 
container_t & getChildren ()
 Returns the map containing the children.
 
const container_t & getChildren () const
 Returns the map containing the children.
 
const node_data_t * operator-> () const
 Fast access to data, applied widely in TreeXML (HTML/SVG)
 
node_data_t * operator-> ()
 Fast access to data, applied widely in TreeXML (HTML/SVG)
 
void swap (tree_t &t)
 Replace children (but no data?)
 

Static Public Member Functions

static bool isExclusive ()
 True, if this tree type can either children or node data, but not both.
 
static bool isMultiple ()
 True, if this tree type can contain children with a same key (path element).
 
static bool isOrdered ()
 True, if the keys of the children of a node are stored in an ordered structure (that is, std::map).s.
 
static void generateKey (const tree_t &tree, typename P::elem_t &key)
 

Public Attributes

node_data_t data
 Contents (data) of the node.
 

Protected Member Functions

bool hasPath (typename path_t::const_iterator it, typename path_t::const_iterator eit) const
 "Default implementation" of key conversion – the identity mapping.
 
tree_tget (typename path_t::const_iterator it, typename path_t::const_iterator eit)
 Returns a descendant. Creates one, if not present.
 
const tree_tget (typename path_t::const_iterator it, typename path_t::const_iterator eit) const
 Given the descendant pointed to by a given path segment.
 

Protected Attributes

container_t children
 

Static Protected Attributes

static const tree_t emptyNode
 

Detailed Description

template<class T, bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
class drain::DRAIN_TREE_NAME< T, EXCLUSIVE, P >

A templated class for directed rooted trees.

This file provides templates of for basic types of trees (directed rooted trees):

Template Parameters
T- node type, containing user-designed data implementing clear(), size() and empty() for future compatibility.
EXCLUSIVE- node can contains either data or children but not both (JSON-like convention)
P- applied path structure, like std::list – must implement empty() both for path and keys (path elements)

Data:

The nodes in a tree contain user defined data, the type of which is given as template T.

Currenty, data Concept is requires the following methods:

A tree is EXCLUSIVE, if a node contains either data or children. For example, a JSON tree is EXCLUSIVE and an XML tree is not (a tag can containg attributes (data) and inner tages (children).

Further, each type listed above has two variants:

Limitation: in OrderedTree the children are in a std::map, and threrefore each child of a node has a unique key. In UnorderedTree there may be children having the same key.

Addressing:

tree(<path>) return a descendant tree[<path_element>] returns childs only (complains separators, for some time)

Child nodes can be addressed with operator[] with std::string valued keys:

OrderedTree<std::string> tree;
t["first"] = "Hello!";
t["second"] = "Another greeting...";

As the operator[] returns a reference, it may be used recursively:

OrderedTree<std::string> & subTree1 = t["first"]["child"]["grandChild"];

Tree also recognizes a path separator, which is '/' by default. Hence, the node created in the above example could be likewise created or addressed as:

OrderedTree<std::string> & subTree = t("first/child/grandChild"); // equivalent

One may iterate the children directly:

for (auto & entry: tree){
std::cout << entry.first << ':' << entry.second << '\n';
}

Each node contains data of type T. It may be unused (empty), that is, a node can be used only as a branching point.

OrderedTree<std::string> t;
t.data = "Hello world";
std::string s = t; // Now contains "Hello world".

(Direct assignments to and from the node data has been supported, but is now disabled for c++ compatibility reasons.)

Also direct referencing of the node data is now disabled:

Tree<std::string> t;
t = "Hello world";
Tree<std::string> &r = t; // Refers to tree.
std::string &s = t; // Refers to data, "Hello world".

Obsolete: template C - path element comparison functor, should be compatible with Path<P>::key_t. #ifndef DRAIN_AMBIVALUE #define DRAIN_AMBIVALUE "0.1beta" template <class T, class K> class CombiValue { public:

typedef T data_t;
typedef K key_t;

T data; CONSIDER! But Hi5Node heavy?

inline
~CombiValue(){};

/ True, if data is structured. virtual bool hasMultipleData() const = 0;

virtual
const data_t & getData() const = 0;

virtual
data_t & getData() = 0;

virtual
const data_t & getData(const key_t &) const = 0;

virtual
data_t & getData(const key_t &) = 0;

}; #endif

Template Parameters
K– key type
K– key type

Member Function Documentation

◆ addChild() [1/2]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
tree_t & addChild ( )
inline

Add a child with an automatically generated key (path element).

For example, specialized methods may generate a key (path element) automatically.

See also
TreeHTML::addChild()

◆ addChild() [2/2]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
tree_t & addChild ( const key_t &  key)
inline

Add a child node. If UNORDERED and not MULTIPLE, reuse existing nodes.

Behaviour of this function varies as follows:

  • OrderedTree: return child named key, if exists
  • UnorderedTree, UNIQUE==true: return the first child named key, if exists, else create one.
  • UnorderedTree, UNIQUE==false: always create a new child named key and return it.

◆ clear()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
void clear ( )
inline

Clear children and node data.

See also
erase()
clearChildren()
clearData()

◆ clearChildren()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
void clearChildren ( )
inline

Clears the children of this node. Does not clear data.

x *

See also
clearData()
clear()
erase()

◆ clearData()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
void clearData ( )
inline

◆ erase()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
void erase ( const path_t &  path)
inline

Deletes a descendant node and hence its subtrees.

As opposite of clear() which focuses on a (current) node, this function refers to a descendant.

See also
clear()

◆ get()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
const tree_t & get ( typename path_t::const_iterator  it,
typename path_t::const_iterator  eit 
) const
inlineprotected

Given the descendant pointed to by a given path segment.

Returns
– descendant node, or the node itself if the path is empty.

◆ getChildren()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
container_t & getChildren ( )
inline

Returns the map containing the children.

This is useful for example for map::swap ?

◆ getEmpty()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
virtual const tree_t & getEmpty ( ) const
inlinevirtual

GENERAL (independent from container type) Empty "default" node; typically initialized with empty data as well.

◆ hasChild()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
virtual bool hasChild ( const key_t &  key) const
inlinevirtual

Check if the tree node has a direct descendant with name key.

A multiple tree may have more than one children with the same name. With them, consider hasChildren(const key_t &key) .

See also
OrderedMultiTree
UnorderedMultiTree
Returns
– the number of children with name 'key'.

◆ hasChildren()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
virtual int hasChildren ( const key_t &  key) const
inlinevirtual

The number of children with name key.

Checks how many immediate descendants with name key exist.

A multiple tree may have more than one children with the same name.

See also
OrderedMultiTree, UnorderedMultiTree

With simple trees, use the faster hasChild(const key_t &key).

See also
OrderedTree
UnorderedTree.
Returns
– the number of children with name 'key'.

◆ hasPath()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
bool hasPath ( typename path_t::const_iterator  it,
typename path_t::const_iterator  eit 
) const
inlineprotected

"Default implementation" of key conversion – the identity mapping.

Conversion of char array to key type, which itself should never be a char array. Mapping of keys of external type - for example an enumerated type - to native key_t type. Checks if there is a node with a given path name. Could be called hasDescendant; hence is like hasChild() but calls children recursively.

Parameters
eit- end()

◆ initChild()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
void initChild ( tree_t child) const
inline

Means for automatically setting something, for example the type of a node.

For example the default type of a node can be set based on the type of the host node.

See also
TreeHTML::initChild()

Future option: also key (of type const key_t &) is considered

◆ isExclusive()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
static bool isExclusive ( )
inlinestatic

True, if this tree type can either children or node data, but not both.

A JSON structure is implemented as an EXCLUSIVE tree: a node contains either a subtree or a value (FlexibleMap).

See also
TreeExamples

◆ operator()() [1/4]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
tree_t & operator() ( const char *  arg)
inline

Redirects the call to operator()(const std::string & arg) .

Parameters
arg- by default, the path from the current node to the child node.

◆ operator()() [2/4]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
tree_t & operator() ( const path_t &  path)
inline

Returns a descendant. Creates one if not existing already.

Parameters
path- the path from the current node to the child node.

This function, using native path_t type, should be reserved for this semantic by not redefining (specialising) this function in template classes.

◆ operator()() [3/4]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
const tree_t & operator() ( const path_t &  path) const
inline

Returns a descendant if that exists, else returns an empty node. Otherwise like non-const counterpart.

Parameters
path- the path from the current node to the child node.

This function, using native path_t type, should be reserved for this semantic by not redefining (specialising) this function in template classes.

◆ operator()() [4/4]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
template<class S >
tree_t & operator() ( const S &  arg)
inline

Returns a descendant. Creates one if not existing already.

Parameters
path- the path from the current node to the child node.

This function, not using native path_t type, can be redefined in template classes. For example, an important attribute can be set using this method.

However, operator()(const char *arg) should be redefined to produce equivalent result with const string &arg.

◆ operator=() [1/5]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
template<typename V >
tree_t & operator= ( const std::initializer_list< V > &  l)
inline

Assign data.

If init list elements are not pairs, assign it to node data.

2025/01 experimental.

◆ operator=() [2/5]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
template<class T2 >
tree_t & operator= ( const T2 &  v)
inline

Assigns value to contents.

Assigns a value to contents. This may be redefined in derived classes. For example, assigning a value of certain class may cause adding a specific child element instead of assigning to data (the default, here).

◆ operator=() [3/5]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
tree_t & operator= ( const tree_t t)
inline

Copies the data of another node. Does not copy the children.

Default use to reset a node (re-initialize its data).

◆ operator=() [4/5]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
tree_t & operator= ( std::initializer_list< std::pair< const char *, const Variable > >  l)
inline

Assign tree structure (of depth one).

Default implementation: assign new nodes (children).

Examples of overriding:

  • TreeXML::operator=()
  • TreeSVG::operator=()

◆ operator=() [5/5]

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
template<typename K , typename V >
tree_t & operator= ( std::initializer_list< std::pair< K, V > >  l)
inline

Assign tree structure (of depth one).

Default implementation: assign new nodes (children).

◆ operator[]()

template<class T , bool EXCLUSIVE = false, class P = drain::Path<std::string,'/'>>
template<class K >
tree_t & operator[] ( const K &  key)
inline

NEW 2025 templated child addressing operator.

Allows using EnumDict enums as keys, for example.


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