[Assorted-commits] SF.net SVN: assorted:[1082] cpp-commons/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-12-02 05:34:29
|
Revision: 1082 http://assorted.svn.sourceforge.net/assorted/?rev=1082&view=rev Author: yangzhang Date: 2008-12-02 05:34:16 +0000 (Tue, 02 Dec 2008) Log Message: ----------- - migrated to boost 1.37 - fixed some check calls - added printf format attributes to check functions - fixed format specifiers/typing by using stringstream/lexical_cast - updated and published the README Modified Paths: -------------- cpp-commons/trunk/README cpp-commons/trunk/src/commons/boost/delegates.h cpp-commons/trunk/src/commons/check.h cpp-commons/trunk/src/commons/die.h cpp-commons/trunk/src/commons/sockets.h cpp-commons/trunk/src/commons/st/st.h Modified: cpp-commons/trunk/README =================================================================== --- cpp-commons/trunk/README 2008-11-30 23:46:31 UTC (rev 1081) +++ cpp-commons/trunk/README 2008-12-02 05:34:16 UTC (rev 1082) @@ -6,33 +6,43 @@ -------- The C++ Commons is a general-purpose utility library for the C++ programming -language. The library is nascent, so it doesn't have enough to warrant a -release. This is a library of header files, in the same spirit as [boost]. +language. This is a library of header files, in the same spirit as [boost]. Here are some of the features present in the library: -- check macros (assertions that aren't meant to be removed in releases) +- C functions for string manipulation - RAII utilities, such as for closing file descriptors -- low-level system information from cpuid +- `array`: thin wrapper around arrays (`scoped_array` + size) +- `pool`: fixed-size object pools +- bit manipulation +- bundles of header includes +- C++ support for pthreads, but allowing the user to access the underlying + `pthread_t` (a major annoyance of using boost threads was its complete + encapsulation) +- C++ support for [State Threads] +- check macros (like assertions but never removed from compilation) +- `deque`: simpler deque implementation that uses coarse-grained allocation +- error handling, such as `die()`, which leverages `strerror` - file I/O utilities, such as reading complete files +- function delegates (for use with C functions that take `(void*)(void*)`) - hash functions +- low-level system information from `cpuid` +- `nullptr`: from C++0x - pseudo-random number generators -- error handling, such as `die()` -- function delegates (for use with C functions that take `(void*)(void*)`) -- region-based allocation -- bundles of header includes +- region-based memory management - socket utilities -- C functions for string manipulation -- bit manipulation -- C++ support for pthreads, but allowing the user to access the underlying - `pthread_t` (a major annoyance of using boost threads was its complete - encapsulation) - portable re-implementations of pthread primitives such as barriers -- C++ support for [State Threads] +- time utilities, including timers and simpler interfaces to system clocks +- utilities for streams - utilities for [tamer] +- x86 architecture-specific tools -[State Threads]: http://state-threads.sourceforge.net/ +Setup +----- +Like [boost], just include these header files in your next C++ project, and +you're ready to go! + Related Work ------------ Modified: cpp-commons/trunk/src/commons/boost/delegates.h =================================================================== --- cpp-commons/trunk/src/commons/boost/delegates.h 2008-11-30 23:46:31 UTC (rev 1081) +++ cpp-commons/trunk/src/commons/boost/delegates.h 2008-12-02 05:34:16 UTC (rev 1082) @@ -14,7 +14,7 @@ void swallow(const function0<void> f) { try { f(); } - catch (exception &ex) { cerr << ex.what() << endl; } + catch (std::exception &ex) { cerr << ex.what() << endl; } } /** @@ -45,7 +45,7 @@ // run_function0_ex(void* p) // { // try { run_function0(p); } -// catch (exception &ex) { return exception(ex); } +// catch (std::exception &ex) { return std::exception(ex); } // } } Modified: cpp-commons/trunk/src/commons/check.h =================================================================== --- cpp-commons/trunk/src/commons/check.h 2008-11-30 23:46:31 UTC (rev 1081) +++ cpp-commons/trunk/src/commons/check.h 2008-12-02 05:34:16 UTC (rev 1082) @@ -5,6 +5,7 @@ #include <sstream> #include <string> #include <commons/die.h> +#include <boost/lexical_cast.hpp> // TODO: rename: // - chk(x): verifies and return x; if not, throw strerror @@ -31,9 +32,10 @@ // TODO: provide strerror in other functions too // TODO: better way to deal with errno? (rather than resetting it) + using namespace boost; using namespace std; - class check_exception : public exception + class check_exception : public std::exception { public: check_exception(const string & name) : name(name) {} @@ -43,7 +45,7 @@ const string name; }; - inline void + __attribute__((format(printf, 4, 0))) inline void _vcheck(bool cond, const char *file, int line, const char *fmt, va_list ap) { if (!cond) { @@ -58,7 +60,7 @@ } } - inline void + __attribute__((format(printf, 4, 5))) inline void _check(bool cond, const char *file, int line, const char *fmt, ...) { va_list ap; @@ -104,7 +106,8 @@ if (!x) { int e = errno; errno = 0; - _check(x, file, line, "expecting !=0, got %d: %s", x, strerror(e)); + _check(x, file, line, "expecting !=0, got %s: %s", + lexical_cast<string>(x).c_str(), strerror(e)); } return x; } @@ -130,7 +133,7 @@ if (l != r) { stringstream ss; ss << "expecting " << l << " == " << r; - _check(false, file, line, ss.str().c_str()); + _check(false, file, line, "%s", ss.str().c_str()); } } @@ -140,8 +143,9 @@ if (l < 0) { int e = errno; errno = 0; - _check(l == r, file, line, - "expecting %d, got %d: %s", r, l, strerror(e)); + stringstream ss; + ss << "expecting " << r << ", got " << l << ": " << strerror(e); + _check(l == r, file, line, "%s", ss.str().c_str()); } else { _checkeq(l, r, file, line); } Modified: cpp-commons/trunk/src/commons/die.h =================================================================== --- cpp-commons/trunk/src/commons/die.h 2008-11-30 23:46:31 UTC (rev 1081) +++ cpp-commons/trunk/src/commons/die.h 2008-12-02 05:34:16 UTC (rev 1082) @@ -15,7 +15,7 @@ * * TODO: move into C Commons. */ - void + __attribute__((format(printf, 1, 2))) void die(const char *format, ...) { const char *errstr; Modified: cpp-commons/trunk/src/commons/sockets.h =================================================================== --- cpp-commons/trunk/src/commons/sockets.h 2008-11-30 23:46:31 UTC (rev 1081) +++ cpp-commons/trunk/src/commons/sockets.h 2008-12-02 05:34:16 UTC (rev 1082) @@ -46,7 +46,7 @@ sa.sin_addr.s_addr = htonl(INADDR_ANY); // Bind the socket to the local socket address. - check0x(bind(fd, (sockaddr*) &sa, sizeof sa)); + check0x(::bind(fd, (sockaddr*) &sa, sizeof sa)); return fd; } catch (...) { Modified: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h 2008-11-30 23:46:31 UTC (rev 1081) +++ cpp-commons/trunk/src/commons/st/st.h 2008-12-02 05:34:16 UTC (rev 1082) @@ -100,7 +100,7 @@ sa.sin_addr = host; // Create the socket. - int sfd = checknneg(socket(PF_INET, SOCK_STREAM, 0)); + int sfd = checknnegerr(socket(PF_INET, SOCK_STREAM, 0)); st_netfd_t nfd = st_netfd_open_socket(sfd); // Connect. @@ -108,7 +108,7 @@ check0x(st_connect(nfd, (sockaddr*) &sa, sizeof sa, timeout)); return nfd; } catch (...) { - st_netfd_close(nfd); + check0x(st_netfd_close(nfd)); throw; } } @@ -313,17 +313,17 @@ st_intr_hub &hub_; }; - class st_group_join_exception : public exception + class st_group_join_exception : public std::exception { public: - st_group_join_exception(const map<st_thread_t, exception> &th2ex) : + st_group_join_exception(const map<st_thread_t, std::exception> &th2ex) : th2ex_(th2ex) {} virtual ~st_group_join_exception() throw() {} virtual const char *what() const throw() { if (!th2ex_.empty() && s == "") { bool first = true; stringstream ss; - typedef pair<st_thread_t, exception> p; + typedef pair<st_thread_t, std::exception> p; foreach (p p, th2ex_) { ss << (first ? "" : ", ") << p.first << " -> " << p.second.what(); first = false; @@ -333,7 +333,7 @@ return s.c_str(); } private: - map<st_thread_t, exception> th2ex_; + map<st_thread_t, std::exception> th2ex_; string s; }; @@ -357,10 +357,10 @@ { public: ~st_thread_group() { - map<st_thread_t, exception> th2ex; + map<st_thread_t, std::exception> th2ex; foreach (st_thread_t t, ts) { try { st_join(t); } - catch (exception &ex) { th2ex[t] = ex; } + catch (std::exception &ex) { th2ex[t] = ex; } } if (!th2ex.empty()) throw st_group_join_exception(th2ex); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |