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>
76 template <
typename Ptr>
79 typedef hboost::python::objects::pointer_holder<Ptr, Pointee>
Holder;
80 typedef std::pair<PyObject*, bool>
Result;
88 if (!p.GetUniqueIdentifier())
89 return Result(hboost::python::detail::none(),
false);
102 PyObject *res = hboost::python::objects::make_ptr_instance
106 return Result(res, res != Py_None);
110 namespace Tf_PyDefHelpers {
112 using namespace hboost::python;
114 template <
typename Ptr>
120 template <
typename U>
130 template <
typename PtrType>
133 PtrType p = extract<PtrType>(
self);
135 }
catch (hboost::python::error_already_set
const &) {
141 template <
typename PtrType>
143 return !_IsPtrExpired<PtrType>(
self);
146 template <
typename PtrType>
148 PtrType
const &other) {
return self == other; }
149 template <
typename PtrType>
151 PtrType
const &other) {
return self != other; }
152 template <
typename PtrType>
154 PtrType
const &other) {
return self < other; }
158 template <
class PtrType>
160 static void Apply(PtrType
const &, PyObject *) { }
165 template <
typename T>
180 static void *convertible(PyObject *p) {
183 void *
result = converter::get_lvalue_from_python
184 (p, converter::registered<Pointee>::converters);
188 static void construct(PyObject*
source, converter::
189 rvalue_from_python_stage1_data*
data) {
190 void*
const storage = ((converter::rvalue_from_python_storage<Ptr>*)
191 data)->storage.bytes;
193 if (data->convertible == source)
196 Ptr
ptr(static_cast<Pointee*>(data->convertible));
210 template <
typename PtrType>
215 type_id<TfAnyWeakPtr>());
221 void *
result = converter::get_lvalue_from_python
222 (p, converter::registered
228 rvalue_from_python_stage1_data* data) {
229 void*
const storage = ((converter::rvalue_from_python_storage
232 if (data->convertible == source)
236 T *
ptr =
static_cast<T*
>(data->convertible);
237 PtrType smartPtr(ptr);
245 template <
typename Ptr>
250 to_python_converter<ConstPtr, _ConstPtrToPython<Ptr> >();
253 return incref(
object(TfConst_cast<NonConstPtr>(p)).
ptr());
257 template <
typename Ptr>
260 to_python_converter<Ptr, _PtrToPython<Ptr> >();
271 template <
typename SrcPtr,
typename DstPtr>
274 to_python_converter<SrcPtr, _ConvertPtrToPython<SrcPtr, DstPtr> >();
278 return incref(
object(dst).
ptr());
282 template <
typename Ptr>
293 Ptr
const &p = *
static_cast<Ptr
const *
>(
x);
296 if (ret.first == Py_None) {
298 Py_DECREF(ret.first);
299 ret.first = _originalConverter(x);
307 template <
typename T>
308 converter::to_python_function_t
309 _PtrToPythonWrapper<T>::_originalConverter = 0;
312 friend class def_visitor_access;
314 template <
typename WrapperPtrType,
typename Wrapper,
typename T>
316 _RegisterConversionsHelper<WrapperPtrType, Wrapper, T>();
319 template <
typename WrapperPtrType,
typename Wrapper,
typename T>
322 static_assert(std::is_same<
325 "Pointee must be same type as Wrapper.");
337 implicitly_convertible<PtrType,
351 converter::registration *
r =
const_cast<converter::registration *
>
355 _originalConverter = r->m_to_python;
369 template <
typename PtrType,
typename CLS,
typename Wrapper,
typename T>
374 c.add_property(
"expired", _IsPtrExpired<UnwrappedPtrType>,
376 "True if this object has expired, False otherwise.");
377 c.def(
"__bool__", _IsPtrValid<UnwrappedPtrType>,
379 "True if this object has not expired. False otherwise.");
380 c.def(
"__eq__", _ArePtrsEqual<UnwrappedPtrType>,
381 "Equality operator: x == y");
382 c.def(
"__ne__", _ArePtrsNotEqual<UnwrappedPtrType>,
383 "Non-equality operator: x != y");
384 c.def(
"__lt__", _ArePtrsLessThan<UnwrappedPtrType>,
385 "Less than operator: x < y");
389 template <
typename CLS>
391 typedef typename CLS::wrapped_type
Type;
392 typedef typename CLS::metadata::held_type_arg PtrType;
394 "Type must support TfWeakPtr.");
396 _RegisterConversions<PtrType>
397 ((Type *)0, detail::unwrap_wrapper((Type *)0));
400 Tf_RegisterPythonObjectFinder<Type, PtrType>();
403 _AddAPI<PtrType>(c, (Type *)0, detail::unwrap_wrapper((Type *)0));
408 friend class def_visitor_access;
410 template <
typename CLS,
typename Wrapper,
typename T>
419 template <
typename CLS>
421 typedef typename CLS::wrapped_type
Type;
423 "Type must support TfRefPtr.");
426 _AddAPI<CLS>((Type *)0, detail::unwrap_wrapper((Type *)0));
437 #endif // PXR_BASE_TF_PY_PTR_HELPERS_H
static PyObject * convert(Ptr const &p)
hboost::add_const< Pointee >::type ConstPointee
static Result Execute(Ptr const &p)
static void construct(PyObject *source, converter::rvalue_from_python_stage1_data *data)
hboost::remove_const< Pointee >::type NonConstPointee
Rebind< NonConstPointee >::Type NonConstPtr
getFileOption("OpenEXR:storage") storage
static void _RegisterConversionsHelper()
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)