Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / asio / generic / raw_protocol.hpp
1 //
2 // generic/raw_protocol.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 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 //
10
11 #ifndef BOOST_ASIO_GENERIC_RAW_PROTOCOL_HPP
12 #define BOOST_ASIO_GENERIC_RAW_PROTOCOL_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #include <typeinfo>
21 #include <boost/asio/basic_raw_socket.hpp>
22 #include <boost/asio/detail/socket_types.hpp>
23 #include <boost/asio/detail/throw_exception.hpp>
24 #include <boost/asio/generic/basic_endpoint.hpp>
25
26 #include <boost/asio/detail/push_options.hpp>
27
28 namespace boost {
29 namespace asio {
30 namespace generic {
31
32 /// Encapsulates the flags needed for a generic raw socket.
33 /**
34  * The boost::asio::generic::raw_protocol class contains flags necessary for
35  * raw sockets of any address family and protocol.
36  *
37  * @par Examples
38  * Constructing using a native address family and socket protocol:
39  * @code raw_protocol p(AF_INET, IPPROTO_ICMP); @endcode
40  * Constructing from a specific protocol type:
41  * @code raw_protocol p(boost::asio::ip::icmp::v4()); @endcode
42  *
43  * @par Thread Safety
44  * @e Distinct @e objects: Safe.@n
45  * @e Shared @e objects: Safe.
46  *
47  * @par Concepts:
48  * Protocol.
49  */
50 class raw_protocol
51 {
52 public:
53   /// Construct a protocol object for a specific address family and protocol.
54   raw_protocol(int address_family, int socket_protocol)
55     : family_(address_family),
56       protocol_(socket_protocol)
57   {
58   }
59
60   /// Construct a generic protocol object from a specific protocol.
61   /**
62    * @throws @c bad_cast Thrown if the source protocol is not raw-oriented.
63    */
64   template <typename Protocol>
65   raw_protocol(const Protocol& source_protocol)
66     : family_(source_protocol.family()),
67       protocol_(source_protocol.protocol())
68   {
69     if (source_protocol.type() != type())
70     {
71       std::bad_cast ex;
72       boost::asio::detail::throw_exception(ex);
73     }
74   }
75
76   /// Obtain an identifier for the type of the protocol.
77   int type() const
78   {
79     return BOOST_ASIO_OS_DEF(SOCK_RAW);
80   }
81
82   /// Obtain an identifier for the protocol.
83   int protocol() const
84   {
85     return protocol_;
86   }
87
88   /// Obtain an identifier for the protocol family.
89   int family() const
90   {
91     return family_;
92   }
93
94   /// Compare two protocols for equality.
95   friend bool operator==(const raw_protocol& p1, const raw_protocol& p2)
96   {
97     return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_;
98   }
99
100   /// Compare two protocols for inequality.
101   friend bool operator!=(const raw_protocol& p1, const raw_protocol& p2)
102   {
103     return !(p1 == p2);
104   }
105
106   /// The type of an endpoint.
107   typedef basic_endpoint<raw_protocol> endpoint;
108
109   /// The generic socket type.
110   typedef basic_raw_socket<raw_protocol> socket;
111
112 private:
113   int family_;
114   int protocol_;
115 };
116
117 } // namespace generic
118 } // namespace asio
119 } // namespace boost
120
121 #include <boost/asio/detail/pop_options.hpp>
122
123 #endif // BOOST_ASIO_GENERIC_RAW_PROTOCOL_HPP