1 /*******************************************************************************
2  * pointer.h
3  *
4  * This module contains to Pointer class which is a limited version of std::auto_ptr.
5  *
6  * ---------------------------------------------------------------------------
7  * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
8  * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
9  *
10  * POV-Ray is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as
12  * published by the Free Software Foundation, either version 3 of the
13  * License, or (at your option) any later version.
14  *
15  * POV-Ray is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * ---------------------------------------------------------------------------
23  * POV-Ray is based on the popular DKB raytracer version 2.12.
24  * DKBTrace was originally written by David K. Buck.
25  * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
26  * ---------------------------------------------------------------------------
27  * $File: //depot/public/povray/3.x/source/base/pointer.h $
28  * $Revision: #1 $
29  * $Change: 6069 $
30  * $DateTime: 2013/11/06 11:59:40 $
31  * $Author: chrisc $
32  *******************************************************************************/
33 
34 #ifndef POINTER_H
35 #define POINTER_H
36 
37 #include "configbase.h"
38 
39 namespace pov_base
40 {
41 
42 // TODO FIXME - the Pointer class should be replaced by one of the boost/std smart pointers
43 
44 template<class X> class Pointer
45 {
46 	public:
47 		explicit Pointer(X *p = NULL)
48 		{
49 			ptr = p;
50 		}
51 
Pointer(X & a)52 		Pointer(X& a)
53 		{
54 			ptr = a.release();
55 		}
56 
Pointer(Pointer<Y> & a)57 		template<class Y> Pointer(Pointer<Y>& a)
58 		{
59 			ptr = a.release();
60 		}
61 
~Pointer()62 		~Pointer()
63 		{
64 			if(ptr != NULL)
65 				delete ptr;
66 			ptr = NULL;
67 		}
68 
69 		Pointer& operator=(Pointer& a)
70 		{
71 			reset(a.release());
72 			return *this;
73 		}
74 
75 		template<class Y> Pointer& operator=(Pointer<Y>& a)
76 		{
77 			reset(a.release());
78 			return *this;
79 		}
80 
81 		X& operator*() const
82 		{
83 			return *ptr;
84 		}
85 
86 		const X *operator->() const
87 		{
88 			return ptr;
89 		}
90 
91 		X *operator->()
92 		{
93 			return ptr;
94 		}
95 
get()96 		const X *get() const
97 		{
98 			return ptr;
99 		}
100 
release()101 		X *release()
102 		{
103 			X *t = ptr;
104 			ptr = NULL;
105 			return t;
106 		}
107 
108 		void reset(X *p = NULL)
109 		{
110 			if(ptr != NULL)
111 				delete ptr;
112 			ptr = p;
113 		}
114 
115 		bool operator==(const void *p) const
116 		{
117 			return (ptr == p);
118 		}
119 
120 		bool operator!=(const void *p) const
121 		{
122 			return (ptr != p);
123 		}
124 	private:
125 		X *ptr;
126 };
127 
128 }
129 
130 #endif
131