Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

socket_holder.hpp 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // detail/socket_holder.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #ifndef ASIO_DETAIL_SOCKET_HOLDER_HPP
  11. #define ASIO_DETAIL_SOCKET_HOLDER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/detail/noncopyable.hpp"
  17. #include "asio/detail/socket_ops.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace detail {
  21. // Implement the resource acquisition is initialisation idiom for sockets.
  22. class socket_holder
  23. : private noncopyable
  24. {
  25. public:
  26. // Construct as an uninitialised socket.
  27. socket_holder()
  28. : socket_(invalid_socket)
  29. {
  30. }
  31. // Construct to take ownership of the specified socket.
  32. explicit socket_holder(socket_type s)
  33. : socket_(s)
  34. {
  35. }
  36. // Destructor.
  37. ~socket_holder()
  38. {
  39. if (socket_ != invalid_socket)
  40. {
  41. asio::error_code ec;
  42. socket_ops::state_type state = 0;
  43. socket_ops::close(socket_, state, true, ec);
  44. }
  45. }
  46. // Get the underlying socket.
  47. socket_type get() const
  48. {
  49. return socket_;
  50. }
  51. // Reset to an uninitialised socket.
  52. void reset()
  53. {
  54. if (socket_ != invalid_socket)
  55. {
  56. asio::error_code ec;
  57. socket_ops::state_type state = 0;
  58. socket_ops::close(socket_, state, true, ec);
  59. socket_ = invalid_socket;
  60. }
  61. }
  62. // Reset to take ownership of the specified socket.
  63. void reset(socket_type s)
  64. {
  65. reset();
  66. socket_ = s;
  67. }
  68. // Release ownership of the socket.
  69. socket_type release()
  70. {
  71. socket_type tmp = socket_;
  72. socket_ = invalid_socket;
  73. return tmp;
  74. }
  75. private:
  76. // The underlying socket.
  77. socket_type socket_;
  78. };
  79. } // namespace detail
  80. } // namespace asio
  81. #include "asio/detail/pop_options.hpp"
  82. #endif // ASIO_DETAIL_SOCKET_HOLDER_HPP