Página Principal   Hierarquia de Classes   Lista de Componentes   Lista de Arquivos   Componentes Membros  

Reference.hpp

00001 /*      Reference.hpp - Morpheous
00002  *
00003  *  $Header$
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #ifndef __REFERENCE_HPP__
00021 #define __REFERENCE_HPP__
00022 
00023 #include <hash_map>
00024 
00025 
00030 template <class T>
00031 class Reference
00032 {
00033 private:
00037     static std::hash_map<T*, int> map;
00038 
00042     T* ptr;
00043 
00044 public:
00048     Reference(T* ptr)
00049     {
00050         this->ptr = ptr;
00051     
00052         if(map.count(ptr) == 0)
00053         {
00054             map[ptr] = 0;
00055         }
00056         map[ptr]++;
00057     }
00058 
00062     ~Reference()
00063     {
00064         map[ptr]--;
00065         if(map[ptr] == 0)
00066         {
00067             map.erase(ptr);
00068             delete ptr;
00069         }
00070     
00071         ptr = NULL;
00072     }
00073 
00077     operator T*()
00078     {
00079         return ptr;
00080     }
00081 
00085     T* operator->()
00086     {
00087         return ptr;
00088     }
00089 };
00090     
00091 #endif // __REFERENCE_HPP__

Gerado em Sun Sep 21 20:18:44 2003 para Morpheous por doxygen1.2.13.1 escrito por Dimitri van Heesch, © 1997-2001