File: memory

package info (click to toggle)
virtualbox-ose 1.6.6-dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 91,740 kB
  • ctags: 207,046
  • sloc: ansic: 541,507; cpp: 462,042; asm: 22,594; sh: 8,644; makefile: 6,630; perl: 1,359; objc: 612; xml: 524; sed: 229; cs: 226; python: 34
file content (86 lines) | stat: -rw-r--r-- 2,664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/** @file
 * IPRT - C++ Extensions: memory.
 */

/*
 * Copyright (C) 2007 Sun Microsystems, Inc.
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation,
 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
 * distribution. VirtualBox OSE is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef ___iprt_memory___
#define ___iprt_memory___

/** @defgroup grp_rt_cppx_memory  IPRT C++ Extensions: memory
 * @ingroup grp_rt_cppx
 * @{
 */

#ifdef __cplusplus

#include <memory> /* for auto_ptr */

namespace cppx
{

/**
 *  A simple std::auto_ptr extension that adds CopyConstructible and
 *  Assignable semantics.
 *
 *  Instances of this class, when constructed from or assigned with instances
 *  of the same class, create a copy of the managed object using the new
 *  operator and the managed object's copy constructor, as opposed to
 *  std::auto_ptr which simply transfers ownership in these cases.
 *
 *  This template is useful for member variables of a class that store
 *  dynamically allocated instances of some other class and these instances
 *  need to be copied when the containing class instance is copied itself.
 *
 *  Be careful when returning instances of this class by value: it will call
 *  cause to create a copy of the managed object which is probably is not what
 *  you want, especially if its constructor is quite an expensive operation.
 */
template <typename T>
class auto_copy_ptr : public std::auto_ptr <T>
{
public:

    /** @see std::auto_ptr <T>::auto_ptr() */
    auto_copy_ptr (T *p = 0) throw() : std::auto_ptr <T> (p) {}

    /**
     * Copy constructor.
     * Takes a copy of the given instance by taking a copy of the managed
     * object using its copy constructor. Both instances will continue to own
     * their objects.
     */
    auto_copy_ptr (const auto_copy_ptr &that) throw()
        : std::auto_ptr <T> (that.get() ? new T (*that.get()) : NULL) {}

    /**
     * Assignment operator.
     * Takes a copy of the given instance by taking a copy of the managed
     * object using its copy constructor. Both instances will continue to own
     * their objects.
     */
    auto_copy_ptr &operator= (const auto_copy_ptr &that) throw()
    {
        std::auto_ptr <T>::reset (that.get() ? new T (*that.get()) : NULL);
        return *this;
    }
};

}; /* namespace cppx */


#endif /* __cplusplus */

/** @} */

#endif