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.

150 lines
2.4 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998
  5. //
  6. // File: smartptr.h
  7. //
  8. // Contents: Classes for smart pointers
  9. //
  10. // History: 24-Oct-98 SitaramR Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #pragma once
  14. template<class CItem> class XPtrST
  15. {
  16. public:
  17. XPtrST(CItem* p = 0) : _p( p )
  18. {
  19. }
  20. ~XPtrST() { delete _p; }
  21. BOOL IsNull() const { return ( 0 == _p ); }
  22. void Set ( CItem* p )
  23. {
  24. _p = p;
  25. }
  26. CItem * Acquire()
  27. {
  28. CItem * pTemp = _p;
  29. _p = 0;
  30. return pTemp;
  31. }
  32. CItem & GetReference() const
  33. {
  34. return *_p;
  35. }
  36. CItem * GetPointer() const { return _p ; }
  37. void Free() { delete Acquire(); }
  38. private:
  39. XPtrST (const XPtrST<CItem> & x);
  40. XPtrST<CItem> & operator=( const XPtrST<CItem> & x);
  41. CItem * _p;
  42. };
  43. //*************************************************************
  44. //
  45. // Class: XBStr
  46. //
  47. // Purpose: Smart pointer class for BSTRs
  48. //
  49. //*************************************************************
  50. class XBStr
  51. {
  52. private:
  53. XBStr(const XBStr& x);
  54. XBStr& operator=(const XBStr& x);
  55. BSTR _p;
  56. public:
  57. XBStr(WCHAR* p = 0) : _p(0)
  58. {
  59. if(p)
  60. {
  61. _p = SysAllocString(p);
  62. }
  63. }
  64. ~XBStr()
  65. {
  66. SysFreeString(_p);
  67. }
  68. operator BSTR(){ return _p; }
  69. void operator=(WCHAR* p)
  70. {
  71. SysFreeString(_p);
  72. _p = p ? SysAllocString(p) : NULL;
  73. }
  74. BSTR Acquire()
  75. {
  76. BSTR p = _p;
  77. _p = 0;
  78. return p;
  79. }
  80. };
  81. //*************************************************************
  82. //
  83. // Class: MyXPtrST
  84. //
  85. // Purpose: Smart pointer template to wrap pointers to a single type.
  86. //
  87. //*************************************************************
  88. template<class T> class MyXPtrST
  89. {
  90. private:
  91. MyXPtrST (const MyXPtrST<T>& x);
  92. MyXPtrST<T>& operator=(const MyXPtrST<T>& x);
  93. T* _p;
  94. public:
  95. MyXPtrST(T* p = NULL) : _p(p){}
  96. ~MyXPtrST(){ delete _p; }
  97. T* operator->(){ return _p; }
  98. T** operator&(){ return &_p; }
  99. operator T*(){ return _p; }
  100. void operator=(T* p)
  101. {
  102. if(_p)
  103. {
  104. delete _p;
  105. }
  106. _p = p;
  107. }
  108. T* Acquire()
  109. {
  110. T* p = _p;
  111. _p = 0;
  112. return p;
  113. }
  114. };