HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_Handle.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_Handle.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_Handle_h__
13 #define __UN_Handle_h__
14 
15 
16 #include "UN_API.h"
17 #include "UN_Include.h"
18 
19 class UN_GraphData;
20 
21 // ============================================================================
22 /// A handle that references a graph entity (eg, node, port, wire, etc).
23 /// It abstracts the APIs that operate on this entity's data stored
24 /// inside graph's data containers.
25 /// It is a constant handle, so allows only queries of the entity's data,
26 /// but does not allow setting new values for.
28 {
29 public:
30  /// Constructs a reference handle for an invalid entity in the graph.
32  : UN_ConstHandle( nullptr, UN_DataID() )
33  {
35  }
36 
37  /// Constructs reference handle to the data for a given entity in the graph.
38  UN_ConstHandle( const UN_GraphData *graph_data, UN_DataID data_id )
39  : myGraphData( graph_data )
40  , myDataID( data_id )
41  {}
42 
43  /// @{ Returns the underlying graph data object.
44  const UN_GraphData *graphData() const
45  { return myGraphData; }
47  { return myGraphData; }
48  /// @}
49 
50  /// The unique number identifying this data object during the lifespan
51  /// of the owner graph. Used for referring to this object in the graph.
52  UN_DataID dataID() const
53  { return myDataID; }
54 
55 protected:
56  /// @{ Comparison operators, for use by the derived classes.
57  /// Note, protected to prevent comparing against incompatible handles,
58  /// for example, UN_Port against UN_Wire.
59  bool operator==( const UN_ConstHandle &other ) const
60  {
61  return // both handles are equal to each other
62  (myGraphData == other.myGraphData &&
63  myDataID == other.myDataID)
64  || // or both handles are invalid
65  (!isNumericallyValid() && !other.isNumericallyValid());
66  }
67 
68  bool operator!=( const UN_ConstHandle &other ) const
69  {
70  return !( *this == other );
71  }
72  /// @}
73 
74  /// Returns true if the handle can refer to a valid data.
75  /// Note, subclasses have a stricter notion of a valid handle:
76  /// not only it needs to be numerically valid, but
77  // /it also must have an entry in the data ID map (ie not deleted).
78  bool isNumericallyValid() const
79  { return myGraphData && myDataID; }
80 
81 private:
82  /// The graph that owns, manages, and operates on data object.
83  const UN_GraphData *myGraphData = nullptr;
84 
85  /// The unique identification number of a data object during the lifespan
86  /// of the graph above. Used for referring to the data object in a graph.
87  UN_DataID myDataID;
88 };
89 
90 
91 // ============================================================================
92 /// A handle that references a graph entity (eg, node, port, wire, etc).
93 /// It abstracts the APIs that operate on this entity's data stored
94 /// inside graph's data containers.
95 /// It is a mutable handle, so allows setting the entity's data.
96 class UN_Handle : public UN_ConstHandle
97 {
98 public:
99  /// Constructs a reference handle for an invalid entity in the graph.
101  : UN_Handle( nullptr, UN_DataID() )
102  {}
103 
104  /// Constructs reference handle to the data for a given entity in the graph.
105  UN_Handle( UN_GraphData *graph_data, UN_DataID data_id )
106  : UN_ConstHandle( graph_data, data_id )
107  {}
108 
109  /// Returns the graph that owns, manages, and operates on the data object.
111  {
112  // Note, casting away the const is fine, since the base class is
113  // always constructed with a *mutable* graph data pointer.
114  return const_cast <UN_GraphData*>( constGraphData());
115  }
116 
117  /// Returns the graph that owns, manages, and operates on the data object.
119  {
121  }
122 };
123 
124 
125 #endif
126 
const UN_GraphData * constGraphData() const
Returns the underlying graph data object.
Definition: UN_Handle.h:46
bool isNumericallyValid() const
Definition: UN_Handle.h:78
const UN_GraphData * constGraphData() const
Returns the graph that owns, manages, and operates on the data object.
Definition: UN_Handle.h:118
UN_ConstHandle()
Constructs a reference handle for an invalid entity in the graph.
Definition: UN_Handle.h:31
UN_ConstHandle(const UN_GraphData *graph_data, UN_DataID data_id)
Constructs reference handle to the data for a given entity in the graph.
Definition: UN_Handle.h:38
UN_Handle(UN_GraphData *graph_data, UN_DataID data_id)
Constructs reference handle to the data for a given entity in the graph.
Definition: UN_Handle.h:105
UN_GraphData * graphData() const
Returns the graph that owns, manages, and operates on the data object.
Definition: UN_Handle.h:110
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_DataID dataID() const
Definition: UN_Handle.h:52
const UN_GraphData * graphData() const
Returns the underlying graph data object.
Definition: UN_Handle.h:44
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
UN_Handle()
Constructs a reference handle for an invalid entity in the graph.
Definition: UN_Handle.h:100
Definition: UNI_ID.h:25