All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pyOverride.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_PY_OVERRIDE_H
8#define PXR_BASE_TF_PY_OVERRIDE_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/base/tf/pyLock.h"
13#include "pxr/base/tf/pyObjWrapper.h"
14
15#include "pxr/external/boost/python/override.hpp"
16#include "pxr/external/boost/python/type.hpp"
17
18PXR_NAMESPACE_OPEN_SCOPE
19
30{
31private:
33 friend class TfPyOverride;
34 explicit TfPyMethodResult(PyObject* x)
35 : m_obj(x)
36 {}
37
38public:
41
44
47
48 template <class T>
49 operator T()
50 {
51 TfPyLock lock;
52 pxr_boost::python::converter::return_from_python<T> converter;
53 return converter(m_obj.release());
54 }
55
56 template <class T>
57 operator T&() const
58 {
59 TfPyLock lock;
60 pxr_boost::python::converter::return_from_python<T&> converter;
61 return converter(
62 const_cast<pxr_boost::python::handle<>&>(m_obj).release());
63 }
64
65 template <class T>
66 T as(pxr_boost::python::type<T>* = 0)
67 {
68 TfPyLock lock;
69 pxr_boost::python::converter::return_from_python<T> converter;
70 return converter(m_obj.release());
71 }
72
73 template <class T>
74 T unchecked(pxr_boost::python::type<T>* = 0)
75 {
76 TfPyLock lock;
77 return pxr_boost::python::extract<T>(m_obj)();
78 }
79
80private:
81 mutable pxr_boost::python::handle<> m_obj;
82};
83
99{
100 // Helper to generate Py_BuildValue-style format strings at compile time.
101 template <typename Arg>
102 constexpr static char _PyObjArg()
103 {
104 return 'O';
105 }
106
107public:
109 TfPyOverride(pxr_boost::python::handle<> callable)
110 : TfPyObjWrapper(pxr_boost::python::object(callable))
111 {}
112
115 template <typename... Args>
117 operator()(Args const&... args) const
118 {
119 TfPyLock lock;
120 // Use the Args parameter pack together with the _PyObjArg helper to
121 // unpack the right number of 'O' elements into the format string.
122 static const char pyCallFormat[] =
123 { '(', _PyObjArg<Args>()..., ')', '\0' };
124
126 PyEval_CallFunction(
127 this->ptr(),
128 const_cast<char*>(pyCallFormat),
129 pxr_boost::python::converter::arg_to_python<Args>(args).get()...));
130 return x;
131 }
132};
133
134PXR_NAMESPACE_CLOSE_SCOPE
135
136#endif // PXR_BASE_TF_PY_OVERRIDE_H
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:105
A reimplementation of pxr_boost::python::detail::method_result.
Definition: pyOverride.h:30
TfPyMethodResult & operator=(TfPyMethodResult const &other)
Implement assign to do python refcounting while holding the GIL.
TfPyMethodResult(TfPyMethodResult const &other)
Implement copy to do python refcounting while holding the GIL.
~TfPyMethodResult()
Implement dtor to do python refcounting while holding the GIL.
Boost Python object wrapper.
Definition: pyObjWrapper.h:79
TF_API PyObject * ptr() const
Underlying PyObject* access.
A reimplementation of pxr_boost::python::override.
Definition: pyOverride.h:99
TfPyOverride(pxr_boost::python::handle<> callable)
Clients must hold the GIL to construct.
Definition: pyOverride.h:109
TfPyMethodResult operator()(Args const &... args) const
Call the override.
Definition: pyOverride.h:117