HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UNI_ID.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: UNI_ID.h ( UNI Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UNI_ID_h__
13 #define __UNI_ID_h__
14 
15 #include <UT/UT_Format.h>
16 #include <UT/UT_Array.h>
17 #include <SYS/SYS_StaticAssert.h>
18 #include <SYS/SYS_Hash.h> // For SYShash() use in hash_value().
19 #include <SYS/SYS_Types.h> // for exint
20 
21 
22 // ============================================================================
23 /// An ID for referencing elements within the UNI library, such as graphs,
24 /// but also local graph's elements, like nodes, wires, etc.
25 class UNI_ID
26 {
27 public:
28  /// Numerical representation of the ID.
29  using NumericType = exint;
30  static constexpr NumericType INVALID_ID = -1;
31 
32  /// Explicit constructor.
33  constexpr explicit UNI_ID( NumericType id_number = INVALID_ID )
34  : myNumericValue( id_number ) {}
35 
36  /// Resets the ID to an invalid value.
37  constexpr void clear()
38  { myNumericValue = INVALID_ID; }
39 
40  /// @{ Returns true if this ID is valid; false otherwise.
41  constexpr bool isValid() const
42  { return myNumericValue != INVALID_ID; }
43  constexpr explicit operator bool() const
44  { return isValid(); }
45  /// @}
46 
47  /// @{ Comparison operators.
48  constexpr bool operator==( const UNI_ID &other ) const
49  { return myNumericValue == other.myNumericValue; }
50  constexpr bool operator!=( const UNI_ID &other ) const
51  { return myNumericValue != other.myNumericValue; }
52  /// @}
53 
54  /// @{ Remaining comparison operators, used for sorting, etc.
55  // Ideally, we should not have these, but since an ID ultimately has
56  // a numerical representation, we provide these for convenience.
57  constexpr bool operator<( const UNI_ID &other ) const
58  { return myNumericValue < other.myNumericValue; }
59  constexpr bool operator<=( const UNI_ID &other ) const
60  { return myNumericValue <= other.myNumericValue; }
61  constexpr bool operator>( const UNI_ID &other ) const
62  { return myNumericValue > other.myNumericValue; }
63  constexpr bool operator>=( const UNI_ID &other ) const
64  { return myNumericValue >= other.myNumericValue; }
65  /// @}
66 
67  /// Returns the numeric value of this ID.
68  constexpr NumericType numericValue() const
69  {
70  return myNumericValue;
71  }
72 
73  /// Returns an exint value of this ID.
74  constexpr exint exintValue() const
75  {
76  SYS_STATIC_ASSERT( sizeof(NumericType) == sizeof(exint) );
77  return static_cast<exint>( myNumericValue );
78  }
79 
80  /// Cast to `exint` for use in template functions that cast templated types,
81  /// but for now make it explicit, to discourage the numerical aspect of IDs.
82  explicit operator exint() const
83  { return exintValue(); }
84 
85  /// Hash function for use of UNI_ID as a key in the UT_Map class.
86  friend size_t hash_value( const UNI_ID &id )
87  {
88  return SYShash( id.exintValue() );
89  }
90 
91 private:
92  /// Numerical representation of the ID.
93  NumericType myNumericValue = INVALID_ID;
94 };
95 
96 // ============================================================================
97 /// An ID for referencing the specific UNI graph elements.
98 #define UNI_ID_SUBCLASS( SubClass ) \
99 class SubClass : public UNI_ID \
100 { \
101 public: \
102  constexpr explicit SubClass( NumericType id = INVALID_ID ) \
103  : UNI_ID( id ) {} \
104  constexpr explicit SubClass( const UNI_ID &id ) \
105  : UNI_ID( id ) {} \
106 }; \
107 \
108 using SubClass##List = UT_Array< SubClass >; \
109 \
110 
111 UNI_ID_SUBCLASS( UNI_GraphID )
112 UNI_ID_SUBCLASS( UNI_MetaDataID )
113 UNI_ID_SUBCLASS( UNI_NodeID )
114 UNI_ID_SUBCLASS( UNI_ParmID )
115 UNI_ID_SUBCLASS( UNI_PortID )
116 UNI_ID_SUBCLASS( UNI_WireID )
117 UNI_ID_SUBCLASS( UNI_StickyNoteID )
118 UNI_ID_SUBCLASS( UNI_SubnetID )
119 
120 // TODO: FIXME: Define UNI_ItemID that encodes the item type and
121 // converts to and fom UNI_NodeID, UNI_WireID, etc,
122 // to allow a single array sof network "items" in OPUI/FUSE.
123 
124 // ============================================================================
125 /// String formatting ID for debug printouts, etc:
126 #define UNI_ID_FORMATTER(UniID) \
127  inline size_t \
128  UTformatBuffer(char *buffer, size_t buffer_size, const UniID &v) \
129  { \
130  UT::Format::Writer w(buffer, buffer_size); \
131  UT::Format::Formatter f; \
132  return f.format(w, #UniID "({})", {v.exintValue()}); \
133  } \
134  inline size_t \
135  UTformatBuffer(char *buffer, size_t buffer_size, const UT_Array<UniID> &v) \
136  { \
137  UT::Format::Writer w(buffer, buffer_size); \
138  UT::Format::Formatter f; \
139  size_t bytesWritten = w( \
140  "UT_Array<" #UniID ">([", \
141  sizeof("UT_Array<" #UniID ">([") - 1); \
142  bool first = true; \
143  for (auto &&item : v) \
144  { \
145  bytesWritten += f.format( \
146  w, first ? "{}" : ", {}", {item.exintValue()}); \
147  first = false; \
148  } \
149  bytesWritten += w("])", 2); \
150  return bytesWritten; \
151  }
152 
154 UNI_ID_FORMATTER( UNI_GraphID )
155 UNI_ID_FORMATTER( UNI_MetaDataID )
156 UNI_ID_FORMATTER( UNI_NodeID )
157 UNI_ID_FORMATTER( UNI_ParmID )
158 UNI_ID_FORMATTER( UNI_PortID )
159 UNI_ID_FORMATTER( UNI_WireID )
160 UNI_ID_FORMATTER( UNI_StickyNoteID )
161 UNI_ID_FORMATTER( UNI_SubnetID )
162 
163 
164 // ============================================================================
165 // Needed for data IDs in UT_ArrayMap and UT_ArraySet classes.
166 namespace UT
167 {
168  template <typename T> struct DefaultClearer;
169 
170  template <>
172  {
173  static void clear( UNI_ID &v ) { v.clear(); }
174  static bool isClear( const UNI_ID &v ) { return !v.isValid(); }
175  static void clearConstruct( UNI_ID *p ) { clear(*p); }
176  static const bool clearNeedsDestruction = false;
177  };
178 
179 #define UNI_ID_CLEARER(ID) \
180  template <> struct DefaultClearer<ID> : public DefaultClearer<UNI_ID> {}; \
181 
182  UNI_ID_CLEARER( UNI_GraphID )
183  UNI_ID_CLEARER( UNI_MetaDataID )
184  UNI_ID_CLEARER( UNI_NodeID )
185  UNI_ID_CLEARER( UNI_ParmID )
186  UNI_ID_CLEARER( UNI_PortID )
187  UNI_ID_CLEARER( UNI_WireID )
188  UNI_ID_CLEARER( UNI_StickyNoteID )
189  UNI_ID_CLEARER( UNI_SubnetID )
190 }
191 
192 // ============================================================================
193 
194 #endif
#define SYS_STATIC_ASSERT(expr)
constexpr size_t SYShash(const SYS_Flicks f)
Definition: SYS_Flicks.h:46
const GLdouble * v
Definition: glcorearb.h:837
int64 exint
Definition: SYS_Types.h:125
constexpr void clear()
Resets the ID to an invalid value.
Definition: UNI_ID.h:37
constexpr UNI_ID(NumericType id_number=INVALID_ID)
Explicit constructor.
Definition: UNI_ID.h:33
OutGridT const XformOp bool bool
static void clearConstruct(UNI_ID *p)
Definition: UNI_ID.h:175
exint NumericType
Numerical representation of the ID.
Definition: UNI_ID.h:29
friend size_t hash_value(const UNI_ID &id)
Hash function for use of UNI_ID as a key in the UT_Map class.
Definition: UNI_ID.h:86
constexpr bool operator>(const UNI_ID &other) const
Remaining comparison operators, used for sorting, etc.
Definition: UNI_ID.h:61
#define UNI_ID_CLEARER(ID)
Definition: UNI_ID.h:179
static bool isClear(const UNI_ID &v)
Definition: UNI_ID.h:174
constexpr bool operator<=(const UNI_ID &other) const
Remaining comparison operators, used for sorting, etc.
Definition: UNI_ID.h:59
static void clear(UNI_ID &v)
Definition: UNI_ID.h:173
constexpr bool operator>=(const UNI_ID &other) const
Remaining comparison operators, used for sorting, etc.
Definition: UNI_ID.h:63
#define UNI_ID_FORMATTER(UniID)
String formatting ID for debug printouts, etc:
Definition: UNI_ID.h:126
constexpr bool operator<(const UNI_ID &other) const
Remaining comparison operators, used for sorting, etc.
Definition: UNI_ID.h:57
constexpr bool isValid() const
Returns true if this ID is valid; false otherwise.
Definition: UNI_ID.h:41
Type-safe formatting, modeled on the Python str.format function.
constexpr exint exintValue() const
Returns an exint value of this ID.
Definition: UNI_ID.h:74
constexpr bool operator==(const UNI_ID &other) const
Comparison operators.
Definition: UNI_ID.h:48
constexpr NumericType numericValue() const
Returns the numeric value of this ID.
Definition: UNI_ID.h:68
#define UNI_ID_SUBCLASS(SubClass)
An ID for referencing the specific UNI graph elements.
Definition: UNI_ID.h:98
constexpr bool operator!=(const UNI_ID &other) const
Comparison operators.
Definition: UNI_ID.h:50
Definition: UNI_ID.h:25
static constexpr NumericType INVALID_ID
Definition: UNI_ID.h:30