fix doc example typo
[boost.git] / boost / asio / detail / socket_holder.hpp
blobbe144a67758b624468d4e919a84578f0750d7ca1
1 //
2 // socket_holder.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
11 #ifndef BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
12 #define BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/noncopyable.hpp>
21 #include <boost/asio/detail/socket_ops.hpp>
23 namespace boost {
24 namespace asio {
25 namespace detail {
27 // Implement the resource acquisition is initialisation idiom for sockets.
28 class socket_holder
29 : private noncopyable
31 public:
32 // Construct as an uninitialised socket.
33 socket_holder()
34 : socket_(invalid_socket)
38 // Construct to take ownership of the specified socket.
39 explicit socket_holder(socket_type s)
40 : socket_(s)
44 // Destructor.
45 ~socket_holder()
47 if (socket_ != invalid_socket)
49 boost::system::error_code ec;
50 socket_ops::close(socket_, ec);
54 // Get the underlying socket.
55 socket_type get() const
57 return socket_;
60 // Reset to an uninitialised socket.
61 void reset()
63 if (socket_ != invalid_socket)
65 boost::system::error_code ec;
66 socket_ops::close(socket_, ec);
67 socket_ = invalid_socket;
71 // Reset to take ownership of the specified socket.
72 void reset(socket_type s)
74 reset();
75 socket_ = s;
78 // Release ownership of the socket.
79 socket_type release()
81 socket_type tmp = socket_;
82 socket_ = invalid_socket;
83 return tmp;
86 private:
87 // The underlying socket.
88 socket_type socket_;
91 } // namespace detail
92 } // namespace asio
93 } // namespace boost
95 #include <boost/asio/detail/pop_options.hpp>
97 #endif // BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP