very simple implementation of Andrei Alexandrescu's ScopeGuard idiom

https://github.com/RSATom/finally_execute/blob/master/finally_execute.h based on http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/18440375... I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README I've used this in many of my projects, and found it very usefull.... don't understand, why this simple solution does not contributed already to Boost by someone else... :)

Le 03/09/12 13:37, Sergey Radionov a écrit :
https://github.com/RSATom/finally_execute/blob/master/finally_execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/18440375...
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
Hi, have you take a look at Boost.ScopedExit? Best, Vicente

2012/9/4 Vicente J. Botet Escriba <vicente.botet@wanadoo.fr>:
Le 03/09/12 13:37, Sergey Radionov a écrit :
https://github.com/RSATom/finally_execute/blob/master/finally_execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/18440375...
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
Hi,
have you take a look at Boost.ScopedExit? Yes, but I think it's give too "dirty" code. And I don't like macros in C++ era :)
Best, Vicente
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Mon, Sep 3, 2012 at 4:51 PM, Sergey Radionov <rsatom@gmail.com> wrote:
2012/9/4 Vicente J. Botet Escriba <vicente.botet@wanadoo.fr>:
Le 03/09/12 13:37, Sergey Radionov a écrit :
https://github.com/RSATom/finally_execute/blob/master/finally_execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/18440375...
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
Hi,
have you take a look at Boost.ScopedExit? Yes, but I think it's give too "dirty" code. And I don't like macros in C++ era :)
Btw, in C++11 era (i.e., lambdas) no macros needed: void world::add_person(person const& a_person) { bool commit = false; persons_.push_back(a_person); scope_exit on_exit1([&commit, this](void) { // Use C++11 lambda. if(!commit) persons_.pop_back(); // `persons_` via captured `this`. }); // ... commit = true; } http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/scope_exit/alt... --Lorenzo

Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler. Given the following: FILE * const file(fopen("test", "r")); if (file) { // A } Your usage, at A, is: finally_execute _(make_fin_exec(boost::bind(fclose, file))); Mine is: scope_guard _(fclose, file); As you can see, a little more complication in the component makes usage much simpler. There's also Boost.Scope Exit, though it's even more verbose: BOOST_SCOPE_EXIT(&file) { fclose(file); } BOOST_SCOPE_EXIT_END If there's interest in it, I'd have to seek permission to submit my library. _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec(boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler. In this case, I think, much simple usage gives less clear code, since
2012/9/4 Stewart, Robert <Robert.Stewart@sig.com>: people reading code will see boost::bind and understand what it mean. With your version, he maybe should have to a look to help or header... It's just my point of view.
There's also Boost.Scope Exit, though it's even more verbose:
BOOST_SCOPE_EXIT(&file) { fclose(file); } BOOST_SCOPE_EXIT_END
If there's interest in it, I'd have to seek permission to submit my library.
_____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com
________________________________
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Sergey Radionov wrote:
2012/9/4 Stewart, Robert <Robert.Stewart@sig.com>:
Sergey Radionov wrote:
<https://github.com/RSATom/finally_execute/blob/master/finally_execute.h>
based on <http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2>
I know about <http://sourceforge.net/projects/loki-lib/> but with boost::bind this can be much simpler:
<https://github.com/RSATom/finally_execute/blob/master/README>
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec( boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
In this case, I think, much simple usage gives less clear code, since people reading code will see boost::bind and understand what it mean. With your version, he maybe should have to a look to help or header...
Objectively, your version requires knowing how to use boost::bind, which arguably is a good thing, and requires calling a function template, both of which make the statement more verbose. My version uses the same calling convention as, say, boost::thread: pass the function object and the arguments it requires. It even accepts a std::tr1::function<void()> so you can bind to your heart's content. As for the concern that one must read help or a header to understand how to use scope_guard, surely you jest. One cannot understand finally_execute or make_fin_exec() without doing likewise. You simply construct an instance of scope_guard with the function object to call, and any required arguments. What could be simpler?
There's also Boost.Scope Exit, though it's even more verbose: [snip unreferenced content] Rob Stewart [snip my signature block] IMPORTANT: The information contained in this email and/or [snip my employer's notice] Unsubscribe & other changes: [snip dev list's notice]
Please read http://www.boost.org/community/policy.html#quoting for guidance on how to quote on this list. _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Hi folks, In contrast to BOOST_SCOPE_EXIT there is a much shorter and less verbose implementation that is especially good with c++11 lambda usage - http://alexander-stoyan.blogspot.com/2012/02/scope-exit-handlers.html ============================== Never lose heart! Regards, Alexander Stoyan http://alexander-stoyan.blogspot.com -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Stewart, Robert Sent: Tuesday, September 04, 2012 3:01 PM To: boost@lists.boost.org Subject: Re: [boost] very simple implementation of Andrei Alexandrescu's ScopeGuard idiom Sergey Radionov wrote:
2012/9/4 Stewart, Robert <Robert.Stewart@sig.com>:
Sergey Radionov wrote:
<https://github.com/RSATom/finally_execute/blob/master/finally_execute.h>
based on
<http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/1844037 58?pgno=2>
I know about <http://sourceforge.net/projects/loki-lib/> but with boost::bind this can be much simpler:
<https://github.com/RSATom/finally_execute/blob/master/README>
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec( boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
In this case, I think, much simple usage gives less clear code, since people reading code will see boost::bind and understand what it mean. With your version, he maybe should have to a look to help or header...
Objectively, your version requires knowing how to use boost::bind, which arguably is a good thing, and requires calling a function template, both of which make the statement more verbose. My version uses the same calling convention as, say, boost::thread: pass the function object and the arguments it requires. It even accepts a std::tr1::function<void()> so you can bind to your heart's content. As for the concern that one must read help or a header to understand how to use scope_guard, surely you jest. One cannot understand finally_execute or make_fin_exec() without doing likewise. You simply construct an instance of scope_guard with the function object to call, and any required arguments. What could be simpler?
There's also Boost.Scope Exit, though it's even more verbose: [snip unreferenced content] Rob Stewart [snip my signature block] IMPORTANT: The information contained in this email and/or [snip my employer's notice] Unsubscribe & other changes: [snip dev list's notice]
Please read http://www.boost.org/community/policy.html#quoting for guidance on how to quote on this list. _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Tue, Sep 4, 2012 at 5:08 AM, Alexander Stoyan <alexander.stoyan@gmail.com> wrote:
Hi folks, In contrast to BOOST_SCOPE_EXIT there is a much shorter and less verbose implementation that is especially good with c++11 lambda usage - http://alexander-stoyan.blogspot.com/2012/02/scope-exit-handlers.html
Looks the same as the one suggested in Boost.ScopeExit docs: http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/scope_exit/alt...
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Stewart, Robert Sent: Tuesday, September 04, 2012 3:01 PM To: boost@lists.boost.org Subject: Re: [boost] very simple implementation of Andrei Alexandrescu's ScopeGuard idiom
Sergey Radionov wrote:
2012/9/4 Stewart, Robert <Robert.Stewart@sig.com>:
Sergey Radionov wrote:
<https://github.com/RSATom/finally_execute/blob/master/finally_execute.h>
based on
<http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/1844037 58?pgno=2>
I know about <http://sourceforge.net/projects/loki-lib/> but with boost::bind this can be much simpler:
<https://github.com/RSATom/finally_execute/blob/master/README>
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec( boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
In this case, I think, much simple usage gives less clear code, since people reading code will see boost::bind and understand what it mean. With your version, he maybe should have to a look to help or header...
Objectively, your version requires knowing how to use boost::bind, which arguably is a good thing, and requires calling a function template, both of which make the statement more verbose. My version uses the same calling convention as, say, boost::thread: pass the function object and the arguments it requires. It even accepts a std::tr1::function<void()> so you can bind to your heart's content.
As for the concern that one must read help or a header to understand how to use scope_guard, surely you jest. One cannot understand finally_execute or make_fin_exec() without doing likewise. You simply construct an instance of scope_guard with the function object to call, and any required arguments. What could be simpler?
There's also Boost.Scope Exit, though it's even more verbose: [snip unreferenced content] Rob Stewart [snip my signature block] IMPORTANT: The information contained in this email and/or [snip my employer's notice] Unsubscribe & other changes: [snip dev list's notice]
Please read http://www.boost.org/community/policy.html#quoting for guidance on how to quote on this list.
--Lorenzo

On Mon, Sep 3, 2012 at 10:28 AM, Stewart, Robert <Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec(boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above. http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/index.html
There's also Boost.Scope Exit, though it's even more verbose:
BOOST_SCOPE_EXIT(&file) { fclose(file); } BOOST_SCOPE_EXIT_END
If there's interest in it, I'd have to seek permission to submit my library.
_____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com
________________________________
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- --Lorenzo

On Mon, Sep 3, 2012 at 10:28 AM, Stewart, Robert <Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec(boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above. Yep, but in 90% of cases I just need call one function on scope exit, and in this case ScopeExit will be too cumbersome.
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>: little example: void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f1) ); FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) ); FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) ); FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) ); } how it will look with ScopeExit? what will be more clear?
http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/index.html
There's also Boost.Scope Exit, though it's even more verbose:
BOOST_SCOPE_EXIT(&file) { fclose(file); } BOOST_SCOPE_EXIT_END
If there's interest in it, I'd have to seek permission to submit my library.
_____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com
________________________________
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- --Lorenzo
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Mon, Sep 3, 2012 at 6:37 PM, Sergey Radionov <rsatom@gmail.com> wrote:
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 10:28 AM, Stewart, Robert <Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec(boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above. Yep, but in 90% of cases I just need call one function on scope exit,
OK if that's what you need but that's not the problem that ScopeExit is trying to solve to "call a function on scope exit".
and in this case ScopeExit will be too cumbersome. little example:
void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f1) );
FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) );
FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) );
FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) ); }
how it will look with ScopeExit?
If you are willing to use macros (as ScopeExit does), maybe something like this: #define EXIT_CLOSE(f) \ BOOST_SCOPE_EXIT(&f) { \ fclose(f); \ } BOOST_SCOPE_EXIT_END void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; EXIT_CLOSE(f1) FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; EXIT_CLOSE(f2) FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; EXIT_CLOSE(f3) FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; EXIT_CLOSE(f4) }
what will be more clear?
That depends on your application domain, maybe your developer's tema, etc so it's your call.
http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/index.html
There's also Boost.Scope Exit, though it's even more verbose:
BOOST_SCOPE_EXIT(&file) { fclose(file); } BOOST_SCOPE_EXIT_END
If there's interest in it, I'd have to seek permission to submit my library.
_____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com
________________________________
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- --Lorenzo
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- --Lorenzo

2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 6:37 PM, Sergey Radionov <rsatom@gmail.com> wrote:
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 10:28 AM, Stewart, Robert <Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec(boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above. Yep, but in 90% of cases I just need call one function on scope exit,
OK if that's what you need but that's not the problem that ScopeExit is trying to solve to "call a function on scope exit".
and in this case ScopeExit will be too cumbersome. little example:
void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f1) );
FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) );
FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) );
FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) ); }
how it will look with ScopeExit?
If you are willing to use macros (as ScopeExit does), maybe something like this:
#define EXIT_CLOSE(f) \ BOOST_SCOPE_EXIT(&f) { \ fclose(f); \ } BOOST_SCOPE_EXIT_END
void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; EXIT_CLOSE(f1)
FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; EXIT_CLOSE(f2)
FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; EXIT_CLOSE(f3)
FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; EXIT_CLOSE(f4) }
what will be more clear?
That depends on your application domain, maybe your developer's tema, etc so it's your call. ok, let's use example more closer to real life:
bool MoveFile(const char* from, const char* to) { FILE* f1 = fopen( from, "rb"); if( !f1 ) return false; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f1) ); FILE* f2 = fopen( to, "wb"); if( !f2 ) return false; finally_execute close_file2 = make_fin_exec( boost::bind(fclose, f2) ); finally_execute remove_file1 = make_fin_exec( boost::bind(remove, from) ); finally_execute remove_file2 = make_fin_exec( boost::bind(remove, to) ); /*do real move*/ if( was_error ) { remove_file1.dismiss(); return false; } else { remove_file2.dismiss(); return true; } }

On Mon, Sep 3, 2012 at 7:56 PM, Sergey Radionov <rsatom@gmail.com> wrote:
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 6:37 PM, Sergey Radionov <rsatom@gmail.com> wrote:
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 10:28 AM, Stewart, Robert <Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
https://github.com/RSATom/finally_execute/blob/master/finally _execute.h
based on http://www.drdobbs.com/cpp/generic-change-the-way- you-write-excepti/184403758?pgno=2
I know about http://sourceforge.net/projects/loki-lib/ but with boost::bind this can be much simpler: https://github.com/RSATom/finally_execute/blob/master/README
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Given the following:
FILE * const file(fopen("test", "r")); if (file) { // A }
Your usage, at A, is:
finally_execute _(make_fin_exec(boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above. Yep, but in 90% of cases I just need call one function on scope exit,
OK if that's what you need but that's not the problem that ScopeExit is trying to solve to "call a function on scope exit".
and in this case ScopeExit will be too cumbersome. little example:
void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f1) );
FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) );
FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) );
FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f4) ); }
how it will look with ScopeExit?
If you are willing to use macros (as ScopeExit does), maybe something like this:
#define EXIT_CLOSE(f) \ BOOST_SCOPE_EXIT(&f) { \ fclose(f); \ } BOOST_SCOPE_EXIT_END
void foo() { FILE* f1 = fopen( "file1.bin", "rb"); if( !f1 ) return; EXIT_CLOSE(f1)
FILE* f2 = fopen( "file2.bin", "rb"); if( !f2 ) return; EXIT_CLOSE(f2)
FILE* f3 = fopen( "file3.bin", "rb"); if( !f3 ) return; EXIT_CLOSE(f3)
FILE* f4 = fopen( "file4.bin", "rb"); if( !f4 ) return; EXIT_CLOSE(f4) }
what will be more clear?
That depends on your application domain, maybe your developer's tema, etc so it's your call. ok, let's use example more closer to real life:
bool MoveFile(const char* from, const char* to) {
FILE* f1 = fopen( from, "rb"); if( !f1 ) return false; finally_execute close_file1 = make_fin_exec( boost::bind(fclose, f1) );
FILE* f2 = fopen( to, "wb"); if( !f2 ) return false; finally_execute close_file2 = make_fin_exec( boost::bind(fclose, f2) );
finally_execute remove_file1 = make_fin_exec( boost::bind(remove, from) ); finally_execute remove_file2 = make_fin_exec( boost::bind(remove, to) );
/*do real move*/
if( was_error ) { remove_file1.dismiss(); return false; } else { remove_file2.dismiss(); return true; }
}
Maybe something like this but I couldn't fully follow the example because I don't understand where was_error comes from (anyway, not_error below would come from the same place...): #define EXIT_CALL_IF(cond, op, var) \ BOOST_SCOPE_EXIT(&cond, &var) { \ if(cond) op(var); } BOOST_SCOPE_EXIT_END bool MoveFile(const char* from, const char* to) { bool const true_ = true; FILE* f1 = fopen( from, "rb"); if( !f1 ) return false; EXIT_CALL_IF(true_, fclase, f1) FILE* f2 = fopen( to, "wb"); if( !f2 ) return false; EXIT_CALL_IF(true_, fclase, f2) EXIT_CALL_IF(was_error, remove, from) EXIT_CALL_IF(not_error, remove, to) /*do real move*/ return not_error = !was_error; } --Lorenzo

