VirtualBox

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

Last change on this file since 102361 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • 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++ Resource Management.
3 */
4
5/*
6 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_cpp_autores_h
37#define IPRT_INCLUDED_cpp_autores_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/types.h>
43#include <iprt/assert.h>
44#include <iprt/cpp/utils.h>
45
46
47
48/** @defgroup grp_rt_cpp_autores C++ Resource Management
49 * @ingroup grp_rt_cpp
50 * @{
51 */
52
53/**
54 * A callable class template which returns the correct value against which an
55 * IPRT type must be compared to see if it is invalid.
56 *
57 * @warning This template *must* be specialised for the types it is to work with.
58 */
59template <class T>
60inline T RTAutoResNil(void)
61{
62 AssertFatalMsgFailed(("Unspecialized template!\n"));
63 return (T)0;
64}
65
66/** Specialisation of RTAutoResNil for RTFILE */
67template <>
68inline RTFILE RTAutoResNil(void)
69{
70 return NIL_RTFILE;
71}
72
73/**
74 * A function template which calls the correct destructor for an IPRT type.
75 *
76 * @warning This template *must* be specialised for the types it is to work with.
77 */
78template <class T>
79inline void RTAutoResDestruct(T a_h)
80{
81 AssertFatalMsgFailed(("Unspecialized template!\n"));
82 NOREF(a_h);
83}
84
85/**
86 * An auto pointer-type class for resources which take a C-style destructor
87 * (RTMemFree() or equivalent).
88 *
89 * The idea of this class is to manage resources which the current code is
90 * responsible for freeing. By wrapping the resource in an RTCAutoRes, you
91 * ensure that the resource will be freed when you leave the scope in which
92 * the RTCAutoRes is defined, unless you explicitly release the resource.
93 *
94 * A typical use case is when a function is allocating a number of resources.
95 * If any single allocation fails then all other resources must be freed. If
96 * all allocations succeed, then the resources should be returned to the
97 * caller. By placing all allocated resources in RTCAutoRes containers, you
98 * ensure that they will be freed on failure, and only have to take care of
99 * releasing them when you return them.
100 *
101 * @param T The type of the resource.
102 * @param Destruct The function to be used to free the resource.
103 * This parameter must be supplied if there is no
104 * specialisation of RTAutoDestruct available for @a T.
105 * @param NilRes The function returning the NIL value for T. Required.
106 * This parameter must be supplied if there is no
107 * specialisation of RTAutoResNil available for @a T.
108 *
109 * @note The class can not be initialised directly using assignment, due
110 * to the lack of a copy constructor. This is intentional.
111 */
112template <class T, void Destruct(T) = RTAutoResDestruct<T>, T NilRes(void) = RTAutoResNil<T> >
113class RTCAutoRes
114 : public RTCNonCopyable
115{
116protected:
117 /** The resource handle. */
118 T m_hRes;
119
120public:
121 /**
122 * Constructor
123 *
124 * @param a_hRes The handle to resource to manage. Defaults to NIL.
125 */
126 RTCAutoRes(T a_hRes = NilRes())
127 : m_hRes(a_hRes)
128 {
129 }
130
131 /**
132 * Destructor.
133 *
134 * This destroys any resource currently managed by the object.
135 */
136 ~RTCAutoRes()
137 {
138 if (m_hRes != NilRes())
139 Destruct(m_hRes);
140 }
141
142 /**
143 * Assignment from a value.
144 *
145 * This destroys any resource currently managed by the object
146 * before taking on the new one.
147 *
148 * @param a_hRes The handle to the new resource.
149 */
150 RTCAutoRes &operator=(T a_hRes)
151 {
152 if (m_hRes != NilRes())
153 Destruct(m_hRes);
154 m_hRes = a_hRes;
155 return *this;
156 }
157
158 /**
159 * Checks if the resource handle is NIL or not.
160 */
161 bool operator!()
162 {
163 return m_hRes == NilRes();
164 }
165
166 /**
167 * Give up ownership the current resource, handing it to the caller.
168 *
169 * @returns The current resource handle.
170 *
171 * @note Nothing happens to the resource when the object goes out of scope.
172 */
173 T release(void)
174 {
175 T Tmp = m_hRes;
176 m_hRes = NilRes();
177 return Tmp;
178 }
179
180 /**
181 * Deletes the current resources.
182 *
183 * @param a_hRes Handle to a new resource to manage. Defaults to NIL.
184 */
185 void reset(T a_hRes = NilRes())
186 {
187 if (a_hRes != m_hRes)
188 {
189 if (m_hRes != NilRes())
190 Destruct(m_hRes);
191 m_hRes = a_hRes;
192 }
193 }
194
195 /**
196 * Get the raw resource handle.
197 *
198 * Typically used passing the handle to some IPRT function while
199 * the object remains in scope.
200 *
201 * @returns The raw resource handle.
202 */
203 T get(void)
204 {
205 return m_hRes;
206 }
207};
208
209/** @} */
210
211
212/* include after template definition */
213#include <iprt/mem.h>
214
215#endif /* !IPRT_INCLUDED_cpp_autores_h */
216
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