Boost logo

Boost :

From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2020-05-31 18:22:52


On Sun, May 31, 2020 at 10:32 AM Rainer Deyke via Boost <
boost_at_[hidden]> wrote:
>
> On 31.05.20 18:03, Christian Mazakas via Boost wrote:
> >> The use of such a low-level API would look something like this:
> >
> > Hmm, I'm failing to see the overwhelming value in exposing these
portions
> > of the API.
>
> Here is one use case for some kind of low-level access to error objects
> without going through lambdas (although it would have a different form
> than the one used by Bjorn). Suppose I have an error type that may or
> may not be annotated with a file name, an IP address, or a user name,
> individually or in any combination. For each of these annotations, I
> want to handle it the same whenever it is present, regardless of the
> others. Without some sort of direct access to error objects, the code
> for this would look something like this:
>
>
> return leaf::try_handle_some(
> some_try_block,
> [](leaf::match<my_error>,
> leaf::e_file_name fname,
> my::e_user_name uname,
> my::ip_address ip) {
> handle_filename(fname);
> handle_user_name(uname);
> handle_ip(ip);
> return handle_error();
> },
> <deleted long list of lambdas

No, you'd simply do:

return leaf::try_handle_some(
   some_try_block,
   [](leaf::match<my_error, my_error::something>,
      leaf::e_file_name const * fname,
      my::e_user_name const * uname,
      my::ip_address const * ip) {
     if( fname )
       handle_filename(*fname);
     if( uname )
       handle_user_name(*uname);
     if( ip )
       handle_ip(*ip);
     return handle_error();
   } );

When you take an argument by const & or by value, LEAF does the ifs for you
before calling your function (once it determines that all of its arguments
are available). If an argument is taken by const *, and the value is not
available, LEAF will still call the function, passing null for that
argument.


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