14420c1ceSMichael Pfeiffer/*
24420c1ceSMichael Pfeiffer * Copyright 2008, Haiku.
34420c1ceSMichael Pfeiffer * Distributed under the terms of the MIT license.
44420c1ceSMichael Pfeiffer *
54420c1ceSMichael Pfeiffer * Authors:
64420c1ceSMichael Pfeiffer *		Michael Pfeiffer <laplace@users.sourceforge.net>
74420c1ceSMichael Pfeiffer */
84420c1ceSMichael Pfeiffer
94420c1ceSMichael Pfeiffer#ifndef _AUTO_DELETE_H
104420c1ceSMichael Pfeiffer#define _AUTO_DELETE_H
114420c1ceSMichael Pfeiffer
124420c1ceSMichael Pfeiffer#include <stdlib.h>
134420c1ceSMichael Pfeiffer
144420c1ceSMichael Pfeiffer/*
154420c1ceSMichael PfeifferTypical usage of this class:
164420c1ceSMichael Pfeiffer
174420c1ceSMichael PfeifferAClass* Klass::Method(int arg) {
184420c1ceSMichael Pfeiffer	AutoDelete<AClass> variable(new AClass());
194420c1ceSMichael Pfeiffer
204420c1ceSMichael Pfeiffer	if (!IsValid(arg)) {
214420c1ceSMichael Pfeiffer		...
224420c1ceSMichael Pfeiffer		// AutoDelete automatically deletes the AClass object.
234420c1ceSMichael Pfeiffer		return NULL;
244420c1ceSMichael Pfeiffer	}
254420c1ceSMichael Pfeiffer
264420c1ceSMichael Pfeiffer	variable.Get()->MethodOfAClass();
274420c1ceSMichael Pfeiffer
284420c1ceSMichael Pfeiffer	// Use Release() to prevent deletion of AClass object.
294420c1ceSMichael Pfeiffer	return variable.Release();
304420c1ceSMichael Pfeiffer}
314420c1ceSMichael Pfeiffer*/
324420c1ceSMichael Pfeiffer
334420c1ceSMichael Pfeiffertemplate <class Tp>
344420c1ceSMichael Pfeifferclass AutoDelete {
354420c1ceSMichael Pfeifferprivate:
364420c1ceSMichael Pfeiffer	Tp*  fObject;
374420c1ceSMichael Pfeiffer	bool fOwnsObject;
384420c1ceSMichael Pfeiffer
394420c1ceSMichael Pfeiffer	// Deletes the object if it owns it
404420c1ceSMichael Pfeiffer	void Delete()
414420c1ceSMichael Pfeiffer	{
424420c1ceSMichael Pfeiffer		if (fOwnsObject) {
434420c1ceSMichael Pfeiffer			delete fObject; fObject = NULL;
444420c1ceSMichael Pfeiffer		}
454420c1ceSMichael Pfeiffer	}
464420c1ceSMichael Pfeiffer
474420c1ceSMichael Pfeifferpublic:
484420c1ceSMichael Pfeiffer
494420c1ceSMichael Pfeiffer	// Sets the object the class owns
504420c1ceSMichael Pfeiffer	AutoDelete(Tp* object = NULL) : fObject(object), fOwnsObject(true) { }
514420c1ceSMichael Pfeiffer
524420c1ceSMichael Pfeiffer	// Deletes the object if it owns it
534420c1ceSMichael Pfeiffer	virtual ~AutoDelete()
544420c1ceSMichael Pfeiffer	{
554420c1ceSMichael Pfeiffer		Delete();
564420c1ceSMichael Pfeiffer	}
574420c1ceSMichael Pfeiffer
584420c1ceSMichael Pfeiffer	// Sets the object the class owns.
594420c1ceSMichael Pfeiffer	// Deletes a previously owned object and
604420c1ceSMichael Pfeiffer	// sets the owning flag for the new object.
614420c1ceSMichael Pfeiffer	void Set(Tp* object)
624420c1ceSMichael Pfeiffer	{
634420c1ceSMichael Pfeiffer		if (fObject == object) return;
644420c1ceSMichael Pfeiffer
654420c1ceSMichael Pfeiffer		Delete();
664420c1ceSMichael Pfeiffer		fOwnsObject = true;
674420c1ceSMichael Pfeiffer		fObject = object;
684420c1ceSMichael Pfeiffer	}
694420c1ceSMichael Pfeiffer
704420c1ceSMichael Pfeiffer	// Returns the object
714420c1ceSMichael Pfeiffer	Tp* Get()
724420c1ceSMichael Pfeiffer	{
734420c1ceSMichael Pfeiffer		return fObject;
744420c1ceSMichael Pfeiffer	}
754420c1ceSMichael Pfeiffer
764420c1ceSMichael Pfeiffer	// Returns the object and sets owning to false
774420c1ceSMichael Pfeiffer	// The Get method can still be used to retrieve the object.
784420c1ceSMichael Pfeiffer	Tp* Release()
794420c1ceSMichael Pfeiffer	{
804420c1ceSMichael Pfeiffer		fOwnsObject = false;
814420c1ceSMichael Pfeiffer		return fObject;
824420c1ceSMichael Pfeiffer	}
834420c1ceSMichael Pfeiffer
844420c1ceSMichael Pfeiffer	// Sets the owning flag
854420c1ceSMichael Pfeiffer	void SetOwnsObject(bool ownsObject)
864420c1ceSMichael Pfeiffer	{
874420c1ceSMichael Pfeiffer		fOwnsObject = ownsObject;
884420c1ceSMichael Pfeiffer	}
894420c1ceSMichael Pfeiffer};
904420c1ceSMichael Pfeiffer
914420c1ceSMichael Pfeiffer
924420c1ceSMichael Pfeiffer#endif
93