Make UEFI boot-platform build again
[haiku.git] / headers / private / fs_shell / fssh_auto_deleter.h
blobd2948951ae6bf38d84a5d26b8694296b291f5c9b
1 /*
2 * Copyright 2001-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _AUTO_DELETER_H
6 #define _AUTO_DELETER_H
8 /*! Scope-based automatic deletion of objects/arrays.
9 ObjectDeleter - deletes an object
10 ArrayDeleter - deletes an array
11 MemoryDeleter - free()s malloc()ed memory
12 CObjectDeleter - calls an arbitrary specified destructor function
15 #include <stdlib.h>
17 namespace FSShell {
19 // AutoDeleter
21 template<typename C, typename DeleteFunc>
22 class AutoDeleter {
23 public:
24 inline AutoDeleter()
25 : fObject(NULL)
29 inline AutoDeleter(C *object)
30 : fObject(object)
34 inline ~AutoDeleter()
36 fDelete(fObject);
39 inline void SetTo(C *object)
41 if (object != fObject) {
42 fDelete(fObject);
43 fObject = object;
47 inline void Unset()
49 SetTo(NULL);
52 inline void Delete()
54 SetTo(NULL);
57 inline C *Get() const
59 return fObject;
62 inline C *Detach()
64 C *object = fObject;
65 fObject = NULL;
66 return object;
69 protected:
70 C *fObject;
71 DeleteFunc fDelete;
75 // ObjectDeleter
77 template<typename C>
78 struct ObjectDelete
80 inline void operator()(C *object)
82 delete object;
86 template<typename C>
87 struct ObjectDeleter : AutoDeleter<C, ObjectDelete<C> >
89 ObjectDeleter() : AutoDeleter<C, ObjectDelete<C> >() {}
90 ObjectDeleter(C *object) : AutoDeleter<C, ObjectDelete<C> >(object) {}
94 // ArrayDeleter
96 template<typename C>
97 struct ArrayDelete
99 inline void operator()(C *array)
101 delete[] array;
105 template<typename C>
106 struct ArrayDeleter : AutoDeleter<C, ArrayDelete<C> >
108 ArrayDeleter() : AutoDeleter<C, ArrayDelete<C> >() {}
109 ArrayDeleter(C *array) : AutoDeleter<C, ArrayDelete<C> >(array) {}
113 // MemoryDeleter
115 struct MemoryDelete
117 inline void operator()(void *memory)
119 free(memory);
123 struct MemoryDeleter : AutoDeleter<void, MemoryDelete >
125 MemoryDeleter() : AutoDeleter<void, MemoryDelete >() {}
126 MemoryDeleter(void *memory) : AutoDeleter<void, MemoryDelete >(memory) {}
130 // CObjectDeleter
132 template<typename Type, typename DestructorReturnType>
133 struct CObjectDelete
135 inline void operator()(Type *object)
137 if (fDestructor != NULL && object != NULL)
138 fDestructor(object);
141 template<typename Destructor>
142 inline void operator=(Destructor destructor)
144 fDestructor = destructor;
147 private:
148 DestructorReturnType (*fDestructor)(Type*);
151 template<typename Type, typename DestructorReturnType = void>
152 struct CObjectDeleter
153 : AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType> >
155 typedef AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType> > Base;
157 template<typename Destructor>
158 CObjectDeleter(Destructor destructor) : Base()
160 Base::fDelete = destructor;
163 template<typename Destructor>
164 CObjectDeleter(Type *object, Destructor destructor) : Base(object)
166 Base::fDelete = destructor;
171 // MethodDeleter
173 template<typename Type, typename DestructorReturnType>
174 struct MethodDelete
176 inline void operator()(Type *object)
178 if (fDestructor && object != NULL)
179 (object->*fDestructor)();
182 template<typename Destructor>
183 inline void operator=(Destructor destructor)
185 fDestructor = destructor;
188 private:
189 DestructorReturnType (Type::*fDestructor)();
193 template<typename Type, typename DestructorReturnType = void>
194 struct MethodDeleter
195 : AutoDeleter<Type, MethodDelete<Type, DestructorReturnType> >
197 typedef AutoDeleter<Type, MethodDelete<Type, DestructorReturnType> > Base;
199 template<typename Destructor>
200 MethodDeleter(Destructor destructor) : Base()
202 Base::fDelete = destructor;
205 template<typename Destructor>
206 MethodDeleter(Type *object, Destructor destructor) : Base(object)
208 Base::fDelete = destructor;
212 } // namespace FSShell
214 using FSShell::ObjectDeleter;
215 using FSShell::ArrayDeleter;
216 using FSShell::MemoryDeleter;
217 using FSShell::CObjectDeleter;
218 using FSShell::MethodDeleter;
220 #endif // _AUTO_DELETER_H