Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-03-30 14:20:57


Vladimir Prus wrote:
>Reece Dunn wrote:
>
> >>Yes, you're right. However, there's one additional wish -- I'd like an
> >>appraoch different macroses for different functions. E.g. for POSIX
>calls
> >>I only encounter -1 returns and NULL returns for error reporting. So I'd
> >>like
> >
> >> int tty = check(open(check(ctermid(0)), O_RDRW);
> >
> >>to work. I guess this could be done by providing two overloads for the
> >>'Wrong' function...., but not, the 'Enforcer' template only works for
> >>specific type of return value :-(
> >
> > My errorcheck class (see other posts) has an error policy where you can
> > change the fail test. The default is to test errorcode < 0.
>
>The code you've posted had the check hardcoded, maybe I've missed some
>other
>post?

The error_storage::failed() function has the hard-coded check, but the
errorcheck class gets the storage (and check) policy as a template, so you
can provide your own class to perform a custom check.

> > A current weakness is that it does not return the value passed to it on
> > assignment, so you cannot use exactly like the above example. I have
>been
> > updating it to support errno handling, but this (at present) requires
> > something like:
> >
> > boost::errorcheck< ... > check;
> >
> > FILE * fp = ::fopen( 0, "r" );
> > check( fp != 0 ); // assert-like syntax
> > ::fclose( fp );
> >
> > If you are interested, I'll work on supporting constructs like your
> > example above.
>
>I actually wonder if we need a class. Does it have any advantages over
>function? E.g:

> template<class Checked = default_checker, class T = void>
> T check(T t)
> {
> if (Checker(t))
> throw ...
> return t;
> }

I suppose it comes down to what you are used to and aesthetics.

I have been using MS COM objects heavily that rely on HRESULT error code
returns, where the code looks like:

   HRESULT hr;
   IHTMLStyle * s = 0;
   if( FAILED( hr = elem -> get_style( &s )))
      goto clean;
   // use the style object
   clean:
   if( elem != 0 ) elem -> Release();
   if( s != 0 ) s -> Release();

Which is ugly IMHO, so I use a smart pointer for COM object management and
have a com::hresult class for storing the HRESULT, mapping it to a string,
etc. The errorcheck class is based on a com::result (throwing version of
com::hresult) now defined using errorcheck:

   typedef boost::errorcheck< hresult > result;

I suppose there are applications for both approaches, depending on what
style you want, what facilities you want, etc.

Regards,
Reece

_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today!
http://www.msn.co.uk/messenger


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