]> git.jsancho.org Git - lugaru.git/blob - Source/Pointer.h
Added GPL license and headers.
[lugaru.git] / Source / Pointer.h
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 // Pointer.h: interface for the Pointer class.
23 //
24 //////////////////////////////////////////////////////////////////////
25
26 #ifndef _POINTER_H
27 #define _POINTER_H
28
29 template <class T>
30 class Pointer 
31 {
32 private:
33         void Destroy()
34         {
35                 p = NULL;
36                 GC::Collect();          
37         }
38
39 public:
40     T* p;
41
42     Pointer( T* p_ = NULL ) : p( p_ ) 
43         {
44         }
45
46     ~Pointer() 
47         { 
48                 GC::SetTotalBytesAllocated( GC::GetTotalBytesAllocated() - sizeof( *p ) );
49
50                 p->~T(); // Explicitely call the destructor
51                 
52                 Destroy();
53
54         }
55         
56     Pointer& operator = ( Pointer<T>& p_ )
57         {
58                 return operator = ( ( T* ) p_);
59         }
60     
61         Pointer& operator = ( T* p_ ) 
62         {     
63                 Destroy();
64                 p = p_; 
65                 return *this;
66     }
67
68     operator T*() 
69         { 
70                 return p; 
71         }
72     
73         T& operator*() 
74         { 
75                 return *p; 
76         }
77     
78         T* operator->() 
79         { 
80                 return p; 
81         }
82     
83 // For automatic type conversion during new call
84         operator void**()
85         {
86                 return ( void** ) & p;
87         }
88 };
89
90
91 #endif 
92