24 #ifndef PXR_BASE_TF_PY_PTR_HELPERS_H
25 #define PXR_BASE_TF_PY_PTR_HELPERS_H
43 #include <hboost/python/class.hpp>
44 #include <hboost/python/converter/from_python.hpp>
45 #include <hboost/python/converter/registered.hpp>
46 #include <hboost/python/converter/registrations.hpp>
47 #include <hboost/python/converter/registry.hpp>
48 #include <hboost/python/converter/rvalue_from_python_data.hpp>
49 #include <hboost/python/converter/to_python_function_type.hpp>
50 #include <hboost/python/def_visitor.hpp>
51 #include <hboost/python/handle.hpp>
52 #include <hboost/python/implicit.hpp>
53 #include <hboost/python/to_python_converter.hpp>
56 #include <type_traits>
77 template <
typename Ptr>
80 typedef hboost::python::objects::pointer_holder<Ptr, Pointee>
Holder;
81 typedef std::pair<PyObject*, bool>
Result;
89 if (!p.GetUniqueIdentifier())
90 return Result(hboost::python::detail::none(),
false);
103 PyObject *res = hboost::python::objects::make_ptr_instance
107 return Result(res, res != Py_None);
111 namespace Tf_PyDefHelpers {
113 using namespace hboost::python;
115 template <
typename Ptr>
121 template <
typename U>
131 template <
typename PtrType>
134 PtrType p = extract<PtrType>(
self);
136 }
catch (hboost::python::error_already_set
const &) {
142 template <
typename PtrType>
144 return !_IsPtrExpired<PtrType>(
self);
147 template <
typename PtrType>
149 PtrType
const &other) {
return self == other; }
150 template <
typename PtrType>
152 PtrType
const &other) {
return self != other; }
153 template <
typename PtrType>
155 PtrType
const &other) {
return self < other; }
159 template <
class PtrType>
161 static void Apply(PtrType
const &, PyObject *) { }
166 template <
typename T>
181 static void *convertible(PyObject *p) {
184 void *
result = converter::get_lvalue_from_python
185 (p, converter::registered<Pointee>::converters);
189 static void construct(PyObject*
source, converter::
190 rvalue_from_python_stage1_data*
data) {
191 void*
const storage = ((converter::rvalue_from_python_storage<Ptr>*)
192 data)->storage.bytes;
194 if (data->convertible == source)
197 Ptr
ptr(static_cast<Pointee*>(data->convertible));
211 template <
typename PtrType>
216 type_id<TfAnyWeakPtr>());
222 void *
result = converter::get_lvalue_from_python
223 (p, converter::registered
229 rvalue_from_python_stage1_data* data) {
230 void*
const storage = ((converter::rvalue_from_python_storage
233 if (data->convertible == source)
237 T *
ptr =
static_cast<T*
>(data->convertible);
238 PtrType smartPtr(ptr);
246 template <
typename Ptr>
251 to_python_converter<ConstPtr, _ConstPtrToPython<Ptr> >();
254 return incref(
object(TfConst_cast<NonConstPtr>(p)).
ptr());
258 template <
typename Ptr>
261 to_python_converter<Ptr, _PtrToPython<Ptr> >();
272 template <
typename SrcPtr,
typename DstPtr>
275 to_python_converter<SrcPtr, _ConvertPtrToPython<SrcPtr, DstPtr> >();
279 return incref(
object(dst).
ptr());
283 template <
typename Ptr>
294 Ptr
const &p = *
static_cast<Ptr
const *
>(
x);
297 if (ret.first == Py_None) {
299 Py_DECREF(ret.first);
300 ret.first = _originalConverter(x);
308 template <
typename T>
309 converter::to_python_function_t
310 _PtrToPythonWrapper<T>::_originalConverter = 0;
313 friend class def_visitor_access;
315 template <
typename WrapperPtrType,
typename Wrapper,
typename T>
317 _RegisterConversionsHelper<WrapperPtrType, Wrapper, T>();
320 template <
typename WrapperPtrType,
typename Wrapper,
typename T>
323 static_assert(std::is_same<
326 "Pointee must be same type as Wrapper.");
338 implicitly_convertible<PtrType,
352 converter::registration *
r =
const_cast<converter::registration *
>
356 _originalConverter = r->m_to_python;
370 template <
typename PtrType,
typename CLS,
typename Wrapper,
typename T>
375 c.add_property(
"expired", _IsPtrExpired<UnwrappedPtrType>,
377 "True if this object has expired, False otherwise.");
378 c.def(
"__bool__", _IsPtrValid<UnwrappedPtrType>,
380 "True if this object has not expired. False otherwise.");
381 c.def(
"__eq__", _ArePtrsEqual<UnwrappedPtrType>,
382 "Equality operator: x == y");
383 c.def(
"__ne__", _ArePtrsNotEqual<UnwrappedPtrType>,
384 "Non-equality operator: x != y");
385 c.def(
"__lt__", _ArePtrsLessThan<UnwrappedPtrType>,
386 "Less than operator: x < y");
390 template <
typename CLS>
392 typedef typename CLS::wrapped_type
Type;
393 typedef typename CLS::metadata::held_type_arg PtrType;
395 "Type must support TfWeakPtr.");
397 _RegisterConversions<PtrType>
398 ((Type *)0, detail::unwrap_wrapper((Type *)0));
401 Tf_RegisterPythonObjectFinder<Type, PtrType>();
404 _AddAPI<PtrType>(
c, (Type *)0, detail::unwrap_wrapper((Type *)0));
409 friend class def_visitor_access;
411 template <
typename CLS,
typename Wrapper,
typename T>
420 template <
typename CLS>
422 typedef typename CLS::wrapped_type
Type;
424 "Type must support TfRefPtr.");
427 _AddAPI<CLS>((Type *)0, detail::unwrap_wrapper((Type *)0));
438 #endif // PXR_BASE_TF_PY_PTR_HELPERS_H
static PyObject * convert(Ptr const &p)
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)
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
hboost::python::objects::pointer_holder< Ptr, Pointee > Holder
_PtrInterface< Ptr >::Pointee Pointee
static void Apply(TfRefPtr< T > const &p, PyObject *obj)
Ptr::template Rebind< U >::Type Type
#define PXR_NAMESPACE_CLOSE_SCOPE
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)