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.

136 lines
4.0 KiB

  1. //+----------------------------------------------------------------------------
  2. // File: smart.hxx
  3. //
  4. // Synopsis:
  5. //
  6. //-----------------------------------------------------------------------------
  7. #ifndef _SMART_HXX
  8. #define _SMART_HXX
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // TSRef: Smart COM reference template
  11. //
  12. template <class TYPE> class TSRef // Hungarian: srp
  13. {
  14. public:
  15. TSRef() { _punk = NULL; }
  16. TSRef(TYPE* punk) { _punk = punk; }
  17. ~TSRef() { ::SRelease(_punk); }
  18. operator TYPE*() { Assert(_punk); return _punk; }
  19. TYPE* operator->() { Assert(_punk); return _punk; }
  20. TYPE** operator&() { Assert(!_punk); return &_punk; }
  21. TYPE* operator=(TYPE* punk) { Assert(!_punk); _punk = punk; return _punk; }
  22. TYPE* Disown() { _punk = NULL; }
  23. TYPE** InOut() { return &_punk; }
  24. void SClear() { ::SClear(_punk); }
  25. protected:
  26. TYPE* _punk;
  27. private:
  28. TSRef(const TSRef<TYPE>& srp);
  29. TSRef<TYPE>& operator=(const TSRef<TYPE>& srp);
  30. };
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // TCRef: Smart COM object pointer template
  33. //
  34. template <class TYPE> class TCRef // Hungarian: crp
  35. {
  36. public:
  37. TCRef() { _punk = NULL; }
  38. TCRef(TYPE* punk) { _punk = punk; }
  39. ~TCRef() { ::SRelease((IUnknown *)_punk); }
  40. operator TYPE*() { Assert(_punk); return _punk; }
  41. TYPE* operator->() { Assert(_punk); return _punk; }
  42. TYPE** operator&() { Assert(!_punk); return &_punk; }
  43. TYPE* operator=(TYPE* punk) { Assert(!_punk); _punk = punk; return _punk; }
  44. TYPE* Disown() { _punk = NULL; }
  45. TYPE** InOut() { return &_punk; }
  46. void SClear() { ::SClear((IUnknown *)_punk); }
  47. protected:
  48. TYPE* _punk;
  49. private:
  50. TCRef(const TCRef<TYPE>& srp);
  51. TCRef<TYPE>& operator=(const TCRef<TYPE>& srp);
  52. };
  53. ///////////////////////////////////////////////////////////////////////////////
  54. // TSPtr: Smart pointer template
  55. //
  56. template <class TYPE> class TSPtr // Hungarian: sp
  57. {
  58. public:
  59. TSPtr() { _pv = NULL; }
  60. TSPtr(TYPE* pv) { _pv = pv; }
  61. ~TSPtr() { if (_pv) delete pv; }
  62. operator TYPE*() { Assert(_pv); return _pv; }
  63. TYPE* operator->() { Assert(_pv); return _pv; }
  64. TYPE& operator*() { Assert(_pv); return *_pv; }
  65. TYPE** operator&() { Assert(!_pv); return &_pv; }
  66. TYPE* operator=(TYPE* pv) { Assert(!_pv); _pv = pv; return _pv; }
  67. TYPE* Disown() { _pv = NULL; }
  68. TYPE** InOut() { return &_pv; }
  69. void SClear() { delete _pv; _pv = NULL; }
  70. protected:
  71. TYPE* _pv;
  72. private:
  73. TSPtr(const TSPtr<TYPE>& srp);
  74. TSPtr<TYPE>& operator=(const TSPtr<TYPE>& srp);
  75. };
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // TSArray: Smart array template
  78. //
  79. template <class TYPE> class TSArray // Hungarian: ssz or sa
  80. {
  81. public:
  82. TSArray() { _pv = NULL; }
  83. TSArray(TYPE* pv) { _pv = pv; }
  84. ~TSArray() { delete [] _pv; }
  85. operator TYPE*() { Assert(_pv); return _pv; }
  86. TYPE* operator->() { Assert(_pv); return _pv; }
  87. TYPE& operator*() { Assert(_pv); return *_pv; }
  88. TYPE** operator&() { Assert(!_pv); return &_pv; }
  89. TYPE* operator=(TYPE* pv) { Assert(!_pv); _pv = pv; return _pv; }
  90. TYPE& operator[](int iItem) { Assert(_pv); return *(_pv + iItem); }
  91. TYPE* operator+(const int cItem) { Assert(_pv); return _pv + cItem; }
  92. TYPE* operator-(const int cItem) { Assert(_pv); return _pv - cItem; }
  93. int operator-(const TSArray<TYPE>& sa) { Assert(_pv); Assert(sz._pv); return _pv - sa._pv; }
  94. int operator-(const TYPE* pv) { Assert(_pv); Assert(pv); return _pv - pv; }
  95. TYPE* Disown() { _pv = NULL; }
  96. TYPE** InOut() { return &_pv; }
  97. void SClear() { delete [] _pv; _pv = NULL; }
  98. protected:
  99. TYPE* _pv;
  100. private:
  101. TSArray(const TSArray<TYPE>& sa);
  102. TSArray<TYPE>& operator=(const TSArray<TYPE>& sa);
  103. };
  104. #endif // _SMART_HXX