Boost logo

Boost :

From: Joe Swatosh (joe-swatosh_at_[hidden])
Date: 2002-06-24 18:44:46

> -----Original Message-----
> From: scleary_at_[hidden] [mailto:scleary_at_[hidden]]

> I've messed around in the past with different versions of
> classes that would
> run arbitrary code when destroyed. Here's some different
> functionality that
> I've found useful:
> . Allow *any* action to be run; Boost.Functional can
> already support this;
> why have ScopeGuard duplicate this functionality?

I'll confess my ignorance: I looked at Boost.Function and didn't see how it could help here.

> . Have a type of ScopeGuard that wraps its action in
> try...catch, similar
> to the article, but have another type that does not.

Yeah, I changed the version in the file section to not have the try/catch at all. It should be easy enough to write a wrapper function to catch everything if that is what is wanted.

I also added tests that exit the block by throwing. In doing so I discovered that BCC isn't invoking the destructors during the stack unwind to the catch. Oh well.

> . Have a type of ScopeGuard that only runs its action if
> the destructor
> was called as part of stack unwinding, not normal block scope
> exit (useful
> for "rollback" operations).

I assume you mean using uncaught_exception to figure that out. That's a neat idea. Obviously it won't work for VC6 users.

> . Have a type of ScopeGuard that follows strict scoping
> rules, but have
> another type whose scope can be "transferred" to a calling function.
> When you start looking at the different useful types of
> ScopeGuard classes,
> two things become apparent:
> 1) It would be best as a policy-designed class

I'd love to see scope_guard implemented in terms of boost::smart_resource or smart_pointer or whatever it turns out to be called. Right now, I see it as naming a point in the "'smart' resource management" space, in the same way that boost::scoped_ptr or std::auto_ptr do.

> 2) It's very close to a SmartPtr. So close, in fact, that
> I make the
> claim that a ScopeGuard *is* a SmartPtr.

Not sure I buy that one yet.

> Food for thought. :)
> -Steve

Thanks for your time. Please spend a little more to explain how Boost.Function can help make this simpler, more maintainable, better? Thanks again.

-Joe Swatosh

Boost list run by bdawes at, gregod at, cpdaniel at, john at