// // auto_h.h // #pragma once template class auto_handle; template class CHandleProxy { public: CHandleProxy (auto_handle& ah) : m_ah(ah) {}; CHandleProxy (const auto_handle& ah) : m_ah(const_cast&> (ah)) {}; operator T* () { return &m_ah.h; } operator const T* () const { return &m_ah.h; } operator auto_handle* () { return &m_ah; } protected: mutable auto_handle& m_ah; }; template class auto_handle { public: auto_handle(T p = 0) : h(p) {}; auto_handle(const auto_handle& rhs) : h(rhs.release()) {}; ~auto_handle() { if (h && INVALID_HANDLE_VALUE != h) CloseHandle(h); }; auto_handle& operator= (const auto_handle& rhs) { if (this != rhs.getThis()) reset (rhs.release() ); return *this; }; auto_handle& operator= (HANDLE rhs) { if ((NULL == rhs) || (INVALID_HANDLE_VALUE == rhs)) { AssertSz(false, "assigning invalid HANDLE to auto_handle- use .reset instead"); // be sure and go through auto_os for dbg.lib //auto_os os; //os = (BOOL)FALSE; //AssertSz(FALSE, "auto_handle::op= Shouldn't ever get here"); } reset (rhs); return *this; }; CHandleProxy operator& () { reset(); return CHandleProxy (*this); }; // &h; const CHandleProxy operator& () const { return CHandleProxy (*this); }; // &h; operator T () { return h; }; // Checks for NULL bool operator! () { return h == NULL; } operator bool() { return h != NULL; } bool operator== (LPVOID lpv) const { return h == lpv; }; bool operator!= (LPVOID lpv) const { return h != lpv; }; bool operator== (const auto_handle& rhs) const { return h == rhs.h; }; bool operator< (const auto_handle& rhs) const { return h < rhs.h; }; // return value of current dumb pointer T get() const { return h; }; // relinquish ownership T release() const { T oldh = h; h = 0; return oldh; }; // delete owned pointer; assume ownership of p BOOL reset (T p = 0) { BOOL rt = TRUE; if (h && INVALID_HANDLE_VALUE != h) rt = CloseHandle(h); h = p; return rt; }; private: friend class CHandleProxy; // operator& throws off operator= const auto_handle * getThis() const { return this; }; // mutable is needed for release call in ctor and copy ctor mutable T h; };