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>
68 #include <hboost/type_traits/is_same.hpp>
69 #include <hboost/type_traits/remove_reference.hpp>
111 namespace Tf_MakePyConstructor {
113 namespace bp = hboost::python;
115 template <
typename CTOR>
121 template <
typename CLS>
123 c.def(
"__init__", CTOR::template init_callable<CLS>(),
_doc.c_str());
126 template <
class CLS,
class Options>
127 void visit(CLS &
c,
char const*
name, Options& options)
const {
129 c.def(name, CTOR::template init_callable<CLS>(options),
_doc.c_str());
138 template <
typename CTOR>
144 template <
typename CLS>
158 if (PyObject_HasAttrString(c.ptr(),
"__new__"))
159 c.attr(
"__new__") = c.attr(
"__new__");
160 c.def(
"__new__", CTOR::template __new__<CLS>,
_doc.c_str());
161 c.staticmethod(
"__new__");
164 c.def(
"__init__", bp::raw_function(
_DummyInit));
167 template <
class CLS,
class Options>
168 void visit(CLS &
c,
char const*
name, Options& options)
const {
181 if (PyObject_HasAttrString(c.ptr(),
"__new__"))
182 c.attr(
"__new__") = c.attr(
"__new__");
183 c.def(
"__new__", CTOR::template __new__<CLS>,
189 c.staticmethod(
"__new__");
192 c.def(
"__init__", bp::raw_function(
_DummyInit));
200 template <
typename T>
207 template <
typename T>
210 "Type T must support refcount unique changed notification.");
212 const void *uniqueId) {
220 template <
typename CLS,
typename T>
224 typedef typename CLS::metadata::holder Holder;
225 typedef typename bp::objects::instance<Holder> instance_t;
227 typedef typename CLS::metadata::held_type HeldType;
230 void *memory = Holder::
232 allocate(
self.
ptr(), offsetof(instance_t,
storage),
sizeof(Holder));
235 Holder *holder = (
new (memory) Holder(held));
238 bp::throw_error_already_set();
244 bp::detail::initialize_wrapper(
self.
ptr(), &(*(held.operator->())));
245 holder->install(
self.
ptr());
250 Policy::PostInstall(
self, t, held.GetUniqueIdentifier());
253 Holder::deallocate(
self.
ptr(), memory);
throw;
259 template <
typename WeakPtr,
typename P>
271 WeakPtr ptr(static_cast<typename WeakPtr::DataType *>
276 return bp::incref(Py_None);
281 Policy::PostInstall(result, p, ptr.GetUniqueIdentifier());
282 return bp::incref(result.ptr());
287 return hboost::python::objects::registered_class_object(
288 hboost::python::type_id<typename WeakPtr::DataType>()).get();
292 template <
typename WeakPtr =
void>
294 template <
typename FactoryResultPtr>
296 typedef typename hboost::mpl::if_<hboost::is_same<WeakPtr, void>,
303 template <
typename SIG>
313 "Duplicate will be ignored.",
320 template <
typename SIG> SIG *CtorBase<SIG>::_func = 0;
329 #define HBOOST_PP_ITERATION_LIMITS (0, TF_MAX_ARITY)
330 #define HBOOST_PP_FILENAME_1 "pxr/base/tf/makePyConstructor.h"
331 #include HBOOST_PP_ITERATE()
339 template <
typename T>
349 template <
typename T>
359 template <
typename T>
369 template <
typename T>
379 template <
typename T>
390 template <
typename T =
void>
398 template <
typename T>
405 template <
typename T>
412 using namespace hboost::python;
417 hboost::python::list
l;
418 for (
typename SeqType::const_iterator i = seq.begin();
419 i != seq.end(); ++i) {
420 l.append(
object(handle<>(RefPtrFactory()(*i))));
422 return hboost::python::incref(
l.ptr());
433 #endif // PXR_BASE_TF_MAKE_PY_CONSTRUCTOR_H
435 #else // HBOOST_PP_IS_ITERATING
437 #define N HBOOST_PP_ITERATION()
439 #define SIGNATURE R (HBOOST_PP_ENUM_PARAMS(N, A))
440 #define PARAMLIST HBOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, A, a)
441 #define ARGLIST HBOOST_PP_ENUM_PARAMS(N, a)
447 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
448 struct InitCtor<SIGNATURE> : CtorBase<SIGNATURE> {
449 typedef CtorBase<SIGNATURE> Base;
450 typedef typename Base::Sig Sig;
451 InitCtor(Sig *func) { Base::SetFunc(func); }
453 template <
typename CLS>
455 return bp::make_function(__init__<CLS>);
458 template <
typename CLS,
typename Options>
460 return bp::make_function(__init__<CLS>, o.policies(), o.keywords()) ;
463 template <
typename CLS>
464 static void __init__(
object &
self PARAMLIST) {
466 Install<CLS>(
self, Base::_func(ARGLIST),
m);
470 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
471 struct NewCtor<SIGNATURE> : CtorBase<SIGNATURE> {
472 typedef CtorBase<SIGNATURE> Base;
473 typedef typename Base::Sig Sig;
474 NewCtor(Sig *func) { Base::SetFunc(func); }
477 static bp::object __new__(
object &cls PARAMLIST) {
478 typedef typename CLS::metadata::held_type HeldType;
480 R r((Base::_func(ARGLIST)));
483 bp::throw_error_already_set();
489 bp::detail::initialize_wrapper(ret.ptr(),
get_pointer(
h));
491 bp::setattr(ret,
"__class__", cls);
493 InstallPolicy<R>::PostInstall(ret,
r,
h.GetUniqueIdentifier());
498 #define VAR_SIGNATURE \
499 R (HBOOST_PP_ENUM_PARAMS(N, A) HBOOST_PP_COMMA_IF(N) \
500 const bp::tuple&, const bp::dict&)
502 #define FORMAT_STR(z, n, data) "%s, "
503 #define ARG_TYPE_STR_A(z, n, data) bp::type_id<A##n>().name()
505 #define EXTRACT_REQ_ARG_A(z, n, data) \
508 bp::extract<typename hboost::remove_reference<A##n>::type>(data[n + 1])
510 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
511 struct InitCtorWithVarArgs<VAR_SIGNATURE> : CtorBase<VAR_SIGNATURE> {
512 typedef CtorBase<VAR_SIGNATURE> Base;
513 typedef typename Base::Sig Sig;
514 InitCtorWithVarArgs(Sig *func) { Base::SetFunc(func); }
516 template <
typename CLS>
521 return bp::raw_function(__init__<CLS>, 1);
524 template <
typename CLS,
typename Options>
525 static bp::object init_callable(Options& options) {
528 return bp::raw_function(
529 bp::make_function(__init__<CLS>, options.policies()),
533 template <
typename CLS>
534 static bp::object __init__(
const bp::tuple&
args,
const bp::dict& kwargs) {
537 const unsigned int numArgs =
bp::len(args);
538 if (numArgs - 1 <
N) {
543 "Arguments to __init__ did not match C++ signature:\n"
544 "\t__init__(" HBOOST_PP_REPEAT(
N, FORMAT_STR, 0)
"...)"
545 HBOOST_PP_COMMA_IF(
N) HBOOST_PP_ENUM(
N, ARG_TYPE_STR_A, 0)
558 HBOOST_PP_ENUM(
N, EXTRACT_REQ_ARG_A, args) HBOOST_PP_COMMA_IF(
N)
559 bp::tuple(args.slice(
N + 1, numArgs)), kwargs),
569 #define PARAMLIST HBOOST_PP_ENUM_BINARY_PARAMS(N, A, a)
577 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
578 struct InitCtorWithBackReference<SIGNATURE> : CtorBase<SIGNATURE> {
579 typedef CtorBase<SIGNATURE> Base;
580 typedef typename Base::Sig Sig;
581 InitCtorWithBackReference(Sig *func) { Base::SetFunc(func); }
583 template <
typename CLS>
585 return bp::make_function(__init__<CLS>);
588 template <
typename CLS,
typename Options>
590 return bp::make_function(__init__<CLS>, o.policies(), o.keywords());
593 template <
typename CLS>
594 static void __init__(PARAMLIST) {
596 Install<CLS>(a0, Base::_func(ARGLIST),
m);
600 template <
typename R HBOOST_PP_ENUM_TRAILING_PARAMS(N,
typename A)>
601 struct NewCtorWithClassReference<SIGNATURE> : CtorBase<SIGNATURE> {
602 typedef CtorBase<SIGNATURE> Base;
603 typedef typename Base::Sig Sig;
604 NewCtorWithClassReference(Sig *func) { Base::SetFunc(func); }
608 typedef typename CLS::metadata::held_type HeldType;
610 R r(Base::_func(ARGLIST));
613 bp::throw_error_already_set();
619 bp::detail::initialize_wrapper(ret.ptr(),
get_pointer(
h));
621 bp::setattr(ret,
"__class__", a0);
623 InstallPolicy<R>::PostInstall(ret,
r,
h.GetUniqueIdentifier());
636 #undef ARG_TYPE_STR_A
637 #undef EXTRACT_REQ_ARG_A
639 #endif // HBOOST_PP_IS_ITERATING
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
hboost::remove_reference< P >::type Ptr
_RefPtrFactoryConverter< WeakPtrType, FactoryResultPtr > type
hboost::remove_reference< T >::type SeqType
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)
hboost::enable_if< Tf_PyIsRefPtr< Ptr > >::type Tf_PySetPythonIdentity(Ptr const &, PyObject *)
GLuint const GLchar * name
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
GLsizei const GLchar *const * string
hboost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
void visit(CLS &c, char const *name, Options &options) const
GLfloat GLfloat GLfloat GLfloat h
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)
TF_API void TfPyThrowTypeError(std::string const &msg)
GA_API const UT_StringHolder N
**If you just want to fire and args
getOption("OpenEXR.storage") storage
static void SetFunc(Sig *func)
static void PostInstall(object const &self, TfRefPtr< T > const &ptr, const void *uniqueId)
hboost::mpl::if_< hboost::is_same< WeakPtr, void >, TfWeakPtr< typename FactoryResultPtr::DataType >, WeakPtr >::type WeakPtrType
TF_API void TfPyThrowRuntimeError(std::string const &msg)
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.
void visit(CLS &c, char const *name, Options &options) const