[Assorted-commits] SF.net SVN: assorted:[1315] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-03-20 20:20:38
|
Revision: 1315 http://assorted.svn.sourceforge.net/assorted/?rev=1315&view=rev Author: yangzhang Date: 2009-03-20 20:20:25 +0000 (Fri, 20 Mar 2009) Log Message: ----------- - fixed all global functions to be either inline or static - added break_exception - added UNUSED - tweaked timer ctor - fixed missing include for snap_map - added operator[]() and at() to array classes Modified Paths: -------------- cpp-commons/trunk/src/commons/algo.h cpp-commons/trunk/src/commons/array.h cpp-commons/trunk/src/commons/delegates.h cpp-commons/trunk/src/commons/exceptions.h cpp-commons/trunk/src/commons/memory.h cpp-commons/trunk/src/commons/snap_map.h cpp-commons/trunk/src/commons/sockets.h cpp-commons/trunk/src/commons/st/st.h cpp-commons/trunk/src/commons/time.h cpp-commons/trunk/src/commons/utility.h Modified: cpp-commons/trunk/src/commons/algo.h =================================================================== --- cpp-commons/trunk/src/commons/algo.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/algo.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -5,7 +5,7 @@ namespace commons { - __attribute__((unused)) inline void + inline void swap(size_t &x, size_t &y) { x = x ^ y; y = x ^ y; Modified: cpp-commons/trunk/src/commons/array.h =================================================================== --- cpp-commons/trunk/src/commons/array.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/array.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -3,7 +3,7 @@ #include <algorithm> #include <commons/algo.h> -#include <commons/check.h> +#include <commons/assert.h> #include <commons/nullptr.h> #include <commons/unique_ptr.h> #include <commons/utility.h> @@ -34,6 +34,8 @@ T *end() const { return p_ + n_; } const T &operator[](size_t i) const { return p_[i]; } T &operator[](size_t i) { return p_[i]; } + const T &at(size_t i) const { ASSERT(i < n_); return p_[i]; } + T &at(size_t i) { ASSERT(i < n_); return p_[i]; } void reset(T *p, size_t n) { p_ = p; n_ = n; } private: T *p_; @@ -74,6 +76,8 @@ T *release() { return p_.release(); } T *begin() const { return p_.get(); } T *end() const { return this->get() + n_; } + const T &at(size_t i) const { ASSERT(i < n_); return p_[i]; } + T &at(size_t i) { ASSERT(i < n_); return p_[i]; } const T &operator[](size_t i) const { return p_[i]; } T &operator[](size_t i) { return p_[i]; } void reset(T *p, size_t n) { p_.reset(p); n_ = n; } @@ -86,7 +90,7 @@ * Swap two arrays. */ template<typename T> - void + inline void swap(array<T> &a, array<T> &b) { std::swap(a.p_, b.p_); @@ -99,7 +103,7 @@ */ template<typename T> void - swap(sized_array<T> &a, sized_array<T> &b) + inline swap(sized_array<T> &a, sized_array<T> &b) { std::swap(a.p_, b.p_); swap(a.n_, b.n_); @@ -118,6 +122,8 @@ T *release() { T *p = p_; p_ = nullptr; scoped_ = false; return p; } T *get() { return p_; } const T *get() const { return p_; } + const T &operator[](size_t i) const { return p_[i]; } + T &operator[](size_t i) { return p_[i]; } operator T*() { return p_; } operator const T*() const { return p_; } bool scoped() const { return scoped_; } Modified: cpp-commons/trunk/src/commons/delegates.h =================================================================== --- cpp-commons/trunk/src/commons/delegates.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/delegates.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -4,6 +4,7 @@ #include <boost/function.hpp> #include <boost/scoped_ptr.hpp> #include <iostream> // for cerr +#include <commons/utility.h> namespace commons { @@ -13,7 +14,7 @@ typedef std::function<void()> fn; - void + UNUSED static void swallow(const fn f) { try { f(); } catch (std::exception &ex) { cerr << ex.what() << endl; } @@ -22,7 +23,7 @@ /** * Delegate for running a 0-ary void function. */ - void + UNUSED static void run_function0(const void *p) { scoped_ptr<const fn> pf(reinterpret_cast<const fn*>(p)); @@ -32,7 +33,7 @@ /** * NULL void*-returning delegate for running a 0-ary void function. */ - void* + UNUSED static void* run_function0_null(void* p) { run_function0(p); Modified: cpp-commons/trunk/src/commons/exceptions.h =================================================================== --- cpp-commons/trunk/src/commons/exceptions.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/exceptions.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -39,6 +39,11 @@ const string msg_; }; + /** + * Convenience class for performing long-jumping break. + */ + class break_exception : public std::exception {}; + #define throw_not_supported() throw not_supported_exception(__PRETTY_FUNCTION__) #define throw_not_implemented() throw not_implemented_exception(__PRETTY_FUNCTION__) Modified: cpp-commons/trunk/src/commons/memory.h =================================================================== --- cpp-commons/trunk/src/commons/memory.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/memory.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -1,6 +1,8 @@ #ifndef COMMONS_MEMORY_H #define COMMONS_MEMORY_H +#include <cstring> // for size_t + namespace commons { Modified: cpp-commons/trunk/src/commons/snap_map.h =================================================================== --- cpp-commons/trunk/src/commons/snap_map.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/snap_map.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -6,6 +6,7 @@ #include <commons/assert.h> #include <commons/exceptions.h> #include <utility> +#include <map> namespace commons { @@ -143,7 +144,7 @@ typedef typename traits::const_reference const_reference; typedef typename traits::pointer pointer; typedef typename traits::const_pointer const_pointer; - typedef map<size_t, unique_ptr<array<value_type> > > shadowmap; + typedef map<size_t, unique_ptr<commons::array<value_type> > > shadowmap; private: static const size_t initsize = 1 << 20, pagesize = 1 << 15, pagemask = pagesize - 1; Modified: cpp-commons/trunk/src/commons/sockets.h =================================================================== --- cpp-commons/trunk/src/commons/sockets.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/sockets.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -31,7 +31,7 @@ /** * Make a TCP socket non-blocking. */ - int + inline int set_non_blocking(int fd) { checknnegerr(fcntl(fd, F_SETFL, @@ -42,7 +42,7 @@ /** * Create a TCP socket. */ - int + inline int tcp_socket(bool nb) { int fd = checknnegerr(socket(PF_INET, SOCK_STREAM, 0)); @@ -63,7 +63,7 @@ /** * Initialize an inet address with just the port. */ - void + inline void sockaddr_init(sockaddr_in &a, uint16_t port) { bzero(&a, sizeof a); @@ -74,7 +74,7 @@ /** * Initialize an inet address. */ - void + static void sockaddr_init(sockaddr_in &a, const char *host, uint16_t port) { sockaddr_init(a, port); @@ -93,7 +93,7 @@ /** * Initialize an inet address. */ - void + inline void sockaddr_init(sockaddr_in &a, in_addr host, uint16_t port) { sockaddr_init(a, port); @@ -104,7 +104,7 @@ * Construct an inet address. */ template <typename T> - sockaddr_in + inline sockaddr_in make_sockaddr(T host, uint16_t port) { sockaddr_in a; @@ -118,7 +118,7 @@ * \param[in] nb Whether the socket should be non-blocking. * \return The server socket. */ - int + UNUSED static int server_socket(uint16_t port, bool nb = false) { // Create the socket. @@ -145,7 +145,7 @@ * \param[in] nb Whether the socket should be non-blocking. * \return The listener socket. */ - int + UNUSED static int tcp_listen(uint16_t port, bool nb = false) { int fd = server_socket(port, nb); @@ -163,7 +163,7 @@ /** * Connect to a TCP socket. */ - int + UNUSED static int tcp_connect(const char *host, uint16_t port) { closingfd c(tcp_socket(false)); Modified: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/st/st.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -50,7 +50,7 @@ st_mutex_t mx_; }; - void + UNUSED static void st_read(st_netfd_t fd, void *buf, size_t len) { checkeqnneg(st_read_fully(fd, buf, len, ST_UTIME_NO_TIMEOUT), ssize_t(len)); @@ -63,7 +63,7 @@ st_read(fd, &x, sizeof x); } - void + UNUSED static void st_write(st_netfd_t fd, const void *buf, size_t len) { checkeqnneg(st_write(fd, buf, len, ST_UTIME_NO_TIMEOUT), ssize_t(len)); @@ -82,7 +82,7 @@ * \return The new pthread_t on success, 0 on failure. * \todo Is it safe to treat the pthread_t as a pointer? */ - st_thread_t + UNUSED static st_thread_t st_spawn(const fn& f) { return st_thread_create(&run_function0_null, @@ -91,7 +91,7 @@ default_stack_size); } - void + UNUSED static void st_join(st_thread_t t) { check0x(st_thread_join(t, nullptr)); @@ -103,7 +103,7 @@ * \param[in] port The destination port. * \param[in] timeout The timeout for the connect operation. */ - st_netfd_t + UNUSED static st_netfd_t st_tcp_connect(in_addr host, uint16_t port, st_utime_t timeout) { // Create remote socket address. @@ -125,7 +125,7 @@ * operation. * \todo Create variants that take and/or return sockaddr_in's. */ - st_netfd_t + UNUSED static st_netfd_t st_tcp_connect(const char *host, uint16_t port, st_utime_t timeout) { in_addr ipaddr; @@ -145,7 +145,7 @@ * \param[in] port The port to listen on. * \return The st_netfd_t. */ - st_netfd_t + UNUSED static st_netfd_t st_tcp_listen(uint16_t port) { int sfd = tcp_listen(port); @@ -193,7 +193,7 @@ bool b; }; - void toggle(st_bool& b) { if (b) b.reset(); else b.set(); } + UNUSED static void toggle(st_bool& b) { if (b) b.reset(); else b.set(); } /** * Wraps st_mutex_* errno-functions with exceptions and cleans up on Modified: cpp-commons/trunk/src/commons/time.h =================================================================== --- cpp-commons/trunk/src/commons/time.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/time.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -32,7 +32,7 @@ class timer { public: - timer(const string label) : + timer(const string &label) : label(label), start(current_time_millis()), last(start) {} void print() { Modified: cpp-commons/trunk/src/commons/utility.h =================================================================== --- cpp-commons/trunk/src/commons/utility.h 2009-03-20 17:45:38 UTC (rev 1314) +++ cpp-commons/trunk/src/commons/utility.h 2009-03-20 20:20:25 UTC (rev 1315) @@ -15,4 +15,6 @@ #define BEGIN_NAMESPACE(ns) namespace ns { #define END_NAMESPACE } +#define UNUSED __attribute__((unused)) + #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |