memory revision e64031e20c39650a7bc902a3e1aba613b9415dee
/** @file
* IPRT - C++ Extensions: memory.
*/
/*
* Copyright (C) 2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#ifndef ___iprt_memory___
#define ___iprt_memory___
/** @defgroup grp_rt_cppx_memory IPRT C++ Extensions: memory
* @ingroup grp_rt_cppx
* @{
*/
#ifdef __cplusplus
#include <memory> /* for auto_ptr */
namespace cppx
{
/**
* A simple std::auto_ptr extension that adds CopyConstructible and
* Assignable semantics.
*
* Instances of this class, when constructed from or assigned with instances
* of the same class, create a copy of the managed object using the new
* operator and the managed object's copy constructor, as opposed to
* std::auto_ptr which simply transfers ownership in these cases.
*
* This template is useful for member variables of a class that store
* dynamically allocated instances of some other class and these instances
* need to be copied when the containing class instance is copied itself.
*
* Be careful when returning instances of this class by value: it will call
* cause to create a copy of the managed object which is probably is not what
* you want, especially if its constructor is quite an expensive operation.
*/
template <typename T>
class auto_copy_ptr : public std::auto_ptr <T>
{
public:
/** @see std::auto_ptr <T>::auto_ptr() */
auto_copy_ptr (T *p = 0) throw() : std::auto_ptr <T> (p) {}
/**
* Copy constructor.
* Takes a copy of the given instance by taking a copy of the managed
* object using its copy constructor. Both instances will continue to own
* their objects.
*/
auto_copy_ptr (const auto_copy_ptr &that) throw()
: std::auto_ptr <T> (that.get() ? new T (*that.get()) : NULL) {}
/**
* Assignment operator.
* Takes a copy of the given instance by taking a copy of the managed
* object using its copy constructor. Both instances will continue to own
* their objects.
*/
auto_copy_ptr &operator= (const auto_copy_ptr &that) throw()
{
std::auto_ptr <T>::reset (that.get() ? new T (*that.get()) : NULL);
return *this;
}
};
} /* namespace cppx */
#endif /* __cplusplus */
/** @} */
#endif