VirtualBox

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

Last change on this file since 76507 was 76507, checked in by vboxsync, 6 years ago

/include: scm --fix-header-guards. bugref:9344

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