2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT license.
6 * Michael Pfeiffer <laplace@users.sourceforge.net>
10 #define _AUTO_DELETE_H
15 Typical usage of this class:
17 AClass* Klass::Method(int arg) {
18 AutoDelete<AClass> variable(new AClass());
22 // AutoDelete automatically deletes the AClass object.
26 variable.Get()->MethodOfAClass();
28 // Use Release() to prevent deletion of AClass object.
29 return variable.Release();
39 // Deletes the object if it owns it
43 delete fObject
; fObject
= NULL
;
49 // Sets the object the class owns
50 AutoDelete(Tp
* object
= NULL
) : fObject(object
), fOwnsObject(true) { }
52 // Deletes the object if it owns it
58 // Sets the object the class owns.
59 // Deletes a previously owned object and
60 // sets the owning flag for the new object.
63 if (fObject
== object
) return;
76 // Returns the object and sets owning to false
77 // The Get method can still be used to retrieve the object.
84 // Sets the owning flag
85 void SetOwnsObject(bool ownsObject
)
87 fOwnsObject
= ownsObject
;