OVR_Win32_IncludeWindows.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /************************************************************************************
  2. Filename : OVR_Win32_IncludeWindows.h
  3. Content : Small helper header to include Windows.h properly
  4. Created : Oct 16, 2014
  5. Authors : Chris Taylor, Scott Bassett
  6. Copyright : Copyright 2014 Oculus, Inc. All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. *************************************************************************************/
  19. #ifndef OVR_Win32_IncludeWindows_h
  20. #define OVR_Win32_IncludeWindows_h
  21. #include "OVR_Types.h"
  22. // Automatically avoid including the Windows header on non-Windows platforms.
  23. #ifdef OVR_OS_MS
  24. // It is common practice to define WIN32_LEAN_AND_MEAN to reduce compile times.
  25. // However this then requires us to define our own NTSTATUS data type and other
  26. // irritations throughout our code-base.
  27. //#define WIN32_LEAN_AND_MEAN
  28. // Prevents <Windows.h> from #including <Winsock.h>, as we use <Winsock2.h> instead.
  29. #ifndef _WINSOCKAPI_
  30. #define _WINSOCKAPI_
  31. #endif
  32. // Prevents <Windows.h> from defining min() and max() macro symbols.
  33. #ifndef NOMINMAX
  34. #define NOMINMAX
  35. #endif
  36. // We support Windows Windows 7 or newer.
  37. #ifndef _WIN32_WINNT
  38. #define _WIN32_WINNT 0x0601 /* Windows 7+ */
  39. #endif
  40. #include <Windows.h>
  41. namespace OVR {
  42. //-----------------------------------------------------------------------------
  43. // ScopedHANDLE
  44. //
  45. // MSVC 2010, and MSVC 2012 do not support the <wrl.h> utility header.
  46. // So we provide our own version of HandleRAII, which is an incredibly
  47. // useful pattern for working with Windows HANDLE values.
  48. //
  49. // HANDLEs have two invalid values in Windows, either nullptr or
  50. // INVALID_HANDLE_VALUE. The invalid value that is correct for the usage must
  51. // be provided as the template argument.
  52. struct ScopedHANDLE_NullTraits
  53. {
  54. // We cannot make this a static member variable as it is not an integral type.
  55. inline static HANDLE InvalidValue()
  56. {
  57. return nullptr;
  58. }
  59. };
  60. struct ScopedHANDLE_InvalidTraits
  61. {
  62. inline static HANDLE InvalidValue()
  63. {
  64. return INVALID_HANDLE_VALUE;
  65. }
  66. };
  67. template<typename Traits>
  68. class ScopedHANDLE
  69. {
  70. HANDLE hAttachedHandle;
  71. public:
  72. ScopedHANDLE(HANDLE handle) :
  73. hAttachedHandle(handle)
  74. {
  75. }
  76. ScopedHANDLE()
  77. {
  78. hAttachedHandle = Traits::InvalidValue();
  79. }
  80. ScopedHANDLE& operator=(HANDLE handle)
  81. {
  82. Close();
  83. hAttachedHandle = handle;
  84. return *this;
  85. }
  86. ~ScopedHANDLE()
  87. {
  88. Close();
  89. }
  90. bool IsValid()
  91. {
  92. return hAttachedHandle != Traits::InvalidValue();
  93. }
  94. HANDLE Get()
  95. {
  96. return hAttachedHandle;
  97. }
  98. HANDLE& GetRawRef()
  99. {
  100. return hAttachedHandle;
  101. }
  102. void Attach(HANDLE handle)
  103. {
  104. Close();
  105. hAttachedHandle = handle;
  106. }
  107. void Detach()
  108. {
  109. // Do not close handle
  110. hAttachedHandle = Traits::InvalidValue();
  111. }
  112. bool Close()
  113. {
  114. bool success = true;
  115. if (hAttachedHandle != Traits::InvalidValue())
  116. {
  117. if (::CloseHandle(hAttachedHandle) != TRUE)
  118. {
  119. success = false;
  120. }
  121. hAttachedHandle = Traits::InvalidValue();
  122. }
  123. return success;
  124. }
  125. };
  126. // Different Windows API functions have different invalid values.
  127. // These types are provided to improve readability.
  128. typedef ScopedHANDLE < ScopedHANDLE_NullTraits > ScopedEventHANDLE;
  129. typedef ScopedHANDLE < ScopedHANDLE_InvalidTraits > ScopedFileHANDLE;
  130. typedef ScopedHANDLE < ScopedHANDLE_NullTraits > ScopedProcessHANDLE;
  131. typedef ScopedHANDLE < ScopedHANDLE_NullTraits > ScopedThreadHANDLE;
  132. typedef ScopedHANDLE < ScopedHANDLE_NullTraits > ScopedSemaphoreHANDLE;
  133. // Scoped registry keys
  134. class ScopedHKEY
  135. {
  136. HKEY hAttachedHandle;
  137. public:
  138. ScopedHKEY(HKEY handle) :
  139. hAttachedHandle(handle)
  140. {
  141. }
  142. ScopedHKEY()
  143. {
  144. hAttachedHandle = nullptr;
  145. }
  146. ScopedHKEY& operator=(HKEY handle)
  147. {
  148. Close();
  149. hAttachedHandle = handle;
  150. return *this;
  151. }
  152. ~ScopedHKEY()
  153. {
  154. Close();
  155. }
  156. bool IsValid()
  157. {
  158. return hAttachedHandle != nullptr;
  159. }
  160. HKEY Get()
  161. {
  162. return hAttachedHandle;
  163. }
  164. HKEY& GetRawRef()
  165. {
  166. return hAttachedHandle;
  167. }
  168. void Attach(HKEY handle)
  169. {
  170. Close();
  171. hAttachedHandle = handle;
  172. }
  173. void Detach()
  174. {
  175. // Do not close handle
  176. hAttachedHandle = nullptr;
  177. }
  178. bool Close()
  179. {
  180. bool success = true;
  181. if (hAttachedHandle != nullptr)
  182. {
  183. if (::RegCloseKey(hAttachedHandle) == ERROR_SUCCESS)
  184. {
  185. success = false;
  186. }
  187. hAttachedHandle = nullptr;
  188. }
  189. return success;
  190. }
  191. };
  192. } // namespace OVR
  193. #endif // OVR_OS_WIN32
  194. #endif // OVR_Win32_IncludeWindows_h