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