| 9 | |
| 10 | template<typename T> |
| 11 | class FancyRefPtr{ |
| 12 | private: |
| 13 | unsigned* refCount = nullptr; // Reference Count |
| 14 | T* obj = nullptr; |
| 15 | |
| 16 | public: |
| 17 | template<class U> |
| 18 | FancyRefPtr(const FancyRefPtr<U>& s, T* p){ |
| 19 | obj = p; |
| 20 | refCount = s.getRefCount(); |
| 21 | |
| 22 | __sync_fetch_and_add(refCount, 1); |
| 23 | } |
| 24 | |
| 25 | FancyRefPtr(){ |
| 26 | refCount = nullptr; |
| 27 | obj = nullptr; |
| 28 | } |
| 29 | |
| 30 | FancyRefPtr(T&& v){ |
| 31 | refCount = new unsigned; |
| 32 | *(refCount) = 1; |
| 33 | |
| 34 | obj = new T(v); |
| 35 | |
| 36 | #ifdef REFPTR_DEBUG |
| 37 | Log::Info("New RefPtr containing %s object, now %d references to object", TTraits<T>::name(), *refCount); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | FancyRefPtr(T* p){ |
| 42 | refCount = new unsigned; |
| 43 | *(refCount) = 1; |
| 44 | |
| 45 | obj = p; |
| 46 | |
| 47 | #ifdef REFPTR_DEBUG |
| 48 | Log::Info("New RefPtr containing existing pointer to %s object, now %d references to object", TTraits<T>::name(), *refCount); |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | FancyRefPtr(const FancyRefPtr<T>& ptr){ |
| 53 | obj = ptr.obj; |
| 54 | refCount = ptr.refCount; |
| 55 | |
| 56 | __sync_fetch_and_add(refCount, 1); |
| 57 | } |
| 58 | |
| 59 | FancyRefPtr(FancyRefPtr<T>&& ptr){ |
| 60 | obj = ptr.obj; |
| 61 | refCount = ptr.refCount; |
| 62 | } |
| 63 | |
| 64 | virtual ~FancyRefPtr(){ |
| 65 | if(refCount && obj){ |
| 66 | if(*refCount > 0){ |
| 67 | __sync_fetch_and_sub(refCount, 1); |
| 68 | } else { |
nothing calls this directly
no outgoing calls
no test coverage detected