2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 6:37 PM, Sergey Radionov <rsatom@gmail.com> wrote: OK if that's what you need but that's not the problem that ScopeExit is trying to solve to "call a function on scope exit".
I am not try to tell that ScopeExit is useless, just "choose right tool for corresponding work "

On Mon, Sep 3, 2012 at 8:02 PM, Sergey Radionov <rsatom@gmail.com> wrote:
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
On Mon, Sep 3, 2012 at 6:37 PM, Sergey Radionov <rsatom@gmail.com> wrote: OK if that's what you need but that's not the problem that ScopeExit is trying to solve to "call a function on scope exit".
I am not try to tell that ScopeExit is useless, just "choose right tool for corresponding work "
Sure, and it's entirely up to you to decide if/when Scope Exit is the right tool for you to use. Keep in mind that the "work" Scope Exit is set to do is to execute arbitrary code on enclosing scope exit. --Lorenzo

Lorenzo Caminiti wrote:
<rsatom@gmail.com> wrote:
2012/9/4 Lorenzo Caminiti <lorcaminiti@gmail.com>:
<Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above.
Yep, but in 90% of cases I just need call one function on scope exit,
OK if that's what you need but that's not the problem that ScopeExit is trying to solve to "call a function on scope exit".
Agreed, but you're arguing with yourself. We were discussing mechanisms for calling a function with a few arguments at the end of a scope, not with replacements for Boost.Scope Exit. _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Lorenzo Caminiti wrote:
<Robert.Stewart@sig.com> wrote:
Sergey Radionov wrote:
<https://github.com/RSATom/finally_execute/blob/master/README>
I've used this in many of my projects, and found it very usefull....
don't understand, why this simple solution does not contributed already to Boost by someone else... :)
I created a version, too, but the usage is much simpler.
Your usage, at A, is:
finally_execute _(make_fin_exec( boost::bind(fclose, file)));
Mine is:
scope_guard _(fclose, file);
As you can see, a little more complication in the component makes usage much simpler.
Boost.ScopeExit allows you to "execute *arbitrary* code when the enclosing scope exits". I don't see how you execute arbitrary code with the above.
No one suggested that the above *can* execute "arbitrary code" as you define it for Boost.Scope Exit. However, in my experience, what's needed is to call a function with a few arguments in nearly all cases, and I'm happy to relying on binding and lambdas to handle the others.
There's also Boost.Scope Exit, though it's even more verbose:
BOOST_SCOPE_EXIT(&file) { fclose(file); } BOOST_SCOPE_EXIT_END
I referenced Boost.Scope Exit because the OP wondered why there was no facility in Boost to do what he proposed. I pointed out that there is, though it is rather verbose by comparison. [snip signature block, etc.] Please don't overquote. _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

