[Assorted-commits] SF.net SVN: assorted: [748] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-05-08 19:32:40
|
Revision: 748 http://assorted.svn.sourceforge.net/assorted/?rev=748&view=rev Author: yangzhang Date: 2008-05-08 12:32:41 -0700 (Thu, 08 May 2008) Log Message: ----------- added closing RAII tool and die() Added Paths: ----------- cpp-commons/trunk/src/commons/closing.h cpp-commons/trunk/src/commons/die.h Added: cpp-commons/trunk/src/commons/closing.h =================================================================== --- cpp-commons/trunk/src/commons/closing.h (rev 0) +++ cpp-commons/trunk/src/commons/closing.h 2008-05-08 19:32:41 UTC (rev 748) @@ -0,0 +1,19 @@ +#ifndef COMMONS_CLOSING_H +#define COMMONS_CLOSING_H + +namespace +{ + + template<typename T> + class closing + { + public: + closing(T x) : x(x) {} + ~closing() { close(x); } + private: + T x; + }; + +} + +#endif Added: cpp-commons/trunk/src/commons/die.h =================================================================== --- cpp-commons/trunk/src/commons/die.h (rev 0) +++ cpp-commons/trunk/src/commons/die.h 2008-05-08 19:32:41 UTC (rev 748) @@ -0,0 +1,33 @@ +#ifndef COMMONS_DIE_H +#define COMMONS_DIE_H + +#include <cerrno> +#include <cstdio> +#include <cstdarg> +#include <cstring> +#include <cstdlib> + +namespace commons { + + /** + * Print an error message and exit. Acts like perror() if \p format doesn't + * end with "\n". + * + * TODO: move into C Commons. + */ + void + die(const char *format, ...) + { + const char *errstr; + va_list val; + va_start(val, format); + errstr = strerror(errno); + vfprintf(stderr, format, val); + if (strchr(format, '\n') == NULL) + fprintf(stderr, ": %s\n", errstr); + exit(1); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |