Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

pointer_cleaner.h

00001 #ifndef s11n_POINTER_CLEANER_H_INCLUDED 00002 #define s11n_POINTER_CLEANER_H_INCLUDED 00003 //////////////////////////////////////////////////////////////////////////////// 00004 // pointer_cleaner.h 00005 // a poor-man's multi-pointer cleaner-upper. 00006 // Author: stephan beal <stephan@s11n.net> 00007 // License: Public Domain 00008 //////////////////////////////////////////////////////////////////////////////// 00009 00010 #include <algorithm> // for_each() 00011 #include "functor.h" // object_deleter 00012 00013 namespace s11n 00014 { 00015 00016 /** 00017 A poor-man's garbage collector. It destroys any pointers 00018 added to it when it is destroyed. 00019 00020 This type models Non-Copyable. The only thing of use 00021 you can do with them is create them and then use their 00022 add() function. 00023 */ 00024 template <typename T> 00025 class pointer_cleaner 00026 { 00027 public: 00028 typedef T value_type; 00029 00030 /** 00031 This is the only way to construct a new cleaner: 00032 they may not be copied. 00033 */ 00034 pointer_cleaner() {} 00035 00036 /** 00037 Deletes any objects added via o. 00038 */ 00039 ~pointer_cleaner() 00040 { 00041 std::for_each( this->m_list.begin(), 00042 this->m_list.end(), 00043 object_deleter() 00044 ); 00045 } 00046 00047 /** 00048 Adds o to be deleted when this object goes out of scope. 00049 Once you do this, there is no way to remove o from the 00050 this object's Path of Destruction. 00051 */ 00052 void add( value_type * o ) { this->m_list.push_back( o ); } 00053 00054 private: 00055 struct object_deleter 00056 { 00057 template <typename X> 00058 void operator()( const X * t ) 00059 { // i don't understand why delete( const * ) is legal. 00060 delete( t ); 00061 } 00062 }; 00063 00064 pointer_cleaner( const pointer_cleaner & ); // unimpl 00065 pointer_cleaner & operator=( const pointer_cleaner & ); // unimpl 00066 typedef std::list<value_type *> list_type; 00067 list_type m_list; 00068 }; 00069 00070 } // namespace 00071 00072 #endif // s11n_POINTER_CLEANER_H_INCLUDED

Generated on Tue Jul 20 10:46:48 2004 for s11n by doxygen 1.3.7