Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: installer/src/installer-lib/handle.h

Issue 6197445574787072: Cleaned up CA exceptions. Integrated free-standing handle class. (Closed)
Patch Set: Created March 30, 2014, 7:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /** 1 /**
2 * \file handle.h The "install session" is the context for all custom installati on behavior. 2 * \file handle.h The "install session" is the context for all custom installati on behavior.
3 */ 3 */
4 4
5 #ifndef HANDLE_H 5 #ifndef HANDLE_H
6 #define HANDLE_H 6 #define HANDLE_H
7 7
8 #include "windows.h" 8 #include "windows.h"
9 #include "msi.h" 9 #include "msi.h"
10 10
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 * Policy class that indicates that a raw handle is permitted to be zero. 62 * Policy class that indicates that a raw handle is permitted to be zero.
63 */ 63 */
64 template< class T > 64 template< class T >
65 struct Allow_Null 65 struct Allow_Null
66 { 66 {
67 inline static bool prohibited_always() { return false ; } 67 inline static bool prohibited_always() { return false ; }
68 inline static bool prohibited_from_outside() { return false ; } 68 inline static bool prohibited_from_outside() { return false ; }
69 } ; 69 } ;
70 70
71 /** 71 /**
72 * Policy class that does not close a file handle when it goes out of scope 72 * Policy class that does not close a handle at all.
73 */ 73 */
74 template< class T > 74 template< class T >
75 class No_Destruction 75 class No_Destruction
76 { 76 {
77 public: 77 public:
78 inline static void close( T handle ) {} ; 78 inline static void close( T handle ) {} ;
79 } ; 79 } ;
80 80
81 /** 81 /**
82 * Policy class that does not close a file handle when it goes out of scope 82 * Policy class that closes an MSI handle when it goes out of scope.
83 */ 83 */
84 template< class T > 84 template< class T >
85 class MSI_Generic_Destruction 85 class MSI_Generic_Destruction
86 { 86 {
87 public: 87 public:
88 inline static void close( T handle ) 88 inline static void close( T handle )
89 { 89 {
90 MsiCloseHandle( handle ) ; 90 MsiCloseHandle( handle ) ;
91 } ; 91 } ;
92 } ; 92 } ;
93 93
94 /**
95 * Policy class that closes a Windows handle when it goes out of scope.
96 */
97 template< class T >
98 class Windows_Generic_Destruction
99 {
100 public:
101 inline static void close( T handle )
102 {
103 CloseHandle( handle ) ;
104 } ;
105 } ;
106
94 107
95 //------------------------------------------------------- 108 //-------------------------------------------------------
96 // Handle 109 // Handle
97 //------------------------------------------------------- 110 //-------------------------------------------------------
98 /** 111 /**
99 * Raw handle is the base class for the generic handle. 112 * Raw handle is the base class for the generic handle.
100 * 113 *
101 * Raw handles always allow null, so that generic handles may allow or disallow them as they please. 114 * Raw handles always allow null, so that generic handles may allow or disallow them as they please.
102 */ 115 */
103 template< class T > 116 template< class T >
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 * Handle class 154 * Handle class
142 */ 155 */
143 template< 156 template<
144 class T, 157 class T,
145 template <class> class Null_Policy, 158 template <class> class Null_Policy,
146 template <class> class Destruction_Policy = No_Destruction 159 template <class> class Destruction_Policy = No_Destruction
147 > 160 >
148 class handle 161 class handle
149 : public handle_raw< T > 162 : public handle_raw< T >
150 { 163 {
164 /**
165 * Copy constructor prohibited.
166 *
167 * This class represents an external resource and is responsible for its lifec ycle.
168 * As such, the semantics here require a one-to-one match between instances an d resource.
169 * Copy construction violates these semantics.
170 *
171 * \par Implementation
172 * Currently, declared private and not defined.
173 * Add "= delete" for C++11.
174 */
175 handle( handle & ) ;
176
177 /**
178 * Copy assignment not implemented.
179 *
180 * It's not used anywhere yet.
181 *
182 * \par Implementation
183 * Currently, declared private and not defined.
184 */
185 handle operator=( const handle & ) ;
186
187 /**
188 * Validate initial handle values, both for construction and reinitialization assignment.
189 */
190 T validate_handle( T handle )
191 {
192 if ( Null_Policy< T >::prohibited_from_outside() && handle == 0 )
193 {
194 throw null_handle_error() ;
195 }
196 return handle ;
197 }
198
151 protected: 199 protected:
152 /** 200 /**
153 * Tag class for null record constructor 201 * Tag class for null record constructor
154 */ 202 */
155 class null_t {} ; 203 class null_t {} ;
156 204
157 /** 205 /**
158 * Null handle constructor initializes its handle to zero. 206 * Null handle constructor initializes its handle to zero.
159 * 207 *
160 * The null record constructor avoids the ordinary check that an external hand le not be zero. 208 * The null record constructor avoids the ordinary check that an external hand le not be zero.
161 * It's declared protected so that it's not ordinarily visible. 209 * It's declared protected so that it's not ordinarily visible.
162 * To use this constructor, derive from it and add a friend declaration. 210 * To use this constructor, derive from it and add a friend declaration.
163 */ 211 */
164 handle( null_t ) 212 handle( null_t )
165 : handle_raw( 0 ) 213 : handle_raw( 0 )
166 { 214 {
167 if ( Null_Policy< T >::prohibited_always() ) 215 if ( Null_Policy< T >::prohibited_always() )
168 { 216 {
169 throw null_handle_error() ; 217 throw null_handle_error() ;
170 } 218 }
171 } 219 }
172 220
173 public: 221 public:
174 /** 222 /**
175 * Ordinary constructor. 223 * Ordinary constructor.
176 * 224 *
177 * A check for a non-zero handle compiles in conditionally based on the Null_P olicy. 225 * A check for a non-zero handle compiles in conditionally based on the Null_P olicy.
178 */ 226 */
179 handle( T _handle ) 227 handle( T handle )
180 : handle_raw( _handle ) 228 : handle_raw( validate_handle( handle ) )
229 {}
230
231 /**
232 * Reinitialization Assignment.
233 *
234 * If we had C++11 move constructors, we wouldn't need this, since this acts e xactly as construct-plus-move would.
235 */
236 handle & operator=( T handle )
181 { 237 {
182 if ( Null_Policy< T >::prohibited_from_outside() && _handle == 0 ) 238 validate_handle( handle ) ;
183 { 239 this -> ~handle() ;
184 throw null_handle_error() ; 240 _handle = handle ;
185 } 241 return * this ;
186 } 242 }
187 243
188 /** 244 /**
189 * Destructor 245 * Destructor
190 * 246 *
191 * The check for a non-zero handle compiles out conditionally based on Null_Po licy. 247 * The check for a non-zero handle compiles out conditionally based on Null_Po licy.
192 * The specific function used to close the handle is given by the Destruction_ Policy. 248 * The specific function used to close the handle is given by the Destruction_ Policy.
193 */ 249 */
194 ~handle() 250 ~handle()
195 { 251 {
196 if ( Null_Policy< T >::prohibited_always() || ( _handle != 0 ) ) { 252 if ( Null_Policy< T >::prohibited_always() || ( _handle != 0 ) ) {
197 Destruction_Policy< T >::close( _handle ) ; 253 Destruction_Policy< T >::close( _handle ) ;
198 } 254 }
199 } 255 }
200 256
201 } ; 257 } ;
202 258
259 //-------------------------------------------------------
260 // Common instantiations of handle
261 //-------------------------------------------------------
262 typedef handle< HANDLE, Disallow_Null, Windows_Generic_Destruction > Windows_Han dle ;
263
203 #endif 264 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld