From 641a15f0ec3746da09aabbb489dee4d02cf877d2 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 7 Oct 2011 21:58:16 +1100 Subject: [PATCH] Use the util namespace for more classes --- nocopy.hpp | 16 +++--- noncopyable.hpp | 23 --------- pool.cpp | 2 +- pool.hpp | 126 ++++++++++++++++++++++++------------------------ version.cpp.rl | 42 +++++++++++++--- version.hpp | 92 ++++++++++++++++++----------------- 6 files changed, 154 insertions(+), 147 deletions(-) delete mode 100644 noncopyable.hpp diff --git a/nocopy.hpp b/nocopy.hpp index d8c2f53e..42d82573 100644 --- a/nocopy.hpp +++ b/nocopy.hpp @@ -22,13 +22,15 @@ #include "debug.hpp" -class nocopy { - public: - nocopy () { ; } +namespace util { + class nocopy { + public: + nocopy () { ; } - private: - nocopy (const nocopy &) = delete; - nocopy& operator =(const nocopy &) = delete; -}; + private: + nocopy (const nocopy &) = delete; + nocopy& operator =(const nocopy &) = delete; + }; +} #endif diff --git a/noncopyable.hpp b/noncopyable.hpp deleted file mode 100644 index 56302501..00000000 --- a/noncopyable.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// Derived from boost::noncopyable -// -// (C) Copyright Beman Dawes 1999-2003. Distributed under the Boost -// Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// Contributed by Dave Abrahams - -#ifndef __UTIL_NONCOPYABLE_HPP -#define __UTIL_NONCOPYABLE_HPP - -class noncopyable -{ - protected: - noncopyable() {} - ~noncopyable() {} - - private: // emphasize the following members are private - noncopyable( const noncopyable& ); - const noncopyable& operator=( const noncopyable& ); -}; - -#endif diff --git a/pool.cpp b/pool.cpp index 8479494e..6327b392 100644 --- a/pool.cpp +++ b/pool.cpp @@ -22,4 +22,4 @@ // Explicitly instance a possibly useful specialisation so that we can more easily catch linker errors. -template class pool; +template class util::pool; diff --git a/pool.hpp b/pool.hpp index 21ac6245..fc16fbcf 100644 --- a/pool.hpp +++ b/pool.hpp @@ -22,79 +22,79 @@ #include "nocopy.hpp" - +namespace util { template -class pool : public nocopy { - protected: - union node { - char _data[sizeof (T)]; - node *_chain; - }; + class pool : public nocopy { + protected: + union node { + char _data[sizeof (T)]; + node *_chain; + }; - node *m_head; - node *m_next; - unsigned int m_capacity; + node *m_head; + node *m_next; + unsigned int m_capacity; - public: - pool (unsigned int _capacity): - m_capacity (_capacity) - { - static_assert (sizeof (T) >= sizeof (uintptr_t), - "pool's chained block system requires that T be at least pointer sized"); + public: + pool (unsigned int _capacity): + m_capacity (_capacity) + { + static_assert (sizeof (T) >= sizeof (uintptr_t), + "pool's chained block system requires that T be at least pointer sized"); - m_head = (node *)operator new (sizeof (T) * m_capacity); - m_next = m_head; + m_head = (node *)operator new (sizeof (T) * m_capacity); + m_next = m_head; - for (unsigned int i = 0; i < m_capacity - 1; ++i) - m_next[i]._chain = &m_next[i + 1]; - m_next[m_capacity - 1]._chain = NULL; - } - - - ~pool () { - check (m_next != NULL); - - unsigned int doomed_count = 0; - for (node *cursor = m_next; cursor != NULL; cursor = cursor->_chain) - ++doomed_count; - - check_eq (doomed_count, m_capacity); - operator delete (m_head); - } - - - unsigned int capacity (void) const - { return m_capacity; } - - - template - T* acquire (Args&... args) { - if (!m_next) - throw std::bad_alloc (); - - node *newnext = m_next->_chain; - T *data = (T*)&m_next->_data; - - try { - new (data) T (args...); - } catch (...) { - m_next->_chain = newnext; - throw; + for (unsigned int i = 0; i < m_capacity - 1; ++i) + m_next[i]._chain = &m_next[i + 1]; + m_next[m_capacity - 1]._chain = NULL; } - m_next = newnext; - return data; - } + + ~pool () { + check (m_next != NULL); + + unsigned int doomed_count = 0; + for (node *cursor = m_next; cursor != NULL; cursor = cursor->_chain) + ++doomed_count; + + check_eq (doomed_count, m_capacity); + operator delete (m_head); + } + + + unsigned int capacity (void) const + { return m_capacity; } - void release (T *data) { - data->~T(); - node *newnode = (node *)data; + template + T* acquire (Args&... args) { + if (!m_next) + throw std::bad_alloc (); - newnode->_chain = m_next; - m_next = newnode; - } -}; + node *newnext = m_next->_chain; + T *data = (T*)&m_next->_data; + + try { + new (data) T (args...); + } catch (...) { + m_next->_chain = newnext; + throw; + } + m_next = newnext; + return data; + } + + + void release (T *data) { + data->~T(); + node *newnode = (node *)data; + + newnode->_chain = m_next; + m_next = newnode; + } + }; +} #endif // __UTIL_POOL_HPP diff --git a/version.cpp.rl b/version.cpp.rl index 2ea51ac3..0fa02121 100644 --- a/version.cpp.rl +++ b/version.cpp.rl @@ -20,12 +20,14 @@ #include "version.hpp" +#include #include #include "debug.hpp" using namespace std; +using namespace util; version::version (unsigned int _major, @@ -45,6 +47,14 @@ version::version (const string& str): } +version::version (const char *str): + m_values (NUM_OFFSETS, 0), + m_release (RELEASE_PRODUCTION) { + m_values.clear (); + parse (str); +} + + static void check_release (version::release_t r) { switch (r) { @@ -109,6 +119,20 @@ version::parse (const string& str) { } +void +version::parse (const char *str) { + unsigned int current; + + size_t cs; + const char *p = str, + *pe = str + strlen (str), + *eof = pe; + + %%write init; + %%write exec; +} + + static string release_string (const version::release_t r) { switch (r) { @@ -140,15 +164,17 @@ version::operator > (const version &rhs) const { } -ostream& -operator <<(ostream& os, const version& rhs) { - auto i = rhs.m_values.begin(); - os << *i; ++i; +namespace util { + ostream& + operator <<(ostream& os, const util::version& rhs) { + auto i = rhs.m_values.begin(); + os << *i; ++i; - for (; i != rhs.m_values.end(); ++i) - os << '.' << *i; + for (; i != rhs.m_values.end(); ++i) + os << '.' << *i; - os << release_string (rhs.m_release); - return os; + os << release_string (rhs.m_release); + return os; + } } diff --git a/version.hpp b/version.hpp index 3f57a3bb..fa0802d8 100644 --- a/version.hpp +++ b/version.hpp @@ -25,62 +25,64 @@ #include -class version { - public: - enum release_t { - RELEASE_ALPHA, - RELEASE_BETA, - RELEASE_GAMMA, - RELEASE_PRODUCTION - }; +namespace util { + class version { + public: + enum release_t { + RELEASE_ALPHA, + RELEASE_BETA, + RELEASE_GAMMA, + RELEASE_PRODUCTION + }; - version (unsigned int _major, - unsigned int _minor); - version (const std::string& str); - virtual ~version () { ; } + version (unsigned int _major, + unsigned int _minor); + version (const std::string& str); + version (const char *str); - virtual void sanity (void) const; + virtual ~version () { ; } - protected: - enum { - OFFSET_MAJOR = 0, - OFFSET_MINOR = 1, - OFFSET_POINT = 2, - OFFSET_BUILD = 3, + virtual void sanity (void) const; - NUM_OFFSETS - }; + protected: + enum { + OFFSET_MAJOR = 0, + OFFSET_MINOR = 1, + OFFSET_POINT = 2, + OFFSET_BUILD = 3, - std::vector m_values; - release_t m_release; + NUM_OFFSETS + }; - void parse (const std::string&); + std::vector m_values; + release_t m_release; - public: + void parse (const std::string&); + void parse (const char*); - unsigned int major (void) const - { return m_values[OFFSET_MAJOR]; } - unsigned int minor (void) const - { return m_values[OFFSET_MINOR]; } - unsigned int point (void) const - { return m_values[OFFSET_POINT]; } - unsigned int build (void) const - { return m_values[OFFSET_BUILD]; } + public: - bool operator < (const version& rhs) const; - bool operator > (const version& rhs) const; - bool operator >= (const version& rhs) const; - bool operator <= (const version& rhs) const; - bool operator == (const version& rhs) const - { return m_values == rhs.m_values && - m_release == rhs.m_release; } + unsigned int major (void) const + { return m_values[OFFSET_MAJOR]; } + unsigned int minor (void) const + { return m_values[OFFSET_MINOR]; } + unsigned int point (void) const + { return m_values[OFFSET_POINT]; } + unsigned int build (void) const + { return m_values[OFFSET_BUILD]; } - friend std::ostream& - operator <<(std::ostream& os, const version& rhs); -}; + bool operator < (const version& rhs) const; + bool operator > (const version& rhs) const; + bool operator >= (const version& rhs) const; + bool operator <= (const version& rhs) const; + bool operator == (const version& rhs) const + { return m_values == rhs.m_values && + m_release == rhs.m_release; } + friend std::ostream& + operator <<(std::ostream& os, const version& rhs); + }; +} -std::ostream& -operator <<(std::ostream& os, const version& rhs); #endif // __VERSION_HPP