HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_GraphUtils.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_GraphUtils.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_GraphUtils_h__
13 #define __UN_GraphUtils_h__
14 
15 
16 #include "UN_API.h"
17 #include "UN_GraphData.h"
18 #include "UN_NodeUtils.h"
19 
20 
21 namespace UN_GraphUtils
22 {
23  /// Returns the root node of the graph's node parental hierarchy.
24  /// The root is a special node that holds graph's top-level nodes.
25  static inline UN_NodeID
26  rootNode( const UN_GraphData *graph_data )
27  {
28  return graph_data ? graph_data->rootNode() : UN_NodeID();
29  }
30 
31  /// Returns true if the graph accepts the given name as a legal node name.
32  /// Note, even though the name is legal, it still may not be unique.
33  static inline bool
34  isValidNodeName( const UN_GraphData *graph_data, const UT_StringRef &name )
35  {
36  return graph_data && graph_data->isValidNodeName( name );
37  }
38 
39  /// Based on the given name, returns a valid name that is not used
40  /// by any of the top level nodes.
41  static inline UT_StringHolder
42  findValidUniqueNodeName( const UN_GraphData *graph_data,
43  const UT_StringRef &name = UT_StringRef() )
44  {
46  graph_data, rootNode(graph_data), name );
47  }
48 
49  /// Returns an ID of a node at a given path, or an invalid ID if not found.
50  ///
51  /// @parm node_path - The node path identifying the node to find.
52  /// In particular, when the node path is just a name, this function
53  /// will look for a top level node by that name inside the graph.
54  /// When the path contains slashes '/', the function will look
55  /// for the nested node inside the subnets.
56  static inline UN_NodeID
57  findNode( const UN_GraphData *graph_data, const UT_StringRef &node_path )
58  {
59  return graph_data
60  ? graph_data->findNode( rootNode(graph_data), node_path )
61  : UN_NodeID();
62  }
63 
64  /// Creates a new node in the given graph.
65  static inline UN_NodeID
66  createNode( UN_GraphData *graph_data,
67  const UT_StringRef &node_name_or_path = UT_StringRef(),
68  const UT_StringRef &node_type_name = UT_StringRef(),
69  const UT_StringRef &node_category_name = UT_StringRef())
70  {
71  UT_String parent_path, child_name;
72  UT_StringWrap(node_name_or_path).splitPath( parent_path, child_name );
73 
74  UN_NodeID parent_id = parent_path
75  ? findNode( graph_data, parent_path )
76  : rootNode( graph_data );
77 
78  if( graph_data && parent_id )
79  return graph_data->createNode( parent_id,
80  child_name, node_type_name, node_category_name );
81 
82  return UN_NodeID();
83  }
84 
85  /// Removes and deletes the node from this graph.
86  static inline void
87  deleteNode( UN_GraphData *graph_data, const UT_StringRef &node_path )
88  {
89  UN_NodeID node_id = findNode( graph_data, node_path );
90  if( graph_data && node_id )
91  graph_data->deleteNode( node_id );
92  }
93 
94  /// Returns the node IDs of the graph's top level nodes.
95  static inline const UN_NodeIDList &
96  topNodes( const UN_GraphData *graph_data )
97  {
98  return UN_NodeUtils::childNodes( graph_data, rootNode(graph_data) );
99  }
100 
101  /// An iterator and a range for iterating over the IDs of graph's top nodes.
104 
105  /// Returns a range for traversing the top nodes of the given graph.
106  /// NOTE, the iterators hold a reference to the underlying node array, and
107  /// therefore child nodes can't be added/removed during the iteration.
108  /// For adding/removing nodes use topNodes() function and make a copy.
109  static inline TopNodeRange
110  topNodeRange( const UN_GraphData *graph_data )
111  {
112  return UN_NodeUtils::childNodeRange( graph_data, rootNode(graph_data) );
113  }
114 
115  /// Returns the names of the graph's top-level nodes.
116  static inline UT_Array< UT_StringHolder >
117  nodeNames( const UN_GraphData *graph_data )
118  {
119  return UN_NodeUtils::childNodeNames( graph_data, rootNode(graph_data) );
120  }
121 
122  /// A range for traversing the all the nodes in the given graph.
125 
126  /// Returns a range for traversing the descendant nodes (children,
127  /// grandchildren, etc) of a given ancestor node.
128  static inline AllNodeRange
129  allNodeRange( const UN_GraphData *graph_data )
130  {
131  return UN_NodeUtils::descendantNodeRange(
132  graph_data, rootNode(graph_data) );
133  }
134 
135  /// Returns the IDs of the top level wires directly inside the given graph.
136  static inline const UN_WireIDList &
137  topWires( const UN_GraphData *graph_data )
138  {
139  return UN_NodeUtils::childWires( graph_data, rootNode(graph_data) );
140  }
141 
142  /// A range for traversing the top level wires contained directly
143  /// inside the graph.
146 
147  /// Returns a range for traversing the wires contained directly inside
148  /// the given node node.
149  static inline TopWireRange
150  topWireRange( const UN_GraphData *graph_data )
151  {
152  return UN_NodeUtils::childWireRange( graph_data, rootNode(graph_data) );
153  }
154 
155  /// An range for traversing the wires between all nodes (top level
156  /// or deeply nested) inside the graph.
159 
160  /// Returns a range for traversing the wires between all nodes (top level
161  /// or deeply nested) inside the graph.
162  static inline AllWireRange
163  allWireRange( const UN_GraphData *graph_data )
164  {
165  return UN_NodeUtils::descendantWireRange(
166  graph_data, rootNode(graph_data));
167  }
168 
169  /// Creates a new sticky note in the given graph.
170  static inline UN_StickyNoteID
171  createStickyNote( UN_GraphData *graph_data,
172  const UT_StringRef &sticky_note_path = UT_StringRef(),
173  const UT_StringRef &sticky_note_text = UT_StringRef())
174  {
175  UT_String parent_path, child_name;
176  UT_StringWrap(sticky_note_path).splitPath( parent_path, child_name );
177 
178  UN_NodeID parent_id = parent_path
179  ? findNode( graph_data, parent_path )
180  : rootNode( graph_data );
181 
182  if( graph_data && parent_id )
183  return graph_data->createStickyNote( parent_id,
184  sticky_note_path, sticky_note_text );
185 
186  return UN_StickyNoteID();
187  }
188 
189  /// Returns an ID of a sticky note at a given path,
190  /// or an invalid ID if not found.
191  ///
192  /// @parm sticky_note_path - The path or name identifying the sticky note
193  /// to find.
194  /// In particular, when the path is just a name, this function
195  /// will look for a top level sticky note by that name inside the graph.
196  /// When the path contains slashes '/', the function will look
197  /// for the nested sticky note inside the subnets.
198  static inline UN_StickyNoteID
199  findStickyNote( const UN_GraphData *graph_data,
200  const UT_StringRef &sticky_note_path )
201  {
202  return graph_data
203  ? graph_data->findStickyNote(rootNode(graph_data), sticky_note_path)
204  : UN_StickyNoteID();
205  }
206 
207  /// Removes and deletes the sticky note from this graph.
208  static inline void
209  deleteStickyNote( UN_GraphData *graph_data,
210  const UT_StringRef &sticky_note_path )
211  {
212  auto note_id = findStickyNote( graph_data, sticky_note_path );
213  if( graph_data && note_id )
214  graph_data->deleteStickyNote( note_id );
215  }
216 
217  /// Returns the options object representing this graph.
218  /// This can be then used for serialization to JSON, etc.
220  asOptions( const UN_GraphData *graph_data );
221 
222  /// Load and set the graph based on the given options.
223  UN_API void
224  setFromOptions( UN_GraphData *graph_data, const UN_Options &graph_opts );
225 }
226 
227 #endif
228 
UN_StickyNoteID createStickyNote(UN_NodeID parent_node, const UT_StringRef &name=UT_StringRef(), const UT_StringRef &text=UT_StringRef())
Creates and adds a new sticky note to the graph.
UN_NodeID rootNode() const
Definition: UN_GraphData.h:151
UN_WireIDList::const_iterator ChildWireIterator
A range for traversing the wires contained directly inside a node.
Definition: UN_NodeUtils.h:845
OIIO_UTIL_API std::string parent_path(string_view filepath) noexcept
bool isValidNodeName(const UT_StringRef &name) const
Returns true if the given name can be used for naming a node.
UN_StickyNoteID findStickyNote(UN_NodeID current_node, const UT_StringRef &sticky_note_path) const
#define UN_API
Definition: UN_API.h:11
UN_API UT_Array< UT_StringHolder > childNodeNames(const UN_GraphData *graph_data, UN_NodeID node_id)
Returns the names of the children nodes of the given parent node.
UN_NodeID createNode(UN_NodeID parent_node, const UT_StringRef &name=UT_StringRef(), const UT_StringRef &type=UT_StringRef(), const UT_StringRef &category=UT_StringRef(), const UT_StringRef &signature=UT_StringRef())
Creates a new node by the given name with the given parent.
UN_API void setFromOptions(UN_GraphData *graph_data, const UN_Options &graph_opts)
Load and set the graph based on the given options.
GLuint const GLchar * name
Definition: glcorearb.h:786
UN_API UN_OptionsPtr asOptions(const UN_GraphData *graph_data)
UN_NodeID findNode(UN_NodeID current_node, const UT_StringRef &node_path) const
void deleteStickyNote(UN_StickyNoteID sticky_note)
Deletes the node given its index.
A map of string to various well defined value types.
Definition: UT_Options.h:87
UN_NodeUtils::ChildWireIterator TopWireIterator
UN_API UT_StringHolder findValidUniqueChildNodeName(const UN_GraphData *graph_data, UN_NodeID node_id, const UT_StringRef &child_name=UT_StringRef())
UN_NodeIDList::const_iterator ChildNodeIterator
An iterator and a range for iterating over child node IDs.
Definition: UN_NodeUtils.h:648
void deleteNode(UN_NodeID node)
Deletes the node given its index.
UN_NodeUtils::ChildNodeIterator TopNodeIterator
An iterator and a range for iterating over the IDs of graph's top nodes.
void splitPath(UT_String &dir_name, UT_String &file_name) const
An iterator for traversing the wires deep inside the given node.
Definition: UN_NodeUtils.h:861
UT_UniquePtr< UN_Options > UN_OptionsPtr
Definition: UN_Include.h:640