From: Buster on 3 Aug 2006 11:39 I'd like to have an arbitrary function with arguments called when execution leaves a function, either normally or because of an exception. I assumed there was a boost structure, not unlike a semaphore guard, that took a generic function object but I can't find one. Does such a thing exist ? void func1(int); void func2(); void func3() { guard G(make_guard(bind(&func1, 5))); func2(); G.release(); } [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Joshua Lehrer on 5 Aug 2006 14:31 > > void func3() > { > guard G(make_guard(bind(&func1, 5))); > > func2(); > > G.release(); see Andrei's scopeguard framework with my improvements: http://www.zete.org/people/jlehrer/scopeguard.html Joshua Lehrer http://www.lehrerfamily.com/ [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Maxim Yegorushkin on 7 Aug 2006 08:52 Buster wrote: > > I'd like to have an arbitrary function with arguments > > called when execution leaves a function, either > > normally or because of an exception. I assumed > > there was a boost structure, not unlike a semaphore > > guard, that took a generic function object but > > I can't find one. Does such a thing exist ? > > > > void func1(int); > > void func2(); > > > > void func3() > > { > > guard G(make_guard(bind(&func1, 5))); > > > > func2(); > > > > G.release(); > > } You can make one yourself easily: namespace detail { class scope_guard_base { public: void release() const { do_rollback_ = false; } scope_guard_base() : do_rollback_(true) {} scope_guard_base(const scope_guard_base& other) : do_rollback_(other.do_rollback_) { other.release(); } protected: mutable bool do_rollback_; }; template<class F> class scope_guard_impl : public scope_guard_base { public: explicit scope_guard_impl(F const& b) : rollback_(b) {} ~scope_guard_impl() { if(do_rollback_) rollback_(); } private: F rollback_; }; } // namespace detail typedef const detail::scope_guard_base& scope_guard; template<class F> inline detail::scope_guard_impl<F> make_guard(F const& f) { return detail::scope_guard_impl<F>(f); } Another one can be found here: http://boost.cvs.sourceforge.net/boost/boost/boost/multi_index/detail/scope_guard.hpp?revision=HEAD&view=markup [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ivan Novick on 7 Aug 2006 23:30 Hi Max, Maxim Yegorushkin wrote: > You can make one yourself easily: Do you have any link to what is a guard? Sorry for newbie question. Ivan [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Buster on 8 Aug 2006 14:03
Ivan Novick wrote: > Hi Max, > > Maxim Yegorushkin wrote: > >>You can make one yourself easily: > > > Do you have any link to what is a guard? > Sorry for newbie question. I'm no expert, but to return the favor... A guard is an object which makes sure an operation is undone, for example, before execution leaves the current function. Guards handle exception throwing too. A common guard is an automatic pointer: autoptr<Foo> ap(new Foo()); which frees the Foo after it's no longer needed. Another is a semaphore guard which gives back the semaphore once execution leaves the mutex section: boost::mutex m; void func() { boost::mutex::scoped_lock l(m); // mutex section } A common property of a guard is that it takes the resource during construction (before giving it back at destruction.) This ensures that no exception can be thrown before getting the resource under the protection of the guard. The guard that I was looking for was a bit different. With it, I'm not looking to protect a resource (so it doesn't "take" anything during construction). All I want is for a generic function to be called before leaving an execution block. To follow up with the previous posts, the ones suggested weren't exactly what I was looking for because they require a hard-to-specify local variable: scoped_guard<???> g = make_guard(boost::bind(function, arg1, arg2)); whereas all I wanted to write was: scoped_guard g = ... If I've misunderstood, I'd appreciate the clarification. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |