Created
November 24, 2010 07:55
-
-
Save masaki/713295 to your computer and use it in GitHub Desktop.
C++ smart_ptr enabled handle class w/ custom deleter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace win32 { | |
| // deleter traits | |
| template <class T> struct handle_deleter_t { | |
| typedef T handle_type; | |
| }; | |
| struct win32_handle_deleter : public handle_deleter_t<HANDLE> { | |
| void operator()(handle_type handle) { | |
| if (handle && handle != INVALID_HANDLE_VALUE) ::CloseHandle(handle); | |
| }; | |
| }; | |
| // smart handle pointer | |
| template <class T, class D> | |
| struct smart_handle_t : public std::tr1::shared_ptr<typename std::tr1::remove_pointer<T>::type> { | |
| typedef smart_handle_t<T, D> type; | |
| typedef std::tr1::shared_ptr<typename std::tr1::remove_pointer<T>::type> base_type; | |
| typedef T handle_type; | |
| typedef D deleter_type; | |
| smart_handle_t() : base_type() {}; | |
| explicit smart_handle_t(handle_type handle) : base_type(handle, deleter_type()) {}; | |
| void reset() { | |
| base_type::reset(); | |
| }; | |
| void reset(handle_type handle) { | |
| base_type::reset(handle, deleter_type()); | |
| }; | |
| type& operator=(handle_type handle) { | |
| base_type::reset(handle, deleter_type()); | |
| return *this; | |
| }; | |
| }; | |
| typedef smart_handle_t<HANDLE, win32_handle_deleter> shared_handle; | |
| typedef shared_handle pipe_handle; | |
| typedef shared_handle event_handle; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment