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