Re: [boost] Standard C Library and C++ / BOOST

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

A trick I use when handling HRESULT error codes is to have a class like
"Reece Dunn" <msclrhd@hotmail.com> wrote in message news:BAY7-F934pawS4PtkyV0000384c@hotmail.com... this
(adapted for int error type):
Maybe you should start on such a small helper library and submit it? br Thorsten

Reece Dunn wrote:
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.
Unless I'm mistaken, you're talking something similar to Andrei Alexandrescu's ENFORCE: http://www.cuj.com/documents/s=8250/cujcexp2106alexandr/ As a side-note, I'm implementing SMART_ENFORCE in the smart_assert library. Unfortunately in the latest months I did not have time for it. Hopefully I'll have some time in the next couple of weeks to port it to vc7.1 and post a new version. Best, John

John Torjo wrote:
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.
Unless I'm mistaken, you're talking something similar to Andrei Alexandrescu's ENFORCE: http://www.cuj.com/documents/s=8250/cujcexp2106alexandr/
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 :-( - Volodya
participants (4)
-
John Torjo
-
Reece Dunn
-
Thorsten Ottosen
-
Vladimir Prus