VirtualBox

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

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

minor header update

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