HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_Port.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_Port.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_Port_h__
13 #define __UN_Port_h__
14 
15 #include "UN_API.h"
16 #include "UN_Iterator.h"
17 #include "UN_Handle.h"
18 #include "UN_PortUtils.h"
19 
20 class UN_ConstGraph;
21 class UN_ConstNode;
22 class UN_ConstPort;
23 class UN_ConstWire;
24 class UN_Graph;
25 class UN_Node;
26 class UN_Port;
27 class UN_Wire;
28 template<typename> class UT_Array;
29 
30 // ============================================================================
31 // Port utility functions that take UN_Port and UN_ConstPort as parameters.
32 namespace UN_PortUtils
33 {
34  /// Returns true if the given destination port is connected
35  /// to the given source port.
36  UN_API bool
37  isConnectedToSrc( const UN_GraphData *dst_graph_data, UN_PortID dst_port_id,
38  const UN_ConstPort &src_port );
39 
40  /// Returns true if the given source port is connected
41  /// to the given destination port.
42  UN_API bool
43  isConnectedToDst( const UN_GraphData *src_graph_data, UN_PortID src_port_id,
44  const UN_ConstPort &dst_port );
45 
46 
47  /// Returns true if the given port is connected to the given other port
48  /// either as a source or a destination.
49  UN_API bool
50  isConnected( const UN_GraphData *graph_data, UN_PortID port_id,
51  const UN_ConstPort &src_or_dst_port );
52 }
53 
54 // ============================================================================
55 /// Base class template for the UN port handle.
56 /// Its main purpose is to factor common code for const and non-const handles.
57 template <typename BASE_CLASS, // class to derive from
58  typename GRAPH_DATA, // UN_GraphData or const UN_GraphData
59  typename PORT, // Port handle class (UN_Port or UN_ConstPort)
60  typename WIRE> // Wire handle class (UN_Wire or UN_ConstWire)
61 class UN_PortBase : public BASE_CLASS
62 {
63 protected:
64  /// Constructs a handle for an invalid port.
65  UN_PortBase() = default;
66 
67  /// Constructs a handle that references the given port in the given graph.
68  UN_PortBase( GRAPH_DATA *graph_data, UN_PortID port_id )
69  : BASE_CLASS( graph_data, port_id )
70  {}
71 
72 public:
73  /// @{ Returns the owner of the underlying graph data structures.
74  GRAPH_DATA * graphData() const
75  { return BASE_CLASS::graphData(); }
76  const UN_GraphData * constGraphData() const
77  { return BASE_CLASS::constGraphData(); }
78  /// @}
79 
80  /// Returns the unique ID of the port this handle refers to.
81  UN_PortID portID() const
82  { return UN_PortID( BASE_CLASS::dataID() ); }
83 
84  /// @{ Returns true if this port is valid within the graph; false otherwise.
85  bool isValid() const
86  {
87  return UN_PortUtils::isValid(
88  graphData(), portID());
89  }
90 
91  explicit operator bool() const
92  { return isValid(); }
93  /// @}
94 
95  /// Returns the kind of a port. Ie, an input or an output.
96  UN_PortKind kind() const
97  {
98  return UN_PortUtils::portKind( graphData(), portID() ) ;
99  }
100 
101  // Returns true if this is an input port.
102  bool isInput() const
103  {
104  return kind() == UN_PortKind::Input;
105  }
106 
107  // Returns true if this is an output port.
108  bool isOutput() const
109  {
110  return kind() == UN_PortKind::Output;
111  }
112 
113  /// Returns the port name that identifies it on the node.
114  const UT_StringHolder & name() const
115  {
116  return UN_PortUtils::name( graphData(), portID() );
117  }
118 
119  /// Returns the port's type that determines the connection compatibility.
120  const UT_StringHolder & typeName() const
121  {
122  return UN_PortUtils::typeName( graphData(), portID() );
123  }
124 
125  /// Returns the port's label.
126  const UT_StringHolder & label() const
127  {
128  return UN_PortUtils::label( graphData(), portID() );
129  }
130 
131  /// Returns the port's tags.
132  const UT_StringArray & tags() const
133  {
134  return UN_PortUtils::tags( graphData(), portID() );
135  }
136 
137 
138  /// Returns the number of source ports connected to the given port.
140  {
141  return UN_PortUtils::srcWireCount( graphData(), portID() );
142  }
143 
144  /// Returns the number of destination ports connected to the given port.
146  {
147  return UN_PortUtils::dstWireCount( graphData(), portID() );
148  }
149 
150  /// Returns the number of ports connected to the given port,
151  /// either as a source or a destination port.
153  {
154  return UN_PortUtils::wireCount( graphData(), portID() );
155  }
156 
157  /// Returns true if this port is connected to any source port.
158  bool isConnectedToSrc() const
159  {
161  }
162 
163  /// Returns true if this port is connected to the given source port.
164  bool isConnectedToSrc( const UN_ConstPort &src_port ) const
165  {
166  return UN_PortUtils::isConnectedToSrc( graphData(), portID(), src_port);
167  }
168 
169  /// Returns true if this port is connected to any destination port.
170  bool isConnectedToDst() const
171  {
173  }
174 
175  /// Returns true if this port is connected to the given destination port.
176  bool isConnectedToDst( const UN_ConstPort &dst_port ) const
177  {
178  return UN_PortUtils::isConnectedToDst( graphData(), portID(), dst_port);
179  }
180 
181  /// Returns true if this port is connected to any other port.
182  bool isConnected() const
183  {
185  }
186 
187  /// Returns true if this port is connected to the given port
188  /// either as a source or a destination.
189  bool isConnected( const UN_ConstPort &port ) const
190  {
191  return UN_PortUtils::isConnected( graphData(), portID(), port );
192  }
193 
194  /// An iterator and a range for iterating over port's
195  /// connected source wires.
197  WIRE, GRAPH_DATA, UN_PortUtils::WireIterator >;
199 
200  /// Returns a range for iterating over this port's source wires.
201  /// NOTE, the iterators hold a reference to the underlying wire array,
202  /// and therefore wires can't be added/removed during the iteration.
203  /// For adding/removing wires, use srcWires() function.
205  {
206  auto wire_range = UN_PortUtils::srcWireRange( graphData(), portID() );
207  return SrcWireRange(
208  SrcWireIterator( graphData(), wire_range.begin() ),
209  SrcWireIterator( graphData(), wire_range.end() ));
210  }
211 
212 
213  /// An iterator and a range for iterating over the port's
214  /// connected destination wires.
216  WIRE, GRAPH_DATA, UN_PortUtils::WireIterator >;
218 
219  /// Returns a range for iterating over this port's destination wires.
220  /// NOTE, the iterators hold a reference to the underlying wire array,
221  /// and therefore wires can't be added/removed during the iteration.
222  /// For adding/removing wires, use dstWires() function.
224  {
225  auto wire_range = UN_PortUtils::dstWireRange( graphData(), portID() );
226  return DstWireRange(
227  DstWireIterator( graphData(), wire_range.begin() ),
228  DstWireIterator( graphData(), wire_range.end() ));
229  }
230 
231 
232  /// Returns the wires connecting this port to its sources.
233  /// Ie, connection wires from which this port takes (receives) data.
235  {
236  return UNhandleArray<WIRE>( graphData(),
237  UN_PortUtils::srcWires( graphData(), portID() ));
238  }
239 
240  /// Returns the wires connecting this port to its destinations.
241  /// Ie, connection wires to which this port provides (sends) data.
243  {
244  return UNhandleArray<WIRE>( graphData(),
245  UN_PortUtils::dstWires( graphData(), portID() ));
246  }
247 
248 
249  /// And iterator and a range for iterating over connected source ports.
250  using SrcPortIterator = UN_HandleIterator< PORT, GRAPH_DATA,
251  UN_PortUtils::PortIterator</*src=*/ true>>;
253 
254  /// Returns a range for iterating over this port's source ports.
255  /// NOTE, the iterators hold a reference to the underlying wire array,
256  /// and therefore ports can't be added/removed during the iteration.
257  /// For adding/removing ports, use srcPorts() function.
259  {
260  auto port_range = UN_PortUtils::srcPortRange( graphData(), portID() );
261 
262  return SrcPortRange(
263  SrcPortIterator( graphData(), port_range.begin() ),
264  SrcPortIterator( graphData(), port_range.end() ));
265  }
266 
267 
268  /// And iterator and a range for iterating over connected destination ports.
269  using DstPortIterator = UN_HandleIterator< PORT, GRAPH_DATA,
270  UN_PortUtils::PortIterator</*src=*/ false>>;
272 
273  /// Returns a range for iterating over this port's destination ports.
274  /// NOTE, the iterators hold a reference to the underlying wire array,
275  /// and therefore ports can't be added/removed during the iteration.
276  /// For adding/removing ports, use dstPorts() function.
278  {
279  auto port_range = UN_PortUtils::dstPortRange( graphData(), portID() );
280 
281  return DstPortRange(
282  DstPortIterator( graphData(), port_range.begin() ),
283  DstPortIterator( graphData(), port_range.end() ));
284  }
285 
286 
287  /// Returns the first source port connected to this port,
288  /// or an invalid port, if no source port is connected.
289  PORT srcPort() const
290  {
291  return PORT( graphData(),
293  }
294 
295  /// Returns the first destination port connected to this port,
296  /// or an invalid port, if no destination port is connected.
297  PORT dstPort() const
298  {
299  return PORT( graphData(),
301  }
302 
303 
304  /// Returns the source ports of this port.
305  /// Ie, ports from which this port takes (receives) data.
307  {
308  return connectedPorts</*src=*/ true>( graphData(),
309  UN_PortUtils::srcWires( graphData(), portID() ));
310  }
311 
312  /// Returns the destination ports of this port.
313  /// Ie, ports to which this port provides (sends) data.
315  {
316  return connectedPorts</*src=*/ false>( graphData(),
317  UN_PortUtils::dstWires( graphData(), portID() ));
318  }
319 
320 private:
321  /// A helper function to build an array of port handles from wire ID list.
322  template <bool IS_SOURCE>
323  static inline UT_Array< PORT >
324  connectedPorts( GRAPH_DATA *graph_data, const UN_WireIDList &wire_ids )
325  {
326  UT_Array<PORT> result( wire_ids.size() );
327  for( auto &&wire_id: wire_ids )
328  {
329  if constexpr (IS_SOURCE)
330  result.emplace_back( graph_data, graph_data->srcPort(wire_id));
331  else
332  result.emplace_back( graph_data, graph_data->dstPort(wire_id));
333  }
334 
335  return result;
336  }
337 };
338 
339 
340 // ============================================================================
341 /// A handle that references a mutable port in a graph.
342 /// It abstracts the APIs that operate on this port's data,
343 /// which is stored inside graph's data containers.
344 /// It is a non-const (read-write) handle, so allows both queries
345 /// and mutations of the port's data.
348 {
349 public:
350  /// Constructs a handle for an invalid node.
351  UN_Port() = default;
352 
353  /// Constructs a handle referencing the given node in the given graph.
354  UN_Port( UN_GraphData *graph_data, UN_PortID node_id )
355  : UN_PortBase( graph_data, node_id )
356  {}
357 
358  /// @{ Comparison operators.
359  // Allows comparing port handles, but guards against
360  // comparing ports to wires, for example.
361  bool operator==( const UN_Port &other ) const
362  { return UN_ConstHandle::operator==( other ); }
363  bool operator!=( const UN_Port &other ) const
364  { return UN_ConstHandle::operator!=( other ); }
365  /// @}
366 
367  /// Returns the graph whithin which this port exists.
368  UN_Graph graph() const;
369 
370  /// Returns the node to which this port belongs.
371  UN_Node node() const;
372 
373 
374  /// Sets the port name that identifies it on the node.
375  void setName( const UT_StringRef &name ) const
376  {
377  UN_PortUtils::setName( graphData(), portID(), name );
378  }
379 
380  /// Sets the port label.
381  void setLabel( const UT_StringRef &label ) const
382  {
383  UN_PortUtils::setLabel( graphData(), portID(), label );
384  }
385 
386  /// Sets the tags for the port (copy-assignment).
387  void setTags( const UT_StringArray &tags ) const
388  {
389  UN_PortUtils::setTags( graphData(), portID(), tags );
390  }
391 
392  /// Sets the tags for the port (move-assignment).
393  void setTags( UT_StringArray &&tags ) const
394  {
395  UN_PortUtils::setTags( graphData(), portID(), std::move(tags) );
396  }
397 
398  /// Returns the tags moved out of the port.
399  /// This is useful in combination with rvalue ref version of setTags()
400  /// to optimize the edits to the tag list.
402  {
403  return UN_PortUtils::stealTags( graphData(), portID() );
404  }
405 
406  /// Edits port's tags array in-place.
407  /// Intended usage:
408  /// n.updateTags([&value](auto &t) { t.set("key", value) });
409  template <typename OP>
410  void updateTags( const OP &op ) const
411  {
412  UN_PortUtils::updateTags( graphData(), portID(), op );
413  }
414 
415  /// Connects this port to a source port from which it takes (receives)
416  /// data or a value.
417  /// Returns the newly created connection wire (may be invalid, if error).
418  UN_Wire connectSrcPort( const UN_Port &src_port ) const;
419 
420  /// Connects this port to a destination port to which it provides (sends)
421  /// data or a value.
422  /// Returns the newly created connection wire (may be invalid, if error).
423  UN_Wire connectDstPort( const UN_Port &dst_port ) const;
424 
425  /// Deletes this port from the graph.
426  void destroy() const
427  {
428  UN_PortUtils::deletePort( graphData(), portID() );
429  }
430 };
431 
432 
433 // ============================================================================
434 /// A handle that references a const port in a graph.
435 /// It abstracts the APIs that operate on this port's data stored inside
436 /// graph's data containers.
437 /// It is a constant handle, so allows only queries of the port's data,
438 /// and does not allow setting any new values for.
439 class UN_API UN_ConstPort : public UN_PortBase< UN_ConstHandle,
440  const UN_GraphData, UN_ConstPort, UN_ConstWire >
441 {
442 public:
443  /// Constructs a handle for an invalid port.
444  UN_ConstPort() = default;
445 
446  /// Constructs a handle referencing the given port in the given graph.
447  UN_ConstPort( const UN_GraphData *graph_data, UN_PortID port_id )
448  : UN_PortBase( graph_data, port_id )
449  {}
450 
451  /// Constructs a const handle from a mutable port handle.
452  UN_ConstPort( const UN_Port &port )
453  : UN_ConstPort( port.constGraphData(), port.portID() )
454  {}
455 
456  /// @{ Comparison operators.
457  // Allows comparing port handles, but guards against
458  // comparing ports to wires, for example.
459  bool operator==( const UN_ConstPort &other ) const
460  { return UN_ConstHandle::operator==( other ); }
461  bool operator!=( const UN_ConstPort &other ) const
462  { return UN_ConstHandle::operator!=( other ); }
463  /// @}
464 
465  /// Returns the graph within which this wire exists.
466  UN_ConstGraph graph() const;
467 
468  /// Returns the node to which this port belongs.
469  UN_ConstNode node() const;
470 };
471 
472 // ============================================================================
473 // Free functions to compare a non-const port handle to a const port handle.
474 // Note, the const to non-const comparison is covered by implicit conversion
475 // of non-const handle to const handle, and using const to const handle comp.
476 static inline bool
477 operator==( const UN_Port &a, const UN_ConstPort &b )
478 {
479  return b == UN_ConstPort(a);
480 }
481 
482 static inline bool
483 operator!=( const UN_Port &a, const UN_ConstPort &b )
484 {
485  return !(b == a);
486 }
487 
488 // ============================================================================
489 
490 #endif
491 
UT_IteratorRange< DstPortIterator > DstPortRange
Definition: UN_Port.h:271
void setName(const UT_StringRef &name) const
Sets the port name that identifies it on the node.
Definition: UN_Port.h:375
UN_API WireRange dstWireRange(const UN_GraphData *graph_data, UN_PortID src_port_id)
UN_PortBase()=default
Constructs a handle for an invalid port.
bool operator!=(const UN_ConstPort &other) const
Comparison operators.
Definition: UN_Port.h:461
bool isConnectedToDst(const UN_ConstPort &dst_port) const
Returns true if this port is connected to the given destination port.
Definition: UN_Port.h:176
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
DstWireRange dstWireRange() const
Definition: UN_Port.h:223
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
UN_API WireRange srcWireRange(const UN_GraphData *graph_data, UN_PortID dst_port_id)
UN_PortID portID() const
Returns the unique ID of the port this handle refers to.
Definition: UN_Port.h:81
UN_HandleIterator< PORT, GRAPH_DATA, UN_PortUtils::PortIterator< false >> DstPortIterator
And iterator and a range for iterating over connected destination ports.
Definition: UN_Port.h:270
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
bool isInput() const
Definition: UN_Port.h:102
UT_Array< PORT > srcPorts() const
Definition: UN_Port.h:306
GRAPH_DATA * graphData() const
Returns the owner of the underlying graph data structures.
Definition: UN_Port.h:74
const UT_StringHolder & label() const
Returns the port's label.
Definition: UN_Port.h:126
**But if you need a result
Definition: thread.h:622
#define UN_API
Definition: UN_API.h:11
UN_DataSize srcWireCount() const
Returns the number of source ports connected to the given port.
Definition: UN_Port.h:139
SrcWireRange srcWireRange() const
Definition: UN_Port.h:204
OutGridT const XformOp bool bool
UN_HandleIterator< WIRE, GRAPH_DATA, UN_PortUtils::WireIterator > DstWireIterator
Definition: UN_Port.h:216
UT_IteratorRange< SrcWireIterator > SrcWireRange
Definition: UN_Port.h:198
UN_WireIDList::const_iterator WireIterator
Definition: UN_PortUtils.h:182
UN_API SrcPortRange srcPortRange(const UN_GraphData *graph_data, UN_PortID dst_port_id)
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
UT_StringArray stealTags() const
Definition: UN_Port.h:401
const UN_GraphData * constGraphData() const
Returns the owner of the underlying graph data structures.
Definition: UN_Port.h:76
bool isValid() const
Returns true if this port is valid within the graph; false otherwise.
Definition: UN_Port.h:85
UT_IteratorRange< DstWireIterator > DstWireRange
Definition: UN_Port.h:217
void setTags(UT_StringArray &&tags) const
Sets the tags for the port (move-assignment).
Definition: UN_Port.h:393
void destroy() const
Deletes this port from the graph.
Definition: UN_Port.h:426
UT_Array< WIRE > srcWires() const
Definition: UN_Port.h:234
SrcPortRange srcPortRange() const
Definition: UN_Port.h:258
UN_ConstPort(const UN_GraphData *graph_data, UN_PortID port_id)
Constructs a handle referencing the given port in the given graph.
Definition: UN_Port.h:447
UNI_PortKind
Specifies the port kind, ie, wheter it is an input or an output port.
Definition: UNI_Include.h:21
bool isConnectedToDst() const
Returns true if this port is connected to any destination port.
Definition: UN_Port.h:170
UN_API bool isConnectedToSrc(const UN_GraphData *dst_graph_data, UN_PortID dst_port_id, const UN_ConstPort &src_port)
UN_ConstPort(const UN_Port &port)
Constructs a const handle from a mutable port handle.
Definition: UN_Port.h:452
GLuint const GLchar * name
Definition: glcorearb.h:786
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
bool isOutput() const
Definition: UN_Port.h:108
UN_API bool isConnectedToDst(const UN_GraphData *src_graph_data, UN_PortID src_port_id, const UN_ConstPort &dst_port)
DstPortRange dstPortRange() const
Definition: UN_Port.h:277
UN_HandleIterator< PORT, GRAPH_DATA, UN_PortUtils::PortIterator< true >> SrcPortIterator
And iterator and a range for iterating over connected source ports.
Definition: UN_Port.h:251
UN_API UN_PortID srcPort(const UN_GraphData *graph_data, UN_PortID dst_port_id)
PORT srcPort() const
Definition: UN_Port.h:289
UT_Array< PORT > dstPorts() const
Definition: UN_Port.h:314
UT_Array< WIRE > dstWires() const
Definition: UN_Port.h:242
bool isConnected(const UN_ConstPort &port) const
Definition: UN_Port.h:189
bool operator!=(const UN_ConstHandle &other) const
Definition: UN_Handle.h:68
bool operator==(const UN_ConstHandle &other) const
Definition: UN_Handle.h:59
UN_PortKind kind() const
Returns the kind of a port. Ie, an input or an output.
Definition: UN_Port.h:96
UN_API DstPortRange dstPortRange(const UN_GraphData *graph_data, UN_PortID src_port_id)
const UT_StringArray & tags() const
Returns the port's tags.
Definition: UN_Port.h:132
UN_HandleIterator< WIRE, GRAPH_DATA, UN_PortUtils::WireIterator > SrcWireIterator
Definition: UN_Port.h:197
void setTags(const UT_StringArray &tags) const
Sets the tags for the port (copy-assignment).
Definition: UN_Port.h:387
UN_Port(UN_GraphData *graph_data, UN_PortID node_id)
Constructs a handle referencing the given node in the given graph.
Definition: UN_Port.h:354
const UT_StringHolder & name() const
Returns the port name that identifies it on the node.
Definition: UN_Port.h:114
UN_DataSize dstWireCount() const
Returns the number of destination ports connected to the given port.
Definition: UN_Port.h:145
bool isConnectedToSrc() const
Returns true if this port is connected to any source port.
Definition: UN_Port.h:158
UN_DataSize wireCount() const
Definition: UN_Port.h:152
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
UN_API UN_PortID dstPort(const UN_GraphData *graph_data, UN_PortID src_port_id)
PORT dstPort() const
Definition: UN_Port.h:297
UN_API bool isConnected(const UN_GraphData *graph_data, UN_PortID port_id, const UN_ConstPort &src_or_dst_port)
bool isConnected() const
Returns true if this port is connected to any other port.
Definition: UN_Port.h:182
UT_IteratorRange< SrcPortIterator > SrcPortRange
Definition: UN_Port.h:252
const UT_StringHolder & typeName() const
Returns the port's type that determines the connection compatibility.
Definition: UN_Port.h:120
void updateTags(const OP &op) const
Definition: UN_Port.h:410
bool operator==(const UN_Port &other) const
Comparison operators.
Definition: UN_Port.h:361
bool isConnectedToSrc(const UN_ConstPort &src_port) const
Returns true if this port is connected to the given source port.
Definition: UN_Port.h:164
bool operator!=(const UN_Port &other) const
Comparison operators.
Definition: UN_Port.h:363
UN_PortBase(GRAPH_DATA *graph_data, UN_PortID port_id)
Constructs a handle that references the given port in the given graph.
Definition: UN_Port.h:68
void setLabel(const UT_StringRef &label) const
Sets the port label.
Definition: UN_Port.h:381
bool operator==(const UN_ConstPort &other) const
Comparison operators.
Definition: UN_Port.h:459