|
Boost : |
From: Andrey Semashev (andysem_at_[hidden])
Date: 2005-08-22 13:26:47
"David Abrahams" <dave_at_[hidden]>
wrote in message news:<uy86u3osd.fsf_at_[hidden]>...
> "Andrey Semashev" <andysem_at_[hidden]> writes:
>
> Andrey, your mailer wraps long lines; you might want to put your
> code
> in an attachment next time.
> http://www.boost.org/more/discussion_policy.htm#longlines
Sorry, I shall try to do so next time.
> > void foo(int n) {
> > leaving_scope_guard<
> > guard::with_catcher< // Indicate that we're expecting some
> > // exceptions to be thrown by
> > // guarded_foo
> > CHandler, // This is the class, which object is to
> > // process exceptions caught
> > guard::exceptions< std::bad_cast,
> > std::bad_alloc, my_exception
> > >, // Exceptions to be caught (up to
> > // 10 exception types supported)
>
> This part worries me thes reasons:
>
> 0. I doubt its utility.
The guard::with_catcher template class is actually a calling policy. In this
case the policy catches exceptions specified. Of course, we
could write a wrapper function that never throws and use it with scope
guard. But why write if it can be generated?
> 1. It induces a catch block and in many cases a catch block
> should be avoided when possible in exception-neutral code.
Yes, in case of this policy (with_catcher) it does. But if you are sure you
don't need exception handling, you can use another policy (namely
simple_invoker) which is the default one. In that case the guard set up
would look as follows:
leaving_scope_guard< > guard(bind(&guarded_foo, n,
"called from foo")); // Guard object
In this case there will be no try/catch blocks at all. In fact, I'm thinking
about typedef'ing this form into something shorter (maybe
"simple_guard"?).
> 2. I think it may encourage abuse with the same lure that
> exception-specifications present to the casual user.
I can't say I quite agree with you (if I got your opinion correctly). A tool
is just a tool and of course it may be used incorrectly or
ineffectively. Besides, I have a doubt that a user would prefer to use
exception-parameterized guards against simple ones without having real
intends to do so. For example, if a guarded function has an exception
specification, we have no right to hope it will not throw it. So we
shall have to catch it anyway.
> > boost::shared_ptr<
> > CHandler > // An optional parameter
> > // describes the way the
> > // pointer to CHandler
> > // object should be stored.
> > // The default value would
> > // be CHandler*.
>
> Do we really need all this parameterization? I think a generalized
> scopeguard is a good idea, but this smells like overkill.
Do you mean this last template parameter in particular? with_catcher policy
needs it because it cannot possibly know what to do with the
handler object after the exception is processed. In many cases you won't
need to worry about it and will be passing this as a pointer to the
handler object. In such cases you can simply skip this template argument.
But sometimes you might want to pass error hanlding to another object and
you will have to guarantee its existance until the guard is
destroyed.
> > >
> > > guard(
> > boost::bind(&guarded_foo, n,
> > "called from foo"),
> > boost::shared_ptr< CHandler >(
> > new CHandler)
> > ); // Guard object
> > // ...
> > }
PS: I attach the source code once more. I hope this time it will be correct.
If it doesn't work this time I can mail it if you'd like me to.
> --
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
begin 666 scope_guard.zip
M4$L#!!0``@`(`'0$%C.MBH8`&PD``,(N```-````<V-O<&5?9W5A<F0N:.U:
MZV_;R!'_+ /^'_;.0(X*=+;BQ'G(M@!!T24&;,>PY: M#EC0Y,KBE2()DG+B
M^M2_O3/[(+G+EY3&27#ME\2:W7G/_&9(:>_I3]M;Y"GY?>;YC!"2.&'$Z.W2
MCMW=N3BQE^D\C,D56]C)G-V14>#&[%Z<N7:*7/L'N_U7N_O]_@$GB[.;V&,S
M0MZQ@,6V[_V+N4(ZX=*)RQS?CNW4"X.,:6][:WMKQYL%+G!2>C7^<#%Y=SVZ
M?$O?4PHG0/8"5G&"7('C+UU&?KX)PR3=FRT#!V5G?_1WYU'T<_EB$ 9_at_U;U]
MX[.:&^D]A"2-;2]-]KR$WM@)HW;@4I?%WAUSU^.*0B](62PO;V_M[9$SVXG#
MA"5D!N&]Y6%*O>"6Q.QV":$A3NBRDM-4>#TYOSZ;7(ZF$WHV&E]^H):WB'RZ
M0(G=W[>W.OE'ZYE)V#<)STW""Y-P8!)>FH17)N&U27C3Y7E:QYT&;_J<L'XP
MZG6>7Y^>3O]^,;FR/$A,ER1IO'12$BQ]'W.WLX-D,B#1\L;WG(Q.'E:'=2*G
MD[.+4[3C8G0Y.E.">P3Y`GO!R.2SPR*LQJD4?PQ]D-J>/QCH>FLU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