2 * Copyright 2001-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
3 * Distributed under the terms of the MIT License.
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
21 template<typename C
, typename DeleteFunc
>
29 inline AutoDeleter(C
*object
)
39 inline void SetTo(C
*object
)
41 if (object
!= fObject
) {
80 inline void operator()(C
*object
)
87 struct ObjectDeleter
: AutoDeleter
<C
, ObjectDelete
<C
> >
89 ObjectDeleter() : AutoDeleter
<C
, ObjectDelete
<C
> >() {}
90 ObjectDeleter(C
*object
) : AutoDeleter
<C
, ObjectDelete
<C
> >(object
) {}
99 inline void operator()(C
*array
)
106 struct ArrayDeleter
: AutoDeleter
<C
, ArrayDelete
<C
> >
108 ArrayDeleter() : AutoDeleter
<C
, ArrayDelete
<C
> >() {}
109 ArrayDeleter(C
*array
) : AutoDeleter
<C
, ArrayDelete
<C
> >(array
) {}
117 inline void operator()(void *memory
)
123 struct MemoryDeleter
: AutoDeleter
<void, MemoryDelete
>
125 MemoryDeleter() : AutoDeleter
<void, MemoryDelete
>() {}
126 MemoryDeleter(void *memory
) : AutoDeleter
<void, MemoryDelete
>(memory
) {}
132 template<typename Type
, typename DestructorReturnType
>
135 inline void operator()(Type
*object
)
137 if (fDestructor
!= NULL
&& object
!= NULL
)
141 template<typename Destructor
>
142 inline void operator=(Destructor destructor
)
144 fDestructor
= destructor
;
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
;
173 template<typename Type
, typename DestructorReturnType
>
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
;
189 DestructorReturnType (Type::*fDestructor
)();
193 template<typename Type
, typename DestructorReturnType
= void>
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