Advertisement
tinyevil

Untitled

Aug 9th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. template<class resource_descriptor>
  2. class resource_t{
  3.     using self_t = resource_t<resource_descriptor>;
  4.     using value_t = typename resource_descriptor::type;
  5. public:
  6.     resource_t()
  7.         : value()
  8.         , deleter() {
  9.     }
  10.  
  11.     template<class U>
  12.     resource_t(U&& value)
  13.         : value(std::forward<U>(value))
  14.         , deleter() {
  15.     }
  16.  
  17.     template<class U, class D>
  18.     resource_t(U&& value, D&& deleter)
  19.         : value(std::forward<U>(value))
  20.         , deleter(std::forward<D>(deleter)){
  21.     }
  22.  
  23.     resource_t(self_t&& other)
  24.         : value(other.deleter.move(other.value))
  25.         , deleter(std::move(other.deleter)){
  26.     }
  27.  
  28.     ~resource_t(){
  29.         reset();
  30.     }
  31.  
  32.     value_t release(){
  33.         return deleter.move(value);
  34.     }
  35.  
  36.     self_t& operator=(self_t&& other){
  37.         reset();
  38.         value = other.deleter.move(other.value);
  39.         deleter = std::move(other.deleter);
  40.         return *this;
  41.     }
  42.  
  43.     value_t& get(){
  44.         return value;
  45.     }
  46.  
  47.     const value_t& get() const{
  48.         return value;
  49.     }
  50.  
  51.     void reset(){
  52.         deleter(value);
  53.     }
  54. private:
  55.     value_t value;
  56.     resource_descriptor deleter;
  57. };
  58.  
  59.  
  60. ---------------------------------
  61.  
  62.  
  63. struct WindowClassResourceDescriptor{
  64.     using type = ATOM;
  65.  
  66.     WindowClassResourceDescriptor()
  67.         : instance(0){
  68.     }
  69.  
  70.     WindowClassResourceDescriptor(HINSTANCE instance)
  71.         : instance(instance){
  72.     }
  73.  
  74.     ATOM move(ATOM& value){
  75.         ATOM ret = value;
  76.         value = 0;
  77.         return ret;
  78.     }
  79.  
  80.     void operator()(ATOM& value){
  81.         if ( value ){
  82.             UnregisterClassW(MAKEINTATOM(value), instance);
  83.             value = 0;
  84.         }
  85.     }
  86. private:
  87.     HINSTANCE instance;
  88. };
  89.  
  90. using WindowClassResource = resource_t<WindowClassResourceDescriptor>;
  91.  
  92. struct HWNDResourceDescriptor {
  93.     using type = HWND;
  94.  
  95.     HWNDResourceDescriptor(){
  96.     }
  97.  
  98.     HWND move(HWND& value) {
  99.         HWND ret = value;
  100.         value = NULL;
  101.         return ret;
  102.     }
  103.  
  104.     void operator()(HWND& value) {
  105.         if (value) {
  106.             DestroyWindow(value);
  107.             value = NULL;
  108.         }
  109.     }
  110. };
  111. using HWNDResource = resource_t<HWNDResourceDescriptor>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement