1 #ifndef ROSE_Tree_Base_H
2 #define ROSE_Tree_Base_H
3 #include <Rose/Tree/Exception.h>
5 #include <Sawyer/Assert.h>
7 #include <boost/lexical_cast.hpp>
49 Base *parent_ =
nullptr;
81 explicit operator bool()
const {
82 return parent_ !=
nullptr;
145 Edge& operator=(
const Edge&) =
delete;
173 bool operator==(
const std::shared_ptr<Base>&)
const;
174 bool operator!=(
const std::shared_ptr<Base>&)
const;
202 explicit operator bool()
const {
203 return child_ !=
nullptr;
212 class Base:
public std::enable_shared_from_this<Base> {
242 virtual void destructorHelper() {}
256 template<
class Visitor>
258 for (
auto node =
pointer(); node; node = node->parent()) {
259 if (
auto result = visitor(node))
262 return decltype(visitor(
BasePtr()))();
273 template<
class Visitor>
277 for (
size_t i = 0;
true; ++i) {
281 }
else if (child.
value) {
282 if (
auto retval = child.
value->traverse(visitor))
292 return visitParents([](
const BasePtr &vertex) -> std::shared_ptr<T> {
293 return std::dynamic_pointer_cast<T>(vertex);
303 std::vector<std::shared_ptr<T>> retval;
306 if (
auto found = std::dynamic_pointer_cast<T>(vertex))
307 retval.push_back(found);
338 virtual ChildDescriptor
findChild(
size_t i)
const;
348 return parent_ == other();
354 return parent_ != other();
360 resetParent(child_->parent);
369 : parent_(parent), child_(child) {
373 setParent(child->parent, parent);
378 const std::shared_ptr<T>&
380 ASSERT_not_null(child_);
385 const std::shared_ptr<T>&
393 return child_ == ptr;
399 return child_ != ptr;
405 return child_ == other();
411 return child_.get() != other();
418 return child_.get() == other.get();
425 return child_.get() != other.get();
431 if (child != child_) {
437 parent_.traverseReverse([&child](
const BasePtr &node) {
449 resetParent(child_->parent);
455 setParent(child->parent, parent_);
465 return (*
this) = parent();
BasePtr operator->() const
Return the parent if there is one, else null.
std::shared_ptr< T > ChildPtr
Type of pointer to the child.
Error when attaching a vertex to a tree and the vertex is already attached somewhere else...
auto traverseReverse(const Visitor &visitor)
Traverse in reverse direction from children to parents.
Traversal
Traversal direction.
Ptr pointer()
Returns a shared pointer to this vertex.
std::shared_ptr< T > findAncestor()
Traversal that finds an ancestor of a particular type.
Main namespace for the ROSE library.
Points from a child to a parent in the tree.
std::string childName(size_t i)
Returns the property name for a child.
BasePtr operator()() const
Return the parent if there is one, else null.
bool operator==(const BasePtr &) const
Compare the parent pointer to another pointer.
const ChildPtr & operator()() const
Return the child if there is one, else null.
std::string name
Property name of the child.
bool operator!=(const BasePtr &) const
Compare the parent pointer to another pointer.
size_t i
Index of the child counted across all inherited child edges.
virtual ChildDescriptor findChild(size_t i) const
Finds information about an indexed child.
Ptr value
Child pointer value.
Information about a child.
bool operator!=(const std::shared_ptr< Base > &) const
Compare the child pointer to another pointer.
size_t nChildren(size_t i)
Returns the number of children.
std::shared_ptr< Base > BasePtr
Shared-ownership pointer for Base.
A parent-to-child edge in a tree.
ReverseEdge parent
Pointer to the parent in the tree.
std::vector< std::shared_ptr< T > > findDescendants()
Traversal that finds all the descendants of a particular type.
auto traverse(const Visitor &visitor)
Traverse in forward direction from parents to children.
bool operator==(const std::shared_ptr< Base > &) const
Compare the child pointer to another pointer.
Base class for tree vertices.
Error when attaching a vertex to a tree would cause a cycle.
Ptr child(size_t i)
Returns the pointer for a child.
const ChildPtr & operator->() const
Return the child if there is one, else null.
BasePtr Ptr
Shared-ownership pointer to a Base.
T Child
Type of child being pointed to.