xref: /haiku/headers/build/private/shared/AutoDeleter.h (revision 893988af824e65e49e55f517b157db8386e8002b)
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 *Detach()
76 	{
77 		C *object = fObject;
78 		fObject = NULL;
79 		return object;
80 	}
81 
82 private:
83 	C			*fObject;
84 	DeleteFunc	fDelete;
85 };
86 
87 
88 // ObjectDeleter
89 
90 template<typename C>
91 struct ObjectDelete
92 {
93 	inline void operator()(C *object)
94 	{
95 		delete object;
96 	}
97 };
98 
99 template<typename C>
100 struct ObjectDeleter : AutoDeleter<C, ObjectDelete<C> >
101 {
102 	ObjectDeleter() : AutoDeleter<C, ObjectDelete<C> >() {}
103 	ObjectDeleter(C *object) : AutoDeleter<C, ObjectDelete<C> >(object) {}
104 };
105 
106 
107 // ArrayDeleter
108 
109 template<typename C>
110 struct ArrayDelete
111 {
112 	inline void operator()(C *array)
113 	{
114 		delete[] array;
115 	}
116 };
117 
118 template<typename C>
119 struct ArrayDeleter : AutoDeleter<C, ArrayDelete<C> >
120 {
121 	ArrayDeleter() : AutoDeleter<C, ArrayDelete<C> >() {}
122 	ArrayDeleter(C *array) : AutoDeleter<C, ArrayDelete<C> >(array) {}
123 };
124 
125 
126 // MemoryDeleter
127 
128 struct MemoryDelete
129 {
130 	inline void operator()(void *memory)
131 	{
132 		free(memory);
133 	}
134 };
135 
136 struct MemoryDeleter : AutoDeleter<void, MemoryDelete >
137 {
138 	MemoryDeleter() : AutoDeleter<void, MemoryDelete >() {}
139 	MemoryDeleter(void *memory) : AutoDeleter<void, MemoryDelete >(memory) {}
140 };
141 
142 }	// namespace BPrivate
143 
144 using BPrivate::ObjectDeleter;
145 using BPrivate::ArrayDeleter;
146 using BPrivate::MemoryDeleter;
147 
148 #endif	// _AUTO_DELETER_H
149