memory revision 93f376c18d1af911e79ce6654c48b35039a343db
b9b0788ea79de3ee230ccb0abf93a0c2c08f6287lars * IPRT - C++ Extensions: memory.
c330021bf3f45cbf187fa644781e67f7e470a58awrowe * Copyright (C) 2007 Sun Microsystems, Inc.
af4c982a7cf4515f124935f99a329744035fc699slive * This file is part of VirtualBox Open Source Edition (OSE), as
af4c982a7cf4515f124935f99a329744035fc699slive * available from http://www.virtualbox.org. This file is free software;
af4c982a7cf4515f124935f99a329744035fc699slive * you can redistribute it and/or modify it under the terms of the GNU
af4c982a7cf4515f124935f99a329744035fc699slive * General Public License as published by the Free Software Foundation,
af4c982a7cf4515f124935f99a329744035fc699slive * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
af4c982a7cf4515f124935f99a329744035fc699slive * distribution. VirtualBox OSE is distributed in the hope that it will
af4c982a7cf4515f124935f99a329744035fc699slive * be useful, but WITHOUT ANY WARRANTY of any kind.
4b62424416882687387923b3130b96241503cbe0jerenkrantz/** @defgroup grp_rt_cppx_memory IPRT C++ Extensions: memory
5ca8e11fadb6f7a8d9d0367c1800205c99d4bcd6jerenkrantz * @ingroup grp_rt_cppx
13402b2193f52031b2acfbee2b0965e02f3f29b4wrowe * A simple std::auto_ptr extension that adds CopyConstructible and
f95a0b59eb24c631f15bd83e20c6cf823c432d83trawick * Assignable semantics.
418f39e60a288f855ec033b566744489c3bbbf39trawick * Instances of this class, when constructed from or assigned with instances
cc22a72861c58dda7f3768613aec864e4c4e0353striker * of the same class, create a copy of the managed object using the new
f95a0b59eb24c631f15bd83e20c6cf823c432d83trawick * operator and the managed object's copy constructor, as opposed to
ddafc111b94558ef4e2d7357ceda623315566ce3slive * std::auto_ptr which simply transfers ownership in these cases.
7718f3d5b4da70eb063877f5300ee361435910f4nd * This template is useful for member variables of a class that store
ddafc111b94558ef4e2d7357ceda623315566ce3slive * dynamically allocated instances of some other class and these instances
93f189f1198f539d3cfa75a15b23dcde60ee35ffrbb * need to be copied when the containing class instance is copied itself.
bca5b27d271b6e1690134a83963424b9825d93bdstriker * Be careful when returning instances of this class by value: it will call
bca5b27d271b6e1690134a83963424b9825d93bdstriker * cause to create a copy of the managed object which is probably is not what
7718f3d5b4da70eb063877f5300ee361435910f4nd * you want, especially if its constructor is quite an expensive operation.
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz /** @see std::auto_ptr <T>::auto_ptr() */
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz auto_copy_ptr (T *p = 0) throw() : std::auto_ptr <T> (p) {}
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz * Copy constructor.
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz * Takes a copy of the given instance by taking a copy of the managed
b9b0788ea79de3ee230ccb0abf93a0c2c08f6287lars * object using its copy constructor. Both instances will continue to own
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz * their objects.
38d2c5d41cdb5eb28668d0290b59f8c76ae2a4bfjim : std::auto_ptr <T> (that.get() ? new T (*that.get()) : NULL) {}
b9b0788ea79de3ee230ccb0abf93a0c2c08f6287lars * Assignment operator.
b9b0788ea79de3ee230ccb0abf93a0c2c08f6287lars * Takes a copy of the given instance by taking a copy of the managed
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz * object using its copy constructor. Both instances will continue to own
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz * their objects.
1af5c0e25a649bb298e25ddfa5418fa18bdcb107aaron auto_copy_ptr &operator= (const auto_copy_ptr &that) throw()
82455c2e3b6991846fbcbf0c9e41f57dbc681217brianp std::auto_ptr <T>::reset (that.get() ? new T (*that.get()) : NULL);
f4cb04eb78da02a38fcdd87489dc7b660107d55fjerenkrantz return *this;
016f2545c9375ec7fc5e9cb70aa1ae0cace83c98jerenkrantz}; /* namespace cppx */