HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_Graph.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UN_Graph.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_Graph_h__
13 #define __UN_Graph_h__
14 
15 #include "UN_API.h"
16 #include "UN_GraphUtils.h"
17 #include "UN_Node.h"
18 #include "UN_StickyNote.h"
19 #include "UN_Wire.h"
20 
21 class UN_GraphData;
22 
23 // ============================================================================
24 /// Base class template for the UN graph handle.
25 /// Its main purpose is to factor common code for const and non-const handles.
26 template <typename GRAPH_DATA, // UN_GraphData or const UN_GraphData
27  typename NODE, // Node handle class (UN_Node or UN_ConstNode)
28  typename WIRE, // Wire handle class (UN_Wire or UN_ConstWire)
29  typename STICKY_NOTE> // Sticky note handle (UN_StickyNote or const)
31 {
32 protected:
33  /// Constructs a handle for an invalid wire.
34  UN_GraphBase() = default;
35 
36  /// Constructs a handle that references the given wire in the given graph.
37  UN_GraphBase( GRAPH_DATA *graph_data )
38  : myGraphData( graph_data )
39  {}
40 
41 public:
42  /// @{ Returns the owner container of the underlying graph data structures.
43  GRAPH_DATA * graphData() const
44  { return myGraphData; }
45  const UN_GraphData * constGraphData() const
46  { return myGraphData; }
47  /// @}
48 
49  /// @{ Returns true if this is a valid graph; false otherwise.
50  bool isValid() const
51  { return myGraphData; }
52  explicit operator bool() const
53  { return isValid(); }
54  /// @}
55 
56  /// Returns the root node of the graph's node parental hierarchy.
57  /// The root is a special node that is a parent to graph's top-level nodes.
58  NODE rootNode() const
59  {
60  return NODE( myGraphData, UN_GraphUtils::rootNode( myGraphData ));
61  }
62 
63  /// Returns true if the graph accepts the given name as a legal node name.
64  /// Note, even though the name is legal, it still may not be unique.
65  bool isValidNodeName( const UT_StringRef &name ) const
66  {
67  return UN_GraphUtils::isValidNodeName( myGraphData, name );
68  }
69 
70  /// Based on the given name, returns a valid name that is not used
71  /// by any of the top level nodes.
73  const UT_StringRef &name = UT_StringRef() ) const
74  {
75  return UN_GraphUtils::findValidUniqueNodeName( myGraphData, name );
76  }
77 
78  /// Returns the names of the graph's top-level nodes.
80  {
81  return UN_GraphUtils::nodeNames( myGraphData );
82  }
83 
84 
85  /// Returns a node at the given path, or an invalid handle if not found.
86  /// If the path is just a name, it looks for the top-level node,
87  /// otherwise if the path contains slashes '/', it looks for
88  /// nested nodes inside subnets.
89  NODE findNode( const UT_StringRef &node_path ) const
90  {
91  return NODE( graphData(),
92  UN_GraphUtils::findNode( myGraphData, node_path ));
93  }
94 
95  /// Returns a sticky note at the given path, or an invalid handle
96  /// if not found.
97  /// If the path is just a name, it looks for the top-level sticky note,
98  /// otherwise if the path contains slashes '/', it looks for
99  /// sticky notes nested inside subnets.
100  STICKY_NOTE findStickyNote( const UT_StringRef &sticky_note_path ) const
101  {
102  return STICKY_NOTE( graphData(),
103  UN_GraphUtils::findStickyNote( myGraphData, sticky_note_path ));
104  }
105 
106  /// Iterarates over the top-level nodes of the graph.
109 
110  /// Returns a range for iterating over graph's top-level nodes.
112  {
113  return rootNode().childNodeRange();
114  }
115 
116  /// Returns the top level nodes directly inside this graph.
118  {
119  return UNhandleArray<NODE>( graphData(),
120  UN_GraphUtils::topNodes( graphData() ));
121  }
122 
123  /// Iterarates over all the nodes in the graph,
124  /// both the top-level nodes and their descendants too.
125  using AllNodeIterator = typename NODE::DescendantNodeIterator;
127 
128  /// Returns a range for iterating over all of the graph's nodes,
129  /// both top-level as well as deeply nested ones.
131  {
132  return rootNode().descendantNodeRange();
133  }
134 
135 
136  /// An iterator and a range for the top-level wires inside this graph.
139 
140  /// Returns a range for iterating over graph's top-level connection wires.
142  {
143  return rootNode().childWireRange();
144  }
145 
146  /// Returns the wires connecting top level nodes directly inside this graph.
148  {
149  return UNhandleArray<WIRE>( graphData(),
150  UN_GraphUtils::topWires( graphData() ));
151  }
152 
153 
154  /// An iterator and a range for all the connection wires in this graph.
155  using AllWireIterator = typename NODE::DescendantWireIterator;
157 
158  /// Returns a range for iterating over all the graph's connection wires,
159  /// both top-level as well as deeply nested ones.
161  {
162  return rootNode().descendantWireRange();
163  }
164 
165 
166  /// Returns the options object representing this graph.
167  /// This can be then used for serialization to JSON, etc.
169  {
171  }
172 
173 protected:
174  /// @{ Comparison operators.
175  bool operator==( const UN_GraphBase &other ) const
176  { return myGraphData == other.myGraphData; }
177  bool operator!=( const UN_GraphBase &other ) const
178  { return !( *this == other ); }
179  /// @}
180 
181 
182 private:
183  /// Graph data object containing the details about the graph as a whole
184  /// such as its nodes and connections between them.
185  GRAPH_DATA * myGraphData = nullptr;
186 };
187 
188 // ============================================================================
189 /// A handle that references a non-const (mutable) graph.
190 /// It abstracts the APIs that operate on this graph's data,
191 /// which is stored inside graph's data container.
192 /// It is a non-const (read-write) handle, so allows both queries
193 /// and mutations of the graph's data.
194 class UN_API UN_Graph : public UN_GraphBase< UN_GraphData,
195  UN_Node, UN_Wire, UN_StickyNote >
196 {
197 public:
198  /// Constructs a handle for an invalid graph.
199  UN_Graph() = default;
200 
201  /// Constructor that creates handle for the given graph data.
202  UN_Graph( UN_GraphData *graph_data )
203  : UN_GraphBase( graph_data )
204  {}
205 
206  /// @{ Comparison operators
207  // Allows comparing graph handles, but guards against
208  // comparing graph to nodes, for example.
209  bool operator==( const UN_Graph &other ) const
210  { return UN_GraphBase::operator==( other ); }
211  bool operator!=( const UN_Graph &other ) const
212  { return UN_GraphBase::operator!=( other ); }
213  /// @}
214 
215 
216  /// Creates a node in the graph.
218  const UT_StringRef &node_name_or_path = UT_StringRef(),
219  const UT_StringRef &node_type = UT_StringRef(),
220  const UT_StringRef &node_category = UT_StringRef()) const
221  {
222  return UN_Node( graphData(),
223  UN_GraphUtils::createNode( graphData(),
224  node_name_or_path, node_type, node_category ));
225  }
226 
227  /// Removes and deletes the node from this graph.
228  void deleteNode( const UT_StringRef &node_name_or_path ) const
229  {
230  UN_GraphUtils::deleteNode( graphData(), node_name_or_path );
231  }
232 
233 
234  /// Creates a sticky note in the graph.
236  const UT_StringRef &sticky_note_name_or_path = UT_StringRef(),
237  const UT_StringRef &sticky_note_text = UT_StringRef()) const
238  {
239  return UN_StickyNote( graphData(),
240  UN_GraphUtils::createStickyNote( graphData(),
241  sticky_note_name_or_path, sticky_note_text ));
242  }
243 
244  /// Removes and deletes the sticky note from this graph.
245  void deleteStickyNote( const UT_StringRef &sticky_note_name_or_path ) const
246  {
247  UN_GraphUtils::deleteStickyNote( graphData(), sticky_note_name_or_path);
248  }
249 
250  /// Load and set the graph based on the given options.
251  void setFromOptions( const UN_Options &graph_opts ) const
252  {
253  UN_GraphUtils::setFromOptions( graphData(), graph_opts );
254  }
255 };
256 
257 
258 // ============================================================================
259 /// A handle that references a const graph.
260 /// It abstracts the APIs that operate on this graph's data.
261 /// It is a constant handle, so allows only queries of the graph's data,
262 /// and does not allow modifying it.
263 class UN_API UN_ConstGraph : public UN_GraphBase< const UN_GraphData,
264  UN_ConstNode, UN_ConstWire, UN_ConstStickyNote>
265 {
266 public:
267  /// Constructs a handle for an invalid graph.
268  UN_ConstGraph() = default;
269 
270  /// Constructor that creates handle for the given graph data.
271  UN_ConstGraph( const UN_GraphData *graph_data )
272  : UN_GraphBase( graph_data )
273  {}
274 
275  /// Constructs a const handle from a mutable graph handle.
276  UN_ConstGraph( const UN_Graph &graph )
277  : UN_ConstGraph( graph.constGraphData() )
278  {}
279 
280  /// @{ Comparison operators
281  // Allows comparing graph handles, but guards against
282  // comparing graph to nodes, for example.
283  bool operator==( const UN_ConstGraph &other ) const
284  { return UN_GraphBase::operator==( other ); }
285  bool operator!=( const UN_ConstGraph &other ) const
286  { return UN_GraphBase::operator!=( other ); }
287  /// @}
288 };
289 
290 // ============================================================================
291 // Free functions to compare a non-const graph handle to a const graph handle.
292 // Note, the const to non-const comparison is covered by implicit conversion
293 // of non-const handle to const handle, and using const to const handle comp.
294 static inline bool
295 operator==( const UN_Graph &a, const UN_ConstGraph &b )
296 {
297  return b == UN_ConstGraph(a);
298 }
299 
300 static inline bool
301 operator!=( const UN_Graph &a, const UN_ConstGraph &b )
302 {
303  return !(b == a);
304 }
305 
306 // ============================================================================
307 #endif
308 
STICKY_NOTE findStickyNote(const UT_StringRef &sticky_note_path) const
Definition: UN_Graph.h:100
bool operator!=(const UN_ConstGraph &other) const
Comparison operators.
Definition: UN_Graph.h:285
UN_WireIDList::const_iterator ChildWireIterator
A range for traversing the wires contained directly inside a node.
Definition: UN_NodeUtils.h:845
UN_Node createNode(const UT_StringRef &node_name_or_path=UT_StringRef(), const UT_StringRef &node_type=UT_StringRef(), const UT_StringRef &node_category=UT_StringRef()) const
Creates a node in the graph.
Definition: UN_Graph.h:217
UN_StickyNote createStickyNote(const UT_StringRef &sticky_note_name_or_path=UT_StringRef(), const UT_StringRef &sticky_note_text=UT_StringRef()) const
Creates a sticky note in the graph.
Definition: UN_Graph.h:235
UN_GraphBase(GRAPH_DATA *graph_data)
Constructs a handle that references the given wire in the given graph.
Definition: UN_Graph.h:37
UT_StringHolder findValidUniqueNodeName(const UT_StringRef &name=UT_StringRef()) const
Definition: UN_Graph.h:72
bool isValidNodeName(const UT_StringRef &name) const
Definition: UN_Graph.h:65
bool operator!=(const UN_Graph &other) const
Comparison operators.
Definition: UN_Graph.h:211
UN_GraphBase()=default
Constructs a handle for an invalid wire.
bool operator==(const UN_Graph &other) const
Comparison operators.
Definition: UN_Graph.h:209
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
typename UN_Node::DescendantWireIterator AllWireIterator
An iterator and a range for all the connection wires in this graph.
Definition: UN_Graph.h:155
AllWireRange allWireRange() const
Definition: UN_Graph.h:160
#define UN_API
Definition: UN_API.h:11
typename UN_Node::DescendantNodeIterator AllNodeIterator
Definition: UN_Graph.h:125
typename UN_Node::ChildNodeIterator TopNodeIterator
Iterarates over the top-level nodes of the graph.
Definition: UN_Graph.h:107
OutGridT const XformOp bool bool
void setFromOptions(const UN_Options &graph_opts) const
Load and set the graph based on the given options.
Definition: UN_Graph.h:251
typename UN_Node::ChildWireIterator TopWireIterator
An iterator and a range for the top-level wires inside this graph.
Definition: UN_Graph.h:137
UN_ConstGraph(const UN_GraphData *graph_data)
Constructor that creates handle for the given graph data.
Definition: UN_Graph.h:271
UN_API void setFromOptions(UN_GraphData *graph_data, const UN_Options &graph_opts)
Load and set the graph based on the given options.
NODE findNode(const UT_StringRef &node_path) const
Definition: UN_Graph.h:89
UN_OptionsPtr asOptions() const
Definition: UN_Graph.h:168
UN_Graph(UN_GraphData *graph_data)
Constructor that creates handle for the given graph data.
Definition: UN_Graph.h:202
UT_Array< UT_StringHolder > nodeNames() const
Returns the names of the graph's top-level nodes.
Definition: UN_Graph.h:79
bool operator==(const UN_ConstGraph &other) const
Comparison operators.
Definition: UN_Graph.h:283
NODE rootNode() const
Definition: UN_Graph.h:58
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
bool operator!=(const UN_GraphBase &other) const
Comparison operators.
Definition: UN_Graph.h:177
GLuint const GLchar * name
Definition: glcorearb.h:786
UT_Array< NODE > topNodes() const
Returns the top level nodes directly inside this graph.
Definition: UN_Graph.h:117
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
UN_API UN_OptionsPtr asOptions(const UN_GraphData *graph_data)
TopNodeRange topNodeRange() const
Returns a range for iterating over graph's top-level nodes.
Definition: UN_Graph.h:111
TopWireRange topWireRange() const
Returns a range for iterating over graph's top-level connection wires.
Definition: UN_Graph.h:141
const UN_GraphData * constGraphData() const
Returns the owner container of the underlying graph data structures.
Definition: UN_Graph.h:45
A map of string to various well defined value types.
Definition: UT_Options.h:87
UT_Array< WIRE > topWires() const
Returns the wires connecting top level nodes directly inside this graph.
Definition: UN_Graph.h:147
void deleteStickyNote(const UT_StringRef &sticky_note_name_or_path) const
Removes and deletes the sticky note from this graph.
Definition: UN_Graph.h:245
void deleteNode(const UT_StringRef &node_name_or_path) const
Removes and deletes the node from this graph.
Definition: UN_Graph.h:228
bool isValid() const
Returns true if this is a valid graph; false otherwise.
Definition: UN_Graph.h:50
UN_ConstGraph(const UN_Graph &graph)
Constructs a const handle from a mutable graph handle.
Definition: UN_Graph.h:276
bool operator==(const UN_GraphBase &other) const
Comparison operators.
Definition: UN_Graph.h:175
AllNodeRange allNodeRange() const
Definition: UN_Graph.h:130
UN_NodeIDList::const_iterator ChildNodeIterator
An iterator and a range for iterating over child node IDs.
Definition: UN_NodeUtils.h:648
GRAPH_DATA * graphData() const
Returns the owner container of the underlying graph data structures.
Definition: UN_Graph.h:43
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542
UT_UniquePtr< UN_Options > UN_OptionsPtr
Definition: UN_Include.h:640