[finally] early draft available

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

"Alexander Nasonov" wrote:
First version of BoostCandidate.Finally is available....
Compiling on Intel 7.0 (Windows, plugges in VC6 IDE, old Dinkumware). Latest VCVS Boost. I get a warning: Compiling... aaaa.cpp using typeof emulation C:\temp\aaaa\aaaa\aaaa.cpp(324): warning #411: class "_boost_finally_params_324" defines no constructor to initialize the following: reference member "_boost_finally_params_324::hello" BOOST_FINALLY_BEGIN( (hello) ) ^ It is dog-slow: the small example takes over minute!!!! on Athlone XP 2200+ (no optimisations enabled). Intel is known for being slow on heavy templatized code and its preprocessor processing is exponential. Just out of hand, perhaps using PP local iteration may be considered. It, however, works, well, the small example. Trying it on BCB 6.4 failed, due to problems within typeof. I do not know about status of typeof vs BCB.
Waiting for comments and suggestions ...
Mr. Alexandrescu had suggested something like that to be included in the language (as a killer feature not seen in mainstream languages). Specifically, he suggested: on_block_exit { .... code .... } on_block_success { ... code ... } on_block_failure { ... code ... } /Pavel

Pavel Vozenilek <pavel_vozenilek <at> hotmail.com> writes:
It is dog-slow: the small example takes over minute!!!! on Athlone XP 2200+ (no optimisations enabled).
Intel is known for being slow on heavy templatized code and its preprocessor processing is exponential.
Unless you passed a lot of variables to BOOST_FINALLY_BEGIN, it's not PP, it is Boost.Typeof that is very slow.
Mr. Alexandrescu had suggested something like that to be included in the language (as a killer feature not seen in mainstream languages). Specifically, he suggested:
on_block_exit { .... code .... } on_block_success { ... code ... } on_block_failure { ... code ... }
Cool, I didn't know this. Is there any proposal for this? -- Alexander Nasonov alnsn <@> yandex <.> ru

"Alexander Nasonov" wrote:
It is dog-slow: the small example takes over minute!!!! on Athlone XP 2200+ (no optimisations enabled).
Intel is known for being slow on heavy templatized code and its preprocessor processing is exponential.
Unless you passed a lot of variables to BOOST_FINALLY_BEGIN, it's not PP, it is Boost.Typeof that is very slow.
I tried dozens of BOOST_FINALLYs in one TU and the compilation time stays the same (the minute). So it is not PP.
Mr. Alexandrescu had suggested something like that to be included in the language (as a killer feature not seen in mainstream languages). Specifically, he suggested:
on_block_exit { .... code .... } on_block_success { ... code ... } on_block_failure { ... code ... }
Cool, I didn't know this. Is there any proposal for this?
This c.l.c++.m thread http://groups.google.com/group/comp.lang.c++.moderated/tree/browse_frm/threa... or http://tinyurl.com/outps He had characterised it as "nice research topic ;-)" and I guess no one had dared to propose it for standardisation. /Pavel

Alexander Nasonov wrote:
Pavel Vozenilek <pavel_vozenilek <at> hotmail.com> writes:
It is dog-slow: the small example takes over minute!!!! on Athlone XP 2200+ (no optimisations enabled).
Intel is known for being slow on heavy templatized code and its preprocessor processing is exponential.
Unless you passed a lot of variables to BOOST_FINALLY_BEGIN, it's not PP, it is Boost.Typeof that is very slow.
Mr. Alexandrescu had suggested something like that to be included in the language (as a killer feature not seen in mainstream languages). Specifically, he suggested:
on_block_exit { .... code .... } on_block_success { ... code ... } on_block_failure { ... code ... }
Cool, I didn't know this. Is there any proposal for this?
Thank you Alexander for emailing me about this thread. There isn't, at least not for C++. I convinced Walter Bright to implement the constructs in D, and helped him write this doc: http://www.digitalmars.com/d/exception-safe.html. Alexander asked me what I think of his implementation, and IMHO it is superior, and closer to intent, than our original ScopeGuard was. The fact that you can write arbitrary code inside the scope of interest, and that code has access (albeit through a less than transparent mechanism) to the local variables, is very useful for implementing exception-safe code easily and expressively. For example, the D examples in Walter's article can be easily translated into C++ (using Alexander's lib) with only little loss in terseness and clarity. Andrei

Pavel Vozenilek wrote:
"Alexander Nasonov" wrote:
First version of BoostCandidate.Finally is available....
Compiling on Intel 7.0 (Windows, plugges in VC6 IDE, old Dinkumware). Latest VCVS Boost.
I'm very interested in implementation details on Intel for Windows. What is a value of BOOST_FINALLY_IMPLEMENTATION? (you can compile and run http://tinyurl.com/jc692) Does it support MSVC-specific __declspec(thread)? -- Alexander

Alexander Nasonov <alnsn-boost@yandex.ru> writes:
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; }
How, exactly, is this an improvement on ScopeGuard? It's longer and more complicated, I'll give you that, but those are not usually thought of as virtues. -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams <dave <at> boost-consulting.com> writes:
How, exactly, is this an improvement on ScopeGuard? It's longer and more complicated, I'll give you that, but those are not usually thought of as virtues.
- You can set a breakpoint at any line inside BOOST_FINALLY block. - If native typeof is available, it should compile very fast. - More scalable then ScopeGuard + Boost.Lambda. -- Alexander Nasonov alnsn <@> yandex <.> ru
participants (5)
-
Alexander Nasonov
-
Alexander Nasonov
-
Andrei Alexandrescu (See Website For Email)
-
David Abrahams
-
Pavel Vozenilek