VirtualBox

source: vbox/trunk/include/iprt/autores.h@ 11605

Last change on this file since 11605 was 11095, checked in by vboxsync, 16 years ago

iprt: The cpputils.h version of RTCNonCopyable doesn't fit the naming scheme used in IPRT and cpptutils.h includes a bit of throwing and rethrowing which makes g++ upset when exceptions are disabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 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_h
22#define ___iprt_autores_h
23
24#include <iprt/types.h>
25#include <iprt/mem.h>
26#include <iprt/assert.h>
27
28
29/**
30 * A simple class used to prevent copying and assignment.
31 *
32 * Inherit from this class in order to prevent automatic generation
33 * of the copy constructor and assignment operator in your class.
34 */
35class RTCNonCopyable
36{
37protected:
38 RTCNonCopyable() {}
39 ~RTCNonCopyable() {}
40private:
41 RTCNonCopyable(RTCNonCopyable const &);
42 RTCNonCopyable const &operator=(RTCNonCopyable const &);
43};
44
45
46/**
47 * A callable class template which returns the correct value against which an
48 * IPRT type must be compared to see if it is invalid.
49 *
50 * @warning This template *must* be specialised for the types it is to work with.
51 */
52template <class T>
53inline T RTAutoResNil(void)
54{
55 AssertFatalMsgFailed(("Unspecialized template!\n"));
56 return (T)0;
57}
58
59/** Specialisation of RTAutoResNil for RTFILE */
60template <>
61inline RTFILE RTAutoResNil(void)
62{
63 return NIL_RTFILE;
64}
65
66/**
67 * A function template which calls the correct destructor for an IPRT type.
68 *
69 * @warning This template *must* be specialised for the types it is to work with.
70 */
71template <class T>
72inline void RTAutoResDestruct(T aHandle)
73{
74 AssertFatalMsgFailed(("Unspecialized template!\n"));
75 NOREF(aHandle);
76}
77
78/**
79 * An auto pointer-type class for resources which take a C-style destructor
80 * (RTMemFree() or equivalent).
81 *
82 * The idea of this class is to manage resources which the current code is
83 * responsible for freeing. By wrapping the resource in an RTAutoRes, you
84 * ensure that the resource will be freed when you leave the scope in which
85 * the RTAutoRes is defined, unless you explicitly release the resource.
86 *
87 * A typical use case is when a function is allocating a number of resources.
88 * If any single allocation fails then all other resources must be freed. If
89 * all allocations succeed, then the resources should be returned to the
90 * caller. By placing all allocated resources in RTAutoRes containers, you
91 * ensure that they will be freed on failure, and only have to take care of
92 * releasing them when you return them.
93 *
94 * @param T The type of the resource.
95 * @param Destruct The function to be used to free the resource.
96 * This parameter must be supplied if there is no
97 * specialisation of RTAutoDestruct available for @a T.
98 * @param NilRes The function returning the NIL value for T. Required.
99 * This parameter must be supplied if there is no
100 * specialisation of RTAutoResNil available for @a T.
101 *
102 * @note The class can not be initialised directly using assignment, due
103 * to the lack of a copy constructor. This is intentional.
104 */
105template <class T, void Destruct(T) = RTAutoResDestruct<T>, T NilRes(void) = RTAutoResNil<T> >
106class RTAutoRes
107 : public RTCNonCopyable
108{
109protected:
110 /** The resource handle. */
111 T m_hRes;
112
113public:
114 /**
115 * Constructor
116 *
117 * @param a_hRes The handle to resource to manage. Defaults to NIL.
118 */
119 RTAutoRes(T a_hRes = NilRes())
120 : m_hRes(a_hRes)
121 {
122 }
123
124 /**
125 * Destructor.
126 *
127 * This destroys any resource currently managed by the object.
128 */
129 ~RTAutoRes()
130 {
131 if (m_hRes != NilRes())
132 Destruct(m_hRes);
133 }
134
135 /**
136 * Assignment from a value.
137 *
138 * This destroys any resource currently managed by the object
139 * before taking on the new one.
140 *
141 * @param a_hRes The handle to the new resource.
142 */
143 RTAutoRes &operator=(T a_hRes)
144 {
145 if (m_hRes != NilRes())
146 Destruct(m_hRes);
147 m_hRes = a_hRes;
148 return *this;
149 }
150
151 /**
152 * Checks if the resource handle is NIL or not.
153 */
154 bool operator!()
155 {
156 return m_hRes == NilRes();
157 }
158
159 /**
160 * Give up ownership the current resource, handing it to the caller.
161 *
162 * @returns The current resource handle.
163 *
164 * @note Nothing happens to the resource when the object goes out of scope.
165 */
166 T release(void)
167 {
168 T Tmp = m_hRes;
169 m_hRes = NilRes();
170 return Tmp;
171 }
172
173 /**
174 * Deletes the current resources.
175 *
176 * @param a_hRes Handle to a new resource to manage. Defaults to NIL.
177 */
178 void reset(T a_hRes = NilRes())
179 {
180 if (a_hRes != m_hRes)
181 {
182 if (m_hRes != NilRes())
183 Destruct(m_hRes);
184 m_hRes = a_hRes;
185 }
186 }
187
188 /**
189 * Get the raw resource handle.
190 *
191 * Typically used passing the handle to some IPRT function while
192 * the object remains in scope.
193 *
194 * @returns The raw resource handle.
195 */
196 T get(void)
197 {
198 return m_hRes;
199 }
200};
201
202#endif
203
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