xref: /haiku/headers/private/shared/AutoDeleter.h (revision 582da17386c4a192ca30270d6b0b95f561cf5843)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		AutoDeleter.h
23 //	Author(s):		Ingo Weinhold (bonefish@users.sf.net)
24 //	Description:	Scope-based automatic deletion of objects/arrays.
25 //					ObjectDeleter - deletes an object
26 //					ArrayDeleter  - deletes an array
27 //					MemoryDeleter - free()s malloc()ed memory
28 //------------------------------------------------------------------------------
29 
30 #ifndef _AUTO_DELETER_H
31 #define _AUTO_DELETER_H
32 
33 #include <stdlib.h>
34 
35 namespace BPrivate {
36 
37 // AutoDeleter
38 
39 template<typename C, typename DeleteFunc>
40 class AutoDeleter {
41 public:
42 	inline AutoDeleter()
43 		: fObject(NULL)
44 	{
45 	}
46 
47 	inline AutoDeleter(C *object)
48 		: fObject(object)
49 	{
50 	}
51 
52 	inline ~AutoDeleter()
53 	{
54 		fDelete(fObject);
55 	}
56 
57 	inline void SetTo(C *object)
58 	{
59 		if (object != fObject) {
60 			fDelete(fObject);
61 			fObject = object;
62 		}
63 	}
64 
65 	inline void Unset()
66 	{
67 		SetTo(NULL);
68 	}
69 
70 	inline void Delete()
71 	{
72 		SetTo(NULL);
73 	}
74 
75 	inline C *Get() const
76 	{
77 		return fObject;
78 	}
79 
80 	inline C *Detach()
81 	{
82 		C *object = fObject;
83 		fObject = NULL;
84 		return object;
85 	}
86 
87 private:
88 	C			*fObject;
89 	DeleteFunc	fDelete;
90 };
91 
92 
93 // ObjectDeleter
94 
95 template<typename C>
96 struct ObjectDelete
97 {
98 	inline void operator()(C *object)
99 	{
100 		delete object;
101 	}
102 };
103 
104 template<typename C>
105 struct ObjectDeleter : AutoDeleter<C, ObjectDelete<C> >
106 {
107 	ObjectDeleter() : AutoDeleter<C, ObjectDelete<C> >() {}
108 	ObjectDeleter(C *object) : AutoDeleter<C, ObjectDelete<C> >(object) {}
109 };
110 
111 
112 // ArrayDeleter
113 
114 template<typename C>
115 struct ArrayDelete
116 {
117 	inline void operator()(C *array)
118 	{
119 		delete[] array;
120 	}
121 };
122 
123 template<typename C>
124 struct ArrayDeleter : AutoDeleter<C, ArrayDelete<C> >
125 {
126 	ArrayDeleter() : AutoDeleter<C, ArrayDelete<C> >() {}
127 	ArrayDeleter(C *array) : AutoDeleter<C, ArrayDelete<C> >(array) {}
128 };
129 
130 
131 // MemoryDeleter
132 
133 struct MemoryDelete
134 {
135 	inline void operator()(void *memory)
136 	{
137 		free(memory);
138 	}
139 };
140 
141 struct MemoryDeleter : AutoDeleter<void, MemoryDelete >
142 {
143 	MemoryDeleter() : AutoDeleter<void, MemoryDelete >() {}
144 	MemoryDeleter(void *memory) : AutoDeleter<void, MemoryDelete >(memory) {}
145 };
146 
147 }	// namespace BPrivate
148 
149 using BPrivate::ObjectDeleter;
150 using BPrivate::ArrayDeleter;
151 using BPrivate::MemoryDeleter;
152 
153 #endif	// _AUTO_DELETER_H
154