Source code of Windows XP (NT5)
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.

112 lines
2.6 KiB

  1. #ifndef __SMART_PTR_H__
  2. #define __SMART_PTR_H__
  3. #include <xmemory>
  4. namespace wmilib
  5. {
  6. // TEMPLATE CLASS auto_ptr
  7. template<class _Ty>
  8. class auto_ptr {
  9. // TEMPLATE CLASS auto_ptr_ref
  10. template<class _U>
  11. struct auto_ptr_ref {
  12. auto_ptr_ref(auto_ptr<_U>& _Y)
  13. : _Ref(_Y) {}
  14. auto_ptr<_U>& _Ref;
  15. };
  16. public:
  17. typedef _Ty element_type;
  18. explicit auto_ptr(_Ty *_P = 0) _THROW0()
  19. : _Ptr(_P) {}
  20. auto_ptr(auto_ptr<_Ty>& _Y) _THROW0()
  21. : _Ptr(_Y.release()) {}
  22. auto_ptr(auto_ptr_ref<_Ty> _Y) _THROW0()
  23. : _Ptr(_Y._Ref.release()) {}
  24. template<class _U>
  25. operator auto_ptr<_U>() _THROW0()
  26. {return (auto_ptr<_U>(*this)); }
  27. template<class _U>
  28. operator auto_ptr_ref<_U>() _THROW0()
  29. {return (auto_ptr_ref<_U>(*this)); }
  30. auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Y) _THROW0()
  31. {reset(_Y.release());
  32. return (*this); }
  33. ~auto_ptr()
  34. {delete _Ptr; }
  35. _Ty& operator*() const _THROW0()
  36. {return (*get()); }
  37. _Ty *operator->() const _THROW0()
  38. {return (get()); }
  39. _Ty *get() const _THROW0()
  40. {return (_Ptr); }
  41. _Ty *release() _THROW0()
  42. {_Ty *_Tmp = _Ptr;
  43. _Ptr = 0;
  44. return (_Tmp); }
  45. void reset(_Ty* _P = 0)
  46. {if (_P != _Ptr)
  47. delete _Ptr;
  48. _Ptr = _P; }
  49. private:
  50. _Ty *_Ptr;
  51. };
  52. template<class _Ty>
  53. class auto_buffer {
  54. // TEMPLATE CLASS auto_ptr_ref
  55. template<class _U>
  56. struct auto_buffer_ref {
  57. auto_buffer_ref(auto_buffer<_U>& _Y)
  58. : _Ref(_Y) {}
  59. auto_buffer<_U>& _Ref;
  60. };
  61. public:
  62. typedef _Ty element_type;
  63. explicit auto_buffer(_Ty *_P = 0, size_t val = -1) _THROW0()
  64. : _size(val),_Ptr(_P) {}
  65. auto_buffer(auto_buffer<_Ty>& _Y) _THROW0()
  66. : _size(_Y.size()), _Ptr(_Y.release()) {}
  67. auto_buffer(auto_buffer_ref<_Ty> _Y) _THROW0()
  68. : _size(_Y._Ref.size()),_Ptr(_Y._Ref.release()) {}
  69. template<class _U>
  70. operator auto_buffer<_U>() _THROW0()
  71. {return (auto_buffer<_U>(*this)); }
  72. template<class _U>
  73. operator auto_buffer_ref<_U>() _THROW0()
  74. {return (auto_buffer_ref<_U>(*this)); }
  75. auto_buffer<_Ty>& operator=(auto_buffer<_Ty>& _Y) _THROW0()
  76. { size_t tmp = _Y.size();
  77. reset(_Y.release());
  78. size(tmp);
  79. return (*this); }
  80. ~auto_buffer()
  81. {delete[] _Ptr; }
  82. _Ty& operator*() const _THROW0()
  83. {return (*get()); }
  84. _Ty& operator[](size_t index) const _THROW0()
  85. {return (*(get()+index));}
  86. _Ty *operator->() const _THROW0()
  87. {return (get()); }
  88. _Ty *get() const _THROW0()
  89. {return (_Ptr); }
  90. _Ty *release() _THROW0()
  91. {_Ty *_Tmp = _Ptr;
  92. _Ptr = 0;
  93. _size = -1;
  94. return (_Tmp); }
  95. void reset(_Ty* _P = 0)
  96. {if (_P != _Ptr){
  97. delete[] _Ptr;
  98. _size = -1;
  99. }
  100. _Ptr = _P; }
  101. void size(size_t val) _THROW0()
  102. { _size = val;}
  103. size_t size(void) _THROW0()
  104. { return _size;}
  105. private:
  106. size_t _size;
  107. _Ty *_Ptr;
  108. };
  109. };
  110. #endif //__SMART_PTR_H__