| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #ifndef TWINKLE_TSMARTPOINT_H_
- #define TWINKLE_TSMARTPOINT_H_
- #pragma once
- #include <assert.h>
- #include <algorithm>
- typedef unsigned CounterType;
- typedef unsigned count_type;
- template<class T>
- class TSmartPointer
- {
- public:
- TSmartPointer(void):counter(0)
- { }
- TSmartPointer(const TSmartPointer&):counter(0)
- { }
- explicit TSmartPointer(T* rhs = 0)
-
- { }
- virtual ~TSmartPointer(void);
- void AddRef() const;
- void RemoveRef() const;
- TSmartPointer& operator=(const TSmartPointer& ) { return *this; }
- private:
- CounterType counter;
- T* pointee;
- };
- class TWSharedObject
- {
- public:
- void AddRef() const
- {
- ++use_count;
- printf("AddRef: %d\n", use_count);
- }
- void RemoveRef() const
- {
- assert(use_count > 0);
- bool destroy = --use_count == 0;
- printf("RemoveRef: %d\n", use_count);
- if (destroy) {
- printf("Before destroy\n");
- delete this;
- }
-
- }
- CounterType GetCount() const { return use_count; }
- protected:
- TWSharedObject()
- : access_mutex()
- , use_count(0)
- {
- printf("TWSharedObject::Constructor\n");
- }
- TWSharedObject(const TWSharedObject&)
- : access_mutex()
- , use_count(0)
- {
- printf("TWSharedObject::Constructor1\n");
- }
- virtual ~TWSharedObject()
- {
- assert(use_count == 0);
- printf("TWSharedObject::Distructor\n");
- }
- TWSharedObject& operator=(const TWSharedObject&) { return *this; }
- public:
- typedef unsigned Mutex;
- mutable Mutex access_mutex;
- private:
-
- mutable CounterType use_count;
- };
- //Use it match with Type<TWSharedObject>
- template<class T>
- class TWSharedPointer
- {
- public:
-
- explicit TWSharedPointer(T* realPtr = 0)
- : pointee(realPtr)
- {
- AddRef();
- }
- TWSharedPointer(const TWSharedPointer& rhs)
- :pointee(rhs.pointee)
- {
- AddRef();
- }
- ~TWSharedPointer()
- {
- if (pointee)
- pointee->RemoveRef();
- }
- // Operators
- bool operator==(const TWSharedPointer& rhs) const
- { return (pointee == rhs.pointee); }
- bool operator!=(const TWSharedPointer& rhs) const
- { return (pointee != rhs.pointee); }
- bool operator==(const T* rhs) const { return (pointee == rhs); }
- bool operator!=(const T* rhs) const { return (pointee != rhs); }
- T* operator->() const {assert (pointee); return pointee; }
- T& operator*() const {assert (pointee); return *pointee; }
- TWSharedPointer& operator=(const TWSharedPointer& rhs)
- {
- return this->operator = (rhs.pointee);
- }
- TWSharedPointer& operator=(T* rhs)
- {
- TWSharedPointer<T>(rhs).Swap(*this);
- return *this;
- }
- void Swap(TWSharedPointer& other)
- {//Core.speciailize.argumemnt-dependent lookup
- using std::swap;
- swap (pointee, other.pointee);
- }
- T* Get() const { return pointee; }
- bool operator ! () const
- {
- return ! pointee;
- }
- bool Unique() const
- {
- if(pointee)
- return (pointee->GetCount() == 1);
- return false;
- }
- private:
- void AddRef() const
- {
- if (pointee)
- pointee->AddRef();
- }
- T* pointee;
- };
- #endif //TWINKLE_TSMARTPOINT_H_
|