memory revision 00c932c764df74915cfc9947f9d2d8e5d31647b7
0N/A/** @file
0N/A * Incredibly Portable Runtime - C++ Extensions: memory.
0N/A */
2362N/A
0N/A/*
0N/A * Copyright (C) 2007 Sun Microsystems, Inc.
0N/A *
0N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License as published by the Free Software Foundation,
0N/A * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
0N/A * distribution. VirtualBox OSE is distributed in the hope that it will
0N/A * be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A */
0N/A
2362N/A#ifndef ___iprt_memory___
0N/A#define ___iprt_memory___
0N/A
0N/A/** @defgroup grp_rt_cppx_memory Incredibly Portable Runtime C++ Extensions: memory
0N/A * @ingroup grp_rt_cppx
0N/A * @{
0N/A */
0N/A
0N/A#ifdef __cplusplus
0N/A
0N/A#include <memory> /* for auto_ptr */
0N/A
0N/Anamespace cppx
0N/A{
0N/A
0N/A/**
0N/A * A simple std::auto_ptr extension that adds CopyConstructible and
0N/A * Assignable semantics.
0N/A *
0N/A * Instances of this class, when constructed from or assigned with instances
0N/A * of the same class, create a copy of the managed object using the new
0N/A * operator and the managed object's copy constructor, as opposed to
0N/A * std::auto_ptr which simply transfers ownership in these cases.
0N/A *
0N/A * This template is useful for member variables of a class that store
0N/A * dynamically allocated instances of some other class and these instances
0N/A * need to be copied when the containing class instance is copied itself.
0N/A *
0N/A * Be careful when returning instances of this class by value: it will call
0N/A * cause to create a copy of the managed object which is probably is not what
0N/A * you want, especially if its constructor is quite an expensive operation.
0N/A */
0N/Atemplate <typename T>
0N/Aclass auto_copy_ptr : public std::auto_ptr <T>
0N/A{
0N/Apublic:
0N/A
0N/A /** @see std::auto_ptr <T>::auto_ptr() */
0N/A auto_copy_ptr (T *p = 0) throw() : std::auto_ptr <T> (p) {}
0N/A
0N/A /**
0N/A * Copy constructor.
0N/A * Takes a copy of the given instance by taking a copy of the managed
0N/A * object using its copy constructor. Both instances will continue to own
0N/A * their objects.
0N/A */
0N/A auto_copy_ptr (const auto_copy_ptr &that) throw()
0N/A : std::auto_ptr <T> (that.get() ? new T (*that.get()) : NULL) {}
0N/A
0N/A /**
0N/A * Assignment operator.
0N/A * Takes a copy of the given instance by taking a copy of the managed
0N/A * object using its copy constructor. Both instances will continue to own
0N/A * their objects.
0N/A */
0N/A auto_copy_ptr &operator= (const auto_copy_ptr &that) throw()
0N/A {
0N/A std::auto_ptr <T>::reset (that.get() ? new T (*that.get()) : NULL);
0N/A return *this;
0N/A }
0N/A};
0N/A
0N/A}; /* namespace cppx */
0N/A
0N/A
0N/A#endif /* __cplusplus */
0N/A
0N/A/** @} */
0N/A
0N/A#endif
0N/A