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.

72 lines
975 B

  1. // auto_bstr.h
  2. //
  3. // Same as auto_ptr but for BSTR
  4. #pragma once
  5. #include <xstddef>
  6. class auto_bstr
  7. {
  8. public:
  9. auto_bstr( BSTR b= 0, bool o= true)
  10. : _bstr(b), _Owns(o)
  11. {}
  12. ~auto_bstr()
  13. {
  14. if(_bstr && _Owns)
  15. ::SysFreeString(_bstr);
  16. }
  17. bool Ownership(bool fOwns)
  18. { return _Owns = fOwns; }
  19. operator BSTR() { return _bstr; }
  20. operator const BSTR() const { return _bstr; }
  21. BSTR* operator &() {return &_bstr; }
  22. auto_bstr& operator=(auto_bstr& rhs)
  23. {
  24. if(_bstr == rhs._bstr)
  25. return *this;
  26. clear();
  27. _Owns= rhs._Owns;
  28. _bstr= rhs.release();
  29. return *this;
  30. }
  31. auto_bstr& operator=(BSTR bstr)
  32. {
  33. clear();
  34. _bstr= bstr;
  35. _Owns= true;
  36. return *this;
  37. }
  38. operator bool()
  39. { return NULL != _bstr; }
  40. operator !()
  41. { return NULL == _bstr; }
  42. void clear()
  43. {
  44. if(_bstr && _Owns)
  45. {
  46. ::SysFreeString(_bstr);
  47. }
  48. _bstr= NULL;
  49. }
  50. BSTR release()
  51. {
  52. BSTR bstr= _bstr;
  53. _bstr= NULL;
  54. return bstr;
  55. }
  56. protected:
  57. bool _Owns;
  58. BSTR _bstr;
  59. };