TWSmartPointer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef TWINKLE_TSMARTPOINT_H_
  2. #define TWINKLE_TSMARTPOINT_H_
  3. #pragma once
  4. #include <assert.h>
  5. #include <algorithm>
  6. typedef unsigned CounterType;
  7. typedef unsigned count_type;
  8. template<class T>
  9. class TSmartPointer
  10. {
  11. public:
  12. TSmartPointer(void):counter(0)
  13. { }
  14. TSmartPointer(const TSmartPointer&):counter(0)
  15. { }
  16. explicit TSmartPointer(T* rhs = 0)
  17. { }
  18. virtual ~TSmartPointer(void);
  19. void AddRef() const;
  20. void RemoveRef() const;
  21. TSmartPointer& operator=(const TSmartPointer& ) { return *this; }
  22. private:
  23. CounterType counter;
  24. T* pointee;
  25. };
  26. class TWSharedObject
  27. {
  28. public:
  29. void AddRef() const
  30. {
  31. ++use_count;
  32. printf("AddRef: %d\n", use_count);
  33. }
  34. void RemoveRef() const
  35. {
  36. assert(use_count > 0);
  37. bool destroy = --use_count == 0;
  38. printf("RemoveRef: %d\n", use_count);
  39. if (destroy) {
  40. printf("Before destroy\n");
  41. delete this;
  42. }
  43. }
  44. CounterType GetCount() const { return use_count; }
  45. protected:
  46. TWSharedObject()
  47. : access_mutex()
  48. , use_count(0)
  49. {
  50. printf("TWSharedObject::Constructor\n");
  51. }
  52. TWSharedObject(const TWSharedObject&)
  53. : access_mutex()
  54. , use_count(0)
  55. {
  56. printf("TWSharedObject::Constructor1\n");
  57. }
  58. virtual ~TWSharedObject()
  59. {
  60. assert(use_count == 0);
  61. printf("TWSharedObject::Distructor\n");
  62. }
  63. TWSharedObject& operator=(const TWSharedObject&) { return *this; }
  64. public:
  65. typedef unsigned Mutex;
  66. mutable Mutex access_mutex;
  67. private:
  68. mutable CounterType use_count;
  69. };
  70. //Use it match with Type<TWSharedObject>
  71. template<class T>
  72. class TWSharedPointer
  73. {
  74. public:
  75. explicit TWSharedPointer(T* realPtr = 0)
  76. : pointee(realPtr)
  77. {
  78. AddRef();
  79. }
  80. TWSharedPointer(const TWSharedPointer& rhs)
  81. :pointee(rhs.pointee)
  82. {
  83. AddRef();
  84. }
  85. ~TWSharedPointer()
  86. {
  87. if (pointee)
  88. pointee->RemoveRef();
  89. }
  90. // Operators
  91. bool operator==(const TWSharedPointer& rhs) const
  92. { return (pointee == rhs.pointee); }
  93. bool operator!=(const TWSharedPointer& rhs) const
  94. { return (pointee != rhs.pointee); }
  95. bool operator==(const T* rhs) const { return (pointee == rhs); }
  96. bool operator!=(const T* rhs) const { return (pointee != rhs); }
  97. T* operator->() const {assert (pointee); return pointee; }
  98. T& operator*() const {assert (pointee); return *pointee; }
  99. TWSharedPointer& operator=(const TWSharedPointer& rhs)
  100. {
  101. return this->operator = (rhs.pointee);
  102. }
  103. TWSharedPointer& operator=(T* rhs)
  104. {
  105. TWSharedPointer<T>(rhs).Swap(*this);
  106. return *this;
  107. }
  108. void Swap(TWSharedPointer& other)
  109. {//Core.speciailize.argumemnt-dependent lookup
  110. using std::swap;
  111. swap (pointee, other.pointee);
  112. }
  113. T* Get() const { return pointee; }
  114. bool operator ! () const
  115. {
  116. return ! pointee;
  117. }
  118. bool Unique() const
  119. {
  120. if(pointee)
  121. return (pointee->GetCount() == 1);
  122. return false;
  123. }
  124. private:
  125. void AddRef() const
  126. {
  127. if (pointee)
  128. pointee->AddRef();
  129. }
  130. T* pointee;
  131. };
  132. #endif //TWINKLE_TSMARTPOINT_H_