[2669] | 1 | /** @file
|
---|
[2988] | 2 | * innotek Portable Runtime - C++ Extensions: memory.
|
---|
[2669] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[2988] | 6 | * Copyright (C) 2007 innotek GmbH
|
---|
[2669] | 7 | *
|
---|
| 8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 11 | * General Public License as published by the Free Software Foundation,
|
---|
| 12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
| 13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
| 14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 15 | */
|
---|
| 16 |
|
---|
[4747] | 17 | #ifndef ___iprt_memory___
|
---|
| 18 | #define ___iprt_memory___
|
---|
[2669] | 19 |
|
---|
[2988] | 20 | /** @defgroup grp_rt_cppx_memory innotek Portable Runtime C++ Extensions: memory
|
---|
[2669] | 21 | * @ingroup grp_rt_cppx
|
---|
| 22 | * @{
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | #ifdef __cplusplus
|
---|
| 26 |
|
---|
| 27 | #include <memory> /* for auto_ptr */
|
---|
| 28 |
|
---|
| 29 | namespace cppx
|
---|
| 30 | {
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * A simple std::auto_ptr extension that adds CopyConstructible and
|
---|
| 34 | * Assignable semantics.
|
---|
| 35 | *
|
---|
| 36 | * Instances of this class, when constructed from or assigned with instances
|
---|
| 37 | * of the same class, create a copy of the managed object using the new
|
---|
| 38 | * operator and the managed object's copy constructor, as opposed to
|
---|
| 39 | * std::auto_ptr which simply transfers ownership in these cases.
|
---|
| 40 | *
|
---|
| 41 | * This template is useful for member variables of a class that store
|
---|
| 42 | * dynamically allocated instances of some other class and these instances
|
---|
| 43 | * need to be copied when the containing class instance is copied itself.
|
---|
| 44 | *
|
---|
| 45 | * Be careful when returning instances of this class by value: it will call
|
---|
| 46 | * cause to create a copy of the managed object which is probably is not what
|
---|
| 47 | * you want, especially if its constructor is quite an expensive operation.
|
---|
| 48 | */
|
---|
| 49 | template <typename T>
|
---|
| 50 | class auto_copy_ptr : public std::auto_ptr <T>
|
---|
| 51 | {
|
---|
| 52 | public:
|
---|
| 53 |
|
---|
| 54 | /** @see std::auto_ptr <T>::auto_ptr() */
|
---|
| 55 | auto_copy_ptr (T *p = 0) throw() : std::auto_ptr <T> (p) {}
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Copy constructor.
|
---|
| 59 | * Takes a copy of the given instance by taking a copy of the managed
|
---|
| 60 | * object using its copy constructor. Both instances will continue to own
|
---|
| 61 | * their objects.
|
---|
| 62 | */
|
---|
| 63 | auto_copy_ptr (const auto_copy_ptr &that) throw()
|
---|
| 64 | : std::auto_ptr <T> (that.get() ? new T (*that.get()) : NULL) {}
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Assignment operator.
|
---|
| 68 | * Takes a copy of the given instance by taking a copy of the managed
|
---|
| 69 | * object using its copy constructor. Both instances will continue to own
|
---|
| 70 | * their objects.
|
---|
| 71 | */
|
---|
| 72 | auto_copy_ptr &operator= (const auto_copy_ptr &that) throw()
|
---|
| 73 | {
|
---|
| 74 | std::auto_ptr <T>::reset (that.get() ? new T (*that.get()) : NULL);
|
---|
| 75 | return *this;
|
---|
| 76 | }
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | }; /* namespace cppx */
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | #endif /* __cplusplus */
|
---|
| 83 |
|
---|
| 84 | /** @} */
|
---|
| 85 |
|
---|
[4747] | 86 | #endif
|
---|