7 #ifndef PXR_USD_SDF_PY_SPEC_H
8 #define PXR_USD_SDF_PY_SPEC_H
52 #include "pxr/external/boost/python/def_visitor.hpp"
53 #include "pxr/external/boost/python/dict.hpp"
54 #include "pxr/external/boost/python/errors.hpp"
55 #include "pxr/external/boost/python/raw_function.hpp"
56 #include "pxr/external/boost/python/pointee.hpp"
57 #include "pxr/external/boost/python/to_python_converter.hpp"
58 #include "pxr/external/boost/python/tuple.hpp"
70 #include <type_traits>
72 namespace PXR_BOOST_NAMESPACE {
87 namespace Sdf_PySpecDetail {
89 namespace bp = pxr_boost::python;
93 template <
typename CTOR>
96 NewVisitor(
const std::string &doc = std::string()) : _doc(doc) {}
98 template <
typename CLS>
113 if (PyObject_HasAttrString(c.ptr(),
"__new__"))
114 c.attr(
"__new__") = c.attr(
"__new__");
115 c.def(
"__new__", CTOR::template __new__<CLS>, _doc.c_str());
116 c.staticmethod(
"__new__");
118 c.def(
"__init__", bp::raw_function(_DummyInit));
121 template <
class CLS,
class Options>
136 if (PyObject_HasAttrString(c.ptr(),
"__new__"))
137 c.attr(
"__new__") = c.attr(
"__new__");
138 c.def(
"__new__", CTOR::template __new__<CLS>,
144 c.staticmethod(
"__new__");
146 c.def(
"__init__", bp::raw_function(_DummyInit));
150 const std::string _doc;
155 template <
typename SIG>
169 "Duplicate will be ignored.",
176 template <
typename SIG> SIG *CtorBase<SIG>::_func = 0;
182 template <
typename T>
191 namespace Sdf_PySpecDetail {
198 typedef PyObject* (*_HolderCreator)(
const SdfSpec&);
202 template <
class _SpecType>
208 bp::to_python_converter<ConstHandle, _ConstHandleToPython<SpecType> >();
211 return bp::incref(
bp::object(TfConst_cast<Handle>(p)).
ptr());
216 template <
class _SpecType,
class _Held,
class _Holder>
226 _originalConverter = _RegisterConverter<Handle>(&This::_Convert);
236 static PyObject* _Creator(
const SdfSpec& spec)
238 Handle x(Sdf_CastAccess::CastSpec<SpecType,SdfSpec>(spec));
239 return bp::objects::make_ptr_instance<SpecType, Holder>::execute(
x);
244 bp::converter::to_python_function_t
245 _RegisterConverter(bp::converter::to_python_function_t
f)
250 bp::converter::registration*
r =
251 const_cast<bp::converter::registration*
>(
254 bp::converter::to_python_function_t old = r->m_to_python;
267 static PyObject* _Convert(
const void* p)
274 static bp::converter::to_python_function_t _originalConverter;
276 template <
class SpecType,
class Held,
class Holder>
277 bp::converter::to_python_function_t
278 _HandleToPython<SpecType, Held, Holder>::_originalConverter = 0;
280 template <
class _SpecType>
288 bp::type_id<Handle>());
292 static void *convertible(PyObject *p)
297 bp::converter::get_lvalue_from_python(p,
298 bp::converter::registered<SpecType>::converters);
302 static void construct(PyObject*
source,
303 bp::converter::rvalue_from_python_stage1_data*
data)
306 ((bp::converter::rvalue_from_python_storage<Handle>*)
307 data)->storage.bytes;
309 if (data->convertible == source)
312 new (
storage)
Handle(*static_cast<SpecType*>(data->convertible));
319 template <
bool Abstract>
322 template<
typename CLS>
325 typedef typename CLS::metadata::held_type
HeldType;
332 const HeldType& held = bp::extract<const HeldType&>(
self);
385 template <
typename CLS>
388 typedef typename CLS::wrapped_type SpecType;
389 typedef typename CLS::metadata::held_type HeldType;
390 typedef typename CLS::metadata::held_type_arg HeldArgType;
391 typedef typename CLS::metadata::holder HolderType;
394 "HeldType must be SdfHandle<SpecType>.");
456 namespace Sdf_PySpecDetail
461 template <
typename R,
typename... Args>
469 typedef typename CLS::metadata::held_type HeldType;
471 HeldType specHandle(Base::_func(
args...));
473 bp::throw_error_already_set();
479 bp::detail::initialize_wrapper(result.ptr(),
get_pointer(specHandle));
481 bp::setattr(result,
"__class__", cls);
491 #endif // PXR_USD_SDF_PY_SPEC_H
SdfHandle< SpecType > Handle
Sdf_PySpecDetail::SpecVisitor< false > SdfPySpec()
static std::string Repr(const bp::object &self)
pxr_boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
getFileOption("OpenEXR:storage") storage
static bool __gt__(const HeldType &a, const HeldType &b)
GLsizei const GLfloat * value
SDF_API bp::object _DummyInit(bp::tuple const &, bp::dict const &)
CLS::wrapped_type SpecType
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
static bool __ne__(const HeldType &a, const HeldType &b)
GLboolean GLboolean GLboolean GLboolean a
SdfHandle< const SpecType > ConstHandle
**But if you need a result
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
PXR_NAMESPACE_OPEN_SCOPE TF_API bool TfPyConvertTfErrorsToPythonException(TfErrorMark const &m)
PyObject *(* _HolderCreator)(const SdfSpec &)
static bool IsValid(const HeldType &self)
Y * get_pointer(TfWeakPtrFacade< X, Y > const &p)
static bool IsExpired(const HeldType &self)
SDF_API std::string _SpecRepr(const bp::object &, const SdfSpec *)
static bool __le__(const HeldType &a, const HeldType &b)
CLS::metadata::holder HolderType
CLS::metadata::held_type_arg HeldArgType
Sdf_PySpecDetail::SpecVisitor< true > SdfPyAbstractSpecNoRepr()
CLS::metadata::held_type HeldType
TF_API bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
Sdf_PySpecDetail::NewVisitor< typename Sdf_PySpecDetail::NewCtor< T > > SdfMakePySpecConstructor(T *func, const std::string &doc=std::string())
SDF_API PyObject * _CreateHolder(const std::type_info &, const SdfSpec &)
GLsizei GLsizei GLchar * source
friend class bp::def_visitor_access
SDF_API void _RegisterHolderCreator(const std::type_info &, _HolderCreator)
void visit(CLS &c, char const *name, Options &options) const
GLuint const GLchar * name
static bool __eq__(const HeldType &a, const HeldType &b)
Sdf_PySpecDetail::SpecVisitor< true > SdfPyAbstractSpec()
GLboolean GLboolean GLboolean b
static size_t __hash__(const HeldType &self)
static bool __ge__(const HeldType &a, const HeldType &b)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
CtorBase< R(Args...)> Base
static bool __lt__(const HeldType &a, const HeldType &b)
static PyObject * convert(const Handle &x)
static bp::object __new__(bp::object &cls, Args...args)
_HandleToPython< SpecType, Handle, Holder > This
static PyObject * convert(ConstHandle const &p)
#define PXR_NAMESPACE_CLOSE_SCOPE
**If you just want to fire and args
OIIO_UTIL_API const char * c_str(string_view str)
NewVisitor(const std::string &doc=std::string())
Sdf_PySpecDetail::SpecVisitor< false > SdfPySpecNoRepr()
SdfHandle< SpecType > Handle
size_t hash_value(const CH_ChannelRef &ref)
static void SetFunc(Sig *func)
TF_API void TfPyThrowRuntimeError(const char *msg)
SpecVisitor(bool addRepr=true)