VirtualBox

source: vbox/trunk/include/iprt/autores@ 10918

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

Runtime: added RTAutoRes (a generalised form of auto_ptr for managing resources locally)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.0 KB
Line 
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. Certain
28 * operations like constructing from AutoRes objects passed by value are only
29 * possible if the object "transitions through" a wrapper struct.
30 * See auto_ptr in the C++ <memory> header.
31 */
32template <class T, void Destruct(T)>
33struct RTAutoResRef
34{
35 T mValue;
36
37 explicit RTAutoResRef(T aValue) { mValue = aValue; }
38};
39
40/** Re-casting template to convert a void fn(void *) to a void fn(T) */
41template <class T, void fn(void *)>
42inline void RTAutoResRecastVoid(T aT) { fn(aT); }
43
44/**
45 * An auto pointer-type class for resources which take a C-style destructor
46 * destructor (free() or equivalent).
47 */
48template <class T, void Destruct(T) = RTAutoResRecastVoid<T, RTMemFree> >
49class RTAutoRes
50{
51private:
52 /** The actual resource value */
53 T mValue;
54public:
55 /** Constructor */
56 RTAutoRes(T aValue = NULL) { mValue = aValue; }
57 /** Destructor */
58 ~RTAutoRes() { if (NULL != mValue) Destruct(mValue); }
59
60 /** release method to get the pointer's value and "reset" the pointer. */
61 T release(void) { T aTmp = mValue; mValue = NULL; return aTmp; }
62
63 /** reset the pointer value to zero or to another pointer. */
64 void reset(T aValue = NULL) { if (aValue != mValue) { Destruct(mValue); mValue = aValue; } }
65
66 /**
67 * Reallocate the resource value. Free the old value if allocation fails.
68 * @returns true if the new allocation succeeds, false otherwise
69 * @param Reallocator the function to be used for reallocating the
70 * resource. It should have equivalent semantics to
71 * C realloc.
72 * @note We can overload this member for other reallocator signatures as
73 * needed.
74 */
75 template <void *Reallocator(void *, size_t)>
76 bool realloc(size_t cchNewSize)
77 {
78 T aNewValue = reinterpret_cast<T>(Reallocator(mValue, cchNewSize));
79 if (aNewValue != NULL)
80 mValue = aNewValue;
81 else
82 Destruct(mValue);
83 return (aNewValue != NULL);
84 }
85
86 /** Copy constructor */
87 RTAutoRes(RTAutoRes &orig) { mValue = orig.release(); }
88
89 /** Copy from equivalent class */
90 template <class T1>
91 RTAutoRes(RTAutoRes<T1, Destruct> &orig) { mValue = orig.release(); }
92
93 /** Assignment operator. */
94 RTAutoRes& operator=(RTAutoRes &orig)
95 {
96 reset(orig.release());
97 return *this;
98 }
99
100 /** Assignment from equivalent class. */
101 template <class T1>
102 RTAutoRes& operator=(RTAutoRes<T1, Destruct> &orig)
103 {
104 reset(orig.release);
105 return *this;
106 }
107
108 /** Assignment from a value. */
109 RTAutoRes& operator=(T aValue)
110 {
111 if (NULL != mValue)
112 {
113 Destruct(mValue);
114 }
115 mValue = aValue;
116 return *this;
117 }
118
119#if 0
120 /**
121 * @todo I'm not sure how to express this now that our type is T and not
122 * T*. Something like return type *T, which would only be valid for T a
123 * pointer type. Or perhaps easiest to just leave it out.
124 */
125
126 /** Dereference with * operator. */
127 T &operator*() { return *mValue; }
128#endif
129
130 /** Dereference with -> operator. Only makes sense for pointer values. */
131 T operator->() { return mValue; }
132
133 /** Accessing the value inside. */
134 T get(void) { return mValue; }
135
136 /** Convert a reference structure into an AutoRes pointer. */
137 RTAutoRes(RTAutoResRef<T, Destruct> ref) { mValue = ref.mValue; }
138
139 /** Assign from a reference structure into an AutoRes pointer. */
140 RTAutoRes& operator=(RTAutoResRef<T, Destruct> ref)
141 {
142 if (ref.mValue != mValue)
143 {
144 Destruct(mValue);
145 mValue = ref.mValue;
146 }
147 return *this;
148 }
149
150 /** Typecast an AutoRes pointer to a reference structure. */
151 template <class T1>
152 operator RTAutoResRef<T1, Destruct>() { return RTAutoResRef<T1, Destruct>(release()); }
153
154 /** Typecast an AutoRes pointer to an AutoRes pointer of a different type. */
155 template <class T1>
156 operator RTAutoRes<T1, Destruct>() { return RTAutoRes<T1, Destruct>(release()); }
157};
158
159#endif /* ___iprt_autores___ not defined */
Note: See TracBrowser for help on using the repository browser.

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