memory revision e64031e20c39650a7bc902a3e1aba613b9415dee
e37422f7fd015be33ea7cdf48b4044f51bd13cf5Timo Sirainen/** @file
e37422f7fd015be33ea7cdf48b4044f51bd13cf5Timo Sirainen * IPRT - C++ Extensions: memory.
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen */
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen/*
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * Copyright (C) 2007 Oracle Corporation
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen *
c51644e9e04effbbc9c415cadcfbcb4d9465855cTimo Sirainen * This file is part of VirtualBox Open Source Edition (OSE), as
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * available from http://www.virtualbox.org. This file is free software;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * you can redistribute it and/or modify it under the terms of the GNU
79977d368df2efc3f3dc0bbb260748b076538103Timo Sirainen * General Public License (GPL) as published by the Free Software
296acbaddb68301e5ae3579a1da9325064c4dca8Timo Sirainen * Foundation, in version 2 as it comes in the "COPYING" file of the
4819488364b20ee82a9d60177d15c79d0b5abbe0Timo Sirainen * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4819488364b20ee82a9d60177d15c79d0b5abbe0Timo Sirainen * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c9d685897f9e7f0d5e27c00d4b03281d6049ccd1Timo Sirainen *
5733207dc3ec10e6e5a6e0a8b30fbd1b061062b9Timo Sirainen * The contents of this file may alternatively be used under the terms
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * of the Common Development and Distribution License Version 1.0
f45da0b456f0f1540c221f9fdc7d8f7b93400782Timo Sirainen * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
f45da0b456f0f1540c221f9fdc7d8f7b93400782Timo Sirainen * VirtualBox OSE distribution, in which case the provisions of the
c77b85e6017a3f15943fd1c47340daefd87b1993Timo Sirainen * CDDL are applicable instead of those of the GPL.
434abef12f61881a5cfa28d27193d0854a9639a0Timo Sirainen *
434abef12f61881a5cfa28d27193d0854a9639a0Timo Sirainen * You may elect to license modified versions of this file under the
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * terms and conditions of either the GPL or the CDDL or both.
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen */
b11269887905780bc8cb7762bbb157aa59961cacTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#ifndef ___iprt_memory___
73f70e35b789fb3647c40755ed679e61c7dcb852Timo Sirainen#define ___iprt_memory___
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen/** @defgroup grp_rt_cppx_memory IPRT C++ Extensions: memory
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen * @ingroup grp_rt_cppx
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * @{
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen */
97db4761382024093f441e4bc78ba8b6a056504dTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#ifdef __cplusplus
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen#include <memory> /* for auto_ptr */
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen
7ceee001c11865a88a311c50c4430867b2d32ce6Timo Sirainennamespace cppx
97db4761382024093f441e4bc78ba8b6a056504dTimo Sirainen{
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen
97db4761382024093f441e4bc78ba8b6a056504dTimo Sirainen/**
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen * A simple std::auto_ptr extension that adds CopyConstructible and
41942258112e4131de96b6a4399c1a8ac83a23cbTimo Sirainen * Assignable semantics.
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen *
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen * Instances of this class, when constructed from or assigned with instances
5d03d9f439e41c90215a3c938ffebe4c2a8ae257Timo Sirainen * of the same class, create a copy of the managed object using the new
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * operator and the managed object's copy constructor, as opposed to
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * 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