Silicon
A realtime platform for creating interactive media.
Node.cpp
Go to the documentation of this file.
1// BSD 2-Clause License
2//
3// Copyright (c) 2022, Matthew McCall
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27//
28// Created by Matthew McCall on 10/9/22.
29//
30
31#include "Silicon/Node.hpp"
32
33namespace Si
34{
35
36unsigned Node::s_currentID = 0;
38
40 : m_id(s_currentID++)
41 , m_graphDescriptor(boost::add_vertex(NotNull<Node *>(this), s_graph))
42{
43}
44
46 : Node()
47{
48 addChildren(children);
49}
50
52{
53 boost::add_edge(m_graphDescriptor, node->m_graphDescriptor, s_graph);
54}
55
57{
59}
60
62{
63 boost::clear_vertex(m_graphDescriptor, s_graph);
64 boost::remove_vertex(m_graphDescriptor, s_graph);
65}
66
68{
69 auto [begin, end] = boost::adjacent_vertices(m_graphDescriptor, s_graph);
70 return Node::ChildIterator { begin };
71}
73{
74 auto [begin, end] = boost::adjacent_vertices(m_graphDescriptor, s_graph);
75 return Node::ChildIterator {end};
76}
78{
79 for (Node *i : children) {
81 }
82
83 return *this;
84}
85
86Node::ChildIterator::ChildIterator(NodeGraph::adjacency_iterator itr)
87: m_itr(itr) { }
88
90{
91 return *s_graph[static_cast<NodeGraph::vertex_descriptor>(*m_itr)];
92}
93
95{
96 return s_graph[static_cast<NodeGraph::vertex_descriptor>(*m_itr)];
97}
99{
100 ++m_itr;
101 return *this;
102}
104{
105 --m_itr;
106 return *this;
107}
109{
110 const auto tmp = *this;
111 ++*this;
112 return tmp;
113}
115{
116 const auto tmp = *this;
117 --*this;
118 return tmp;
119}
121{
122 return m_itr == other.m_itr;
123}
125{
126 return !(*this == other);
127}
128
129}
130
ChildIterator & operator--()
Definition: Node.cpp:103
bool operator==(const ChildIterator &other)
Definition: Node.cpp:120
bool operator!=(const ChildIterator &other)
Definition: Node.cpp:124
Node * operator->()
Definition: Node.cpp:94
ChildIterator(NodeGraph::adjacency_iterator)
Definition: Node.cpp:86
ChildIterator & operator++()
Definition: Node.cpp:98
Node & operator*()
Definition: Node.cpp:89
static unsigned s_currentID
Definition: Node.hpp:125
static NodeGraph s_graph
Definition: Node.hpp:126
NodeGraph::vertex_descriptor m_graphDescriptor
Definition: Node.hpp:123
ChildIterator end() const
Gets the end iterator for the children of this node.
Definition: Node.cpp:72
Node & addChildren(std::initializer_list< Node * > children)
Add a list of children to this node.
Definition: Node.cpp:77
Node()
Construct a new Node object.
Definition: Node.cpp:39
ChildIterator begin() const
Gets the begin iterator for the children of this node.
Definition: Node.cpp:67
void addChild(NotNull< Node * >)
Add a child to this node.
Definition: Node.cpp:51
virtual ~Node()
Destroys this node.
Definition: Node.cpp:61
Definition: Allocator.hpp:36
Graph< NotNull< Node * >, GraphList > NodeGraph
Definition: Node.hpp:42
gsl::strict_not_null< T > NotNull
Definition: Types.hpp:48