Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-03-26 10:51:27


Vladimir Prus wrote:
>Eljay Love-Jensen wrote:
>
> > Take the Standard C Library.
> > Create a new namespace, "boost::xtd::" for instance.
> > For every function in the Standard C Library, create an analog in the
> > "boost::xtd::" namespace.
> > The analog function would have the same functional behavior as the
>StdCLib
> > function, with the notable difference that any errors will generate an
> > exception.
>
>Personally, I find this to be a good idea. Whenever I had to write code
>like:
>
> int controlling_tty = open(ctermid(0), O_RDWR);
> if (controlling_tty == -1)
> perror("open");
>
> int result = tcsetpgrp(controlling_tty, parent_group);
> if (result == -1)
> perror("tcsetpgrp");
>
>I really wish there were exception-throwing versions -- since that code is
>boring and still contains a couple of error-handling problems. However,
>most
>of the functions above are acutally from POSIX, not from standard C, so I'm
>not sure how much wrapped C functions would help me.

A trick I use when handling HRESULT error codes is to have a class like this
(adapted for int error type):

   class errorcheck
   {
      private:
         int error;
      public:
         inline operator int()
         {
            return( error );
         }
      public:
         inline errorcheck & operator=( int ec )
         {
            error = ec;
            if( ec < 0 ) throw( *this );
            return( *this );
         }
         inline errorcheck & operator=( const errorecheck & ec )
         {
            error = ec.error;
            return( *this );
         }
      public:
         inline errorcheck( int ec ): error( ec )
         {
            if( ec < 0 ) throw( *this );
         }
         inline errorcheck( const errorcheck & ec ): error( ec.error )
         {
         }
   };

   try
   {
      errorcheck ec;
      ec = open(ctermid(0), O_RDWR);
      ec = tcsetpgrp(controlling_tty, parent_group);
   }
   catch( errocheck error )
   {
      // ...
   }

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