1 | /** @file
|
---|
2 | * IPRT - C++ Extensions: resource lifetime management
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
17 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
18 | * additional information or have any questions.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef ___iprt_autores___
|
---|
22 | # define ___iprt_autores___
|
---|
23 |
|
---|
24 | #include <iprt/mem.h>
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Wrapper class around RTAutoRes to provide reference semantics. This is
|
---|
28 | * needed to construct AutoRes objects from constant references to other
|
---|
29 | * AutoRes objects (such as temporary objects returned from functions) which
|
---|
30 | * can not be modified due to their const modifier, but are not actually
|
---|
31 | * constant objects. See a discussion of auto_ptr and auto_ptr_ref for more
|
---|
32 | * details.
|
---|
33 | */
|
---|
34 | template <class T, void Destruct(T)>
|
---|
35 | struct RTAutoResRef
|
---|
36 | {
|
---|
37 | T mValue;
|
---|
38 |
|
---|
39 | explicit RTAutoResRef(T aValue) { mValue = aValue; }
|
---|
40 | };
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * An auto pointer-type class for resources which take a C-style destructor
|
---|
44 | * destructor (free() or equivalent).
|
---|
45 | */
|
---|
46 | template <class T, void Destruct(T)>
|
---|
47 | class RTAutoRes
|
---|
48 | {
|
---|
49 | private:
|
---|
50 | /** The actual resource value */
|
---|
51 | T mValue;
|
---|
52 | public:
|
---|
53 | /** Constructor */
|
---|
54 | RTAutoRes(T aValue = 0) { mValue = aValue; }
|
---|
55 |
|
---|
56 | /** Destructor */
|
---|
57 | ~RTAutoRes() { if (mValue != 0) Destruct(mValue); }
|
---|
58 |
|
---|
59 | /** Copy constructor */
|
---|
60 | RTAutoRes(RTAutoRes &orig) { mValue = orig.release(); }
|
---|
61 |
|
---|
62 | /** Copy from equivalent class */
|
---|
63 | template <class T1>
|
---|
64 | RTAutoRes(RTAutoRes<T1, Destruct> &orig)
|
---|
65 | { mValue = orig.release(); }
|
---|
66 |
|
---|
67 | /** Convert a reference structure into an AutoRes pointer. */
|
---|
68 | RTAutoRes(RTAutoResRef<T, Destruct> ref) { mValue = ref.mValue; }
|
---|
69 |
|
---|
70 | /** Assignment operator. */
|
---|
71 | RTAutoRes& operator=(RTAutoRes &orig)
|
---|
72 | {
|
---|
73 | reset(orig.release());
|
---|
74 | return *this;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Assignment from equivalent class. */
|
---|
78 | template <class T1>
|
---|
79 | RTAutoRes& operator=(RTAutoRes<T1, Destruct> &orig)
|
---|
80 | {
|
---|
81 | reset(orig.release);
|
---|
82 | return *this;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Assignment from a value. */
|
---|
86 | RTAutoRes& operator=(T aValue)
|
---|
87 | {
|
---|
88 | if (mValue != 0)
|
---|
89 | {
|
---|
90 | Destruct(mValue);
|
---|
91 | }
|
---|
92 | mValue = aValue;
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Assign from a reference structure into an AutoRes pointer. */
|
---|
97 | RTAutoRes& operator=(RTAutoResRef<T, Destruct> ref)
|
---|
98 | {
|
---|
99 | if (ref.mValue != mValue)
|
---|
100 | {
|
---|
101 | Destruct(mValue);
|
---|
102 | mValue = ref.mValue;
|
---|
103 | }
|
---|
104 | return *this;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Typecast an AutoRes pointer to an AutoRes pointer of a different type. */
|
---|
108 | template <class T1>
|
---|
109 | operator RTAutoRes<T1, Destruct>()
|
---|
110 | { return RTAutoRes<T1, Destruct>(release()); }
|
---|
111 |
|
---|
112 | /** Typecast an AutoRes pointer to a reference structure. */
|
---|
113 | template <class T1>
|
---|
114 | operator RTAutoResRef<T1, Destruct>()
|
---|
115 | { return RTAutoResRef<T1, Destruct>(release()); }
|
---|
116 |
|
---|
117 | /** release method to get the pointer's value and "reset" the pointer. */
|
---|
118 | T release(void) { T aTmp = mValue; mValue = 0; return aTmp; }
|
---|
119 |
|
---|
120 | /** reset the pointer value to zero or to another pointer. */
|
---|
121 | void reset(T aValue = 0) { if (aValue != mValue) { Destruct(mValue); mValue = aValue; } }
|
---|
122 |
|
---|
123 | /** Accessing the value inside. */
|
---|
124 | T get(void) { return mValue; }
|
---|
125 | };
|
---|
126 |
|
---|
127 | /** Re-casting template to convert a void fn(void *) to a void fn(T *) */
|
---|
128 | template <class T, void fn(void *)>
|
---|
129 | inline void RTAutoResRecastVoid(T *aValue) { fn(aValue); }
|
---|
130 |
|
---|
131 | template <class T, void Destruct(T *) = RTAutoResRecastVoid <T, RTMemFree> >
|
---|
132 | class RTMemAutoPtr : public RTAutoRes <T *, Destruct>
|
---|
133 | {
|
---|
134 | public:
|
---|
135 | /** Constructor */
|
---|
136 | RTMemAutoPtr(T *aValue = NULL) : RTAutoRes <T *, Destruct>(aValue) {}
|
---|
137 |
|
---|
138 | /** Copy constructors */
|
---|
139 | RTMemAutoPtr(RTMemAutoPtr &orig) : RTAutoRes <T *, Destruct>(orig) {}
|
---|
140 | template <class T1>
|
---|
141 | RTMemAutoPtr(RTMemAutoPtr<T1, Destruct> &orig) : RTAutoRes<T1 *, Destruct>(orig) {}
|
---|
142 |
|
---|
143 | /** Assignment operators */
|
---|
144 | RTMemAutoPtr& operator=(RTMemAutoPtr &orig)
|
---|
145 | { this->RTAutoRes <T *, Destruct>::operator=(orig); }
|
---|
146 | template <class T1>
|
---|
147 | RTMemAutoPtr& operator=(RTMemAutoPtr<T1, Destruct> &orig)
|
---|
148 | { this->RTAutoRes<T1 *, Destruct>::operator=(orig); return *this; }
|
---|
149 | RTMemAutoPtr& operator=(T *aValue)
|
---|
150 | { this->RTAutoRes <T *, Destruct>::operator=(aValue); return *this; }
|
---|
151 | RTMemAutoPtr& operator=(RTAutoResRef<T *, Destruct> ref)
|
---|
152 | { this->RTAutoRes <T *, Destruct>::operator=(ref); return *this; }
|
---|
153 |
|
---|
154 | /** Dereference with * operator. */
|
---|
155 | T &operator*() { return *this->get(); }
|
---|
156 |
|
---|
157 | /** Dereference with -> operator. */
|
---|
158 | T* operator->() { return this->get(); }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Reallocate the resource value. Free the old value if allocation fails.
|
---|
162 | * @returns true if the new allocation succeeds, false otherwise
|
---|
163 | * @param Reallocator the function to be used for reallocating the
|
---|
164 | * resource. It should have equivalent semantics to
|
---|
165 | * C realloc.
|
---|
166 | * @note We can overload this member for other reallocator signatures as
|
---|
167 | * needed.
|
---|
168 | */
|
---|
169 | template <void *Reallocator(void *, size_t)>
|
---|
170 | bool realloc(size_t cchNewSize)
|
---|
171 | {
|
---|
172 | T *aNewValue = reinterpret_cast<T *>(Reallocator(this->release(), cchNewSize));
|
---|
173 | if (aNewValue != NULL) /* This line is technically not necessary */
|
---|
174 | this->reset(aNewValue);
|
---|
175 | return (aNewValue != NULL);
|
---|
176 | }
|
---|
177 | };
|
---|
178 |
|
---|
179 | #endif /* ___iprt_autores___ not defined */
|
---|