7 #ifndef PXR_BASE_TF_PY_PTR_HELPERS_H
8 #define PXR_BASE_TF_PY_PTR_HELPERS_H
26 #include "pxr/external/boost/python/class.hpp"
27 #include "pxr/external/boost/python/converter/from_python.hpp"
28 #include "pxr/external/boost/python/converter/registered.hpp"
29 #include "pxr/external/boost/python/converter/registrations.hpp"
30 #include "pxr/external/boost/python/converter/registry.hpp"
31 #include "pxr/external/boost/python/converter/rvalue_from_python_data.hpp"
32 #include "pxr/external/boost/python/converter/to_python_function_type.hpp"
33 #include "pxr/external/boost/python/def_visitor.hpp"
34 #include "pxr/external/boost/python/handle.hpp"
35 #include "pxr/external/boost/python/implicit.hpp"
36 #include "pxr/external/boost/python/to_python_converter.hpp"
39 #include <type_traits>
60 template <
typename Ptr>
63 typedef pxr_boost::python::objects::pointer_holder<Ptr, Pointee>
Holder;
64 typedef std::pair<PyObject*, bool>
Result;
72 if (!p.GetUniqueIdentifier())
86 PyObject *res = pxr_boost::python::objects::make_ptr_instance
90 return Result(res, res != Py_None);
94 namespace Tf_PyDefHelpers {
96 using namespace pxr_boost::python;
98 template <
typename Ptr>
104 template <
typename U>
114 template <
typename PtrType>
117 PtrType p = extract<PtrType>(
self);
119 }
catch (pxr_boost::python::error_already_set
const &) {
125 template <
typename PtrType>
127 return !_IsPtrExpired<PtrType>(
self);
130 template <
typename PtrType>
132 PtrType
const &other) {
return self == other; }
133 template <
typename PtrType>
135 PtrType
const &other) {
return self != other; }
136 template <
typename PtrType>
138 PtrType
const &other) {
return self < other; }
142 template <
class PtrType>
144 static void Apply(PtrType
const &, PyObject *) { }
149 template <
typename T>
164 static void *convertible(PyObject *p) {
167 void *
result = converter::get_lvalue_from_python
168 (p, converter::registered<Pointee>::converters);
172 static void construct(PyObject*
source, converter::
173 rvalue_from_python_stage1_data*
data) {
174 void*
const storage = ((converter::rvalue_from_python_storage<Ptr>*)
175 data)->storage.bytes;
177 if (data->convertible == source)
180 Ptr
ptr(static_cast<Pointee*>(data->convertible));
194 template <
typename PtrType>
199 type_id<TfAnyWeakPtr>());
205 void *
result = converter::get_lvalue_from_python
206 (p, converter::registered
212 rvalue_from_python_stage1_data* data) {
213 void*
const storage = ((converter::rvalue_from_python_storage
216 if (data->convertible == source)
220 T *
ptr =
static_cast<T*
>(data->convertible);
221 PtrType smartPtr(ptr);
229 template <
typename Ptr>
234 to_python_converter<ConstPtr, _ConstPtrToPython<Ptr> >();
237 return incref(
object(TfConst_cast<NonConstPtr>(p)).
ptr());
241 template <
typename Ptr>
244 to_python_converter<Ptr, _PtrToPython<Ptr> >();
255 template <
typename SrcPtr,
typename DstPtr>
258 to_python_converter<SrcPtr, _ConvertPtrToPython<SrcPtr, DstPtr> >();
262 return incref(
object(dst).
ptr());
266 template <
typename Ptr>
277 Ptr
const &p = *
static_cast<Ptr
const *
>(
x);
280 if (ret.first == Py_None) {
282 Py_DECREF(ret.first);
283 ret.first = _originalConverter(x);
291 template <
typename T>
292 converter::to_python_function_t
293 _PtrToPythonWrapper<T>::_originalConverter = 0;
296 friend class def_visitor_access;
298 template <
typename WrapperPtrType,
typename Wrapper,
typename T>
300 _RegisterConversionsHelper<WrapperPtrType, Wrapper, T>();
303 template <
typename WrapperPtrType,
typename Wrapper,
typename T>
306 static_assert(std::is_same<
309 "Pointee must be same type as Wrapper.");
321 implicitly_convertible<PtrType,
335 converter::registration *
r =
const_cast<converter::registration *
>
339 _originalConverter = r->m_to_python;
353 template <
typename PtrType,
typename CLS,
typename Wrapper,
typename T>
358 c.add_property(
"expired", _IsPtrExpired<UnwrappedPtrType>,
360 "True if this object has expired, False otherwise.");
361 c.def(
"__bool__", _IsPtrValid<UnwrappedPtrType>,
363 "True if this object has not expired. False otherwise.");
364 c.def(
"__eq__", _ArePtrsEqual<UnwrappedPtrType>,
365 "Equality operator: x == y");
366 c.def(
"__ne__", _ArePtrsNotEqual<UnwrappedPtrType>,
367 "Non-equality operator: x != y");
368 c.def(
"__lt__", _ArePtrsLessThan<UnwrappedPtrType>,
369 "Less than operator: x < y");
373 template <
typename CLS>
375 typedef typename CLS::wrapped_type
Type;
376 typedef typename CLS::metadata::held_type_arg PtrType;
378 "Type must support TfWeakPtr.");
380 _RegisterConversions<PtrType>
381 ((Type *)0, detail::unwrap_wrapper((Type *)0));
384 Tf_RegisterPythonObjectFinder<Type, PtrType>();
387 _AddAPI<PtrType>(
c, (Type *)0, detail::unwrap_wrapper((Type *)0));
392 friend class def_visitor_access;
394 template <
typename CLS,
typename Wrapper,
typename T>
403 template <
typename CLS>
405 typedef typename CLS::wrapped_type
Type;
407 "Type must support TfRefPtr.");
410 _AddAPI<CLS>((Type *)0, detail::unwrap_wrapper((Type *)0));
421 #endif // PXR_BASE_TF_PY_PTR_HELPERS_H
static PyObject * convert(Ptr const &p)
bool none(const vbool4 &v)
static Result Execute(Ptr const &p)
static void construct(PyObject *source, converter::rvalue_from_python_stage1_data *data)
Rebind< NonConstPointee >::Type NonConstPtr
getFileOption("OpenEXR:storage") storage
static void _RegisterConversionsHelper()
std::remove_const_t< Pointee > NonConstPointee
std::add_const_t< Pointee > ConstPointee
GLsizei const GLfloat * value
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
static void _RegisterConversions(Wrapper *, T *)
**But if you need a result
static PyObject * convert(ConstPtr const &p)
_PtrInterface< Ptr >::NonConstPtr NonConstPtr
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Y * get_pointer(TfWeakPtrFacade< X, Y > const &p)
static PyObject * Convert(void const *x)
static void Apply(PtrType const &, PyObject *)
#define TF_SUPPORTS_REFPTR(T)
#define TF_SUPPORTS_WEAKPTR(T)
static converter::to_python_function_t _originalConverter
std::weak_ptr< T > WeakPtr
static void _AddAPI(CLS &c, Wrapper *, T *)
bool _IsPtrValid(object const &self)
GLsizei GLsizei GLchar * source
_PtrInterface< Ptr >::ConstPtr ConstPtr
bool _ArePtrsLessThan(PtrType const &self, PtrType const &other)
bool _IsPtrExpired(object const &self)
bool _ArePtrsEqual(PtrType const &self, PtrType const &other)
pxr_boost::python::objects::pointer_holder< Ptr, Pointee > Holder
Rebind< ConstPointee >::Type ConstPtr
void Tf_PyRemovePythonOwnership(Ptr const &t, PyObject *obj)
bool _ArePtrsNotEqual(PtrType const &self, PtrType const &other)
std::enable_if_t< Tf_PyIsRefPtr< Ptr >::value > Tf_PySetPythonIdentity(Ptr const &, PyObject *)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
_PtrInterface< Ptr >::Pointee Pointee
static void Apply(TfRefPtr< T > const &p, PyObject *obj)
Ptr::template Rebind< U >::Type Type
#define PXR_NAMESPACE_CLOSE_SCOPE
OIIO_UTIL_API const char * c_str(string_view str)
PyObject * Tf_PyGetPythonIdentity(Ptr const &ptr)
static PyObject * convert(SrcPtr const &p)
static void _AddAPI(Wrapper *, T *)
std::pair< PyObject *, bool > Result
static void * convertible(PyObject *p)