VirtualBox

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

Last change on this file since 30254 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 3.0 KB
Line 
1/** @file
2 * IPRT - C++ Extensions: memory.
3 */
4
5/*
6 * Copyright (C) 2007 Oracle Corporation
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 (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_memory___
27#define ___iprt_memory___
28
29/** @defgroup grp_rt_cppx_memory IPRT C++ Extensions: memory
30 * @ingroup grp_rt_cppx
31 * @{
32 */
33
34#ifdef __cplusplus
35
36#include <memory> /* for auto_ptr */
37
38namespace cppx
39{
40
41/**
42 * A simple std::auto_ptr extension that adds CopyConstructible and
43 * Assignable semantics.
44 *
45 * Instances of this class, when constructed from or assigned with instances
46 * of the same class, create a copy of the managed object using the new
47 * operator and the managed object's copy constructor, as opposed to
48 * std::auto_ptr which simply transfers ownership in these cases.
49 *
50 * This template is useful for member variables of a class that store
51 * dynamically allocated instances of some other class and these instances
52 * need to be copied when the containing class instance is copied itself.
53 *
54 * Be careful when returning instances of this class by value: it will call
55 * cause to create a copy of the managed object which is probably is not what
56 * you want, especially if its constructor is quite an expensive operation.
57 */
58template <typename T>
59class auto_copy_ptr : public std::auto_ptr <T>
60{
61public:
62
63 /** @see std::auto_ptr <T>::auto_ptr() */
64 auto_copy_ptr (T *p = 0) throw() : std::auto_ptr <T> (p) {}
65
66 /**
67 * Copy constructor.
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 (const auto_copy_ptr &that) throw()
73 : std::auto_ptr <T> (that.get() ? new T (*that.get()) : NULL) {}
74
75 /**
76 * Assignment operator.
77 * Takes a copy of the given instance by taking a copy of the managed
78 * object using its copy constructor. Both instances will continue to own
79 * their objects.
80 */
81 auto_copy_ptr &operator= (const auto_copy_ptr &that) throw()
82 {
83 std::auto_ptr <T>::reset (that.get() ? new T (*that.get()) : NULL);
84 return *this;
85 }
86};
87
88} /* namespace cppx */
89
90
91#endif /* __cplusplus */
92
93/** @} */
94
95#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette