HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PY_Callback.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  * Instances of this class hold a Python callback (any callable Python
8  * object). You can invoke this callback with the call() method, passing
9  * it optional arguments. When this object is destroyed, the reference
10  * count on the Python callback will be decremented.
11  */
12 
13 #ifndef __PY_Callback_h__
14 #define __PY_Callback_h__
15 
16 #include "PY_API.h"
17 #include "PY_CompiledCode.h"
18 #include "PY_InterpreterAutoLock.h"
19 #include "PY_EvaluationContext.h"
20 #include "PY_OpaqueObject.h"
21 #include "PY_Result.h"
22 #include "PY_Kwargs.h"
23 
24 #include <UT/UT_Function.h>
25 #include <UT/UT_NonCopyable.h>
26 #include <UT/UT_StringHolder.h>
27 #include <UT/UT_StringMap.h>
28 #include <UT/UT_UniquePtr.h>
29 
30 class PY_PyObject;
31 class BM_View;
32 class FUSE_Office;
33 class OP_Node;
34 
35 // A cache to avoid recompiling PY_CompiledCode.
37 {
38 public:
39  PY_CallbackCompiledCodeCache(exint cache_size=100);
41 
42  PY_CompiledCode *compile(const UT_StringHolder &code);
43 
46 
48 };
49 
50 // Interface class implemented in HOMUI/HOMF which allows allows code in low
51 // level libraries such as PY and BM to create HOM wrapper instances.
52 // This can be used to avoid generating code that has a pointer as an int64 in
53 // its python code string.
55 {
56 public:
57  PY_CallbackHOM() = default;
58  virtual ~PY_CallbackHOM(){}
59 
61 
62  virtual bool getDesktopOffice(const BM_View& v, int &desktop_id, int &office_id) = 0;
63  virtual FUSE_Office *getOffice(const BM_View& v) = 0;
64 
65  virtual PY_PyObject *newNode(OP_Node &node) = 0;
66  virtual PY_PyObject *newSceneViewer(FUSE_Office &office, bool convert_context_viewer=false) = 0;
67  virtual PY_PyObject *newViewerStateContext(int64 val) = 0;
68  virtual PY_PyObject *newUIEvent(int64 val, int64 val2) = 0;
69  virtual PY_PyObject *newViewerEvent(int desktop, int office, int64 val, int64 val2) = 0;
70  virtual PY_PyObject *newCompositorViewer(FUSE_Office &office) = 0;
71 };
72 
74 {
75 public:
77 
78  PY_Callback() noexcept : PY_OpaqueObject()
79  {};
80  PY_Callback(std::nullptr_t n) noexcept : PY_OpaqueObject(n)
81  {};
82 
83  // Construct instances of this class with an opaque PY_PyObject
84  // containing a callable Python object.
85  explicit
86  PY_Callback(void *python_callable_object, bool new_ref = false) noexcept
87  : PY_OpaqueObject(python_callable_object, new_ref)
88  , myExpressionCache(nullptr)
89  {}
90 
91  /// Copy construct/assign
92  /// @{
93  PY_Callback(const PY_Callback &callback) noexcept
94  : PY_OpaqueObject(callback)
95  , myExpressionCache(nullptr)
96  {}
97  PY_Callback &operator=(const PY_Callback &callback) noexcept
98  { return (PY_Callback &)PY_OpaqueObject::operator=(callback); }
99  /// @}
100 
101  /// Move construct/assign
102  /// @{
103  PY_Callback(PY_Callback &&callback) noexcept
104  : PY_OpaqueObject(std::forward<PY_Callback>(callback))
105  , myExpressionCache(nullptr)
106  {
107  }
108  PY_Callback &operator=(PY_Callback &&callback) noexcept
109  {
111  std::forward<PY_Callback>(callback));
112  }
113  /// @}
114 
115  // Given two strings containing Python expressions (one that evaluates to
116  // a tuple of arguments and another that evaluates to a dictionary of
117  // keyword arguments), call the callback. Either or both of the argument
118  // tuple and keyword argument dictionary may be null. The result is
119  // returned in the PY_Result object.
120  void call(PY_Result &result, const char *args_expression=NULL,
121  const char *kwargs_expression=NULL) const;
122 
123  // This convenience version of call() returns the PY_Result by value,
124  // instead of passing it in by reference. This versions can be used by
125  // code that isn't performance-critical.
126  PY_Result call(const char *args_expression=NULL,
127  const char *kwargs_expression=NULL) const
128  {
130  call(result, args_expression, kwargs_expression);
131  return result;
132  }
133 
134  // For callbacks returning values
135  PY_Result call(
136  const char *args_expression,
137  const char *kwargs_expression,
138  PY_Result::Type desired_result_type ) const;
139 
140  // Similar to the above methods but take a lambda function to add elements to
141  // the evaluated expressions before firing the callback. Also takes a module name
142  // to evaluate the expressions, the module is optional and can be set to nullptr if
143  // not required.
144  //
146  const char* args_expression,
147  const char* kwargs_expression,
148  const char* module_name,
149  PY_Result::Type desired_result_type,
150  PY_OpaqueObject& out_args,
151  PY_OpaqueObject& out_kwargs,
152  ArgHandler arg_handler ) const
153  {
154  PY_InterpreterAutoLock auto_lock;
155 
156  PY_Result result = evaluateArgs(
157  args_expression, kwargs_expression, module_name, out_args, out_kwargs, arg_handler);
158  if (result.myResultType == PY_Result::ERR)
159  {
160  return result;
161  }
162 
163  return call(out_args.pyObject(), out_kwargs.pyObject(),
164  desired_result_type);
165  }
166 
167  // Evaluates argument expressions and use a lambda function for
168  // adding extra elements to the arg and kwarg objects.
170  const char* args_expression,
171  const char* kwargs_expression,
172  const char* module_name,
173  PY_OpaqueObject& out_args,
174  PY_OpaqueObject& out_kwargs,
175  ArgHandler arg_handler ) const
176  {
177  PY_InterpreterAutoLock auto_lock;
178 
179  PY_Result result = evaluate(
180  args_expression, kwargs_expression, module_name, out_args, out_kwargs);
181  if (result.myResultType == PY_Result::ERR)
182  {
183  return result;
184  }
185 
186  arg_handler(out_args.pyObject(), out_kwargs.pyObject());
187 
188  return result;
189  }
190 
192  { myExpressionCache = c; }
193 
194  static void setHOM(PY_CallbackHOM *hom)
195  { theHOM = hom; }
196 
198  { return theHOM; }
199 
200 private:
201  // Performs a call with pre-evaluated args and kwargs
202  PY_Result call(
203  PY_PyObject * args,
204  PY_PyObject * kwargs,
205  PY_Result::Type desired_result_type=PY_Result::NONE ) const;
206 
207  // Evaluates positional and keyworded arguments. The returned arguments can
208  // be expanded with other values before performing a call.
209  PY_Result evaluate(
210  const char *args_expression,
211  const char *kwargs_expression,
212  const char *module_name,
213  PY_OpaqueObject& out_args,
214  PY_OpaqueObject& out_kwargs ) const;
215 
216  PY_OpaqueObject evaluateExpression(const char *expression, PY_Result &result) const;
217  // This version use an input module to evaluate the expression
218  PY_OpaqueObject evaluateExpression(
219  const char *expression, const char* module_name, PY_Result &result) const;
220 
221  PY_CallbackCompiledCodeCache *myExpressionCache;
222 
223  static PY_CallbackHOM *theHOM;
224 };
225 
226 #endif
virtual ~PY_CallbackHOM()
Definition: PY_Callback.h:58
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
const GLdouble * v
Definition: glcorearb.h:837
int64 exint
Definition: SYS_Types.h:125
PY_Callback & operator=(PY_Callback &&callback) noexcept
Definition: PY_Callback.h:108
**But if you need a result
Definition: thread.h:622
static void setHOM(PY_CallbackHOM *hom)
Definition: PY_Callback.h:194
GLdouble n
Definition: glcorearb.h:2008
static PY_CallbackHOM * getHOM()
Definition: PY_Callback.h:197
UT_StringMap< PY_CompiledCode * > myExpressionCache
Definition: PY_Callback.h:45
PY_Result evaluateArgs(const char *args_expression, const char *kwargs_expression, const char *module_name, PY_OpaqueObject &out_args, PY_OpaqueObject &out_kwargs, ArgHandler arg_handler) const
Definition: PY_Callback.h:169
PY_Callback(void *python_callable_object, bool new_ref=false) noexcept
Definition: PY_Callback.h:86
PY_Callback(std::nullptr_t n) noexcept
Definition: PY_Callback.h:80
PY_EvaluationContext myContext
Definition: PY_Callback.h:44
PY_PyObject
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define PY_API
Definition: PY_API.h:10
long long int64
Definition: SYS_Types.h:116
std::function< T > UT_Function
Definition: UT_Function.h:37
PY_Callback & operator=(const PY_Callback &callback) noexcept
Definition: PY_Callback.h:97
PY_OpaqueObject & operator=(const PY_OpaqueObject &opaque_object) noexcept
PY_Callback(const PY_Callback &callback) noexcept
Definition: PY_Callback.h:93
PY_Result call(const char *args_expression, const char *kwargs_expression, const char *module_name, PY_Result::Type desired_result_type, PY_OpaqueObject &out_args, PY_OpaqueObject &out_kwargs, ArgHandler arg_handler) const
Definition: PY_Callback.h:145
PY_Callback() noexcept
Definition: PY_Callback.h:78
void setExpressionCache(PY_CallbackCompiledCodeCache *c)
Definition: PY_Callback.h:191
GLuint GLfloat * val
Definition: glcorearb.h:1608
**If you just want to fire and args
Definition: thread.h:618
PY_Callback(PY_Callback &&callback) noexcept
Definition: PY_Callback.h:103
UT_Function< void(PY_PyObject *args, PY_PyObject *kwargs)> ArgHandler
Definition: PY_Callback.h:76
Type myResultType
Definition: PY_Result.h:45
PY_Result call(const char *args_expression=NULL, const char *kwargs_expression=NULL) const
Definition: PY_Callback.h:126
PY_PyObject * pyObject() const noexcept