Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:44

0001 //
0002 // detail/socket_holder.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
0012 #define BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/noncopyable.hpp>
0020 #include <boost/asio/detail/socket_ops.hpp>
0021 
0022 #include <boost/asio/detail/push_options.hpp>
0023 
0024 namespace boost {
0025 namespace asio {
0026 namespace detail {
0027 
0028 // Implement the resource acquisition is initialisation idiom for sockets.
0029 class socket_holder
0030   : private noncopyable
0031 {
0032 public:
0033   // Construct as an uninitialised socket.
0034   socket_holder()
0035     : socket_(invalid_socket)
0036   {
0037   }
0038 
0039   // Construct to take ownership of the specified socket.
0040   explicit socket_holder(socket_type s)
0041     : socket_(s)
0042   {
0043   }
0044 
0045   // Destructor.
0046   ~socket_holder()
0047   {
0048     if (socket_ != invalid_socket)
0049     {
0050       boost::system::error_code ec;
0051       socket_ops::state_type state = 0;
0052       socket_ops::close(socket_, state, true, ec);
0053     }
0054   }
0055 
0056   // Get the underlying socket.
0057   socket_type get() const
0058   {
0059     return socket_;
0060   }
0061 
0062   // Reset to an uninitialised socket.
0063   void reset()
0064   {
0065     if (socket_ != invalid_socket)
0066     {
0067       boost::system::error_code ec;
0068       socket_ops::state_type state = 0;
0069       socket_ops::close(socket_, state, true, ec);
0070       socket_ = invalid_socket;
0071     }
0072   }
0073 
0074   // Reset to take ownership of the specified socket.
0075   void reset(socket_type s)
0076   {
0077     reset();
0078     socket_ = s;
0079   }
0080 
0081   // Release ownership of the socket.
0082   socket_type release()
0083   {
0084     socket_type tmp = socket_;
0085     socket_ = invalid_socket;
0086     return tmp;
0087   }
0088 
0089 private:
0090   // The underlying socket.
0091   socket_type socket_;
0092 };
0093 
0094 } // namespace detail
0095 } // namespace asio
0096 } // namespace boost
0097 
0098 #include <boost/asio/detail/pop_options.hpp>
0099 
0100 #endif // BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP