destroy.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 
00016 #ifndef __DERS__DESTROY_HPP__
00017 #define __DERS__DESTROY_HPP__
00018 
00019 #include <ders/config.hpp>
00020 #include <typeinfo>
00021 #include <assert.h>
00022 #include <ders/hard_asrt.hpp>
00023 #include <ders/mem_pool.hpp>
00024 
00025 namespace ders {  // ::ders
00026 
00031 class destroyable {
00032       destroyable& operator=(const destroyable&);
00033       void* operator new(size_t size);
00034 
00035  protected:
00036       template<class T>
00037       static void destroy_this(T* ptr, mem_pool& mp2);
00038 
00039       void operator delete(void* ptr, size_t size) { hard_assert(false); }
00040 
00041  public:
00042       virtual void destroy(mem_pool& mp2)=0;
00043       virtual ~destroyable() {}
00044 };
00045 
00046 template<class T>
00047 inline void destroyable::destroy_this(T* ptr, mem_pool& mp2)
00048 {
00049  assert(typeid(*ptr)==typeid(T));
00050 
00051  ptr->~T();
00052  mp2.deallocate(ptr, sizeof(T));
00053 }
00054 
00055 namespace detail {  // ::ders::detail
00056 
00057 template<class T>
00058 inline void destroy(void* p, mem_pool& mp)
00059 {
00060  T* ptr=static_cast<T*>(p);
00061 
00062  ptr->~T();
00063  mp.deallocate(ptr, sizeof(T));
00064 }
00065 
00066 template<class T>
00067 inline void destroy(destroyable* ptr, mem_pool& mp)
00068 { ptr->destroy(mp); }
00069 
00070 }  // namespace ::ders::detail
00071 
00078 template<class T>
00079 inline void destroy(T* ptr, mem_pool& mp)
00080 { if (ptr) detail::destroy<T>(ptr, mp); }
00081 
00082 }  // namespace ::ders
00083 
00084 #endif  // __DERS__DESTROY_HPP__
00085 

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