Advertisement
Fernando_Fiore

traits

Aug 30th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. struct WinHandleTraits
  2. {
  3.     typedef HANDLE handle_type;
  4.  
  5.     static BOOL Destroy(HANDLE h)
  6.     {
  7.         return ::CloseHandle(h);
  8.     }
  9.     static HANDLE InvalidHandle(void)
  10.     {
  11.         return NULL;
  12.     }
  13. };
  14.  
  15.  
  16. template <class HandleTraits , BOOL t_Owner = TRUE>
  17. class CHandleT : public HandleTraits
  18. {
  19. public:
  20.     typedef typename HandleTraits::handle_type handle_type;
  21. private:
  22.     handle_type m_h;
  23.     typedef CHandleT<HandleTraits, t_Owner> _ThisClass;
  24. public:
  25.     CHandleT(handle_type h = HandleTraits::InvalidHandle()) :m_h(h)
  26.     {
  27.         //
  28.     }
  29.     CHandleT(_ThisClass& h) :m_h(HandleTraits::InvalidHandle())
  30.     {
  31.         Assign(h);
  32.     }
  33.  
  34.     ~CHandleT()
  35.     {
  36.         BOOL res = CloseHandle();
  37.         _ASSERTE(res);
  38.     }
  39.     BOOL CloseHandle(void)
  40.     {
  41.         BOOL res = TRUE;
  42.  
  43.         if (IsOwner() && IsHandleValid()) {
  44.             if (res = HandleTraits::Destroy(m_h))
  45.                 m_h = HandleTraits::InvalidHandle();
  46.         }
  47.         else if (!IsOwner()) {
  48.             m_h = HandleTraits::InvalidHandle();
  49.         }
  50.         return res;
  51.     }
  52.     BOOL IsOwner(void) const
  53.     {
  54.         return t_Owner;
  55.     }
  56.     BOOL IsHandleValid(void) const
  57.     {
  58.         return m_h != HandleTraits::InvalidHandle();
  59.     }
  60.     // this will destroy any previous handle, if its owner
  61.     BOOL AttachHandle(handle_type h)
  62.     {
  63.         BOOL res = CloseHandle();
  64.  
  65.         if (res) {
  66.             m_h = h;
  67.         }
  68.         return res;
  69.     }
  70.     handle_type DetachHandle(void)
  71.     {
  72.         handle_type hOld = m_h;
  73.         m_h = HandleTraits::InvalidHandle();
  74.         return hOld;
  75.     }
  76.     // takes ownership if _ThisClass owns the handle
  77.     _ThisClass& Assign(_ThisClass& rhs)
  78.     {
  79.         if (GetHandle() != rhs.GetHandle()) {
  80.             BOOL res = AttachHandle(rhs.DetachHandle());
  81.             _ASSERTE(res);
  82.         }
  83.         return *this;
  84.     }
  85.     _ThisClass& Assign(handle_type t)
  86.     {
  87.         BOOL res = AttachHandle(t);
  88.         _ASSERTE(res);
  89.         return *this;
  90.     }
  91.  
  92.     operator handle_type() const
  93.     {
  94.         return m_h;
  95.     }
  96.  
  97.     const handle_type& GetHandle(void) const
  98.     {
  99.         return m_h;
  100.     }
  101.     handle_type& GetHandle(void)
  102.     {
  103.         return m_h;
  104.     }
  105.     _ThisClass& operator = (_ThisClass& rhs)
  106.     {
  107.         return Assign(rhs);
  108.     }
  109.     _ThisClass& operator = (handle_type h)
  110.     {
  111.         return Assign(h);
  112.     }
  113.  
  114.     BOOL operator == (handle_type h) const
  115.     {
  116.         return m_h == h ? TRUE : FALSE;
  117.     }
  118.     BOOL operator != (handle_type h) const
  119.     {
  120.         return m_h != h ? TRUE : FALSE;
  121.     }
  122. };
  123.  
  124. // win32 handle resource
  125. typedef CHandleT<WinHandleTraits, TRUE> CWinHandle;
  126.  
  127. int main()
  128. {
  129.     CWinHandle handle;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement