WinHandle.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2014 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. class WinHandle {
  18. HANDLE handle = INVALID_HANDLE_VALUE;
  19. inline void Clear()
  20. {
  21. if (handle && handle != INVALID_HANDLE_VALUE)
  22. CloseHandle(handle);
  23. }
  24. public:
  25. inline WinHandle() {}
  26. inline WinHandle(HANDLE handle_) : handle(handle_) {}
  27. inline ~WinHandle() { Clear(); }
  28. inline operator HANDLE() const { return handle; }
  29. inline WinHandle &operator=(HANDLE handle_)
  30. {
  31. if (handle_ != handle) {
  32. Clear();
  33. handle = handle_;
  34. }
  35. return *this;
  36. }
  37. inline HANDLE *operator&() { return &handle; }
  38. inline bool Valid() const
  39. {
  40. return handle && handle != INVALID_HANDLE_VALUE;
  41. }
  42. };
  43. class WinModule {
  44. HMODULE handle = NULL;
  45. inline void Clear()
  46. {
  47. if (handle)
  48. FreeLibrary(handle);
  49. }
  50. public:
  51. inline WinModule() {}
  52. inline WinModule(HMODULE handle_) : handle(handle_) {}
  53. inline ~WinModule() { Clear(); }
  54. inline operator HMODULE() const { return handle; }
  55. inline WinModule &operator=(HMODULE handle_)
  56. {
  57. if (handle_ != handle) {
  58. Clear();
  59. handle = handle_;
  60. }
  61. return *this;
  62. }
  63. inline HMODULE *operator&() { return &handle; }
  64. inline bool Valid() const { return handle != NULL; }
  65. };