scoped_ptr.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // detail/scoped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 BOOST_ASIO_DETAIL_SCOPED_PTR_HPP
  11. #define BOOST_ASIO_DETAIL_SCOPED_PTR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. namespace boost {
  18. namespace asio {
  19. namespace detail {
  20. template <typename T>
  21. class scoped_ptr
  22. {
  23. public:
  24. // Constructor.
  25. explicit scoped_ptr(T* p = 0)
  26. : p_(p)
  27. {
  28. }
  29. // Destructor.
  30. ~scoped_ptr()
  31. {
  32. delete p_;
  33. }
  34. // Access.
  35. T* get()
  36. {
  37. return p_;
  38. }
  39. // Access.
  40. T* operator->()
  41. {
  42. return p_;
  43. }
  44. // Dereference.
  45. T& operator*()
  46. {
  47. return *p_;
  48. }
  49. // Reset pointer.
  50. void reset(T* p = 0)
  51. {
  52. delete p_;
  53. p_ = p;
  54. }
  55. // Release ownership of the pointer.
  56. T* release()
  57. {
  58. T* tmp = p_;
  59. p_ = 0;
  60. return tmp;
  61. }
  62. private:
  63. // Disallow copying and assignment.
  64. scoped_ptr(const scoped_ptr&);
  65. scoped_ptr& operator=(const scoped_ptr&);
  66. T* p_;
  67. };
  68. } // namespace detail
  69. } // namespace asio
  70. } // namespace boost
  71. #include <boost/asio/detail/pop_options.hpp>
  72. #endif // BOOST_ASIO_DETAIL_SCOPED_PTR_HPP