On 12-09-03 7:37 AM, Sergey Radionov wrote:
don't understand, why this simple solution does not contributed already to Boost by someone else...:)
They did, and it was called Boost Scope Exit. I'd also like to have a boost::scope_exit class that took in a boost function in its constructor with the ability to dismiss whether or not that function is actually called :) Sohail

Sergey Radionov wrote on Monday, September 03, 2012 7:38 AM
Subject: [boost] very simple implementation of Andrei Alexandrescu's ScopeGuard idiom don't understand, why this simple solution does not contributed already to Boost by someone else... :)
There is (or at one time was) also an implementation in "boost/multi_index/detail/scope_guard.hpp" Erik ---------------------------------------------------------------------- This message w/attachments (message) is intended solely for the use of the intended recipient(s) and may contain information that is privileged, confidential or proprietary. If you are not an intended recipient, please notify the sender, and then please delete and destroy all copies and attachments, and be advised that any review or dissemination of, or the taking of any action in reliance on, the information contained in or attached to this message is prohibited. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Sender. Subject to applicable law, Sender may intercept, monitor, review and retain e-communications (EC) traveling through its networks/systems and may produce any such EC to regulators, law enforcement, in litigation and as required by law. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or free of errors or viruses. References to "Sender" are references to any subsidiary of Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this EC may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.bankofamerica.com/emaildisclaimer. By messaging with Sender you consent to the foregoing.
participants (7)
-
Alexander Stoyan
-
Lorenzo Caminiti
-
Nelson, Erik - 2
-
Sergey Radionov
-
Sohail Somani
-
Stewart, Robert
-
Vicente J. Botet Escriba