Skip to content

Instantly share code, notes, and snippets.

@dranger003
Created October 19, 2015 00:52
Show Gist options
  • Save dranger003/a17e3bd19a8e12541a6a to your computer and use it in GitHub Desktop.
Save dranger003/a17e3bd19a8e12541a6a to your computer and use it in GitHub Desktop.
#pragma once
struct CFileHandleTraits
{
typedef HANDLE HTYPE;
static const HTYPE INVALID_HANDLE;
static BOOL Close(HTYPE h)
{
return ::CloseHandle(h) != FALSE;
}
};
struct CThreadHandleTraits
{
typedef HANDLE HTYPE;
static const HTYPE INVALID_HANDLE;
static BOOL Close(HTYPE h)
{
return ::CloseHandle(h) != FALSE;
}
};
struct CEventHandleTraits
{
typedef HANDLE HTYPE;
static const HTYPE INVALID_HANDLE;
static BOOL Close(HTYPE h)
{
return ::CloseHandle(h) != FALSE;
}
};
struct CFindFileHandleTraits
{
typedef HANDLE HTYPE;
static const HTYPE INVALID_HANDLE;
static BOOL Close(HTYPE h)
{
return ::FindClose(h) != FALSE;
}
};
const CFileHandleTraits::HTYPE CFileHandleTraits::INVALID_HANDLE = INVALID_HANDLE_VALUE;
const CThreadHandleTraits::HTYPE CThreadHandleTraits::INVALID_HANDLE = NULL;
const CEventHandleTraits::HTYPE CEventHandleTraits::INVALID_HANDLE = NULL;
const CFindFileHandleTraits::HTYPE CFindFileHandleTraits::INVALID_HANDLE = INVALID_HANDLE_VALUE;
template<typename CHandleExTraits>
class CHandleEx
{
public:
CHandleEx() throw();
CHandleEx(CHandleEx& h) throw();
explicit CHandleEx(typename CHandleExTraits::HTYPE h) throw();
virtual ~CHandleEx() throw();
CHandleEx& operator=(CHandleEx& h) throw();
operator typename CHandleExTraits::HTYPE() const throw();
void Attach(typename CHandleExTraits::HTYPE h) throw();
typename CHandleExTraits::HTYPE Detach() throw();
void Close() throw();
public:
typename CHandleExTraits::HTYPE m_h;
};
template<typename CHandleExTraits>
inline CHandleEx<CHandleExTraits>::CHandleEx() throw() :
m_h(CHandleExTraits::INVALID_HANDLE)
{
}
template<typename CHandleExTraits>
inline CHandleEx<CHandleExTraits>::CHandleEx(CHandleEx& h) throw() :
m_h(CHandleExTraits::INVALID_HANDLE)
{
Attach(h.Detach());
}
template<typename CHandleExTraits>
inline CHandleEx<CHandleExTraits>::CHandleEx(typename CHandleExTraits::HTYPE h) throw() :
m_h(h)
{
}
template<typename CHandleExTraits>
inline CHandleEx<CHandleExTraits>::~CHandleEx() throw()
{
if (m_h != CHandleExTraits::INVALID_HANDLE)
Close();
}
template<typename CHandleExTraits>
inline CHandleEx<CHandleExTraits>& CHandleEx<CHandleExTraits>::operator=(CHandleEx& h) throw()
{
if (this != &h)
{
if (m_h != CHandleExTraits::INVALID_HANDLE)
Close();
Attach(h.Detach());
}
return *this;
}
template<typename CHandleExTraits>
inline CHandleEx<CHandleExTraits>::operator typename CHandleExTraits::HTYPE() const throw()
{
return m_h;
}
template<typename CHandleExTraits>
inline void CHandleEx<CHandleExTraits>::Attach(typename CHandleExTraits::HTYPE h) throw()
{
_ASSERT(m_h == CHandleExTraits::INVALID_HANDLE);
m_h = h;
}
template<typename CHandleExTraits>
inline typename CHandleExTraits::HTYPE CHandleEx<CHandleExTraits>::Detach() throw()
{
CHandleExTraits::HTYPE h;
h = m_h;
m_h = CHandleExTraits::INVALID_HANDLE;
return h;
}
template<typename CHandleExTraits>
inline void CHandleEx<CHandleExTraits>::Close() throw()
{
if (m_h != CHandleExTraits::INVALID_HANDLE)
{
CHandleExTraits::Close(m_h);
m_h = CHandleExTraits::INVALID_HANDLE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment