HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
connectorSpecs.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_CONNECTOR_SPECS_H
8 #define PXR_EXEC_VDF_CONNECTOR_SPECS_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/inputSpec.h"
16 #include "pxr/exec/vdf/tokens.h"
17 
18 #include "pxr/base/tf/type.h"
20 
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 ///
25 /// The classes contained in this file are the containers for input and
26 /// output specs. The typical usage pattern is this:
27 ///
28 /// \code
29 /// {
30 /// ....
31 /// VdfInputSpecs inputs;
32 /// inputs
33 /// .ReadConnector<GfVec3d>(TfToken("axis"))
34 /// .ReadConnector<double>(TfToken("length"))
35 /// .ReadWriteConnector<GfVec3d>>(TfToken("moves"),
36 /// TfToken("out"))
37 /// ;
38 /// }
39 /// \endcode
40 ///
41 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 ///
45 /// VdfConnectorSpecs is a container for VdfConnectorSpec objects. This is a
46 /// base class for the concrete containers of input connector specs and output
47 /// connector specs.
48 ///
49 template < typename T >
51 {
52  // We choose a small vector with default size 1 to optimize for common
53  // cases, including nodes with a single output.
55 
56 public:
57  /// Iterator type.
59 
60  /// Constructor.
62 
63  /// Copy constructor
65  // Copy other vector into our new one.
66  Append(rhs);
67  }
68 
69  /// Move constructor
71  : _specs(std::move(rhs._specs)) {
72  }
73 
74  /// Copy assignment operator
76  // Clear our specs, and copy the others into them.
77  _ClearSpecs();
78  Append(rhs);
79  return *this;
80  }
81 
82  /// Move assignment operator
84  // Clear our specs, and move the others into them.
85  _ClearSpecs();
86  _specs = std::move(rhs._specs);
87  return *this;
88  }
89 
90  /// Appends a set of specs to this one.
91  void Append(const VdfConnectorSpecs<T> &specs) {
92  _specs.reserve(_specs.size() + specs._specs.size());
93  for (const T *spec : specs._specs) {
94  _specs.push_back( new T(*spec) );
95  }
96  }
97 
98  /// Allocates space for \p numSpecs connector specs, to avoid re-allocation
99  /// when adding specs, when the number of specs is known ahead of time.
100  void Reserve(size_t numSpecs) {
101  _specs.reserve(numSpecs);
102  }
103 
104  /// Returns number of connectors in this spec
105  size_t GetSize() const { return _specs.size(); }
106 
107  /// Returns a const_iterator to the beginning of the connectors.
108  const_iterator begin() const { return _specs.begin(); }
109 
110  /// Returns a const_iterator to the end of the connectors.
111  const_iterator end() const { return _specs.end(); }
112 
113 protected:
114 
115  /// Destructor.
116  ///
117  /// Note, the destructor is not virtual on purpose (we don't want the
118  /// vtable), but we make it protected to prevent destruction through a
119  /// base class pointer.
121  _ClearSpecs();
122  }
123 
124  /// Clears list of specs.
125  void _ClearSpecs() {
126  const_iterator e = _specs.end();
127  for (const_iterator i = _specs.begin(); i != e; ++i) {
128  delete *i;
129  }
130  _specs.clear();
131  }
132 
133  /// Returns connector spec at index \c idx.
134  const T *_GetConnectorSpec(int idx) const
135  {
136  return _specs[idx];
137  }
138 
139  /// Adds a connector to our list.
140  void _AddConnector(T *cs)
141  {
142  _specs.push_back(cs);
143  }
144 
145 private:
146 
147  // A list of specs held in this container class.
148  _VectorType _specs;
149 };
150 
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 ///
154 /// VdfInputSpecs is a container for VdfInputSpec objects.
155 /// These objects are used to describe inputs on a VdfNode.
156 ///
157 class VdfInputSpecs : public VdfConnectorSpecs<VdfInputSpec>
158 {
159 public:
160 
161  /// Create a "Read" connector with the name \p inName and optionally
162  /// associated with the output named \p outName.
163  ///
164  /// A read connector that has an associated output, tells the system
165  /// that the masks coming in from \p outName can be propogated to
166  /// the input \p inName.
167  ///
168  template <typename T> VdfInputSpecs &
169  ReadConnector(const TfToken &inName,
170  const TfToken &outName = VdfTokens->empty,
171  bool prerequisite = false)
172  {
173  _AddConnector(VdfInputSpec::New<T>(
174  inName, outName, VdfInputSpec::READ, prerequisite));
175  return *this;
176  }
177 
178  /// Create a "Read/Write" connector with the name \p inName associated
179  /// with the output named \p outName.
180  ///
181  template <typename T> VdfInputSpecs &
182  ReadWriteConnector(const TfToken &inName, const TfToken &outName)
183  {
184  _AddConnector(VdfInputSpec::New<T>(
185  inName, outName, VdfInputSpec::READWRITE, /* prerequisite */false));
186  return *this;
187  }
188 
189  /// Returns connector spec at index \c idx.
190  ///
191  const VdfInputSpec *GetInputSpec(int idx) const
192  {
193  return _GetConnectorSpec(idx);
194  }
195 
196  /// Non-templated version of ReadConnector(), the given type must be
197  /// registered for runtime type dispatching.
198  VdfInputSpecs &
200  const TfType &type,
201  const TfToken &inName,
202  const TfToken &outName = VdfTokens->empty,
203  bool prerequisite = false)
204  {
207  type, inName, outName, VdfInputSpec::READ, prerequisite));
208  return *this;
209  }
210 
211  /// Non-templated version of ReadWriteConnector(), the given type must be
212  /// registered for runtime type dispatching.
213  VdfInputSpecs &
215  const TfType &type,
216  const TfToken &inName,
217  const TfToken &outName)
218  {
221  type, inName, outName, VdfInputSpec::READWRITE,
222  /*prerequisite*/ false));
223  return *this;
224  }
225 
226  /// Returns true if two InputSpecs are equal.
227  ///
228  VDF_API
229  bool operator==(const VdfInputSpecs &rhs) const;
230  bool operator!=(const VdfInputSpecs &rhs) const {
231  return !(*this == rhs);
232  }
233 };
234 
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 ///
238 /// VdfOutputSpecs is a container for VdfOutputSpec objects.
239 /// These objects are used to describe outputs on a VdfNode.
240 ///
241 class VdfOutputSpecs : public VdfConnectorSpecs<VdfOutputSpec>
242 {
243 public:
244 
245  /// Create an "Out" connector with the given name.
246  template <typename T> VdfOutputSpecs &
248  {
249  _AddConnector(VdfOutputSpec::New<T>(name));
250  return *this;
251  }
252 
253  /// Returns connector spec at index \c idx.
254  const VdfOutputSpec *GetOutputSpec(int idx) const
255  {
256  return _GetConnectorSpec(idx);
257  }
258 
259  /// Non-templated version of Connector(), the given type must be
260  /// registered for runtime type dispatching.
262  Connector(const TfType &type, const TfToken &name)
263  {
264  _AddConnector(VdfOutputSpec::New(type, name));
265  return *this;
266  }
267 
268  /// Returns true if two OutputSpecs are equal.
269  ///
270  VDF_API
271  bool operator==(const VdfOutputSpecs &rhs) const;
272  bool operator!=(const VdfOutputSpecs &rhs) const {
273  return !(*this == rhs);
274  }
275 };
276 
277 ////////////////////////////////////////////////////////////////////////////////
278 
280 
281 #endif
VdfConnectorSpecs()
Constructor.
void push_back(const value_type &v)
Definition: smallVector.h:478
VdfInputSpecs & ReadConnector(const TfType &type, const TfToken &inName, const TfToken &outName=VdfTokens->empty, bool prerequisite=false)
iterator end()
Definition: smallVector.h:649
bool operator!=(const VdfInputSpecs &rhs) const
void Reserve(size_t numSpecs)
void reserve(size_type newCapacity)
Definition: smallVector.h:410
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
const VdfInputSpec * GetInputSpec(int idx) const
VDF_API bool operator==(const VdfInputSpecs &rhs) const
size_type size() const
Definition: smallVector.h:596
#define VDF_API
Definition: api.h:25
size_t GetSize() const
Returns number of connectors in this spec.
static VdfInputSpec * New(const TfToken &inName, const TfToken &outName, Access access, bool prerequisite)
Definition: inputSpec.h:41
void _AddConnector(T *cs)
Adds a connector to our list.
VdfConnectorSpecs(VdfConnectorSpecs< T > &&rhs)
Move constructor.
bool operator!=(const VdfOutputSpecs &rhs) const
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
Definition: token.h:70
VdfConnectorSpecs< T > & operator=(const VdfConnectorSpecs< T > &rhs)
Copy assignment operator.
static VdfOutputSpec * New(const TfToken &name)
Definition: outputSpec.h:45
VdfConnectorSpecs(const VdfConnectorSpecs< T > &rhs)
Copy constructor.
VDF_API bool operator==(const VdfOutputSpecs &rhs) const
VdfConnectorSpecs< T > & operator=(VdfConnectorSpecs< T > &&rhs)
Move assignment operator.
iterator begin()
Definition: smallVector.h:632
const_iterator end() const
Returns a const_iterator to the end of the connectors.
GLuint const GLchar * name
Definition: glcorearb.h:786
VdfOutputSpecs & Connector(const TfType &type, const TfToken &name)
const T * _GetConnectorSpec(int idx) const
Returns connector spec at index idx.
const_iterator begin() const
Returns a const_iterator to the beginning of the connectors.
void _ClearSpecs()
Clears list of specs.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const VdfOutputSpec * GetOutputSpec(int idx) const
Returns connector spec at index idx.
VdfOutputSpecs & Connector(const TfToken &name)
Create an "Out" connector with the given name.
Definition: type.h:47
void Append(const VdfConnectorSpecs< T > &specs)
Appends a set of specs to this one.
typename _VectorType::const_iterator const_iterator
Iterator type.
VdfInputSpecs & ReadConnector(const TfToken &inName, const TfToken &outName=VdfTokens->empty, bool prerequisite=false)
VdfInputSpecs & ReadWriteConnector(const TfType &type, const TfToken &inName, const TfToken &outName)
VdfInputSpecs & ReadWriteConnector(const TfToken &inName, const TfToken &outName)