home *** CD-ROM | disk | FTP | other *** search
- #ifndef __SMARTPTR__
- #define __SMARTPTR__
- #pragma once
-
- #include "Object.h"
-
- //----------------------------------------------------------------------------
- // ObjectReference - a reference to a reference counted object.
- //----------------------------------------------------------------------------
-
- template<class T>
- class SmartPtr
- {
- public:
- T* ptr;
-
- inline SmartPtr() : ptr(nil) {}
- inline SmartPtr(T* obj) : ptr(reinterpret_cast<T*>(NewObjectRef(obj))) {}
- inline SmartPtr(const SmartPtr<T>& obj) : ptr(reinterpret_cast<T*>(NewObjectRef(obj.ptr))) {}
-
- // SmartPtr(const T& obj) const : ptr(&obj) { NewObjectRef(const_cast<TObject*>(&obj)); }
- // SmartPtr(T& obj) : ptr(&obj) { NewObjectRef(&obj); }
- // SmartPtr(const T* obj) const : ptr(const_cast<T*>(obj)) { NewObjectRef(const_cast<T*>(obj)); }
-
- #if MEMBER_TEMPLATES_SUPPORTED
-
- template<class U>
- inline SmartPtr(SmartPtr<U>& rhs) : ptr(&rhs) { rhs.NewReference(); }
-
- #else
-
- // SmartPtr(TObject* rhs) : ptr(dynamic_cast<T*>(rhs)) { NewObjectRef(ptr); }
-
- #endif
-
- ~SmartPtr()
- {
- TObject::DeleteObjectRef(ptr);
- }
-
- operator T*() { return ptr; }
-
- #if MEMBER_TEMPLATES_SUPPORTED
-
- inline SmartPtr<T>& operator=(const SmartPtr<U&> rhs)
- {
- if (this != &rhs)
- {
- ptr = SwapObjectRef(ptr, rhs);
- }
-
- return *this
- }
-
- #else
-
- inline SmartPtr<T>& operator=(const SmartPtr<T>& rhs)
- {
- TObject::SwapObjectRef(ptr, rhs.ptr);
-
- ptr = rhs.ptr;
-
- return *this;
- }
-
- inline SmartPtr<T>& operator=(const T* obj)
- {
- TObject::SwapObjectRef(static_cast<const TObject*>(ptr),
- static_cast<const TObject*>(obj));
-
- ptr = const_cast<T*>(obj);
-
- return *this;
- }
-
- /*
- SmartPtr<T>& operator=(TObject* param)
- {
- T* obj = dynamic_cast<T*>(param);
-
- if (ptr != obj)
- {
- SwapObjectRef(ptr, obj);
- ptr = obj;
- }
-
- return *this;
- }
- */
-
- #endif
-
- inline bool operator==(const T* obj) const
- {
- return ptr == obj;
- }
-
- inline bool operator==(const SmartPtr<T>& obj) const
- {
- return ptr == obj.ptr;
- }
-
- inline bool operator!=(const T* obj) const
- {
- return ptr != obj;
- }
-
- inline bool operator!=(const SmartPtr<T>& obj) const
- {
- return ptr != obj.ptr;
- }
-
- // Named helpers for out operators for when C++ is stupid
-
- inline void SetToNull()
- {
- TObject::DeleteObjectRef(ptr);
- ptr = nil;
- }
-
- inline bool IsNull() const
- {
- return ptr == nil;
- }
-
- inline const T* Dereference() const
- {
- if (ptr == nil)
- {
- TObject::NilObjectReference();
- }
-
- #if qDebug
- if (ptr->GetReferenceCount() == 0)
- Warning("Zero reference count in dereferenced object");
- #endif
- return ptr;
- }
-
- inline T* Dereference()
- {
- if (ptr == nil)
- {
- TObject::NilObjectReference();
- }
-
- #if qDebug
- if (ptr->GetReferenceCount() == 0)
- Warning("Zero reference count in dereferenced object");
- #endif
- return ptr;
- }
-
- inline T* GetPtr() const
- {
- return const_cast<T*>(ptr);
- }
-
- inline const T& operator*() const
- {
- return *Dereference();
- }
-
- inline T& operator*()
- {
- return *Dereference();
- }
-
- inline const T* operator->() const
- {
- return Dereference();
- }
-
- inline T* operator->()
- {
- return Dereference();
- }
-
- inline operator bool() const
- {
- return !IsNull();
- }
-
- inline bool operator!() const
- {
- return IsNull();
- }
- };
-
- template<class T, class U>
- SmartPtr<T>& operator=(SmartPtr<U&> rhs)
- {
- if (ptr != &rhs.ptr)
- {
- ptr = SwapObjectRef(ptr, rhs);
- }
-
- return *this
- }
-
- typedef SmartPtr<const TObject> ConstObjectPtr;
- typedef SmartPtr<TObject> ObjectPtr;
-
-
- #endif __SMARTPTR__
-
-