ref.h
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 1k
Category:

Windows Develop

Development Platform:

Visual C++

  1. #pragma once
  2. #if !defined(_ref_h)
  3. #define _ref_h 1
  4. #define refNil 0
  5. template <class TARG>
  6. class RefPtr {
  7.   private:
  8. TARG * _ptarg;
  9. // unuse pointer and free if last user
  10. void Release() {
  11. if (_ptarg != NULL && _ptarg->FUnUse()) {
  12. delete _ptarg;
  13. }
  14. // put garbage in pointer
  15. Debug(_ptarg = (TARG *) 0xdadadada);
  16. }
  17.   public:
  18. // constructors
  19. RefPtr<TARG>() {
  20. _ptarg = NULL;
  21. }
  22. RefPtr<TARG>(TARG * ptargNew) {
  23. _ptarg = ptargNew;
  24. if (ptargNew != NULL) {
  25. ptargNew->Use();
  26. }
  27. }
  28. RefPtr<TARG>(const RefPtr<TARG> & rtarg) {
  29. _ptarg = rtarg._ptarg;
  30. if (rtarg._ptarg != NULL) {
  31. rtarg._ptarg->Use();
  32. }
  33. }
  34. // destructor
  35.    ~RefPtr<TARG>() {
  36. Release();
  37. }
  38. // assignment functions (mirror the ctors)
  39. RefPtr<TARG>& operator=(TARG *ptargNew) {
  40. if (ptargNew != NULL) {
  41. ptargNew->Use();
  42. }
  43. Release();
  44. _ptarg = ptargNew;
  45. return *this;
  46. }
  47. RefPtr<TARG>& operator=(const RefPtr<TARG> &rtarg) {
  48. if (rtarg._ptarg != NULL) {
  49. rtarg._ptarg->Use();
  50. }
  51. Release();
  52. _ptarg = rtarg._ptarg;
  53. return *this;
  54. }
  55. bool  operator==(TARG *ptarg) const { return  _ptarg == ptarg; }
  56. bool  operator!=(TARG *ptarg) const { return  _ptarg != ptarg; }
  57. TARG &operator*()   const { return *_ptarg;    }
  58. TARG *operator->()   const { return  _ptarg;    }
  59.   operator TARG *()    const { return  _ptarg;    }
  60. };
  61. #endif