ScopedPtr.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // "$Id$"
  3. //
  4. // Copyright (c)1992-2011, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. /// \file Infra/ScopedPtr.h
  11. /// 从 boost.scoped_ptr 移植过来的作用域内智能指针
  12. ///
  13. #ifndef INFRA_SCOPED_PTR_H__
  14. #define INFRA_SCOPED_PTR_H__
  15. #include <cassert>
  16. #include <memory>
  17. #include "Detail/checked_delete.hpp"
  18. namespace Dahua {
  19. namespace Memory {
  20. /// \brief scoped smart pointer.
  21. ///
  22. /// \ingroup smart pointers
  23. ///
  24. /// scoped_ptr mimics a built-in pointer except that it guarantees deletion
  25. /// of the object pointed to, either on destruction of the scoped_ptr or via
  26. /// an explicit reset(). scoped_ptr is a simple solution for simple needs;
  27. /// use shared_ptr or std::auto_ptr if your needs are more complex.
  28. ///
  29. template<class T> class TScopedPtr // noncopyable
  30. {
  31. T* m_ptr;
  32. TScopedPtr(TScopedPtr const&);
  33. TScopedPtr & operator=(TScopedPtr const&);
  34. public:
  35. typedef TScopedPtr<T> this_type;
  36. typedef T element_type;
  37. explicit TScopedPtr(T* p = 0): m_ptr(p) // never throws
  38. {
  39. }
  40. explicit TScopedPtr(std::auto_ptr<T> p): m_ptr(p.release()) // never throws
  41. {
  42. }
  43. ~TScopedPtr() // never throws
  44. {
  45. Detail::checked_delete(m_ptr);
  46. }
  47. void reset(T* p = 0) // never throws
  48. {
  49. assert(p == 0 || p != m_ptr); // catch self-reset errors
  50. this_type(p).swap(*this);
  51. }
  52. T& operator*() const // never throws
  53. {
  54. assert(m_ptr != 0);
  55. return *m_ptr;
  56. }
  57. T* operator->() const // never throws
  58. {
  59. assert(m_ptr != 0);
  60. return m_ptr;
  61. }
  62. T* get() const // never throws
  63. {
  64. return m_ptr;
  65. }
  66. // implicit conversion to "bool"
  67. typedef T* this_type::*unspecified_bool_type;
  68. operator unspecified_bool_type() const // never throws
  69. {
  70. return m_ptr == 0 ? 0 : &this_type::m_ptr;
  71. }
  72. bool operator!() const // never throws
  73. {
  74. return m_ptr == 0;
  75. }
  76. void swap(TScopedPtr& b) // never throws
  77. {
  78. T* tmp = b.m_ptr;
  79. b.m_ptr = m_ptr;
  80. m_ptr = tmp;
  81. }
  82. };
  83. } // namespace Memory
  84. } // namespace Dahua
  85. template<class T> inline void swap(Dahua::Memory::TScopedPtr<T>& a, Dahua::Memory::TScopedPtr<T>& b) // never throws
  86. {
  87. a.swap(b);
  88. }
  89. // get_pointer(p) is a generic way to say p.get()
  90. template<class T> inline T * get_pointer(Dahua::Memory::TScopedPtr<T> const & p)
  91. {
  92. return p.get();
  93. }
  94. #endif // INFRA_SCOPED_PTR_H__