Leaked source code of windows server 2003
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.

59 lines
1.3 KiB

  1. // auto_tm.h
  2. //
  3. // Same as auto_ptr but for memory freed with CoTaskFreeMem
  4. #pragma once
  5. #include <xstddef>
  6. template<class _Ty>
  7. class auto_tm
  8. {
  9. public:
  10. typedef _Ty element_type;
  11. explicit auto_tm(_Ty *_P = 0) _THROW0()
  12. : _Owns(_P != 0), _Ptr(_P) {}
  13. auto_tm(const auto_tm<_Ty>& _Y) _THROW0()
  14. : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
  15. auto_tm<_Ty>& operator=(const auto_tm<_Ty>& _Y) _THROW0()
  16. {if (_Ptr != _Y.get())
  17. {if (_Owns)
  18. CoTaskMemFree(_Ptr);
  19. _Owns = _Y._Owns;
  20. _Ptr = _Y.release(); }
  21. else if (_Y._Owns)
  22. _Owns = true;
  23. return (*this); }
  24. auto_tm<_Ty>& operator=(_Ty* _Y) _THROW0()
  25. { {if (_Owns)
  26. CoTaskMemFree(_Ptr);
  27. _Owns = _Y != 0;
  28. _Ptr = _Y; }
  29. return (*this); }
  30. ~auto_tm()
  31. {if (_Owns)
  32. CoTaskMemFree(_Ptr);}
  33. _Ty** operator&() _THROW0()
  34. { return &_Ptr; }
  35. operator _Ty* () const
  36. { return _Ptr; }
  37. _Ty& operator*() const _THROW0()
  38. {return (*get()); }
  39. _Ty *operator->() const _THROW0()
  40. {return (get()); }
  41. _Ty& operator[] (int ndx) const _THROW0()
  42. {return *(get() + ndx); }
  43. _Ty *get() const _THROW0()
  44. {return (_Ptr); }
  45. _Ty *release() const _THROW0()
  46. {((auto_tm<_Ty> *)this)->_Owns = false;
  47. return (_Ptr); }
  48. bool Ownership(bool fOwns)
  49. { return _Owns = fOwns; }
  50. protected:
  51. bool _Owns;
  52. _Ty *_Ptr;
  53. };