scoped_ptr.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // detail/scoped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_SCOPED_PTR_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. namespace asio {
  18. namespace detail {
  19. template <typename T>
  20. class scoped_ptr
  21. {
  22. public:
  23. // Constructor.
  24. explicit scoped_ptr(T* p = 0)
  25. : p_(p)
  26. {
  27. }
  28. // Destructor.
  29. ~scoped_ptr()
  30. {
  31. delete p_;
  32. }
  33. // Access.
  34. T* get()
  35. {
  36. return p_;
  37. }
  38. // Access.
  39. T* operator->()
  40. {
  41. return p_;
  42. }
  43. // Dereference.
  44. T& operator*()
  45. {
  46. return *p_;
  47. }
  48. // Reset pointer.
  49. void reset(T* p = 0)
  50. {
  51. delete p_;
  52. p_ = p;
  53. }
  54. private:
  55. // Disallow copying and assignment.
  56. scoped_ptr(const scoped_ptr&);
  57. scoped_ptr& operator=(const scoped_ptr&);
  58. T* p_;
  59. };
  60. } // namespace detail
  61. } // namespace asio
  62. #include "asio/detail/pop_options.hpp"
  63. #endif // ASIO_DETAIL_SCOPED_PTR_HPP