VirtualBox

source: vbox/trunk/include/iprt/cpp/autores.h@ 36499

Last change on this file since 36499 was 36499, checked in by vboxsync, 14 years ago

Doc-IPRT: add a RT C++ group to the docu

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