1 /* 2 * Copyright Andrey Semashev 2016. 3 * Distributed under the Boost Software License, Version 1.0. 4 * (See accompanying file LICENSE_1_0.txt or copy at 5 * http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 /*! 8 * \file windows/auto_handle.hpp 9 * \author Andrey Semashev 10 * \date 07.03.2016 11 * 12 * \brief This header is the Boost.Log library implementation, see the library documentation 13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. 14 */ 15 16 #ifndef BOOST_LOG_WINDOWS_AUTO_HANDLE_HPP_INCLUDED_ 17 #define BOOST_LOG_WINDOWS_AUTO_HANDLE_HPP_INCLUDED_ 18 19 #include <boost/log/detail/config.hpp> 20 #include <boost/assert.hpp> 21 #include <boost/winapi/handles.hpp> 22 #include <boost/log/detail/header.hpp> 23 24 namespace boost { 25 26 BOOST_LOG_OPEN_NAMESPACE 27 28 namespace ipc { 29 30 namespace aux { 31 32 //! A wrapper around a kernel object handle. Automatically closes the handle on destruction. 33 class auto_handle 34 { 35 private: 36 boost::winapi::HANDLE_ m_handle; 37 38 public: auto_handle(boost::winapi::HANDLE_ h=NULL)39 explicit auto_handle(boost::winapi::HANDLE_ h = NULL) BOOST_NOEXCEPT : m_handle(h) 40 { 41 } 42 ~auto_handle()43 ~auto_handle() BOOST_NOEXCEPT 44 { 45 if (m_handle) 46 BOOST_VERIFY(boost::winapi::CloseHandle(m_handle) != 0); 47 } 48 init(boost::winapi::HANDLE_ h)49 void init(boost::winapi::HANDLE_ h) BOOST_NOEXCEPT 50 { 51 BOOST_ASSERT(m_handle == NULL); 52 m_handle = h; 53 } 54 get() const55 boost::winapi::HANDLE_ get() const BOOST_NOEXCEPT { return m_handle; } get_ptr()56 boost::winapi::HANDLE_* get_ptr() BOOST_NOEXCEPT { return &m_handle; } 57 swap(auto_handle & that)58 void swap(auto_handle& that) BOOST_NOEXCEPT 59 { 60 boost::winapi::HANDLE_ h = m_handle; 61 m_handle = that.m_handle; 62 that.m_handle = h; 63 } 64 65 BOOST_DELETED_FUNCTION(auto_handle(auto_handle const&)) 66 BOOST_DELETED_FUNCTION(auto_handle& operator=(auto_handle const&)) 67 }; 68 69 } // namespace aux 70 71 } // namespace ipc 72 73 BOOST_LOG_CLOSE_NAMESPACE // namespace log 74 75 } // namespace boost 76 77 #include <boost/log/detail/footer.hpp> 78 79 #endif // BOOST_LOG_WINDOWS_AUTO_HANDLE_HPP_INCLUDED_ 80