2 // detail/socket_holder.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
12 #define BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/noncopyable.hpp>
20 #include <boost/asio/detail/socket_ops.hpp>
22 #include <boost/asio/detail/push_options.hpp>
28 // Implement the resource acquisition is initialisation idiom for sockets.
33 // Construct as an uninitialised socket.
35 : socket_(invalid_socket)
39 // Construct to take ownership of the specified socket.
40 explicit socket_holder(socket_type s)
48 if (socket_ != invalid_socket)
50 boost::system::error_code ec;
51 socket_ops::state_type state = 0;
52 socket_ops::close(socket_, state, true, ec);
56 // Get the underlying socket.
57 socket_type get() const
62 // Reset to an uninitialised socket.
65 if (socket_ != invalid_socket)
67 boost::system::error_code ec;
68 socket_ops::state_type state = 0;
69 socket_ops::close(socket_, state, true, ec);
70 socket_ = invalid_socket;
74 // Reset to take ownership of the specified socket.
75 void reset(socket_type s)
81 // Release ownership of the socket.
84 socket_type tmp = socket_;
85 socket_ = invalid_socket;
90 // The underlying socket.
98 #include <boost/asio/detail/pop_options.hpp>
100 #endif // BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP