Advertisement
Fernando_Fiore

CHandleT

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