VirtualBox

source: vbox/trunk/include/iprt/memory@ 8155

Last change on this file since 8155 was 4747, checked in by vboxsync, 17 years ago

file header.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 2.6 KB
Line 
1/** @file
2 * innotek Portable Runtime - C++ Extensions: memory.
3 */
4
5/*
6 * Copyright (C) 2007 innotek GmbH
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
17#ifndef ___iprt_memory___
18#define ___iprt_memory___
19
20/** @defgroup grp_rt_cppx_memory innotek Portable Runtime C++ Extensions: memory
21 * @ingroup grp_rt_cppx
22 * @{
23 */
24
25#ifdef __cplusplus
26
27#include <memory> /* for auto_ptr */
28
29namespace 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 */
49template <typename T>
50class auto_copy_ptr : public std::auto_ptr <T>
51{
52public:
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
86#endif
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use