xref: /haiku/headers/private/shared/AutoDeleter.h (revision 06b932a49d65e82cdfa7d28a04f48eef6de9ea49)
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 Delete>
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 		fDelete(fObject);
60 		fObject = object;
61 	}
62 
63 	inline C *Detach()
64 	{
65 		C *object = fObject;
66 		fObject = NULL;
67 		return object;
68 	}
69 
70 private:
71 	C		*fObject;
72 	Delete	fDelete;
73 };
74 
75 
76 // ObjectDeleter
77 
78 template<typename C>
79 struct ObjectDelete
80 {
81 	inline void operator()(C *object)
82 	{
83 		delete object;
84 	}
85 };
86 
87 template<typename C>
88 struct ObjectDeleter : AutoDeleter<C, ObjectDelete<C> >
89 {
90 	ObjectDeleter() : AutoDeleter<C, ObjectDelete<C> >() {}
91 	ObjectDeleter(C *object) : AutoDeleter<C, ObjectDelete<C> >(object) {}
92 };
93 
94 
95 // ArrayDeleter
96 
97 template<typename C>
98 struct ArrayDelete
99 {
100 	inline void operator()(C *array)
101 	{
102 		delete[] array;
103 	}
104 };
105 
106 template<typename C>
107 struct ArrayDeleter : AutoDeleter<C, ArrayDelete<C> >
108 {
109 	ArrayDeleter() : AutoDeleter<C, ArrayDelete<C> >() {}
110 	ArrayDeleter(C *array) : AutoDeleter<C, ArrayDelete<C> >(array) {}
111 };
112 
113 
114 // MemoryDeleter
115 
116 struct MemoryDelete
117 {
118 	inline void operator()(void *memory)
119 	{
120 		free(memory);
121 	}
122 };
123 
124 struct MemoryDeleter : AutoDeleter<void, MemoryDelete >
125 {
126 	MemoryDeleter() : AutoDeleter<void, MemoryDelete >() {}
127 	MemoryDeleter(void *memory) : AutoDeleter<void, MemoryDelete >(memory) {}
128 };
129 
130 }	// namespace BPrivate
131 
132 using BPrivate::ObjectDeleter;
133 using BPrivate::ArrayDeleter;
134 using BPrivate::MemoryDeleter;
135 
136 #endif	// _AUTO_DELETER_H
137