annotate DEPENDENCIES/generic/include/boost/asio/posix/basic_descriptor.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 // posix/basic_descriptor.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_POSIX_BASIC_DESCRIPTOR_HPP
Chris@16 12 #define BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_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
Chris@16 20 #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
Chris@16 21 || defined(GENERATING_DOCUMENTATION)
Chris@16 22
Chris@16 23 #include <boost/asio/basic_io_object.hpp>
Chris@16 24 #include <boost/asio/detail/throw_error.hpp>
Chris@16 25 #include <boost/asio/error.hpp>
Chris@16 26 #include <boost/asio/posix/descriptor_base.hpp>
Chris@16 27
Chris@16 28 #include <boost/asio/detail/push_options.hpp>
Chris@16 29
Chris@16 30 namespace boost {
Chris@16 31 namespace asio {
Chris@16 32 namespace posix {
Chris@16 33
Chris@16 34 /// Provides POSIX descriptor functionality.
Chris@16 35 /**
Chris@16 36 * The posix::basic_descriptor class template provides the ability to wrap a
Chris@16 37 * POSIX descriptor.
Chris@16 38 *
Chris@16 39 * @par Thread Safety
Chris@16 40 * @e Distinct @e objects: Safe.@n
Chris@16 41 * @e Shared @e objects: Unsafe.
Chris@16 42 */
Chris@16 43 template <typename DescriptorService>
Chris@16 44 class basic_descriptor
Chris@16 45 : public basic_io_object<DescriptorService>,
Chris@16 46 public descriptor_base
Chris@16 47 {
Chris@16 48 public:
Chris@16 49 /// (Deprecated: Use native_handle_type.) The native representation of a
Chris@16 50 /// descriptor.
Chris@16 51 typedef typename DescriptorService::native_handle_type native_type;
Chris@16 52
Chris@16 53 /// The native representation of a descriptor.
Chris@16 54 typedef typename DescriptorService::native_handle_type native_handle_type;
Chris@16 55
Chris@16 56 /// A basic_descriptor is always the lowest layer.
Chris@16 57 typedef basic_descriptor<DescriptorService> lowest_layer_type;
Chris@16 58
Chris@16 59 /// Construct a basic_descriptor without opening it.
Chris@16 60 /**
Chris@16 61 * This constructor creates a descriptor without opening it.
Chris@16 62 *
Chris@16 63 * @param io_service The io_service object that the descriptor will use to
Chris@16 64 * dispatch handlers for any asynchronous operations performed on the
Chris@16 65 * descriptor.
Chris@16 66 */
Chris@16 67 explicit basic_descriptor(boost::asio::io_service& io_service)
Chris@16 68 : basic_io_object<DescriptorService>(io_service)
Chris@16 69 {
Chris@16 70 }
Chris@16 71
Chris@16 72 /// Construct a basic_descriptor on an existing native descriptor.
Chris@16 73 /**
Chris@16 74 * This constructor creates a descriptor object to hold an existing native
Chris@16 75 * descriptor.
Chris@16 76 *
Chris@16 77 * @param io_service The io_service object that the descriptor will use to
Chris@16 78 * dispatch handlers for any asynchronous operations performed on the
Chris@16 79 * descriptor.
Chris@16 80 *
Chris@16 81 * @param native_descriptor A native descriptor.
Chris@16 82 *
Chris@16 83 * @throws boost::system::system_error Thrown on failure.
Chris@16 84 */
Chris@16 85 basic_descriptor(boost::asio::io_service& io_service,
Chris@16 86 const native_handle_type& native_descriptor)
Chris@16 87 : basic_io_object<DescriptorService>(io_service)
Chris@16 88 {
Chris@16 89 boost::system::error_code ec;
Chris@16 90 this->get_service().assign(this->get_implementation(),
Chris@16 91 native_descriptor, ec);
Chris@16 92 boost::asio::detail::throw_error(ec, "assign");
Chris@16 93 }
Chris@16 94
Chris@16 95 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
Chris@16 96 /// Move-construct a basic_descriptor from another.
Chris@16 97 /**
Chris@16 98 * This constructor moves a descriptor from one object to another.
Chris@16 99 *
Chris@16 100 * @param other The other basic_descriptor object from which the move will
Chris@16 101 * occur.
Chris@16 102 *
Chris@16 103 * @note Following the move, the moved-from object is in the same state as if
Chris@16 104 * constructed using the @c basic_descriptor(io_service&) constructor.
Chris@16 105 */
Chris@16 106 basic_descriptor(basic_descriptor&& other)
Chris@16 107 : basic_io_object<DescriptorService>(
Chris@16 108 BOOST_ASIO_MOVE_CAST(basic_descriptor)(other))
Chris@16 109 {
Chris@16 110 }
Chris@16 111
Chris@16 112 /// Move-assign a basic_descriptor from another.
Chris@16 113 /**
Chris@16 114 * This assignment operator moves a descriptor from one object to another.
Chris@16 115 *
Chris@16 116 * @param other The other basic_descriptor object from which the move will
Chris@16 117 * occur.
Chris@16 118 *
Chris@16 119 * @note Following the move, the moved-from object is in the same state as if
Chris@16 120 * constructed using the @c basic_descriptor(io_service&) constructor.
Chris@16 121 */
Chris@16 122 basic_descriptor& operator=(basic_descriptor&& other)
Chris@16 123 {
Chris@16 124 basic_io_object<DescriptorService>::operator=(
Chris@16 125 BOOST_ASIO_MOVE_CAST(basic_descriptor)(other));
Chris@16 126 return *this;
Chris@16 127 }
Chris@16 128 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
Chris@16 129
Chris@16 130 /// Get a reference to the lowest layer.
Chris@16 131 /**
Chris@16 132 * This function returns a reference to the lowest layer in a stack of
Chris@16 133 * layers. Since a basic_descriptor cannot contain any further layers, it
Chris@16 134 * simply returns a reference to itself.
Chris@16 135 *
Chris@16 136 * @return A reference to the lowest layer in the stack of layers. Ownership
Chris@16 137 * is not transferred to the caller.
Chris@16 138 */
Chris@16 139 lowest_layer_type& lowest_layer()
Chris@16 140 {
Chris@16 141 return *this;
Chris@16 142 }
Chris@16 143
Chris@16 144 /// Get a const reference to the lowest layer.
Chris@16 145 /**
Chris@16 146 * This function returns a const reference to the lowest layer in a stack of
Chris@16 147 * layers. Since a basic_descriptor cannot contain any further layers, it
Chris@16 148 * simply returns a reference to itself.
Chris@16 149 *
Chris@16 150 * @return A const reference to the lowest layer in the stack of layers.
Chris@16 151 * Ownership is not transferred to the caller.
Chris@16 152 */
Chris@16 153 const lowest_layer_type& lowest_layer() const
Chris@16 154 {
Chris@16 155 return *this;
Chris@16 156 }
Chris@16 157
Chris@16 158 /// Assign an existing native descriptor to the descriptor.
Chris@16 159 /*
Chris@16 160 * This function opens the descriptor to hold an existing native descriptor.
Chris@16 161 *
Chris@16 162 * @param native_descriptor A native descriptor.
Chris@16 163 *
Chris@16 164 * @throws boost::system::system_error Thrown on failure.
Chris@16 165 */
Chris@16 166 void assign(const native_handle_type& native_descriptor)
Chris@16 167 {
Chris@16 168 boost::system::error_code ec;
Chris@16 169 this->get_service().assign(this->get_implementation(),
Chris@16 170 native_descriptor, ec);
Chris@16 171 boost::asio::detail::throw_error(ec, "assign");
Chris@16 172 }
Chris@16 173
Chris@16 174 /// Assign an existing native descriptor to the descriptor.
Chris@16 175 /*
Chris@16 176 * This function opens the descriptor to hold an existing native descriptor.
Chris@16 177 *
Chris@16 178 * @param native_descriptor A native descriptor.
Chris@16 179 *
Chris@16 180 * @param ec Set to indicate what error occurred, if any.
Chris@16 181 */
Chris@16 182 boost::system::error_code assign(const native_handle_type& native_descriptor,
Chris@16 183 boost::system::error_code& ec)
Chris@16 184 {
Chris@16 185 return this->get_service().assign(
Chris@16 186 this->get_implementation(), native_descriptor, ec);
Chris@16 187 }
Chris@16 188
Chris@16 189 /// Determine whether the descriptor is open.
Chris@16 190 bool is_open() const
Chris@16 191 {
Chris@16 192 return this->get_service().is_open(this->implementation);
Chris@16 193 }
Chris@16 194
Chris@16 195 /// Close the descriptor.
Chris@16 196 /**
Chris@16 197 * This function is used to close the descriptor. Any asynchronous read or
Chris@16 198 * write operations will be cancelled immediately, and will complete with the
Chris@16 199 * boost::asio::error::operation_aborted error.
Chris@16 200 *
Chris@16 201 * @throws boost::system::system_error Thrown on failure. Note that, even if
Chris@16 202 * the function indicates an error, the underlying descriptor is closed.
Chris@16 203 */
Chris@16 204 void close()
Chris@16 205 {
Chris@16 206 boost::system::error_code ec;
Chris@16 207 this->get_service().close(this->get_implementation(), ec);
Chris@16 208 boost::asio::detail::throw_error(ec, "close");
Chris@16 209 }
Chris@16 210
Chris@16 211 /// Close the descriptor.
Chris@16 212 /**
Chris@16 213 * This function is used to close the descriptor. Any asynchronous read or
Chris@16 214 * write operations will be cancelled immediately, and will complete with the
Chris@16 215 * boost::asio::error::operation_aborted error.
Chris@16 216 *
Chris@16 217 * @param ec Set to indicate what error occurred, if any. Note that, even if
Chris@16 218 * the function indicates an error, the underlying descriptor is closed.
Chris@16 219 */
Chris@16 220 boost::system::error_code close(boost::system::error_code& ec)
Chris@16 221 {
Chris@16 222 return this->get_service().close(this->get_implementation(), ec);
Chris@16 223 }
Chris@16 224
Chris@16 225 /// (Deprecated: Use native_handle().) Get the native descriptor
Chris@16 226 /// representation.
Chris@16 227 /**
Chris@16 228 * This function may be used to obtain the underlying representation of the
Chris@16 229 * descriptor. This is intended to allow access to native descriptor
Chris@16 230 * functionality that is not otherwise provided.
Chris@16 231 */
Chris@16 232 native_type native()
Chris@16 233 {
Chris@16 234 return this->get_service().native_handle(this->implementation);
Chris@16 235 }
Chris@16 236
Chris@16 237 /// Get the native descriptor representation.
Chris@16 238 /**
Chris@16 239 * This function may be used to obtain the underlying representation of the
Chris@16 240 * descriptor. This is intended to allow access to native descriptor
Chris@16 241 * functionality that is not otherwise provided.
Chris@16 242 */
Chris@16 243 native_handle_type native_handle()
Chris@16 244 {
Chris@16 245 return this->get_service().native_handle(this->implementation);
Chris@16 246 }
Chris@16 247
Chris@16 248 /// Release ownership of the native descriptor implementation.
Chris@16 249 /**
Chris@16 250 * This function may be used to obtain the underlying representation of the
Chris@16 251 * descriptor. After calling this function, @c is_open() returns false. The
Chris@16 252 * caller is responsible for closing the descriptor.
Chris@16 253 *
Chris@16 254 * All outstanding asynchronous read or write operations will finish
Chris@16 255 * immediately, and the handlers for cancelled operations will be passed the
Chris@16 256 * boost::asio::error::operation_aborted error.
Chris@16 257 */
Chris@16 258 native_handle_type release()
Chris@16 259 {
Chris@16 260 return this->get_service().release(this->implementation);
Chris@16 261 }
Chris@16 262
Chris@16 263 /// Cancel all asynchronous operations associated with the descriptor.
Chris@16 264 /**
Chris@16 265 * This function causes all outstanding asynchronous read or write operations
Chris@16 266 * to finish immediately, and the handlers for cancelled operations will be
Chris@16 267 * passed the boost::asio::error::operation_aborted error.
Chris@16 268 *
Chris@16 269 * @throws boost::system::system_error Thrown on failure.
Chris@16 270 */
Chris@16 271 void cancel()
Chris@16 272 {
Chris@16 273 boost::system::error_code ec;
Chris@16 274 this->get_service().cancel(this->get_implementation(), ec);
Chris@16 275 boost::asio::detail::throw_error(ec, "cancel");
Chris@16 276 }
Chris@16 277
Chris@16 278 /// Cancel all asynchronous operations associated with the descriptor.
Chris@16 279 /**
Chris@16 280 * This function causes all outstanding asynchronous read or write operations
Chris@16 281 * to finish immediately, and the handlers for cancelled operations will be
Chris@16 282 * passed the boost::asio::error::operation_aborted error.
Chris@16 283 *
Chris@16 284 * @param ec Set to indicate what error occurred, if any.
Chris@16 285 */
Chris@16 286 boost::system::error_code cancel(boost::system::error_code& ec)
Chris@16 287 {
Chris@16 288 return this->get_service().cancel(this->get_implementation(), ec);
Chris@16 289 }
Chris@16 290
Chris@16 291 /// Perform an IO control command on the descriptor.
Chris@16 292 /**
Chris@16 293 * This function is used to execute an IO control command on the descriptor.
Chris@16 294 *
Chris@16 295 * @param command The IO control command to be performed on the descriptor.
Chris@16 296 *
Chris@16 297 * @throws boost::system::system_error Thrown on failure.
Chris@16 298 *
Chris@16 299 * @sa IoControlCommand @n
Chris@16 300 * boost::asio::posix::descriptor_base::bytes_readable @n
Chris@16 301 * boost::asio::posix::descriptor_base::non_blocking_io
Chris@16 302 *
Chris@16 303 * @par Example
Chris@16 304 * Getting the number of bytes ready to read:
Chris@16 305 * @code
Chris@16 306 * boost::asio::posix::stream_descriptor descriptor(io_service);
Chris@16 307 * ...
Chris@16 308 * boost::asio::posix::stream_descriptor::bytes_readable command;
Chris@16 309 * descriptor.io_control(command);
Chris@16 310 * std::size_t bytes_readable = command.get();
Chris@16 311 * @endcode
Chris@16 312 */
Chris@16 313 template <typename IoControlCommand>
Chris@16 314 void io_control(IoControlCommand& command)
Chris@16 315 {
Chris@16 316 boost::system::error_code ec;
Chris@16 317 this->get_service().io_control(this->get_implementation(), command, ec);
Chris@16 318 boost::asio::detail::throw_error(ec, "io_control");
Chris@16 319 }
Chris@16 320
Chris@16 321 /// Perform an IO control command on the descriptor.
Chris@16 322 /**
Chris@16 323 * This function is used to execute an IO control command on the descriptor.
Chris@16 324 *
Chris@16 325 * @param command The IO control command to be performed on the descriptor.
Chris@16 326 *
Chris@16 327 * @param ec Set to indicate what error occurred, if any.
Chris@16 328 *
Chris@16 329 * @sa IoControlCommand @n
Chris@16 330 * boost::asio::posix::descriptor_base::bytes_readable @n
Chris@16 331 * boost::asio::posix::descriptor_base::non_blocking_io
Chris@16 332 *
Chris@16 333 * @par Example
Chris@16 334 * Getting the number of bytes ready to read:
Chris@16 335 * @code
Chris@16 336 * boost::asio::posix::stream_descriptor descriptor(io_service);
Chris@16 337 * ...
Chris@16 338 * boost::asio::posix::stream_descriptor::bytes_readable command;
Chris@16 339 * boost::system::error_code ec;
Chris@16 340 * descriptor.io_control(command, ec);
Chris@16 341 * if (ec)
Chris@16 342 * {
Chris@16 343 * // An error occurred.
Chris@16 344 * }
Chris@16 345 * std::size_t bytes_readable = command.get();
Chris@16 346 * @endcode
Chris@16 347 */
Chris@16 348 template <typename IoControlCommand>
Chris@16 349 boost::system::error_code io_control(IoControlCommand& command,
Chris@16 350 boost::system::error_code& ec)
Chris@16 351 {
Chris@16 352 return this->get_service().io_control(
Chris@16 353 this->get_implementation(), command, ec);
Chris@16 354 }
Chris@16 355
Chris@16 356 /// Gets the non-blocking mode of the descriptor.
Chris@16 357 /**
Chris@16 358 * @returns @c true if the descriptor's synchronous operations will fail with
Chris@16 359 * boost::asio::error::would_block if they are unable to perform the requested
Chris@16 360 * operation immediately. If @c false, synchronous operations will block
Chris@16 361 * until complete.
Chris@16 362 *
Chris@16 363 * @note The non-blocking mode has no effect on the behaviour of asynchronous
Chris@16 364 * operations. Asynchronous operations will never fail with the error
Chris@16 365 * boost::asio::error::would_block.
Chris@16 366 */
Chris@16 367 bool non_blocking() const
Chris@16 368 {
Chris@16 369 return this->get_service().non_blocking(this->implementation);
Chris@16 370 }
Chris@16 371
Chris@16 372 /// Sets the non-blocking mode of the descriptor.
Chris@16 373 /**
Chris@16 374 * @param mode If @c true, the descriptor's synchronous operations will fail
Chris@16 375 * with boost::asio::error::would_block if they are unable to perform the
Chris@16 376 * requested operation immediately. If @c false, synchronous operations will
Chris@16 377 * block until complete.
Chris@16 378 *
Chris@16 379 * @throws boost::system::system_error Thrown on failure.
Chris@16 380 *
Chris@16 381 * @note The non-blocking mode has no effect on the behaviour of asynchronous
Chris@16 382 * operations. Asynchronous operations will never fail with the error
Chris@16 383 * boost::asio::error::would_block.
Chris@16 384 */
Chris@16 385 void non_blocking(bool mode)
Chris@16 386 {
Chris@16 387 boost::system::error_code ec;
Chris@16 388 this->get_service().non_blocking(this->get_implementation(), mode, ec);
Chris@16 389 boost::asio::detail::throw_error(ec, "non_blocking");
Chris@16 390 }
Chris@16 391
Chris@16 392 /// Sets the non-blocking mode of the descriptor.
Chris@16 393 /**
Chris@16 394 * @param mode If @c true, the descriptor's synchronous operations will fail
Chris@16 395 * with boost::asio::error::would_block if they are unable to perform the
Chris@16 396 * requested operation immediately. If @c false, synchronous operations will
Chris@16 397 * block until complete.
Chris@16 398 *
Chris@16 399 * @param ec Set to indicate what error occurred, if any.
Chris@16 400 *
Chris@16 401 * @note The non-blocking mode has no effect on the behaviour of asynchronous
Chris@16 402 * operations. Asynchronous operations will never fail with the error
Chris@16 403 * boost::asio::error::would_block.
Chris@16 404 */
Chris@16 405 boost::system::error_code non_blocking(
Chris@16 406 bool mode, boost::system::error_code& ec)
Chris@16 407 {
Chris@16 408 return this->get_service().non_blocking(
Chris@16 409 this->get_implementation(), mode, ec);
Chris@16 410 }
Chris@16 411
Chris@16 412 /// Gets the non-blocking mode of the native descriptor implementation.
Chris@16 413 /**
Chris@16 414 * This function is used to retrieve the non-blocking mode of the underlying
Chris@16 415 * native descriptor. This mode has no effect on the behaviour of the
Chris@16 416 * descriptor object's synchronous operations.
Chris@16 417 *
Chris@16 418 * @returns @c true if the underlying descriptor is in non-blocking mode and
Chris@16 419 * direct system calls may fail with boost::asio::error::would_block (or the
Chris@16 420 * equivalent system error).
Chris@16 421 *
Chris@16 422 * @note The current non-blocking mode is cached by the descriptor object.
Chris@16 423 * Consequently, the return value may be incorrect if the non-blocking mode
Chris@16 424 * was set directly on the native descriptor.
Chris@16 425 */
Chris@16 426 bool native_non_blocking() const
Chris@16 427 {
Chris@16 428 return this->get_service().native_non_blocking(this->implementation);
Chris@16 429 }
Chris@16 430
Chris@16 431 /// Sets the non-blocking mode of the native descriptor implementation.
Chris@16 432 /**
Chris@16 433 * This function is used to modify the non-blocking mode of the underlying
Chris@16 434 * native descriptor. It has no effect on the behaviour of the descriptor
Chris@16 435 * object's synchronous operations.
Chris@16 436 *
Chris@16 437 * @param mode If @c true, the underlying descriptor is put into non-blocking
Chris@16 438 * mode and direct system calls may fail with boost::asio::error::would_block
Chris@16 439 * (or the equivalent system error).
Chris@16 440 *
Chris@16 441 * @throws boost::system::system_error Thrown on failure. If the @c mode is
Chris@16 442 * @c false, but the current value of @c non_blocking() is @c true, this
Chris@16 443 * function fails with boost::asio::error::invalid_argument, as the
Chris@16 444 * combination does not make sense.
Chris@16 445 */
Chris@16 446 void native_non_blocking(bool mode)
Chris@16 447 {
Chris@16 448 boost::system::error_code ec;
Chris@16 449 this->get_service().native_non_blocking(
Chris@16 450 this->get_implementation(), mode, ec);
Chris@16 451 boost::asio::detail::throw_error(ec, "native_non_blocking");
Chris@16 452 }
Chris@16 453
Chris@16 454 /// Sets the non-blocking mode of the native descriptor implementation.
Chris@16 455 /**
Chris@16 456 * This function is used to modify the non-blocking mode of the underlying
Chris@16 457 * native descriptor. It has no effect on the behaviour of the descriptor
Chris@16 458 * object's synchronous operations.
Chris@16 459 *
Chris@16 460 * @param mode If @c true, the underlying descriptor is put into non-blocking
Chris@16 461 * mode and direct system calls may fail with boost::asio::error::would_block
Chris@16 462 * (or the equivalent system error).
Chris@16 463 *
Chris@16 464 * @param ec Set to indicate what error occurred, if any. If the @c mode is
Chris@16 465 * @c false, but the current value of @c non_blocking() is @c true, this
Chris@16 466 * function fails with boost::asio::error::invalid_argument, as the
Chris@16 467 * combination does not make sense.
Chris@16 468 */
Chris@16 469 boost::system::error_code native_non_blocking(
Chris@16 470 bool mode, boost::system::error_code& ec)
Chris@16 471 {
Chris@16 472 return this->get_service().native_non_blocking(
Chris@16 473 this->get_implementation(), mode, ec);
Chris@16 474 }
Chris@16 475
Chris@16 476 protected:
Chris@16 477 /// Protected destructor to prevent deletion through this type.
Chris@16 478 ~basic_descriptor()
Chris@16 479 {
Chris@16 480 }
Chris@16 481 };
Chris@16 482
Chris@16 483 } // namespace posix
Chris@16 484 } // namespace asio
Chris@16 485 } // namespace boost
Chris@16 486
Chris@16 487 #include <boost/asio/detail/pop_options.hpp>
Chris@16 488
Chris@16 489 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
Chris@16 490 // || defined(GENERATING_DOCUMENTATION)
Chris@16 491
Chris@16 492 #endif // BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP