Use the util namespace for more classes

This commit is contained in:
Danny Robson 2011-10-07 21:58:16 +11:00
parent 6e2ba427f9
commit 641a15f0ec
6 changed files with 154 additions and 147 deletions

View File

@ -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

View File

@ -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

View File

@ -22,4 +22,4 @@
// Explicitly instance a possibly useful specialisation so that we can more easily catch linker errors.
template class pool<std::string>;
template class util::pool<std::string>;

126
pool.hpp
View File

@ -22,79 +22,79 @@
#include "nocopy.hpp"
namespace util {
template <typename T>
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<T>'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<T>'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 <typename ...Args>
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 <typename ...Args>
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

View File

@ -20,12 +20,14 @@
#include "version.hpp"
#include <cstring>
#include <stdexcept>
#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;
}
}

View File

@ -25,62 +25,64 @@
#include <iostream>
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 <unsigned int> m_values;
release_t m_release;
NUM_OFFSETS
};
void parse (const std::string&);
std::vector <unsigned int> 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