Advertisement
Guest User

Untitled

a guest
Oct 4th, 2020
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. // WTFPL
  2. #pragma once
  3. #include <type_traits>  // is_same
  4.  
  5. #ifndef NOMINMAX
  6. #define NOMINMAX 1
  7. #define NOMINMAX_WAS_NOT_DEFINED 1
  8. #endif
  9.  
  10. #ifndef WIN32_LEAN_AND_MEAN
  11. #define WIN32_LEAN_AND_MEAN 1
  12. #define LM_WAS_NOT_DEFINED 1
  13. #endif
  14.  
  15. #include <windows.h>
  16.  
  17. #ifdef LM_WAS_NOT_DEFINED
  18. #undef LMS_WAS_NOT_DEFINED
  19. #undef WIN32_LEAN_AND_MEAN
  20. #endif
  21.  
  22. #ifdef NOMINMAX_WAS_NOT_DEFINED
  23. #undef NOMINMAX_WAS_NOT_DEFINED
  24. #undef NOMINMAX
  25. #endif
  26.  
  27. namespace ApiResource
  28. {
  29.     template<typename T, BOOL(WINAPI *Deleter)(T)>
  30.     struct UniqueApiResource {
  31.         UniqueApiResource() noexcept :
  32.             res(nullptr)
  33.         {
  34.         }
  35.  
  36.         UniqueApiResource(T res) noexcept :
  37.             res(res)
  38.         {
  39.         }
  40.  
  41.         UniqueApiResource(UniqueApiResource && other) noexcept :
  42.             res(other.res)
  43.         {
  44.             other.res = nullptr;
  45.         }
  46.  
  47.         UniqueApiResource & operator=(UniqueApiResource && other) noexcept
  48.         {
  49.             res = other.res;
  50.             other.res = nullptr;
  51.             return *this;
  52.         }
  53.  
  54.         UniqueApiResource & operator=(nullptr_t) noexcept
  55.         {
  56.             if (res) {
  57.                 Deleter(res);
  58.                 res = nullptr;
  59.             }
  60.             return *this;
  61.         }
  62.  
  63.         ~UniqueApiResource() noexcept(noexcept(Deleter))
  64.         {
  65.             if (!operator!()) {
  66.                 Deleter(res);
  67.             }
  68.         }
  69.  
  70.         T get() noexcept
  71.         {
  72.             return res;
  73.         }
  74.  
  75.         const T get() const noexcept
  76.         {
  77.             return res;
  78.         }
  79.  
  80.         T eject() noexcept
  81.         {
  82.             T temp{ res };
  83.             res = nullptr;
  84.             return temp;
  85.         }
  86.  
  87.         bool operator!() const noexcept
  88.         {
  89.             if constexpr (std::is_same_v<T, HANDLE>)
  90.             {
  91.                 return res == nullptr || res == INVALID_HANDLE_VALUE;
  92.             } else {
  93.                 return res == nullptr;
  94.             }
  95.         }
  96.         operator bool() const noexcept
  97.         {
  98.             return !operator!();
  99.         }
  100.  
  101.  
  102.         UniqueApiResource(const UniqueApiResource &) = delete;
  103.         UniqueApiResource & operator=(const UniqueApiResource &) = delete;
  104.     protected:
  105.         T res;
  106.     };
  107.  
  108.     inline BOOL WINAPI unmapViewOfFile_const_fix(LPVOID addr)
  109.     {
  110.         return UnmapViewOfFile(addr);
  111.     }
  112.  
  113.     using UniqueLibrary = UniqueApiResource<HMODULE, FreeLibrary>;
  114.     using UniqueHandle = UniqueApiResource<HANDLE, CloseHandle>;
  115.     using UniqueViewOfFile = UniqueApiResource<LPVOID, &unmapViewOfFile_const_fix>;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement