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.

89 lines
1.3 KiB

  1. namespace provsubsys{
  2. template<class T>
  3. class RefCountingTraits{
  4. public:
  5. void add_ref(T * ptr){ ptr->AddRef();};
  6. void release(T * ptr){ ptr->Release();};
  7. };
  8. template<class T, class ref_traits = RefCountingTraits<T> >
  9. class auto_ref: private ref_traits {
  10. T* pointee_;
  11. public:
  12. auto_ref():pointee_(NULL)
  13. { };
  14. explicit auto_ref(T * _P):pointee_(_P)
  15. {
  16. add_ref();
  17. };
  18. explicit auto_ref(const int):pointee_(NULL)
  19. { } ;
  20. auto_ref(const auto_ref& _S):pointee_(_S.pointee_)
  21. {
  22. add_ref();
  23. };
  24. ~auto_ref()
  25. {
  26. release();
  27. };
  28. bool operator==(const auto_ref& _R)
  29. {
  30. return pointee_ == _R.pointee_;
  31. };
  32. const auto_ref& operator=( const auto_ref& _R)
  33. {
  34. if(pointee_ == _R.pointee_)
  35. return *this;
  36. release();
  37. pointee_ = _R.pointee_;
  38. add_ref();
  39. return *this;
  40. };
  41. T& operator*( ) const
  42. {
  43. return *pointee_;
  44. };
  45. T* operator->( ) const
  46. {
  47. return pointee_;
  48. };
  49. operator bool( ) const throw( )
  50. {
  51. return pointee_ != NULL;
  52. };
  53. void Dettach()
  54. {
  55. release();
  56. pointee_ = NULL;
  57. };
  58. protected:
  59. void add_ref()
  60. {
  61. if( pointee_ != NULL )
  62. ref_traits::add_ref(pointee_);
  63. };
  64. void release()
  65. {
  66. if( pointee_ != NULL )
  67. ref_traits::release(pointee_);
  68. };
  69. };
  70. };