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_CPythonAPI.h"
18 #include "PY_InterpreterAutoLock.h"
19 #include "PY_OpaqueObject.h"
20 #include "PY_Result.h"
21 
23 {
24 public:
25  using ArgHandler = std::function<void (PY_PyObject* args, PY_PyObject* kwargs)>;
26 
27  // Construct instances of this class with an opaque PY_PyObject
28  // containing a callable Python object.
29  PY_Callback(void *python_callable_object)
30  : PY_OpaqueObject(python_callable_object)
31  {}
32 
33  PY_Callback(const PY_Callback &callback)
34  : PY_OpaqueObject(callback)
35  {}
36 
37  PY_Callback &operator=(const PY_Callback &callback)
38  { return (PY_Callback &)PY_OpaqueObject::operator=(callback); }
39 
40  // Given two strings containing Python expressions (one that evaluates to
41  // a tuple of arguments and another that evaluates to a dictionary of
42  // keyword arguments), call the callback. Either or both of the argument
43  // tuple and keyword argument dictionary may be null. The result is
44  // returned in the PY_Result object.
45  void call(PY_Result &result, const char *args_expression=NULL,
46  const char *kwargs_expression=NULL) const;
47 
48  // This convenience version of call() returns the PY_Result by value,
49  // instead of passing it in by reference. This versions can be used by
50  // code that isn't performance-critical.
51  PY_Result call(const char *args_expression=NULL,
52  const char *kwargs_expression=NULL) const
53  {
55  call(result, args_expression, kwargs_expression);
56  return result;
57  }
58 
59  // For callbacks returning values
60  PY_Result call(
61  const char *args_expression,
62  const char *kwargs_expression,
63  PY_Result::Type desired_result_type ) const;
64 
65  // Similar to the above methods but take a lambda function to add elements to
66  // the evaluated expressions before firing the callback. Also takes a module name
67  // to evaluate the expressions, the module is optional and can be set to nullptr if
68  // not required.
69  //
71  const char* args_expression,
72  const char* kwargs_expression,
73  const char* module_name,
74  PY_Result::Type desired_result_type,
75  PY_PyObject*& out_args,
76  PY_PyObject*& out_kwargs,
77  ArgHandler arg_handler ) const
78  {
79  PY_InterpreterAutoLock auto_lock;
80 
81  PY_Result result = evaluateArgs(
82  args_expression, kwargs_expression, module_name, out_args, out_kwargs, arg_handler);
83  if (result.myResultType == PY_Result::ERR)
84  {
85  return result;
86  }
87 
88  return call(out_args, out_kwargs, desired_result_type);
89  }
90 
91  // Evaluates argument expressions and use a lambda function for
92  // adding extra elements to the arg and kwarg objects.
94  const char* args_expression,
95  const char* kwargs_expression,
96  const char* module_name,
97  PY_PyObject*& out_args,
98  PY_PyObject*& out_kwargs,
99  ArgHandler arg_handler ) const
100  {
101  PY_InterpreterAutoLock auto_lock;
102 
103  PY_Result result = evaluate(
104  args_expression, kwargs_expression, module_name, out_args, out_kwargs);
105  if (result.myResultType == PY_Result::ERR)
106  {
107  return result;
108  }
109 
110  arg_handler(out_args, out_kwargs);
111 
112  return result;
113  }
114 
115  private:
116  // Performs a call with pre-evaluated args and kwargs
117  PY_Result call(
118  PY_PyObject * args,
119  PY_PyObject * kwargs,
120  PY_Result::Type desired_result_type=PY_Result::NONE ) const;
121 
122  // Evaluates positional and keyworded arguments. The returned arguments can
123  // be expanded with other values before performing a call.
124  PY_Result evaluate(
125  const char *args_expression,
126  const char *kwargs_expression,
127  const char *module_name,
128  PY_PyObject*& out_args,
129  PY_PyObject*& out_kwargs ) const;
130 
131  PY_PyObject* evaluateExpression(const char *expression, PY_Result &result) const;
132  // This version use an input module to evaluate the expression
133  PY_PyObject* evaluateExpression(
134  const char *expression, const char* module_name, PY_Result &result) const;
135 };
136 
137 #endif
PY_Callback(void *python_callable_object)
Definition: PY_Callback.h:29
const Args & args
Definition: printf.h:628
PY_Result call(const char *args_expression, const char *kwargs_expression, const char *module_name, PY_Result::Type desired_result_type, PY_PyObject *&out_args, PY_PyObject *&out_kwargs, ArgHandler arg_handler) const
Definition: PY_Callback.h:70
PY_Callback & operator=(const PY_Callback &callback)
Definition: PY_Callback.h:37
PY_PyObject
#define PY_API
Definition: PY_API.h:10
PY_OpaqueObject & operator=(const PY_OpaqueObject &opaque_object)
std::function< void(PY_PyObject *args, PY_PyObject *kwargs)> ArgHandler
Definition: PY_Callback.h:25
PY_Result evaluateArgs(const char *args_expression, const char *kwargs_expression, const char *module_name, PY_PyObject *&out_args, PY_PyObject *&out_kwargs, ArgHandler arg_handler) const
Definition: PY_Callback.h:93
GLuint64EXT * result
Definition: glew.h:14007
PY_Callback(const PY_Callback &callback)
Definition: PY_Callback.h:33
Type myResultType
Definition: PY_Result.h:47
PY_Result call(const char *args_expression=NULL, const char *kwargs_expression=NULL) const
Definition: PY_Callback.h:51