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