ROSE  0.11.145.0
Public Types | Public Member Functions | List of all members
Sawyer::Tree::Vertex< B >::Edge< T > Class Template Reference

Description

template<class B>
template<class T>
class Sawyer::Tree::Vertex< B >::Edge< T >

A parent-to-child edge in a tree.

A parent-to-child edge is a pointer-like object that points from an parent vertex to a child vertex or nullptr. It is also responsible for adjusting the child's parent pointer. The term "edge" is used instead of "pointer" because the relationship between the parent and child is bidirectional. The full term is "forward edge", but since this is the only kind of edge that users will work with, we've shortened the name to just "edge". A "reverse edge" is the pointer from child to parent.

An forward edge is always a data member of a vertex and never instantiated in other circumstances. Thus users don't normally instanticate these directly, but they do interact with them to obtain pointers to children from a parent.

This type is used to define a data member in the parent that points to a child. For instance, the following binary expression vertex has left-hand-side and right-hand-side children that are part of the tree. If lhs and rhs were not intended to be part of the tree data structure, then their types would be pointers (ExpressionPtr) instead of edges.

class BinaryExpression: public Expression {
public:
using Ptr = std::shared_ptr<BinaryExpression>;
public:
protected:
BinaryExpression()
: lhs(*this), rhs(*this) {}
static Ptr instance() {
return Ptr(new BinaryExpression);
}
};

Definition at line 127 of file Tree.h.

#include <util/Sawyer/Tree.h>

Inheritance diagram for Sawyer::Tree::Vertex< B >::Edge< T >:
Inheritance graph
[legend]
Collaboration diagram for Sawyer::Tree::Vertex< B >::Edge< T >:
Collaboration graph
[legend]

Public Types

using Child = T
 Type of child being pointed to. More...
 
using ChildPtr = std::shared_ptr< Child >
 Type of pointer to the child. More...
 

Public Member Functions

 ~Edge ()
 Destructor clears child's parent. More...
 
 operator bool () const
 True if child is not null. More...
 
 Edge (UserBase &parent)
 Construct a child edge that belongs to the specified parent. More...
 
 Edge (UserBase &parent, const ChildPtr &child)
 Construct a child edge that belongs to the specified parent. More...
 
const ChildPtroperator-> () const
 Return the child if there is one, else null.
 
const ChildPtroperator() () const
 Return the child if there is one, else null.
 
bool operator== (const UserBasePtr &) const
 Compare the child pointer to another pointer.
 
bool operator!= (const UserBasePtr &) const
 Compare the child pointer to another pointer.
 
bool operator== (const ReverseEdge &) const
 Compare the child pointer to another pointer.
 
bool operator!= (const ReverseEdge &) const
 Compare the child pointer to another pointer.
 
template<class U >
bool operator== (const Edge< U > &) const
 Compare the child pointer to another pointer.
 
template<class U >
bool operator!= (const Edge< U > &) const
 Compare the child pointer to another pointer.
 
Edgeoperator= (const ChildPtr &child)
 Assign a pointer to a child. More...
 
Edgeoperator= (const ReverseEdge &parent)
 Assign a pointer to a child. More...
 

Member Typedef Documentation

template<class B>
template<class T>
using Sawyer::Tree::Vertex< B >::Edge< T >::Child = T

Type of child being pointed to.

Definition at line 324 of file Tree.h.

template<class B>
template<class T>
using Sawyer::Tree::Vertex< B >::Edge< T >::ChildPtr = std::shared_ptr<Child>

Type of pointer to the child.

Definition at line 327 of file Tree.h.

Constructor & Destructor Documentation

template<class B >
template<class T >
Sawyer::Tree::Vertex< B >::Edge< T >::~Edge ( )

Destructor clears child's parent.

Definition at line 952 of file Tree.h.

References Sawyer::Tree::Vertex< B >::parent.

template<class B >
template<class T >
Sawyer::Tree::Vertex< B >::Edge< T >::Edge ( UserBase parent)
explicit

Construct a child edge that belongs to the specified parent.

When constructing a class containing a data member of this type (i.e., a tree edge that points to a child of this vertex), the data member must be initialized by passing *this as the argument. See the example in this class documentation.

An optional second argument initializes the child pointer for the edge. The initialization is the same as if the child had been assigned with operator= later. I.e., the child must not already have a parent.

Definition at line 959 of file Tree.h.

template<class B>
template<class T>
Sawyer::Tree::Vertex< B >::Edge< T >::Edge ( UserBase parent,
const ChildPtr child 
)

Construct a child edge that belongs to the specified parent.

When constructing a class containing a data member of this type (i.e., a tree edge that points to a child of this vertex), the data member must be initialized by passing *this as the argument. See the example in this class documentation.

An optional second argument initializes the child pointer for the edge. The initialization is the same as if the child had been assigned with operator= later. I.e., the child must not already have a parent.

Member Function Documentation

template<class B>
template<class T>
Edge& Sawyer::Tree::Vertex< B >::Edge< T >::operator= ( const ChildPtr child)
inline

Assign a pointer to a child.

If this edge points to an old child then that child is removed and its parent is reset. If the specified new child is non-null, then it is inserted and its parent pointer set to the parent of this edge.

However, if the new child already has a non-null parent, then no changes are made and an InsertionError is thrown with the error's vertex pointing to the intended child. Otherwise, if the new child is non-null and is the parent or any more distant ancestor of this edge's vertex, then a CycleError is thrown. Cycle errors are only thrown if debugging is enabled (i.e., the CPP macro NDEBUG is undefined).

Attempting to assign one child edge object to another is a compile-time error (its operator= is not declared) because every non-null child edge points to a child whose parent is non-null, which would trigger an InsertionError. Therefore only null child edges could be assigned. But since only null child edges can be assigned, its more concise and clear to assign the null pointer directly.

Definition at line 391 of file Tree.h.

References Sawyer::Tree::Vertex< B >::child().

template<class B>
template<class T>
Edge& Sawyer::Tree::Vertex< B >::Edge< T >::operator= ( const ReverseEdge< T > &  parent)
inline

Assign a pointer to a child.

If this edge points to an old child then that child is removed and its parent is reset. If the specified new child is non-null, then it is inserted and its parent pointer set to the parent of this edge.

However, if the new child already has a non-null parent, then no changes are made and an InsertionError is thrown with the error's vertex pointing to the intended child. Otherwise, if the new child is non-null and is the parent or any more distant ancestor of this edge's vertex, then a CycleError is thrown. Cycle errors are only thrown if debugging is enabled (i.e., the CPP macro NDEBUG is undefined).

Attempting to assign one child edge object to another is a compile-time error (its operator= is not declared) because every non-null child edge points to a child whose parent is non-null, which would trigger an InsertionError. Therefore only null child edges could be assigned. But since only null child edges can be assigned, its more concise and clear to assign the null pointer directly.

Definition at line 410 of file Tree.h.

References Sawyer::Tree::Vertex< B >::parent.

template<class B>
template<class T>
Sawyer::Tree::Vertex< B >::Edge< T >::operator bool ( ) const
inlineexplicit

True if child is not null.

Definition at line 416 of file Tree.h.


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