stl_alloc.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) Sergey P. Derevyago, 2008-2009.
00003  *
00004  * Permission to copy, use, modify, sell and distribute this software is granted
00005  * provided this copyright notice appears in all copies.
00006  * This software is provided "as is" without express or implied warranty, and
00007  * with no claim as to its suitability for any purpose.
00008  *
00009  */
00010 
00015 #ifndef __DERS__STL_ALLOC_HPP__
00016 #define __DERS__STL_ALLOC_HPP__
00017 
00018 #include <ders/config.hpp>
00019 #include <ders/mem_pool.hpp>
00020 
00021 namespace ders {  // ::ders
00022 
00026 template<class T>
00027 class stl_allocator {
00028       mem_pool& mp;
00029 
00030  public:
00031       typedef size_t size_type;
00032       typedef ptrdiff_t difference_type;
00033       typedef T* pointer;
00034       typedef const T* const_pointer;
00035       typedef T& reference;
00036       typedef const T& const_reference;
00037       typedef T value_type;
00038 
00040       template<class U> struct rebind { typedef stl_allocator<U> other; };
00041 
00042       stl_allocator();  // not implemented
00043       explicit stl_allocator(mem_pool& m) : mp(m) {}
00044       template<class U> stl_allocator(const stl_allocator<U>& sa) :
00045         mp(sa.pool()) {}
00046 
00047       pointer address(reference x) const { return &x; }
00048       const_pointer address(const_reference x) const { return &x; }
00049       pointer allocate(size_type n, std::allocator<void>::const_pointer = 0);
00050       void deallocate(pointer p, size_type n);
00051       size_type max_size() const throw() { return size_t(-1)/sizeof(T); }
00052       void construct(pointer p, const T& val) { new(p) T(val); }
00053       void destroy(pointer p) { p->~T(); }
00054 
00055       mem_pool& pool() const { return mp; }
00056 };
00057 
00058 template<class T>
00059 inline typename stl_allocator<T>::pointer stl_allocator<T>::allocate(size_type
00060   n, std::allocator<void>::const_pointer)
00061 { return (n!=0) ? static_cast<pointer>(mp.allocate(n*sizeof(T))) : 0; }
00062 
00063 template<class T>
00064 inline void stl_allocator<T>::deallocate(pointer p, size_type n)
00065 { if (n!=0) mp.deallocate(p, n*sizeof(T)); }
00066 
00067 template<class T, class U>
00068 inline bool operator==(const stl_allocator<T>& at, const stl_allocator<U>& au)
00069 { return &at.pool()==&au.pool(); }
00070 
00071 template<class T, class U>
00072 inline bool operator!=(const stl_allocator<T>& at, const stl_allocator<U>& au)
00073 { return !(at==au); }
00074 
00075 }  // namespace ::ders
00076 
00077 #endif  // __DERS__STL_ALLOC_HPP__
00078 

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5