HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
object.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_OBJECT_H
8 #define PXR_EXEC_VDF_OBJECT_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
15 
16 #include "pxr/base/tf/token.h"
17 
18 #include <set>
19 #include <vector>
20 
22 
23 class VdfConnection;
24 class VdfInput;
25 class VdfNode;
26 class VdfOutput;
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 ///
30 /// \class VdfObjectPtr
31 ///
32 /// An universal class to represent pointers to various Vdf types.
33 ///
34 ///XXX: We are considering using a base class within Vdf, but we want to
35 /// measure the speed/size impact first.
36 ///
37 ///XXX: We could pack everything into 8 bytes and use it as value type.
38 ///
40 {
41 public:
42 
43  /// Type of object.
44  enum Type
45  {
46  Undefined = -1, // Marks the undefined type.
47 
48  Node, // The object is a VdfNode.
49  Connection, // The object is a VdfConnection.
50  Input, // The object is a VdfInput.
51  Output // The object is a VdfOutput.
52  };
53 
54  /// Ctor to create the NULL object.
55  ///
57  : _node(NULL),
58  _type(Undefined),
59  _isConst(false) {}
60 
61  /// Ctor to create an anchor from a \p node.
62  ///
64  : _node(node),
65  _type(Node),
66  _isConst(false) {}
67 
68  /// Ctor to create an anchor from a const \p node.
69  ///
70  VdfObjectPtr(const VdfNode *node)
71  : _constNode(node),
72  _type(Node),
73  _isConst(true) {}
74 
75  /// Ctor to create an anchor from a \p connection.
76  ///
78  : _connection(connection),
79  _type(Connection),
80  _isConst(false) {}
81 
82  /// Ctor to create an anchor from a const \p connection.
83  ///
84  VdfObjectPtr(const VdfConnection *connection)
85  : _constConnection(connection),
86  _type(Connection),
87  _isConst(true) {}
88 
89  /// Ctor to create an anchor from a \p input.
90  ///
92  : _input(input),
93  _type(Input),
94  _isConst(false) {}
95 
96  /// Ctor to create an anchor from a \p input.
97  ///
98  VdfObjectPtr(const VdfInput *input)
99  : _constInput(input),
100  _type(Input),
101  _isConst(true) {}
102 
103  /// Ctor to create an anchor from a \p output.
104  ///
106  : _output(output),
107  _type(Output),
108  _isConst(false) {}
109 
110  /// Ctor to create an anchor from a \p output.
111  ///
112  VdfObjectPtr(const VdfOutput *output)
113  : _constOutput(output),
114  _type(Output),
115  _isConst(true) {}
116 
117  /// Assignment operator.
118  ///
120  {
121  _node = rhs._node;
122  _type = rhs._type;
123  _isConst = rhs._isConst;
124 
125  return *this;
126  }
127 
128  /// Less than operator used for map.
129  ///
130  bool operator<(const VdfObjectPtr &rhs) const
131  {
132  return _node < rhs._node;
133  }
134 
135  bool operator<=(const VdfObjectPtr &rhs) const
136  {
137  return !(rhs < *this);
138  }
139 
140  /// More than operator used for map.
141  ///
142  bool operator>(const VdfObjectPtr &rhs) const
143  {
144  return rhs < *this;
145  }
146 
147  bool operator>=(const VdfObjectPtr &rhs) const
148  {
149  return !(*this < rhs);
150  }
151 
152  /// Equality operator. Note that constness doesn't matter.
153  ///
154  bool operator==(const VdfObjectPtr &rhs) const
155  {
156  return _type == rhs._type &&
157  _node == rhs._node;
158  }
159 
160  /// Not equal operator.
161  ///
162  bool operator!=(const VdfObjectPtr &rhs) const
163  {
164  return !(*this == rhs);
165  }
166 
167  /// Functor to use for hash maps.
168  ///
169  struct HashFunctor
170  {
171  size_t operator()(const VdfObjectPtr &obj) const
172  {
173  return (size_t)obj._node;
174  }
175  };
176 
177  /// Returns false, if the object holds the NULL object.
178  ///
179  operator bool() const
180  {
181  return _node;
182  }
183 
184  /// Returns the type of the object.
185  ///
186  Type GetType() const
187  {
188  return _type;
189  }
190 
191  /// Returns true, if the object being hold is const.
192  ///
193  bool IsConst() const
194  {
195  return _isConst;
196  }
197 
198  /// Returns true if the object is a node.
199  ///
200  bool IsNode() const
201  {
202  return _type == Node;
203  }
204 
205  /// Returns true if the object is a node.
206  ///
207  bool IsConnection() const
208  {
209  return _type == Connection;
210  }
211 
212  /// Returns true if the object is a node.
213  ///
214  bool IsInput() const
215  {
216  return _type == Input;
217  }
218 
219  /// Returns true if the object is a node.
220  ///
221  bool IsOutput() const
222  {
223  return _type == Output;
224  }
225 
226  /// Returns a non-const pointer to a node. Fails if object is const or
227  /// not a node (cf. GetIfNode()).
228  ///
230  {
231  TF_AXIOM(IsNode());
232  TF_AXIOM(!IsConst());
233  return _node;
234  }
235 
236  /// Returns a non-const pointer to a connection. Fails if object is const
237  /// or not a connection (cf. GetIfNode()).
238  ///
240  {
242  TF_AXIOM(!IsConst());
243  return _connection;
244  }
245 
246  /// Returns a non-const pointer to an input. Fails if object is const or
247  /// not an input (cf. GetIfNode()).
248  ///
250  {
251  TF_AXIOM(IsInput());
252  TF_AXIOM(!IsConst());
253  return _input;
254  }
255 
256  /// Returns a non-const pointer to an output. Fails if object is const or
257  /// not an output (cf. GetIfNode()).
258  ///
260  {
261  TF_AXIOM(IsOutput());
262  TF_AXIOM(!IsConst());
263  return _output;
264  }
265 
266  /// Returns a const reference to a node. Fails if object is not a node.
267  ///
268  const VdfNode &GetNode() const
269  {
270  TF_AXIOM(IsNode());
271  return *_constNode;
272  }
273 
274  /// Returns a const reference to a connection. Fails if object is not a
275  /// connection.
276  ///
278  {
280  return *_constConnection;
281  }
282 
283  /// Returns a const reference to an input. Fails if object is not an input.
284  ///
285  const VdfInput &GetInput() const
286  {
287  TF_AXIOM(IsInput());
288  return *_constInput;
289  }
290 
291  /// Returns a const reference to an output. Fails if object is not an
292  /// output.
293  ///
294  const VdfOutput &GetOutput() const
295  {
296  TF_AXIOM(IsOutput());
297  return *_constOutput;
298  }
299 
300  /// Returns a pointer to a VdfNode if the object is holding a node,
301  /// NULL otherwise.
302  ///
303  const VdfNode *GetIfNode() const
304  {
305  return IsNode() ? _constNode : NULL;
306  }
307 
308  /// Returns a pointer to a VdfConnection if the object is holding a
309  /// connection, NULL otherwise.
310  ///
312  {
313  return IsConnection() ? _constConnection : NULL;
314  }
315 
316  /// Returns a pointer to a VdfInput if the object is holding a input,
317  /// NULL otherwise.
318  ///
319  const VdfInput *GetIfInput() const
320  {
321  return IsInput() ? _constInput : NULL;
322  }
323 
324  /// Returns a pointer to a VdfOutput if the object is holding a output,
325  /// NULL otherwise.
326  ///
327  const VdfOutput *GetIfOutput() const
328  {
329  return IsOutput() ? _constOutput : NULL;
330  }
331 
332  /// Returns a pointer to a VdfNode if the object is holding a non-const node,
333  /// NULL otherwise.
334  ///
336  {
337  return (!IsConst() && IsNode()) ? _node : NULL;
338  }
339 
340  /// Returns a pointer to a VdfConnection if the object is holding a
341  /// non-const connection, NULL otherwise.
342  ///
344  {
345  return (!IsConst() && IsConnection()) ? _connection : NULL;
346  }
347 
348  /// Returns a pointer to a VdfInput if the object is holding a non-const
349  /// input, NULL otherwise.
350  ///
352  {
353  return (!IsConst() && IsInput()) ? _input : NULL;
354  }
355 
356  /// Returns a pointer to a VdfOutput if the object is holding a non-const
357  /// output, NULL otherwise.
358  ///
360  {
361  return (!IsConst() && IsOutput()) ? _output : NULL;
362  }
363 
364  /// Returns the owning node of this object if object is an input or output.
365  /// Returns the node itself if object is a node and returns NULL if object
366  /// is a connection.
367  ///
368  VDF_API
369  const VdfNode *GetOwningNode() const;
370 
371  /// Returns a debug name for this object.
372  ///
373  VDF_API
374  std::string GetDebugName() const;
375 
376  /// Returns the identity of this object as a void *.
377  ///
378  const void *GetIdentity() const
379  {
380  // Note: Since all different types are stored in an union, we just
381  // pick the first const type to return.
382  return _constNode;
383  }
384 
385 // -----------------------------------------------------------------------------
386 
387 private:
388 
389  // Stores pointers as needed for the different object types.
390  union
391  {
396 
401  };
402 
403  // Holds the type of the object.
404  Type _type;
405 
406  // If set, the object being held is const.
407  bool _isConst;
408 };
409 
410 /// An object vector.
411 ///
412 typedef std::vector<VdfObjectPtr> VdfObjectPtrVector;
413 
414 /// An object set.
415 ///
416 typedef std::set<VdfObjectPtr> VdfObjectPtrSet;
417 
419 
420 #endif
const VdfInput * GetIfInput() const
Definition: object.h:319
const void * GetIdentity() const
Definition: object.h:378
VdfOutput * _output
Definition: object.h:395
VdfNode * GetNonConstNode() const
Definition: object.h:229
VdfObjectPtr(VdfOutput *output)
Definition: object.h:105
Type
Type of object.
Definition: object.h:44
VdfObjectPtr(const VdfInput *input)
Definition: object.h:98
VdfNode * _node
Definition: object.h:392
Definition: Node.h:52
bool IsNode() const
Definition: object.h:200
VdfInput * _input
Definition: object.h:394
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
std::set< VdfObjectPtr > VdfObjectPtrSet
Definition: object.h:416
Type GetType() const
Definition: object.h:186
VDF_API const VdfNode * GetOwningNode() const
VDF_API std::string GetDebugName() const
VdfObjectPtr(VdfNode *node)
Definition: object.h:63
Definition: node.h:52
bool IsOutput() const
Definition: object.h:221
const VdfNode * GetIfNode() const
Definition: object.h:303
VdfObjectPtr(const VdfConnection *connection)
Definition: object.h:84
const VdfOutput & GetOutput() const
Definition: object.h:294
size_t operator()(const VdfObjectPtr &obj) const
Definition: object.h:171
bool operator<=(const VdfObjectPtr &rhs) const
Definition: object.h:135
VdfObjectPtr(VdfConnection *connection)
Definition: object.h:77
#define VDF_API
Definition: api.h:25
OutGridT const XformOp bool bool
VdfConnection * GetNonConstConnection() const
Definition: object.h:239
Definition: input.h:35
std::vector< VdfObjectPtr > VdfObjectPtrVector
Definition: object.h:412
VdfObjectPtr(VdfInput *input)
Definition: object.h:91
VdfInput * GetNonConstInput() const
Definition: object.h:249
VdfOutput * GetNonConstOutput() const
Definition: object.h:259
bool operator<(const VdfObjectPtr &rhs) const
Definition: object.h:130
bool IsConst() const
Definition: object.h:193
bool operator>(const VdfObjectPtr &rhs) const
Definition: object.h:142
VdfObjectPtr(const VdfNode *node)
Definition: object.h:70
VdfObjectPtr & operator=(const VdfObjectPtr &rhs)
Definition: object.h:119
const VdfConnection * _constConnection
Definition: object.h:398
bool operator!=(const VdfObjectPtr &rhs) const
Definition: object.h:162
const VdfNode & GetNode() const
Definition: object.h:268
VdfInput * GetIfNonConstInput() const
Definition: object.h:351
const VdfNode * _constNode
Definition: object.h:397
VdfOutput * GetIfNonConstOutput() const
Definition: object.h:359
const VdfInput * _constInput
Definition: object.h:399
#define TF_AXIOM(cond)
VdfObjectPtr(const VdfOutput *output)
Definition: object.h:112
bool operator>=(const VdfObjectPtr &rhs) const
Definition: object.h:147
const VdfOutput * GetIfOutput() const
Definition: object.h:327
VdfObjectPtr()
Definition: object.h:56
const VdfOutput * _constOutput
Definition: object.h:400
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
VdfConnection * _connection
Definition: object.h:393
const VdfInput & GetInput() const
Definition: object.h:285
const VdfConnection & GetConnection() const
Definition: object.h:277
const VdfConnection * GetIfConnection() const
Definition: object.h:311
bool operator==(const VdfObjectPtr &rhs) const
Definition: object.h:154
VdfNode * GetIfNonConstNode() const
Definition: object.h:335
bool IsConnection() const
Definition: object.h:207
VdfConnection * GetIfNonConstConnection() const
Definition: object.h:343
bool IsInput() const
Definition: object.h:214