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