HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDGT_RegisteredType.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  * COMMENTS:
7  */
8 
9 #ifndef __PDGT_REGISTERED_TYPE_H__
10 #define __PDGT_REGISTERED_TYPE_H__
11 
12 #include "PDGT_API.h"
13 #include "PDGT_BaseType.h"
14 
15 #include <UT/UT_ConcurrentVector.h>
16 #include <UT/UT_Function.h>
17 #include <UT/UT_SharedPtr.h>
18 #include <UT/UT_WeakPtr.h>
19 #include <UT/UT_WorkBuffer.h>
20 
21 class PDGT_ValueArgs;
22 
23 /**
24  * This class represents a concrete type in a type system. It subclasses from
25  * PDGT_BaseType, and defines a number of new template arguments that describe
26  * the type. These include:
27  *
28  * The Class, which is the backing C++ class that this type will produce.
29  * For example, the PDG_NodeCallbackType produces instances of the
30  * PDG_NodeCallback class.
31  *
32  * The pointer type. If the pointer type is a shared pointer, this object
33  * will be able to safely tracking the list of instances created.
34  * Otherwise, instance tracking will be disabled and attempts to use the
35  * reload functionality will error out.
36  *
37  * The specfic entry in the EnumType that's associated with this type,
38  * for example PDG_RegistrationType::eNode.
39  *
40  * The variadic argument pack of constructor arguments that are needed to
41  * create instances of Class using type object.
42  *
43  * By default, a registered type expects to be given a functor to use to
44  * construct Class instances. A default functor which simply calls `new Class`
45  * with the appropriate args is already included. It is also possible to
46  * completely override the functor-based constructor and instead redefine the
47  * `instantiate` function to perform construction logic. This is done for
48  * Python-backed node/scheduler types in PDG, which implement part their
49  * construction logic in the PYPDG module using pybind11.
50  *
51  * An instance of this class provides three main pieces of functionality:
52  *
53  * The ability to construct new instances of Class, using logic defined
54  * by the type object and the input Args...
55  *
56  * Optional thread safe tracking of all instances of the type that have
57  * been created.
58  *
59  * And, assuming the tracking is enabled, the ability to run some sort of
60  * reloading logic. For example, for PDG nodes defined in Python, this
61  * functionality can be used to reload the Python module and recreate
62  * any underlying PyObjects used by the node callback type.
63  */
64 template <typename Class, typename Pointer, typename EnumType, EnumType Type, typename... Args>
66 {
67 public:
68  /// The factory function type, which returns a new instance of Class as a
69  /// Pointer.
70  using Function = UT_Function<Pointer (
73  const PDGT_ValueArgs& extra_args,
74  Args...)>;
75 
76  /// Array of instances that refer to Class objects created using this type.
77  /// The array contains weak references, which is why instance tracking can
78  /// only be enabled when Pointer is some sort of shared_ptr type.
79  using Instances = UT_ConcurrentVector<UT_WeakPtr<Class>>;
80 
81  /// The enum entry this registered type is associated with, from the
82  /// EnumType declared as a template argument.
83  static constexpr EnumType Enum = Type;
84 
85 
87  const UT_StringHolder& label,
89  bool track_instances)
90  : PDGT_BaseType<EnumType>(name, label, lang, Type)
91  , myTrackInstances(track_instances)
92  {
93  }
94 
95  ~PDGT_RegisteredType() override {}
96 
97  /// Constructs a new instance of Class using this registered type. If the
98  /// Pointer type is a raw pointer, the caller takes owernship of the object.
99  /// Otherwise, if Pointer is a shared pointer type, the ownership is of
100  /// course bound to the lifetime of that shared pointer. The caller is
101  /// responsible for making sure that they keep it alive for as long as
102  /// they need it.
103  Pointer instance(UT_WorkBuffer& errors,
104  const PDGT_ValueArgs& extra_args,
105  Args... args) const
106  {
107  Pointer instance =
108  instantiate(errors, extra_args, args...);
109  addInstance(instance);
110  return instance;
111  }
112 
113  /// Reloads a particular instance of Class, using this type object. This
114  /// is a "re-initialize" call, to reset whatever state in the Class*
115  /// instance is dependent on this type object.
117  const PDGT_ValueArgs& extra_args,
118  Class* instance,
119  Args... args) const
120  {
121  return reload(errors, extra_args, instance,
122  args...);
123  }
124 
125  /// Reloads this type object itself, and requests that each existing
126  /// instance of Class also resets itself. If Pointer is not a shared pointer
127  /// type, there will be no instances to reset.
128  bool reloadType(UT_WorkBuffer& errors)
129  {
130  bool result = reload(errors);
131  for (auto&& instance : myInstances)
132  {
133  if (auto p = instance.lock())
134  {
135  p->setReloading(true);
136  result &= p->reloadInstance(errors);
137  p->setReloading(false);
138  }
139  }
140  return result;
141  }
142 
143  /// Returns the combined memory usage of all instances of this type
144  int64 instanceMemoryUsage() const override
145  {
146  int64 mem = 0;
147  for (auto&& instance : myInstances)
148  {
149  if (auto p = instance.lock())
150  mem += p->getMemoryUsage(true);
151  }
152 
153  return mem;
154  }
155 
156  /// Sets the factory function to use when contructing Class instances.
157  void setFunction(const Function& function)
158  { myFunction = function; }
159 
160  /// Default factory method, which simple allocates a new instance of
161  /// ConcreteType, which much be Class or some subclass of it. The object
162  /// is returned as Pointer.
163  template <typename ConcreteType>
164  static Pointer typeInit(const PDGT_BaseType<EnumType>* type,
165  UT_WorkBuffer& errors,
166  const PDGT_ValueArgs& extra_args,
167  Args... args)
168  {
169  return Pointer(
170  new ConcreteType(type, extra_args, args...));
171  }
172 
173 protected:
174  /// These methods conditionally enable instance tracking. We can only safely
175  /// keep weak-refs to the instances if our Pointer type is a shared ptr, so
176  /// an overload to addInstance that's conditional on that being the case
177  /// is required. It's also possible to disable instance tracking with a
178  /// member variable, so that gets checked in the codepath that may actually
179  /// append an instance to our tracking vector.
180  template <typename P = Pointer>
181  typename std::enable_if<std::is_same<P, UT_SharedPtr<Class>>::value>::type
182  addInstance(P instance) const
183  {
184  if (myTrackInstances)
185  myInstances.push_back(instance);
186  }
187  template <typename P = Pointer>
188  typename std::enable_if<!std::is_same<P, UT_SharedPtr<Class>>::value>::type
189  addInstance(P instance) const
190  {
191  }
192 
194  {
195  return instance.lock();
196  }
197 
198 
199  /// This function is responsible for constructing an instance of Class. It
200  /// uses the functor by default, but subclasses are permitted to redefine
201  /// it to construct objects in a different way.
202  virtual Pointer instantiate(UT_WorkBuffer& errors,
203  const PDGT_ValueArgs& extra_args,
204  Args... variadic_args) const
205  {
206  if (!myFunction)
207  {
208  errors.appendFormat(
209  "Incomplete or invalid type '{}'",
211  }
212 
213  return myFunction(this, errors, extra_args,
214  variadic_args...);
215  }
216 
217  /// This function is responsible for reloading this type object itself.
218  /// For example, for a Python-backed node as defined in the PYPDG module,
219  /// this function will reload the underlying Python module.
220  virtual bool reload(UT_WorkBuffer& errors)
221  {
222  errors.format(
223  "Type '{}' does not support reloading",
225  return false;
226  }
227 
228  /// This function is responsible for reloading a particular instance of
229  /// Class created using this type object. By default, it does nothing, but
230  /// subclasses that wish to support reloading should customize it. For
231  /// example, the PYPDG subclass will use this hook to reconstruct the Py
232  /// Object inside of a PDG_NodeCallback, when reloading that instance.
233  virtual bool reload(UT_WorkBuffer& errors,
234  const PDGT_ValueArgs& extra_args,
235  Class* instance,
236  Args... variadic_args) const
237  {
238  errors.format(
239  "Instances of type '{}' cannot be reloaded",
241  return false;
242  }
243 
244 private:
245  Function myFunction;
246  mutable Instances myInstances;
247  bool myTrackInstances;
248 };
249 
250 #endif
void setFunction(const Function &function)
Sets the factory function to use when contructing Class instances.
bool reloadType(UT_WorkBuffer &errors)
PXL_API void reload()
Reload the configuration.
virtual Pointer instantiate(UT_WorkBuffer &errors, const PDGT_ValueArgs &extra_args, Args...variadic_args) const
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
UT_ConcurrentVector< UT_WeakPtr< Class >> Instances
GLsizei const GLfloat * value
Definition: glcorearb.h:824
virtual bool reload(UT_WorkBuffer &errors, const PDGT_ValueArgs &extra_args, Class *instance, Args...variadic_args) const
**But if you need a result
Definition: thread.h:622
int64 instanceMemoryUsage() const override
Returns the combined memory usage of all instances of this type.
bool reloadInstance(UT_WorkBuffer &errors, const PDGT_ValueArgs &extra_args, Class *instance, Args...args) const
std::enable_if<!std::is_same< P, UT_SharedPtr< Class > >::value >::type addInstance(P instance) const
static Pointer typeInit(const PDGT_BaseType< EnumType > *type, UT_WorkBuffer &errors, const PDGT_ValueArgs &extra_args, Args...args)
Pointer instance(UT_WorkBuffer &errors, const PDGT_ValueArgs &extra_args, Args...args) const
size_t appendFormat(const char *fmt, const Args &...args)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
#define PDGT_API_TMPL
Definition: PDGT_API.h:24
long long int64
Definition: SYS_Types.h:116
virtual bool reload(UT_WorkBuffer &errors)
GLuint const GLchar * name
Definition: glcorearb.h:786
UT_Array< PDGT_Value > Args
std::function< T > UT_Function
Definition: UT_Function.h:37
size_t format(const char *fmt, const Args &...args)
UT_SharedPtr< Class > lockInstance(UT_WeakPtr< Class > instance) const
**If you just want to fire and args
Definition: thread.h:618
UT_Function< Pointer(const PDGT_BaseType< EnumType > *, UT_WorkBuffer &, const PDGT_ValueArgs &extra_args, Args...)> Function
std::enable_if< std::is_same< P, UT_SharedPtr< Class > >::value >::type addInstance(P instance) const
PDGT_RegisteredType(const UT_StringHolder &name, const UT_StringHolder &label, typename PDGT_BaseType< EnumType >::Language lang, bool track_instances)
std::weak_ptr< T > UT_WeakPtr
Definition: UT_SharedPtr.h:49