[shared_ptr] commit/rolback

Wouldn't it be nice to extend shared_ptr to support transaction-like semantics? Or is it possible already? Sometimes it's needed to execute code on exit only until we haven't reached some point (of "no return"): { shared_ptr<void> cr_guard(static_cast<void*>(0), bind(rollback_fun, x, y)); [...some critical part which should call rollback_fun on failure only...] //don't call rollback_fun if we get here cr_guard.commit(); [...any code that follows...] } Thanks! Wish you well! -=-=-=-=-=-=-=-=-=- Denis G. Priyomov

Denis G. Priyomov wrote:
Wouldn't it be nice to extend shared_ptr to support transaction-like semantics? Or is it possible already? Sometimes it's needed to execute code on exit only until we haven't reached some point (of "no return"):
This really is an abuse of shared_ptr. Search this list and the developer list for "scope guard", there was some interesting discussion a few weeks ago. Sebastian Redl

Dear Sirs, Does anyone have a working example of serialization of shared_ptrs with non-default deleter and factory construction as described in http://www.boost.org/libs/smart_ptr/sp_techniques.html#preventing_delete? The code below (correctly) triggers error in boost/checked_delete.hpp because the destructor of class X is private. #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/shared_ptr.hpp> #include <boost/shared_ptr.hpp> class X { private: ~X(); class deleter; friend class deleter; class deleter { public: void operator()(X * p) { delete p; } }; friend class boost::serialization::access; template< typename Archive > void serialize(Archive & ar, const unsigned int version) { ar & some_data; } public: static boost::shared_ptr<X> create() { boost::shared_ptr<X> px(new X, X::deleter()); return px; } int some_data; }; inline std::ostream& operator<<( std::ostream& os, const X& x ) { return os << "x = " << x.some_data << std::endl; } using namespace std; int main ( int argc, char* argv[] ) { boost::shared_ptr<X> x = X::create(); x->some_data = 1; cout << *x; std::ofstream ofs("filename"); boost::archive::text_oarchive oa(ofs); oa & x; ofs.close(); std::ifstream ifs("filename", std::ios::binary); boost::archive::text_iarchive ia(ifs); x->some_data = 3; cout << *x; ia & x; cout << *x; } I'm using boost 1.33.1 and gcc 4.0.1 on linux. Any help on this is greatly appreciated. Thanking in advance, Mikko Vainio

The following "blunt instrument" will make things work - though I'm not sure that its not a little tooo crude. That is it will still prevent your destructor from getting called via delete - though it could now be called via checked_delete. Robert Ramey Mikko Vainio wrote:
Dear Sirs,
Does anyone have a working example of serialization of shared_ptrs with non-default deleter and factory construction as described in http://www.boost.org/libs/smart_ptr/sp_techniques.html#preventing_delete?
The code below (correctly) triggers error in boost/checked_delete.hpp because the destructor of class X is private.
#include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
class X { private:
template<class T> friend inline void boost::checked_delete(T * x);
~X();
participants (4)
-
Denis G. Priyomov
-
Mikko Vainio
-
Robert Ramey
-
Sebastian Redl