25 #if !HBOOST_PP_IS_ITERATING
27 #ifndef PXR_BASE_TF_MAKE_PY_CONSTRUCTOR_H
28 #define PXR_BASE_TF_MAKE_PY_CONSTRUCTOR_H
35 # define TF_MAX_ARITY 7
36 #endif // TF_MAX_ARITY
51 #include <hboost/preprocessor/iterate.hpp>
52 #include <hboost/preprocessor/punctuation/comma_if.hpp>
53 #include <hboost/preprocessor/repetition/enum.hpp>
54 #include <hboost/preprocessor/repetition/enum_binary_params.hpp>
55 #include <hboost/preprocessor/repetition/enum_params.hpp>
56 #include <hboost/preprocessor/repetition/enum_trailing_binary_params.hpp>
57 #include <hboost/preprocessor/repetition/enum_trailing_params.hpp>
58 #include <hboost/preprocessor/repetition/repeat.hpp>
59 #include <hboost/preprocessor/seq/for_each.hpp>
60 #include <hboost/python/def_visitor.hpp>
61 #include <hboost/python/dict.hpp>
62 #include <hboost/python/errors.hpp>
63 #include <hboost/python/list.hpp>
64 #include <hboost/python/object/iterator.hpp>
65 #include <hboost/python/raw_function.hpp>
66 #include <hboost/python/tuple.hpp>
67 #include <hboost/python/type_id.hpp>
109 namespace Tf_MakePyConstructor {
111 namespace bp = hboost::python;
113 template <
typename CTOR>
119 template <
typename CLS>
121 c.def(
"__init__", CTOR::template init_callable<CLS>(),
_doc.c_str());
124 template <
class CLS,
class Options>
125 void visit(CLS &c,
char const*
name, Options& options)
const {
127 c.def(name, CTOR::template init_callable<CLS>(options),
_doc.c_str());
136 template <
typename CTOR>
142 template <
typename CLS>
156 if (PyObject_HasAttrString(c.ptr(),
"__new__"))
157 c.attr(
"__new__") = c.attr(
"__new__");
158 c.def(
"__new__", CTOR::template __new__<CLS>,
_doc.c_str());
159 c.staticmethod(
"__new__");
162 c.def(
"__init__", bp::raw_function(
_DummyInit));
165 template <
class CLS,
class Options>
166 void visit(CLS &c,
char const*
name, Options& options)
const {
179 if (PyObject_HasAttrString(c.ptr(),
"__new__"))
180 c.attr(
"__new__") = c.attr(
"__new__");
181 c.def(
"__new__", CTOR::template __new__<CLS>,
187 c.staticmethod(
"__new__");
190 c.def(
"__init__", bp::raw_function(
_DummyInit));
198 template <
typename T>
205 template <
typename T>
208 "Type T must support refcount unique changed notification.");
210 const void *uniqueId) {
218 template <
typename CLS,
typename T>
222 typedef typename CLS::metadata::holder Holder;
223 typedef typename bp::objects::instance<Holder> instance_t;
225 typedef typename CLS::metadata::held_type HeldType;
230 allocate(
self.
ptr(), offsetof(instance_t,
storage),
sizeof(Holder));
233 Holder *holder = (
new (
memory) Holder(held));
236 bp::throw_error_already_set();
242 bp::detail::initialize_wrapper(
self.
ptr(), &(*(held.operator->())));
243 holder->install(
self.
ptr());
248 Policy::PostInstall(
self, t, held.GetUniqueIdentifier());
251 Holder::deallocate(
self.
ptr(), memory);
throw;
257 template <
typename WeakPtr,
typename P>
259 typedef std::remove_reference_t<P>
Ptr;
269 WeakPtr ptr(static_cast<typename WeakPtr::DataType *>
274 return bp::incref(Py_None);
279 Policy::PostInstall(result, p, ptr.GetUniqueIdentifier());
280 return bp::incref(result.ptr());
285 return hboost::python::objects::registered_class_object(
286 hboost::python::type_id<typename WeakPtr::DataType>()).get();
290 template <
typename WeakPtr =
void>
292 template <
typename FactoryResultPtr>
302 template <
typename SIG>
312 "Duplicate will be ignored.",
319 template <
typename SIG> SIG *CtorBase<SIG>::_func = 0;
328 #define HBOOST_PP_ITERATION_LIMITS (0, TF_MAX_ARITY)
329 #define HBOOST_PP_FILENAME_1 "pxr/base/tf/makePyConstructor.h"
330 #include HBOOST_PP_ITERATE()
338 template <
typename T>
348 template <
typename T>
358 template <
typename T>
368 template <
typename T>
378 template <
typename T>
389 template <
typename T =
void>
397 template <
typename T>
404 template <
typename T>
411 using namespace hboost::python;
416 hboost::python::list l;
417 for (
typename SeqType::const_iterator i = seq.begin();
418 i != seq.end(); ++i) {
419 l.append(
object(
handle<>(RefPtrFactory()(*i))));
421 return hboost::python::incref(l.ptr());
432 #endif // PXR_BASE_TF_MAKE_PY_CONSTRUCTOR_H
434 #else // HBOOST_PP_IS_ITERATING
436 #define N HBOOST_PP_ITERATION()
438 #define SIGNATURE R (HBOOST_PP_ENUM_PARAMS(N, A))
439 #define PARAMLIST HBOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, A, a)
440 #define ARGLIST HBOOST_PP_ENUM_PARAMS(N, a)
446 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
447 struct InitCtor<SIGNATURE> : CtorBase<SIGNATURE> {
448 typedef CtorBase<SIGNATURE> Base;
449 typedef typename Base::Sig Sig;
450 InitCtor(Sig *func) { Base::SetFunc(func); }
452 template <
typename CLS>
454 return bp::make_function(__init__<CLS>);
457 template <
typename CLS,
typename Options>
459 return bp::make_function(__init__<CLS>, o.policies(), o.keywords()) ;
462 template <
typename CLS>
463 static void __init__(
object &
self PARAMLIST) {
465 Install<CLS>(
self, Base::_func(ARGLIST), m);
469 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
470 struct NewCtor<SIGNATURE> : CtorBase<SIGNATURE> {
471 typedef CtorBase<SIGNATURE> Base;
472 typedef typename Base::Sig Sig;
473 NewCtor(Sig *func) { Base::SetFunc(func); }
476 static bp::object __new__(
object &cls PARAMLIST) {
477 typedef typename CLS::metadata::held_type HeldType;
479 R r((Base::_func(ARGLIST)));
482 bp::throw_error_already_set();
488 bp::detail::initialize_wrapper(ret.ptr(),
get_pointer(
h));
490 bp::setattr(ret,
"__class__", cls);
492 InstallPolicy<R>::PostInstall(ret,
r,
h.GetUniqueIdentifier());
497 #define VAR_SIGNATURE \
498 R (HBOOST_PP_ENUM_PARAMS(N, A) HBOOST_PP_COMMA_IF(N) \
499 const bp::tuple&, const bp::dict&)
501 #define FORMAT_STR(z, n, data) "%s, "
502 #define ARG_TYPE_STR_A(z, n, data) bp::type_id<A##n>().name()
504 #define EXTRACT_REQ_ARG_A(z, n, data) \
507 bp::extract<typename std::remove_reference_t<A##n>>(data[n + 1])
509 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
510 struct InitCtorWithVarArgs<VAR_SIGNATURE> : CtorBase<VAR_SIGNATURE> {
511 typedef CtorBase<VAR_SIGNATURE> Base;
512 typedef typename Base::Sig Sig;
513 InitCtorWithVarArgs(Sig *func) { Base::SetFunc(func); }
515 template <
typename CLS>
520 return bp::raw_function(__init__<CLS>, 1);
523 template <
typename CLS,
typename Options>
524 static bp::object init_callable(Options& options) {
527 return bp::raw_function(
528 bp::make_function(__init__<CLS>, options.policies()),
532 template <
typename CLS>
533 static bp::object __init__(
const bp::tuple&
args,
const bp::dict& kwargs) {
536 const unsigned int numArgs = bp::len(args);
537 if (numArgs - 1 <
N) {
542 "Arguments to __init__ did not match C++ signature:\n"
543 "\t__init__(" HBOOST_PP_REPEAT(
N, FORMAT_STR, 0)
"...)"
544 HBOOST_PP_COMMA_IF(
N) HBOOST_PP_ENUM(
N, ARG_TYPE_STR_A, 0)
557 HBOOST_PP_ENUM(
N, EXTRACT_REQ_ARG_A, args) HBOOST_PP_COMMA_IF(
N)
558 bp::tuple(args.slice(
N + 1, numArgs)), kwargs),
568 #define PARAMLIST HBOOST_PP_ENUM_BINARY_PARAMS(N, A, a)
576 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
577 struct InitCtorWithBackReference<SIGNATURE> : CtorBase<SIGNATURE> {
578 typedef CtorBase<SIGNATURE> Base;
579 typedef typename Base::Sig Sig;
580 InitCtorWithBackReference(Sig *func) { Base::SetFunc(func); }
582 template <
typename CLS>
584 return bp::make_function(__init__<CLS>);
587 template <
typename CLS,
typename Options>
589 return bp::make_function(__init__<CLS>, o.policies(), o.keywords());
592 template <
typename CLS>
593 static void __init__(PARAMLIST) {
595 Install<CLS>(a0, Base::_func(ARGLIST), m);
599 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
600 struct NewCtorWithClassReference<SIGNATURE> : CtorBase<SIGNATURE> {
601 typedef CtorBase<SIGNATURE> Base;
602 typedef typename Base::Sig Sig;
603 NewCtorWithClassReference(Sig *func) { Base::SetFunc(func); }
607 typedef typename CLS::metadata::held_type HeldType;
609 R r(Base::_func(ARGLIST));
612 bp::throw_error_already_set();
618 bp::detail::initialize_wrapper(ret.ptr(),
get_pointer(
h));
620 bp::setattr(ret,
"__class__", a0);
622 InstallPolicy<R>::PostInstall(ret,
r,
h.GetUniqueIdentifier());
635 #undef ARG_TYPE_STR_A
636 #undef EXTRACT_REQ_ARG_A
638 #endif // HBOOST_PP_IS_ITERATING
GLsizei GLenum GLsizei GLsizei GLuint memory
Tf_MakePyConstructor::NewVisitor< typename Tf_MakePyConstructor::NewCtor< T > > TfMakePyNew(T *func, const std::string &doc=std::string())
TF_API std::string TfStringPrintf(const char *fmt,...)
Tf_MakePyConstructor::InitVisitor< typename Tf_MakePyConstructor::InitCtor< T > > TfMakePyConstructor(T *func, const std::string &doc=std::string())
friend class bp::def_visitor_access
_RefPtrFactoryConverter< WeakPtrType, FactoryResultPtr > type
getFileOption("OpenEXR:storage") storage
typename std::conditional< B, T, F >::type conditional_t
GLsizei const GLchar *const * string
GLsizei const GLfloat * value
Tf_MakePyConstructor::InitVisitor< typename Tf_MakePyConstructor::InitCtorWithVarArgs< T > > TfMakePyConstructorWithVarArgs(T *func, const std::string &doc=std::string())
Tf_PySequenceToListConverterRefPtrFactory< T > type
PyObject * operator()(Ptr const &p) const
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
**But if you need a result
PyTypeObject const * get_pytype() const
PyTypeObject const * get_pytype() const
Tf_MakePyConstructor::InitVisitor< typename Tf_MakePyConstructor::InitCtorWithBackReference< T > > TfMakePyConstructorWithBackReference(T *func, const std::string &doc=std::string())
PXR_NAMESPACE_OPEN_SCOPE TF_API bool TfPyConvertTfErrorsToPythonException(TfErrorMark const &m)
Tf_MakePyConstructor::NewVisitor< typename Tf_MakePyConstructor::NewCtorWithClassReference< T > > TfMakePyNewWithClassReference(T *func, const std::string &doc=std::string())
Y * get_pointer(TfWeakPtrFacade< X, Y > const &p)
friend class bp::def_visitor_access
std::weak_ptr< T > WeakPtr
NewVisitor(const std::string &doc=std::string())
InitVisitor(const std::string &doc=std::string())
PyObject * operator()(T seq) const
TF_API void TfPyThrowTypeError(const char *msg)
hboost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
GLuint const GLchar * name
std::remove_reference_t< T > SeqType
void visit(CLS &c, char const *name, Options &options) const
GLfloat GLfloat GLfloat GLfloat h
std::enable_if_t< Tf_PyIsRefPtr< Ptr >::value > Tf_PySetPythonIdentity(Ptr const &, PyObject *)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
void Tf_PyAddPythonOwnership(Ptr const &t, const void *uniqueId, PyObject *obj)
TF_API bp::object _DummyInit(bp::tuple const &, bp::dict const &)
#define PXR_NAMESPACE_CLOSE_SCOPE
void Install(object const &self, T const &t, TfErrorMark const &m)
GA_API const UT_StringHolder N
**If you just want to fire and args
std::conditional_t< std::is_same< WeakPtr, void >::value, TfWeakPtr< typename FactoryResultPtr::DataType >, WeakPtr > WeakPtrType
static void SetFunc(Sig *func)
static void PostInstall(object const &self, TfRefPtr< T > const &ptr, const void *uniqueId)
static void PostInstall(object const &self, T const &t, const void *)
TF_API bool TfPyIsNone(hboost::python::object const &obj)
Return true iff obj is None.
TF_API void TfPyThrowRuntimeError(const char *msg)
void visit(CLS &c, char const *name, Options &options) const
std::remove_reference_t< P > Ptr