annotate DEPENDENCIES/generic/include/boost/asio/detail/socket_option.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //
Chris@16 2 // detail/socket_option.hpp
Chris@16 3 // ~~~~~~~~~~~~~~~~~~~~~~~~
Chris@16 4 //
Chris@101 5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
Chris@16 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9 //
Chris@16 10
Chris@16 11 #ifndef BOOST_ASIO_DETAIL_SOCKET_OPTION_HPP
Chris@16 12 #define BOOST_ASIO_DETAIL_SOCKET_OPTION_HPP
Chris@16 13
Chris@16 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 15 # pragma once
Chris@16 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 17
Chris@16 18 #include <boost/asio/detail/config.hpp>
Chris@16 19 #include <cstddef>
Chris@16 20 #include <stdexcept>
Chris@16 21 #include <boost/asio/detail/socket_types.hpp>
Chris@16 22 #include <boost/asio/detail/throw_exception.hpp>
Chris@16 23
Chris@16 24 #include <boost/asio/detail/push_options.hpp>
Chris@16 25
Chris@16 26 namespace boost {
Chris@16 27 namespace asio {
Chris@16 28 namespace detail {
Chris@16 29 namespace socket_option {
Chris@16 30
Chris@16 31 // Helper template for implementing boolean-based options.
Chris@16 32 template <int Level, int Name>
Chris@16 33 class boolean
Chris@16 34 {
Chris@16 35 public:
Chris@16 36 // Default constructor.
Chris@16 37 boolean()
Chris@16 38 : value_(0)
Chris@16 39 {
Chris@16 40 }
Chris@16 41
Chris@16 42 // Construct with a specific option value.
Chris@16 43 explicit boolean(bool v)
Chris@16 44 : value_(v ? 1 : 0)
Chris@16 45 {
Chris@16 46 }
Chris@16 47
Chris@16 48 // Set the current value of the boolean.
Chris@16 49 boolean& operator=(bool v)
Chris@16 50 {
Chris@16 51 value_ = v ? 1 : 0;
Chris@16 52 return *this;
Chris@16 53 }
Chris@16 54
Chris@16 55 // Get the current value of the boolean.
Chris@16 56 bool value() const
Chris@16 57 {
Chris@16 58 return !!value_;
Chris@16 59 }
Chris@16 60
Chris@16 61 // Convert to bool.
Chris@16 62 operator bool() const
Chris@16 63 {
Chris@16 64 return !!value_;
Chris@16 65 }
Chris@16 66
Chris@16 67 // Test for false.
Chris@16 68 bool operator!() const
Chris@16 69 {
Chris@16 70 return !value_;
Chris@16 71 }
Chris@16 72
Chris@16 73 // Get the level of the socket option.
Chris@16 74 template <typename Protocol>
Chris@16 75 int level(const Protocol&) const
Chris@16 76 {
Chris@16 77 return Level;
Chris@16 78 }
Chris@16 79
Chris@16 80 // Get the name of the socket option.
Chris@16 81 template <typename Protocol>
Chris@16 82 int name(const Protocol&) const
Chris@16 83 {
Chris@16 84 return Name;
Chris@16 85 }
Chris@16 86
Chris@16 87 // Get the address of the boolean data.
Chris@16 88 template <typename Protocol>
Chris@16 89 int* data(const Protocol&)
Chris@16 90 {
Chris@16 91 return &value_;
Chris@16 92 }
Chris@16 93
Chris@16 94 // Get the address of the boolean data.
Chris@16 95 template <typename Protocol>
Chris@16 96 const int* data(const Protocol&) const
Chris@16 97 {
Chris@16 98 return &value_;
Chris@16 99 }
Chris@16 100
Chris@16 101 // Get the size of the boolean data.
Chris@16 102 template <typename Protocol>
Chris@16 103 std::size_t size(const Protocol&) const
Chris@16 104 {
Chris@16 105 return sizeof(value_);
Chris@16 106 }
Chris@16 107
Chris@16 108 // Set the size of the boolean data.
Chris@16 109 template <typename Protocol>
Chris@16 110 void resize(const Protocol&, std::size_t s)
Chris@16 111 {
Chris@16 112 // On some platforms (e.g. Windows Vista), the getsockopt function will
Chris@16 113 // return the size of a boolean socket option as one byte, even though a
Chris@16 114 // four byte integer was passed in.
Chris@16 115 switch (s)
Chris@16 116 {
Chris@16 117 case sizeof(char):
Chris@16 118 value_ = *reinterpret_cast<char*>(&value_) ? 1 : 0;
Chris@16 119 break;
Chris@16 120 case sizeof(value_):
Chris@16 121 break;
Chris@16 122 default:
Chris@16 123 {
Chris@16 124 std::length_error ex("boolean socket option resize");
Chris@16 125 boost::asio::detail::throw_exception(ex);
Chris@16 126 }
Chris@16 127 }
Chris@16 128 }
Chris@16 129
Chris@16 130 private:
Chris@16 131 int value_;
Chris@16 132 };
Chris@16 133
Chris@16 134 // Helper template for implementing integer options.
Chris@16 135 template <int Level, int Name>
Chris@16 136 class integer
Chris@16 137 {
Chris@16 138 public:
Chris@16 139 // Default constructor.
Chris@16 140 integer()
Chris@16 141 : value_(0)
Chris@16 142 {
Chris@16 143 }
Chris@16 144
Chris@16 145 // Construct with a specific option value.
Chris@16 146 explicit integer(int v)
Chris@16 147 : value_(v)
Chris@16 148 {
Chris@16 149 }
Chris@16 150
Chris@16 151 // Set the value of the int option.
Chris@16 152 integer& operator=(int v)
Chris@16 153 {
Chris@16 154 value_ = v;
Chris@16 155 return *this;
Chris@16 156 }
Chris@16 157
Chris@16 158 // Get the current value of the int option.
Chris@16 159 int value() const
Chris@16 160 {
Chris@16 161 return value_;
Chris@16 162 }
Chris@16 163
Chris@16 164 // Get the level of the socket option.
Chris@16 165 template <typename Protocol>
Chris@16 166 int level(const Protocol&) const
Chris@16 167 {
Chris@16 168 return Level;
Chris@16 169 }
Chris@16 170
Chris@16 171 // Get the name of the socket option.
Chris@16 172 template <typename Protocol>
Chris@16 173 int name(const Protocol&) const
Chris@16 174 {
Chris@16 175 return Name;
Chris@16 176 }
Chris@16 177
Chris@16 178 // Get the address of the int data.
Chris@16 179 template <typename Protocol>
Chris@16 180 int* data(const Protocol&)
Chris@16 181 {
Chris@16 182 return &value_;
Chris@16 183 }
Chris@16 184
Chris@16 185 // Get the address of the int data.
Chris@16 186 template <typename Protocol>
Chris@16 187 const int* data(const Protocol&) const
Chris@16 188 {
Chris@16 189 return &value_;
Chris@16 190 }
Chris@16 191
Chris@16 192 // Get the size of the int data.
Chris@16 193 template <typename Protocol>
Chris@16 194 std::size_t size(const Protocol&) const
Chris@16 195 {
Chris@16 196 return sizeof(value_);
Chris@16 197 }
Chris@16 198
Chris@16 199 // Set the size of the int data.
Chris@16 200 template <typename Protocol>
Chris@16 201 void resize(const Protocol&, std::size_t s)
Chris@16 202 {
Chris@16 203 if (s != sizeof(value_))
Chris@16 204 {
Chris@16 205 std::length_error ex("integer socket option resize");
Chris@16 206 boost::asio::detail::throw_exception(ex);
Chris@16 207 }
Chris@16 208 }
Chris@16 209
Chris@16 210 private:
Chris@16 211 int value_;
Chris@16 212 };
Chris@16 213
Chris@16 214 // Helper template for implementing linger options.
Chris@16 215 template <int Level, int Name>
Chris@16 216 class linger
Chris@16 217 {
Chris@16 218 public:
Chris@16 219 // Default constructor.
Chris@16 220 linger()
Chris@16 221 {
Chris@16 222 value_.l_onoff = 0;
Chris@16 223 value_.l_linger = 0;
Chris@16 224 }
Chris@16 225
Chris@16 226 // Construct with specific option values.
Chris@16 227 linger(bool e, int t)
Chris@16 228 {
Chris@16 229 enabled(e);
Chris@16 230 timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION(t);
Chris@16 231 }
Chris@16 232
Chris@16 233 // Set the value for whether linger is enabled.
Chris@16 234 void enabled(bool value)
Chris@16 235 {
Chris@16 236 value_.l_onoff = value ? 1 : 0;
Chris@16 237 }
Chris@16 238
Chris@16 239 // Get the value for whether linger is enabled.
Chris@16 240 bool enabled() const
Chris@16 241 {
Chris@16 242 return value_.l_onoff != 0;
Chris@16 243 }
Chris@16 244
Chris@16 245 // Set the value for the linger timeout.
Chris@16 246 void timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION(int value)
Chris@16 247 {
Chris@16 248 #if defined(WIN32)
Chris@16 249 value_.l_linger = static_cast<u_short>(value);
Chris@16 250 #else
Chris@16 251 value_.l_linger = value;
Chris@16 252 #endif
Chris@16 253 }
Chris@16 254
Chris@16 255 // Get the value for the linger timeout.
Chris@16 256 int timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION() const
Chris@16 257 {
Chris@16 258 return static_cast<int>(value_.l_linger);
Chris@16 259 }
Chris@16 260
Chris@16 261 // Get the level of the socket option.
Chris@16 262 template <typename Protocol>
Chris@16 263 int level(const Protocol&) const
Chris@16 264 {
Chris@16 265 return Level;
Chris@16 266 }
Chris@16 267
Chris@16 268 // Get the name of the socket option.
Chris@16 269 template <typename Protocol>
Chris@16 270 int name(const Protocol&) const
Chris@16 271 {
Chris@16 272 return Name;
Chris@16 273 }
Chris@16 274
Chris@16 275 // Get the address of the linger data.
Chris@16 276 template <typename Protocol>
Chris@16 277 detail::linger_type* data(const Protocol&)
Chris@16 278 {
Chris@16 279 return &value_;
Chris@16 280 }
Chris@16 281
Chris@16 282 // Get the address of the linger data.
Chris@16 283 template <typename Protocol>
Chris@16 284 const detail::linger_type* data(const Protocol&) const
Chris@16 285 {
Chris@16 286 return &value_;
Chris@16 287 }
Chris@16 288
Chris@16 289 // Get the size of the linger data.
Chris@16 290 template <typename Protocol>
Chris@16 291 std::size_t size(const Protocol&) const
Chris@16 292 {
Chris@16 293 return sizeof(value_);
Chris@16 294 }
Chris@16 295
Chris@16 296 // Set the size of the int data.
Chris@16 297 template <typename Protocol>
Chris@16 298 void resize(const Protocol&, std::size_t s)
Chris@16 299 {
Chris@16 300 if (s != sizeof(value_))
Chris@16 301 {
Chris@16 302 std::length_error ex("linger socket option resize");
Chris@16 303 boost::asio::detail::throw_exception(ex);
Chris@16 304 }
Chris@16 305 }
Chris@16 306
Chris@16 307 private:
Chris@16 308 detail::linger_type value_;
Chris@16 309 };
Chris@16 310
Chris@16 311 } // namespace socket_option
Chris@16 312 } // namespace detail
Chris@16 313 } // namespace asio
Chris@16 314 } // namespace boost
Chris@16 315
Chris@16 316 #include <boost/asio/detail/pop_options.hpp>
Chris@16 317
Chris@16 318 #endif // BOOST_ASIO_DETAIL_SOCKET_OPTION_HPP