| OLD | NEW | 
|---|
| 1 /** | 1 /** | 
| 2 * \file handle.h The "install session" is the context for all custom installatio
     n behavior. | 2 * \file handle.h The "install session" is the context for all custom installatio
     n 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 | 
| 11 //------------------------------------------------------- | 11 //------------------------------------------------------- | 
| 12 // msi_handle | 12 // MsiHandle | 
| 13 //------------------------------------------------------- | 13 //------------------------------------------------------- | 
| 14 /** | 14 /** | 
| 15 * Disambiguation class holding an MSIHANDLE. | 15 * Disambiguation class holding an MSIHANDLE. | 
| 16 * | 16 * | 
| 17 * We need constructors for Record that accept both handles and record counts. | 17 * We need constructors for Record that accept both handles and record counts. | 
| 18 * Since the underlying type of a handle is integral, without its own type these 
     constructors are ambiguous. | 18 * Since the underlying type of a handle is integral, without its own type these 
     constructors are ambiguous. | 
| 19 */ | 19 */ | 
| 20 class msi_handle | 20 class MsiHandle | 
| 21 { | 21 { | 
| 22   MSIHANDLE _handle ; | 22   MSIHANDLE handle ; | 
| 23 | 23 | 
| 24 public: | 24 public: | 
| 25   /** | 25   /** | 
| 26   * Ordinary constructor is explicit to avoid inadvertent conversions. | 26   * Ordinary constructor is explicit to avoid inadvertent conversions. | 
| 27   */ | 27   */ | 
| 28   explicit msi_handle( MSIHANDLE handle ) | 28   explicit MsiHandle( MSIHANDLE handle ) | 
| 29     : _handle( handle ) | 29     : handle( handle ) | 
| 30   {} | 30   {} | 
| 31 | 31 | 
| 32   operator MSIHANDLE() | 32   operator MSIHANDLE() | 
| 33   { | 33   { | 
| 34     return _handle ; | 34     return handle ; | 
| 35   } | 35   } | 
| 36 } ; | 36 } ; | 
| 37 | 37 | 
| 38 //------------------------------------------------------- | 38 //------------------------------------------------------- | 
| 39 // Handle Policies | 39 // Handle Policies | 
| 40 //------------------------------------------------------- | 40 //------------------------------------------------------- | 
| 41 /** | 41 /** | 
| 42 * Policy class that indicates that a raw handle may not be zero. | 42 * Policy class that indicates that a raw handle may not be zero. | 
| 43 */ | 43 */ | 
| 44 template< class T > | 44 template< class T > | 
| 45 struct Disallow_Null | 45 struct DisallowNull | 
| 46 { | 46 { | 
| 47   inline static bool prohibited_always() { return true ; } | 47   inline static bool ProhibitedAlways() { return true ; } | 
| 48   inline static bool prohibited_from_outside() { return true ; } | 48   inline static bool ProhibitedFromOutside() { return true ; } | 
| 49 } ; | 49 } ; | 
| 50 | 50 | 
| 51 /** | 51 /** | 
| 52 * Policy class that indicates that a raw handle may be zero only when constructe
     d internally. | 52 * Policy class that indicates that a raw handle may be zero only when constructe
     d internally. | 
| 53 */ | 53 */ | 
| 54 template< class T > | 54 template< class T > | 
| 55 struct Special_Null | 55 struct SpecialNull | 
| 56 { | 56 { | 
| 57   inline static bool prohibited_always() { return false ; } | 57   inline static bool ProhibitedAlways() { return false ; } | 
| 58   inline static bool prohibited_from_outside() { return true ; } | 58   inline static bool ProhibitedFromOutside() { return true ; } | 
| 59 } ; | 59 } ; | 
| 60 | 60 | 
| 61 /** | 61 /** | 
| 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 AllowNull | 
| 66 { | 66 { | 
| 67   inline static bool prohibited_always() { return false ; } | 67   inline static bool ProhibitedAlways() { return false ; } | 
| 68   inline static bool prohibited_from_outside() { return false ; } | 68   inline static bool ProhibitedFromOutside() { return false ; } | 
| 69 } ; | 69 } ; | 
| 70 | 70 | 
| 71 /** | 71 /** | 
| 72 * Policy class that does not close a handle at all. | 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 NoDestruction | 
| 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 closes an MSI 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 GenericMsiDestruction | 
| 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 /** | 94 /** | 
| 95 * Policy class that closes a Windows handle when it goes out of scope. | 95 * Policy class that closes a Windows handle when it goes out of scope. | 
| 96 */ | 96 */ | 
| 97 template< class T > | 97 template< class T > | 
| 98 class Windows_Generic_Destruction | 98 class GenericWindowsDestruction | 
| 99 { | 99 { | 
| 100 public: | 100 public: | 
| 101   inline static void close( T handle ) | 101   inline static void Close( T handle ) | 
| 102   { | 102   { | 
| 103     CloseHandle( handle ) ; | 103     CloseHandle( handle ) ; | 
| 104   } ; | 104   } ; | 
| 105 } ; | 105 } ; | 
| 106 | 106 | 
| 107 | 107 | 
| 108 //------------------------------------------------------- | 108 //------------------------------------------------------- | 
| 109 // Handle | 109 // Handle | 
| 110 //------------------------------------------------------- | 110 //------------------------------------------------------- | 
| 111 /** | 111 /** | 
| 112 * Raw handle is the base class for the generic handle. | 112 * Raw handle is the base class for the generic handle. | 
| 113 * | 113 * | 
| 114 * Raw handles always allow null, so that generic handles may allow or disallow t
     hem as they please. | 114 * Raw handles always allow null, so that generic handles may allow or disallow t
     hem as they please. | 
| 115 */ | 115 */ | 
| 116 template< class T > | 116 template< class T > | 
| 117 class handle_raw | 117 class HandleRaw | 
| 118 { | 118 { | 
| 119 protected: | 119 protected: | 
| 120   /** | 120   /** | 
| 121   * The underlying handle. | 121   * The underlying handle. | 
| 122   * | 122   * | 
| 123   * This is the only data member of the class. | 123   * This is the only data member of the class. | 
| 124   * Everything else is for life cycle and type conversion. | 124   * Everything else is for life cycle and type conversion. | 
| 125   */ | 125   */ | 
| 126   T _handle ; | 126   T handle ; | 
| 127 | 127 | 
| 128   /** | 128   /** | 
| 129   * Basic constructor is protected to cede creation policy entirely to subclasse
     s. | 129   * Basic constructor is protected to cede creation policy entirely to subclasse
     s. | 
| 130   */ | 130   */ | 
| 131   handle_raw( T _handle ) | 131   HandleRaw( T handle ) | 
| 132     : _handle( _handle ) | 132     : handle( handle ) | 
| 133   {} | 133   {} | 
| 134 | 134 | 
| 135 public: | 135 public: | 
| 136   /** | 136   /** | 
| 137   * Conversion operator to underlying handle type. | 137   * Conversion operator to underlying handle type. | 
| 138   */ | 138   */ | 
| 139   operator T() | 139   operator T() | 
| 140   { | 140   { | 
| 141     return _handle ; | 141     return handle ; | 
| 142   } | 142   } | 
| 143 | 143 | 
| 144   /** | 144   /** | 
| 145   * Error thrown when initialize or assigning a null handle against policy. | 145   * Error thrown when initialize or assigning a null handle against policy. | 
| 146   * | 146   * | 
| 147   * Note that this error is a logic_error, not a runtime error. | 147   * Note that this error is a logic_error, not a runtime error. | 
| 148   * If it's against policy for a handle to be null, it's an error for the caller
      to try to make it null. | 148   * If it's against policy for a handle to be null, it's an error for the caller
      to try to make it null. | 
| 149   * Policy enforcment here is not a substitute for good error handling by the ca
     ller. | 149   * Policy enforcment here is not a substitute for good error handling by the ca
     ller. | 
| 150   * In many cases, the caller ought to be throwing windows_api_error. | 150   * In many cases, the caller ought to be throwing WindowsApiError. | 
| 151   */ | 151   */ | 
| 152   struct null_handle_error | 152   struct NullHandleError | 
| 153     : public std::logic_error | 153     : public std::logic_error | 
| 154   { | 154   { | 
| 155     null_handle_error() | 155     NullHandleError() | 
| 156       : std::logic_error( "May not initialize with null handle" ) | 156       : std::logic_error( "May not initialize with null handle" ) | 
| 157     {} | 157     {} | 
| 158   } ; | 158   } ; | 
| 159 } ; | 159 } ; | 
| 160 | 160 | 
| 161 /* | 161 /* | 
| 162 * Handle class | 162 * Handle class | 
| 163 */ | 163 */ | 
| 164 template< | 164 template< | 
| 165   class T, | 165   class T, | 
| 166     template <class> class Null_Policy, | 166   template <class> class NullPolicy, | 
| 167     template <class> class Destruction_Policy = No_Destruction | 167   template <class> class DestructionPolicy = NoDestruction | 
| 168 > | 168 > | 
| 169 class handle | 169 class Handle | 
| 170   : public handle_raw< T > | 170   : public HandleRaw< T > | 
| 171 { | 171 { | 
| 172   /** | 172   /** | 
| 173   * Copy constructor prohibited. | 173    * Copy constructor prohibited. | 
| 174   * | 174    * | 
| 175   * This class represents an external resource and is responsible for its lifecy
     cle. | 175    * This class represents an external resource and is responsible for its lifec
     ycle. | 
| 176   * As such, the semantics here require a one-to-one match between instances and
      resource. | 176    * As such, the semantics here require a one-to-one match between instances an
     d resource. | 
| 177   * Copy construction violates these semantics. | 177    * Copy construction violates these semantics. | 
| 178   * | 178    * | 
| 179   * \par Implementation | 179    * \par Implementation | 
| 180   *   Currently, declared private and not defined. | 180    *   Currently, declared private and not defined. | 
| 181   *   Add "= delete" for C++11. | 181    *   Add "= delete" for C++11. | 
| 182   */ | 182    */ | 
| 183   handle( handle & ) ; | 183   Handle( Handle & ) ; | 
| 184 | 184 | 
| 185   /** | 185   /** | 
| 186   * Copy assignment not implemented. | 186    * Copy assignment not implemented. | 
| 187   * | 187    * | 
| 188   * It's not used anywhere yet. | 188    * It's not used anywhere yet. | 
| 189   * | 189    * | 
| 190   * \par Implementation | 190    * \par Implementation | 
| 191   *   Currently, declared private and not defined. | 191    *   Currently, declared private and not defined. | 
| 192   */ | 192    */ | 
| 193   handle operator=( const handle & ) ; | 193   Handle operator=( const Handle & ) ; | 
| 194 | 194 | 
| 195   /** | 195   /** | 
| 196   * Validate initial handle values, both for construction and reinitialization a
     ssignment. | 196    * Validate initial handle values, both for construction and reinitialization 
     assignment. | 
| 197   */ | 197    */ | 
| 198   T validate_handle( T handle ) | 198   T ValidateHandle( T handle ) | 
| 199   { | 199   { | 
| 200     if ( Null_Policy< T >::prohibited_from_outside() && handle == 0 ) | 200     if ( NullPolicy< T >::ProhibitedFromOutside() && handle == 0 ) | 
| 201     { | 201     { | 
| 202       throw null_handle_error() ; | 202       throw NullHandleError() ; | 
| 203     } | 203     } | 
| 204     return handle ; | 204     return handle ; | 
| 205   } | 205   } | 
| 206 | 206 | 
| 207 protected: | 207 protected: | 
| 208   /** | 208   /** | 
| 209   * Tag class for null record constructor | 209    * Tag class for null record constructor | 
| 210   */ | 210    */ | 
| 211   class null_t {} ; | 211   class NullType {} ; | 
| 212 | 212 | 
| 213   /** | 213   /** | 
| 214   * Null handle constructor initializes its handle to zero. | 214    * Null handle constructor initializes its handle to zero. | 
| 215   * | 215    * | 
| 216   * The null record constructor avoids the ordinary check that an external handl
     e not be zero. | 216    * The null record constructor avoids the ordinary check that an external hand
     le not be zero. | 
| 217   * It's declared protected so that it's not ordinarily visible. | 217    * It's declared protected so that it's not ordinarily visible. | 
| 218   * To use this constructor, derive from it and add a friend declaration. | 218    * To use this constructor, derive from it and add a friend declaration. | 
| 219   */ | 219    */ | 
| 220   handle( null_t ) | 220   Handle( NullType ) | 
| 221     : handle_raw( 0 ) | 221     : HandleRaw( 0 ) | 
| 222   { | 222   { | 
| 223     if ( Null_Policy< T >::prohibited_always() ) | 223     if ( NullPolicy< T >::ProhibitedAlways() ) | 
| 224     { | 224     { | 
| 225       throw null_handle_error() ; | 225       throw NullHandleError() ; | 
| 226     } | 226     } | 
| 227   } | 227   } | 
| 228 | 228 | 
| 229 public: | 229 public: | 
| 230   /** | 230   /** | 
| 231   * Ordinary constructor. | 231    * Ordinary constructor. | 
| 232   * | 232    * | 
| 233   * A check for a non-zero handle compiles in conditionally based on the Null_Po
     licy. | 233    * A check for a non-zero handle compiles in conditionally based on the NullPo
     licy. | 
| 234   */ | 234    */ | 
| 235   handle( T handle ) | 235   Handle( T handle ) | 
| 236     : handle_raw( validate_handle( handle ) ) | 236     : HandleRaw( ValidateHandle( handle ) ) | 
| 237   {} | 237   {} | 
| 238 | 238 | 
| 239   /** | 239   /** | 
| 240   * Reinitialization Assignment. | 240    * Reinitialization Assignment. | 
| 241   * | 241    * | 
| 242   * If we had C++11 move constructors, we wouldn't need this, since this acts ex
     actly as construct-plus-move would. | 242    * If we had C++11 move constructors, we wouldn't need this, since this acts e
     xactly as construct-plus-move would. | 
| 243   */ | 243    */ | 
| 244   handle & operator=( T handle ) | 244   Handle & operator=( T handle ) | 
| 245   { | 245   { | 
| 246     validate_handle( handle ) ; | 246     ValidateHandle( handle ) ; | 
| 247     this -> ~handle() ; | 247     this -> ~Handle() ; | 
| 248     _handle = handle ; | 248     this -> handle = handle ; | 
| 249     return * this ; | 249     return * this ; | 
| 250   } | 250   } | 
| 251 | 251 | 
| 252   /** | 252   /** | 
| 253   * Destructor | 253    * Destructor | 
| 254   * | 254    * | 
| 255   * The check for a non-zero handle compiles out conditionally based on Null_Pol
     icy. | 255    * The check for a non-zero handle compiles out conditionally based on NullPol
     icy. | 
| 256   * The specific function used to close the handle is given by the Destruction_P
     olicy. | 256    * The specific function used to close the handle is given by the DestructionP
     olicy. | 
| 257   */ | 257    */ | 
| 258   ~handle() | 258   ~Handle() | 
| 259   { | 259   { | 
| 260     if ( Null_Policy< T >::prohibited_always() || ( _handle != 0 ) ) { | 260     if ( NullPolicy< T >::ProhibitedAlways() || ( handle != 0 ) ) { | 
| 261       Destruction_Policy< T >::close( _handle ) ; | 261       DestructionPolicy< T >::Close( handle ) ; | 
| 262     } | 262     } | 
| 263   } | 263   } | 
| 264 | 264 | 
| 265   /** | 265   /** | 
| 266   * Expose the underlying handle type. | 266    * Expose the underlying handle type. | 
| 267   */ | 267    */ | 
| 268   typedef T handle_type ; | 268   typedef T HandleType ; | 
| 269 } ; | 269 } ; | 
| 270 | 270 | 
| 271 //------------------------------------------------------- | 271 //------------------------------------------------------- | 
| 272 // Common instantiations of handle | 272 // Common instantiations of handle | 
| 273 //------------------------------------------------------- | 273 //------------------------------------------------------- | 
| 274 typedef handle< HANDLE, Disallow_Null, Windows_Generic_Destruction > Windows_Han
     dle ; | 274 typedef Handle< HANDLE, DisallowNull, GenericWindowsDestruction > WindowsHandle 
     ; | 
| 275 | 275 | 
| 276 #endif | 276 #endif | 
| OLD | NEW | 
|---|