Boost logo

Boost :

From: Alexander Nasonov (alnsn-boost_at_[hidden])
Date: 2006-03-09 02:45:50


First version of BoostCandidate.Finally is available. There is only one header
file so I didn't put the library to boost vault. I can do this upon request.

Header file boost/finally.hpp: http://tinyurl.com/fwy6q

Examples:
libs/finally/example/hello.cpp: http://tinyurl.com/errx4
libs/finally/example/finally.cpp: http://tinyurl.com/jc692

Documentation in qbk format: http://tinyurl.com/h655f

Alternatively, you can browse CVS tree online:
http://cvs.sourceforge.net/viewcvs.py/cpp-experiment/finally/

In short, BoostCandidate.Finally is a (better, in my opinion) replacement both
for try-finally block and for ScopeGuard.
It can be used when RAII approach is not quite applicable. Typical example is
VectorInserter class in ScopeGuard article by Andrei Alexandrescu and Petru
Marginean ( http://tinyurl.com/26vzp ).

Below is a comparison with ScopeGuard approach (typeof registration is omitted
for brevity).

 // code snipset copied from ScopeGuard article:
 void User::AddFriend(User& newFriend)
 {
     friends_.push_back(&newFriend);
     ScopeGuard guard = MakeObjGuard(
         friends_, &UserCont::pop_back);
     pDB_->AddFriend(GetName(), newFriend.GetName());
     guard.Dismiss();
 }

 // First form of BoostCandidate.Finally
 void User::AddFriend(User& newFriend)
 {
     friends_.push_back(&newFriend);
     bool dismiss = false;
     BOOST_FINALLY_BEGIN( (friends_)(dismiss) )
     {
         if(!dismiss)
             friends_.pop_back();
     } BOOST_FINALLY_END;
     pDB_->AddFriend(GetName(), newFriend.GetName());
     dismiss = true;
 }

 // Second form of BoostCandidate.Finally
 void User::AddFriend(User& newFriend)
 {
     friends_.push_back(&newFriend);
     boost::scope_guard guard;
     BOOST_FINALLY_BEGIN( (guard)(friends_) )
     {
         friends_.pop_back();
     } BOOST_FINALLY_END(guard);
     pDB_->AddFriend(GetName(), newFriend.GetName());
     guard.dismiss();
 }

Waiting for comments and suggestions ...

--
Alexander Nasonov
alnsn <@> yandex <.> ru

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk