autores.h revision 7c930c3ee58f55e1a2cebfa2ad3ae918569a8f44
/** @file
* IPRT - C++ Extensions: resource lifetime management
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#ifndef ___iprt_autores___
# define ___iprt_autores___
#include <iprt/cpputils.h>
/**
* A callable class template which returns the correct value against which an
* IPRT type must be compared to see if it is invalid.
* @note this template must be specialised for the types it is to work with.
*/
inline T RTResNul(void)
{ AssertLogRelMsgFailedReturn (("Unspecialized template!\n"), (T) 0); }
/**
* A function template which calls the correct destructor for an IPRT type.
* @note this template must be specialised for the types it is to work with.
*/
{ AssertLogRelMsgFailedReturn (("Unspecialized template!\n"), (T) 0); }
/**
* An auto pointer-type class for resources which take a C-style destructor
* (RTMemFree() or equivalent).
*
* The idea of this class is to manage resources which the current code is
* responsible for freeing. By wrapping the resource in an RTAutoRes, you
* ensure that the resource will be freed when you leave the scope in which
* the RTAutoRes is defined, unless you explicitly release the resource again.
*
* A typical use case is when a function is allocating a number of resources.
* If any single allocation fails then all other resources must be freed. If
* all allocations succeed, then the resources should be returned to the
* caller. By placing all allocated resources in RTAutoRes containers, you
* ensure that they will be freed on failure, and only have to take care of
* releasing them when you return them.
* @param T the type of the resource
* @param Destruct the function to be used to free the resource
* @note The class can not be initialised directly using assignment, due to
* the lack of a copy constructor.
*/
{
/** The actual resource value */
T mValue;
/** Constructor */
/** Destructor */
/** Assignment from a value. */
{
{
}
return *this;
}
/** release method to get the pointer's value and "reset" the pointer. */
/** reset the pointer value to zero or to another pointer. */
/** Accessing the value inside. */
};
/**
* Inline wrapper around RTMemFree to correct the signature. We can't use a
* more complex template here, because the G++ on RHEL 3 chokes on it with an
* internal compiler error.
*/
T *RTMemAutoNul(void) { return (T *)(NULL); }
/**
* An auto pointer-type class for structures allocated using C-like APIs.
*
* The idea of this class is to manage resources which the current code is
* responsible for freeing. By wrapping the resource in an RTAutoRes, you
* ensure that the resource will be freed when you leave the scope in which
* the RTAutoRes is defined, unless you explicitly release the resource again.
*
* A typical use case is when a function is allocating a number of resources.
* If any single allocation fails then all other resources must be freed. If
* all allocations succeed, then the resources should be returned to the
* caller. By placing all allocated resources in RTAutoRes containers, you
* ensure that they will be freed on failure, and only have to take care of
* releasing them when you return them.
* @param T the type of the resource
* @param Destruct the function to be used to free the resource
*/
{
/** Constructor */
/** Assignment from a void pointer. */
{
return *this;
}
/** Dereference with * operator. */
/** Dereference with -> operator. */
/** Dereference with [] operator. */
/**
* Reallocate the resource value. Free the old value if allocation fails.
* @returns true if the new allocation succeeds, false otherwise
* @param Reallocator the function to be used for reallocating the
* resource. It should have equivalent semantics to
* C realloc.
* @note We can overload this member for other reallocator signatures as
* needed.
*/
{
/* We want this both if aNewValue is non-NULL and if it is NULL. */
}
};
#endif /* ___iprt_autores___ not defined */