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  // Returns True if the viewer office is usable
66  virtual bool isValidViewerOffice(const BM_View& v) const = 0;
67 
68  virtual PY_PyObject *newNode(OP_Node &node) = 0;
69  virtual PY_PyObject *newSceneViewer(FUSE_Office &office, bool convert_context_viewer=false) = 0;
70  virtual PY_PyObject *newViewerStateContext(int64 val) = 0;
71  virtual PY_PyObject *newUIEvent(int64 val, int64 val2) = 0;
72  virtual PY_PyObject *newViewerEvent(int desktop, int office, int64 val, int64 val2) = 0;
73  virtual PY_PyObject *newCompositorViewer(FUSE_Office &office) = 0;
74 };
75 
77 {
78 public:
80 
81  PY_Callback() noexcept : PY_OpaqueObject()
82  {};
83  PY_Callback(std::nullptr_t n) noexcept : PY_OpaqueObject(n)
84  {};
85 
86  // Construct instances of this class with an opaque PY_PyObject
87  // containing a callable Python object.
88  explicit
89  PY_Callback(void *python_callable_object, bool new_ref = false) noexcept
90  : PY_OpaqueObject(python_callable_object, new_ref)
91  , myExpressionCache(nullptr)
92  {}
93 
94  /// Copy construct/assign
95  /// @{
96  PY_Callback(const PY_Callback &callback) noexcept
97  : PY_OpaqueObject(callback)
98  , myExpressionCache(nullptr)
99  {}
100  PY_Callback &operator=(const PY_Callback &callback) noexcept
101  { return (PY_Callback &)PY_OpaqueObject::operator=(callback); }
102  /// @}
103 
104  /// Move construct/assign
105  /// @{
106  PY_Callback(PY_Callback &&callback) noexcept
107  : PY_OpaqueObject(std::forward<PY_Callback>(callback))
108  , myExpressionCache(nullptr)
109  {
110  }
111  PY_Callback &operator=(PY_Callback &&callback) noexcept
112  {
114  std::forward<PY_Callback>(callback));
115  }
116  /// @}
117 
118  // Given two strings containing Python expressions (one that evaluates to
119  // a tuple of arguments and another that evaluates to a dictionary of
120  // keyword arguments), call the callback. Either or both of the argument
121  // tuple and keyword argument dictionary may be null. The result is
122  // returned in the PY_Result object.
123  void call(PY_Result &result, const char *args_expression=NULL,
124  const char *kwargs_expression=NULL) const;
125 
126  // This convenience version of call() returns the PY_Result by value,
127  // instead of passing it in by reference. This versions can be used by
128  // code that isn't performance-critical.
129  PY_Result call(const char *args_expression=NULL,
130  const char *kwargs_expression=NULL) const
131  {
133  call(result, args_expression, kwargs_expression);
134  return result;
135  }
136 
137  // For callbacks returning values
138  PY_Result call(
139  const char *args_expression,
140  const char *kwargs_expression,
141  PY_Result::Type desired_result_type ) const;
142 
143  // Similar to the above methods but take a lambda function to add elements to
144  // the evaluated expressions before firing the callback. Also takes a module name
145  // to evaluate the expressions, the module is optional and can be set to nullptr if
146  // not required.
147  //
149  const char* args_expression,
150  const char* kwargs_expression,
151  const char* module_name,
152  PY_Result::Type desired_result_type,
153  PY_OpaqueObject& out_args,
154  PY_OpaqueObject& out_kwargs,
155  ArgHandler arg_handler ) const
156  {
157  PY_InterpreterAutoLock auto_lock;
158 
159  PY_Result result = evaluateArgs(
160  args_expression, kwargs_expression, module_name, out_args, out_kwargs, arg_handler);
161  if (result.myResultType == PY_Result::ERR)
162  {
163  return result;
164  }
165 
166  return call(out_args.pyObject(), out_kwargs.pyObject(),
167  desired_result_type);
168  }
169 
170  // Evaluates argument expressions and use a lambda function for
171  // adding extra elements to the arg and kwarg objects.
173  const char* args_expression,
174  const char* kwargs_expression,
175  const char* module_name,
176  PY_OpaqueObject& out_args,
177  PY_OpaqueObject& out_kwargs,
178  ArgHandler arg_handler ) const
179  {
180  PY_InterpreterAutoLock auto_lock;
181 
182  PY_Result result = evaluate(
183  args_expression, kwargs_expression, module_name, out_args, out_kwargs);
184  if (result.myResultType == PY_Result::ERR)
185  {
186  return result;
187  }
188 
189  arg_handler(out_args.pyObject(), out_kwargs.pyObject());
190 
191  return result;
192  }
193 
195  { myExpressionCache = c; }
196 
197  static void setHOM(PY_CallbackHOM *hom)
198  { theHOM = hom; }
199 
201  { return theHOM; }
202 
203 private:
204  // Performs a call with pre-evaluated args and kwargs
205  PY_Result call(
206  PY_PyObject * args,
207  PY_PyObject * kwargs,
208  PY_Result::Type desired_result_type=PY_Result::NONE ) const;
209 
210  // Evaluates positional and keyworded arguments. The returned arguments can
211  // be expanded with other values before performing a call.
212  PY_Result evaluate(
213  const char *args_expression,
214  const char *kwargs_expression,
215  const char *module_name,
216  PY_OpaqueObject& out_args,
217  PY_OpaqueObject& out_kwargs ) const;
218 
219  PY_OpaqueObject evaluateExpression(const char *expression, PY_Result &result) const;
220  // This version use an input module to evaluate the expression
221  PY_OpaqueObject evaluateExpression(
222  const char *expression, const char* module_name, PY_Result &result) const;
223 
224  PY_CallbackCompiledCodeCache *myExpressionCache;
225 
226  static PY_CallbackHOM *theHOM;
227 };
228 
229 #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:111
**But if you need a result
Definition: thread.h:622
static void setHOM(PY_CallbackHOM *hom)
Definition: PY_Callback.h:197
GLdouble n
Definition: glcorearb.h:2008
static PY_CallbackHOM * getHOM()
Definition: PY_Callback.h:200
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:172
PY_Callback(void *python_callable_object, bool new_ref=false) noexcept
Definition: PY_Callback.h:89
PY_Callback(std::nullptr_t n) noexcept
Definition: PY_Callback.h:83
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:100
PY_OpaqueObject & operator=(const PY_OpaqueObject &opaque_object) noexcept
PY_Callback(const PY_Callback &callback) noexcept
Definition: PY_Callback.h:96
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:148
PY_Callback() noexcept
Definition: PY_Callback.h:81
void setExpressionCache(PY_CallbackCompiledCodeCache *c)
Definition: PY_Callback.h:194
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:106
UT_Function< void(PY_PyObject *args, PY_PyObject *kwargs)> ArgHandler
Definition: PY_Callback.h:79
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:129
PY_PyObject * pyObject() const noexcept