1
0

WinHandle.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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;
  19. inline void Clear()
  20. {
  21. if (handle && handle != INVALID_HANDLE_VALUE)
  22. CloseHandle(handle);
  23. }
  24. public:
  25. inline WinHandle() : handle(NULL) {}
  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&()
  38. {
  39. Clear();
  40. handle = NULL;
  41. return &handle;
  42. }
  43. inline bool Valid() const
  44. {
  45. return handle && handle != INVALID_HANDLE_VALUE;
  46. }
  47. };