/** @file * Incredibly Portable Runtime - C++ Extensions: memory. */ /* * Copyright (C) 2007 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; * you can redistribute it and/or modify it under the terms of the GNU * General Public License 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. */ #ifndef ___iprt_memory___ #define ___iprt_memory___ /** @defgroup grp_rt_cppx_memory Incredibly Portable Runtime C++ Extensions: memory * @ingroup grp_rt_cppx * @{ */ #ifdef __cplusplus #include /* 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 class auto_copy_ptr : public std::auto_ptr { public: /** @see std::auto_ptr ::auto_ptr() */ auto_copy_ptr (T *p = 0) throw() : std::auto_ptr (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 (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 ::reset (that.get() ? new T (*that.get()) : NULL); return *this; } }; }; /* namespace cppx */ #endif /* __cplusplus */ /** @} */ #endif