// SafeHandle.hpp #if !defined(SAFE_HANDLE_HPP) #define SAFE_HANDLE_HPP #include namespace CipresJNI { // SafeHandle class SafeHandle { public: explicit SafeHandle(HANDLE value = NULL) throw() : m_handle(value) { } SafeHandle(SafeHandle &value) throw() : m_handle(value.m_handle) { // maintain exclusive ownership value.m_handle = NULL; } ~SafeHandle() throw() { if(m_handle != NULL) ::CloseHandle(m_handle); } // Get HANDLE Get() const throw() { return m_handle; } // Reset void Reset(HANDLE value) throw() { if(m_handle != NULL) ::CloseHandle(m_handle); m_handle = value; } // operator & HANDLE *operator &() throw() { if(m_handle != NULL) { ::CloseHandle(m_handle); m_handle = NULL; } return &m_handle; } // operator == bool operator ==(int null_value) const throw() { assert(null_value == NULL); return m_handle == NULL; } // operator != bool operator !=(int null_value) const throw() { assert(null_value == NULL); return m_handle != NULL; } // bool conversion operator operator bool() const throw() { return m_handle != NULL; } // operator ! bool operator !() const throw() { return m_handle == NULL; } private: HANDLE m_handle; }; } // namespace CipresJNI #endif // SAFE_HANDLE_HPP