manage_new_object using release() instead of delete
Hi all, I am new to Boost.Python, but so far have been extremely impressed. I was wondering if there was a version of the manage_new_object return policy that called a release() function on an interface rather than calling delete? I am trying to wrap a chunk of code that works like this: class Interface { public: virtual void doSomething() = 0; virtual void release() = 0; protected: virtual ~Interface() {} }; Interface * SomeFactory( ... ); Now, if Interface was a normal data struct, I'd just use manage_new_object as the policy for SomeFactory, but that wouldn't work here because the destructor is protected. Is there some way to have the release function called at the end of the Interface pointer's life in python? And is it further possible to have the release() function not be callable from python (to protect against double-deletes)? Thanks!
"Eric" <wstwej03@sneakemail.com> writes:
Hi all,
I am new to Boost.Python, but so far have been extremely impressed. I was wondering if there was a version of the manage_new_object return policy that called a release() function on an interface rather than calling delete?
I am trying to wrap a chunk of code that works like this:
class Interface {
public:
virtual void doSomething() = 0;
virtual void release() = 0;
protected:
virtual ~Interface() {}
};
Interface *
SomeFactory( ... );
Now, if Interface was a normal data struct, I'd just use manage_new_object as the policy for SomeFactory, but that wouldn't work here because the destructor is protected. Is there some way to have the release function called at the end of the Interface pointer's life in python? And is it further possible to have the release() function not be callable from python (to protect against double-deletes)? Thanks!
Solution: Put a thin wrapper around SomeFactory that builds a boost::shared_ptr using a custom deleter that calls release(), and wrap that. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
This has worked like a charm, thanks very much. In order to get it to work, I had do to two things which I thought might be candidates for improvement. The first is that I had to manually add some code to my BP initialization file to create a class_value_wrapper for each type I wanted to use shared_ptr with. Perhaps this is the kind of code that Pyste could be used to auto-generate? The second was that I had to use a special functor so that I could use a member-function pointer instead of a void function pointer for the custom delete. Both issues are illustrated below. I wonder if there is also something like ReleaseFunctor below in boost for use with shared_ptr...? Consider these two classes: // in MyClasses.h class ClassOne { public: void releaseOne() = 0; }; class ClassTwo { public: void releaseTwo() = 0; }; typedef boost::shared_ptr<ClassOne> ClassOnePtr; ClassOnePtr MakeClassOne(); // in boost_exports.cpp template<class T> class SharedPtrClassValueWrapper : public objects::class_value_wrapper< boost::shared_ptr<T> , objects::make_ptr_instance<T, objects::pointer_holder<boost::shared_ptr<T>,T> > > { }; BOOST_PYTHON_MODULE(myclasses) { SharedPtrClassValueWrapper<ClassOne>(); SharedPtrClassValueWrapper<ClassTwo>(); // etc. } // in myclasses.cpp template<class T> class ReleaseFunctor { void (T::*releaseFunc_)(); public: operator()( T * t ) { (t->*releaseFunc_)(); } ReleaseFunctor(void (T::*rf)()) : releaseFunc_(rf) { } }; template<class T> boost::shared_ptr<T> MakeSharedPtr(T *y, void (T::*rf)()) { ReleaseFunctor<T> functor(rf); boost::shared_ptr<T> ret( y, functor ); return ret; } ClassOne * MakeClassOne() { return MakeSharedPtr( new ConcreteImpl(), &ClassOne::releaseOne ); } -----Original Message----- From: c++-sig-bounces@python.org [mailto:c++-sig-bounces@python.org]On Behalf Of David Abrahams dave-at-boost-consulting.com |cppsig| Sent: Tuesday, April 13, 2004 8:27 AM To: eric.ries@aya.yale.edu Subject: [C++-sig] Re: manage_new_object using release() instead of delete "Eric" <wstwej03@sneakemail.com> writes:
Hi all,
I am new to Boost.Python, but so far have been extremely impressed. I was wondering if there was a version of the manage_new_object return policy that called a release() function on an interface rather than calling delete?
I am trying to wrap a chunk of code that works like this:
class Interface {
public:
virtual void doSomething() = 0;
virtual void release() = 0;
protected:
virtual ~Interface() {}
};
Interface *
SomeFactory( ... );
Now, if Interface was a normal data struct, I'd just use manage_new_object as the policy for SomeFactory, but that wouldn't work here because the destructor is protected. Is there some way to have the release function called at the end of the Interface pointer's life in python? And is it further possible to have the release() function not be callable from python (to protect against double-deletes)? Thanks!
Solution: Put a thin wrapper around SomeFactory that builds a boost::shared_ptr using a custom deleter that calls release(), and wrap that. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com _______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
"Eric" <wstwej03@sneakemail.com> writes:
This has worked like a charm, thanks very much.
In order to get it to work, I had do to two things which I thought might be candidates for improvement.
The first is that I had to manually add some code to my BP initialization file to create a class_value_wrapper for each type I wanted to use shared_ptr with.
Why are you messing around with that undocumented stuff? It's a Boost.Python library implementation detail and not for public consumption. Are you wrapping lots of functions that return these objects by value?
Perhaps this is the kind of code that Pyste could be used to auto-generate?
The second was that I had to use a special functor so that I could use a member-function pointer instead of a void function pointer for the custom delete. Both issues are illustrated below.
I wonder if there is also something like ReleaseFunctor below in boost for use with shared_ptr...?
boost::mem_fn(&T::release) <snip> [Please try to limit the amount of quoted text in your replies.] -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
No offense intended. I was using class_value_wrapper based on a post in the mailing list archives detailing that as a step to wrap functions that return boost::shared_ptr. Is there a better way? -----Original Message----- From: c++-sig-bounces@python.org [mailto:c++-sig-bounces@python.org]On Behalf Of David Abrahams dave-at-boost-consulting.com |cppsig| Sent: Friday, May 07, 2004 5:36 PM To: XXXXXXXXXXXXXXXXXXXXXX Subject: [C++-sig] Re: manage_new_object using release() instead of delete "Eric" <wstwej03@sneakemail.com> writes:
This has worked like a charm, thanks very much.
In order to get it to work, I had do to two things which I thought might be candidates for improvement.
The first is that I had to manually add some code to my BP initialization file to create a class_value_wrapper for each type I wanted to use shared_ptr with.
Why are you messing around with that undocumented stuff? It's a Boost.Python library implementation detail and not for public consumption. Are you wrapping lots of functions that return these objects by value?
Perhaps this is the kind of code that Pyste could be used to auto-generate?
The second was that I had to use a special functor so that I could use a member-function pointer instead of a void function pointer for the custom delete. Both issues are illustrated below.
I wonder if there is also something like ReleaseFunctor below in boost for use with shared_ptr...?
boost::mem_fn(&T::release) <snip> [Please try to limit the amount of quoted text in your replies.]
"Eric" <wstwej03@sneakemail.com> writes:
No offense intended.
None taken.
I was using class_value_wrapper based on a post in the mailing list archives detailing that as a step to wrap functions that return boost::shared_ptr. Is there a better way?
http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html tells all. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
I was using class_value_wrapper based on a post in the mailing list archives detailing that as a step to wrap functions that return boost::shared_ptr. Is there a better way?
http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html tells all.
This is indeed very helpful. I have found almost all of the boost and boost.python documentation very helpful, although I admit to sometimes being at a loss for how to find the right documentation for the right job. Does anyone have suggestions for ways to find the right info without bugging the list so often? Much thanks, Eric
"Eric" <wstwej03@sneakemail.com> writes:
I was using class_value_wrapper based on a post in the mailing list archives detailing that as a step to wrap functions that return boost::shared_ptr. Is there a better way?
http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html tells all.
This is indeed very helpful. I have found almost all of the boost and boost.python documentation very helpful, although I admit to sometimes being at a loss for how to find the right documentation for the right job. Does anyone have suggestions for ways to find the right info without bugging the list so often?
My only suggestion is that you use the categorized index in the reference docs. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (2)
-
David Abrahams -
Eric