]> git.jsancho.org Git - lugaru.git/blob - Source/Pointer.h
b9c728778887e1c0926ba78e3877fe1c3378c0e1
[lugaru.git] / Source / Pointer.h
1 // Pointer.h: interface for the Pointer class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #ifndef _POINTER_H
6 #define _POINTER_H
7
8 template <class T>
9 class Pointer 
10 {
11 private:
12         void Destroy()
13         {
14                 p = NULL;
15                 GC::Collect();          
16         }
17
18 public:
19     T* p;
20
21     Pointer( T* p_ = NULL ) : p( p_ ) 
22         {
23         }
24
25     ~Pointer() 
26         { 
27                 GC::SetTotalBytesAllocated( GC::GetTotalBytesAllocated() - sizeof( *p ) );
28
29                 p->~T(); // Explicitely call the destructor
30                 
31                 Destroy();
32
33         }
34         
35     Pointer& operator = ( Pointer<T>& p_ )
36         {
37                 return operator = ( ( T* ) p_);
38         }
39     
40         Pointer& operator = ( T* p_ ) 
41         {     
42                 Destroy();
43                 p = p_; 
44                 return *this;
45     }
46
47     operator T*() 
48         { 
49                 return p; 
50         }
51     
52         T& operator*() 
53         { 
54                 return *p; 
55         }
56     
57         T* operator->() 
58         { 
59                 return p; 
60         }
61     
62 // For automatic type conversion during new call
63         operator void**()
64         {
65                 return ( void** ) & p;
66         }
67 };
68
69
70 #endif 
71