pyste, shared_ptr and constructor override
I use boost.python to export class which is enclosed in smart pointer and has protected constructor to force smart pointer usage, like that: class MyClass; typedef boost::shared_ptr<MyClass> MyClassPtr; class MyClass { public: static MyClassPtr Create(); virtual ~MyClass(); protected: MyClass(); <skip> }; So, after pyste has processed the code for MyClass I have something like that: class_< MyClass, bases< boost::noncopyable_::noncopyable > , boost::noncopyable >("MyClass", no_init) .def("Create", &MyClass::Create) .... register_ptr_to_python< boost::shared_ptr< space::http::SimpleGet > >(); So after compiling extension module I can use MyClass from python like that mc = MyClass.Create() But that's not exactly what I want, because I really meant that shared_ptr IS MyClass, so I want to write mc = MyClass() and have C++ function MyClass::Create being called instead. Of course, I can override this with Python (which rox), even procedurally, like that: def override_constructor(cls, cons, d): def override_func(cls, *args, **kwargs): return cons(*args, **kwargs) dc = {'__new__' : override_func} from new import classobj d[cls.__name__] = classobj(cls.__name__, (cls,), dc) override_constructor(MyClass, MyClass.Create, globals()) Is there a way to do the same thing with pyste at wrapper generation step?
Hi Vladmir, Vladimir Sukhoy wrote:
But that's not exactly what I want, because I really meant that shared_ptr IS MyClass, so I want to write
mc = MyClass()
and have C++ function MyClass::Create being called instead.
Of course, I can override this with Python (which rox), even procedurally, like that:
def override_constructor(cls, cons, d): def override_func(cls, *args, **kwargs): return cons(*args, **kwargs) dc = {'__new__' : override_func} from new import classobj d[cls.__name__] = classobj(cls.__name__, (cls,), dc)
override_constructor(MyClass, MyClass.Create, globals())
Is there a way to do the same thing with pyste at wrapper generation step?
Not right now, and I don't have any definite plans for it either. :/ I suggest you keep inserting the constructor like you are doing. Regards, Bruno.
participants (2)
-
Nicodemus -
Vladimir Sukhoy