- /************************************************************************
- * chxsmartptr.h
- * -------------
- * Smart pointer base class.
- *
- * Target:
- * Symbian OS
- *
- *
- * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
- *
- ************************************************************************/
- #ifndef _chxsmartptr_h_
- #define _chxsmartptr_h_
- template<class T>
- class CHXSmartCPtr { // const smart pointer
- public:
- CHXSmartCPtr(const T* p = 0);
- CHXSmartCPtr(const CHXSmartCPtr<T>& sp);
- ~CHXSmartCPtr();
- CHXSmartCPtr<T>& operator=(const CHXSmartCPtr<T>& sp);
- CHXSmartCPtr<T>& operator=(const T* p);
- int operator==(const CHXSmartCPtr<T>& sp) const;
- int operator==(const T* p) const;
- int operator!=(const CHXSmartCPtr<T>& sp) const;
- int operator!=(const T* p) const;
- const T* operator->() const;
- const T& operator*() const;
- operator bool() const;
- const T* raw_ptr() const;
- protected:
- union {
- const T* cptr_; // const pointer
- T* ptr_; // mutable pointer
- };
- };
- template<class T>
- class CHXSmartPtr : public CHXSmartCPtr<T> { // mutable smart pointer
- public:
- CHXSmartPtr(T* p = 0);
- CHXSmartPtr<T>& operator=(T* p);
- T* operator->() const;
- T& operator*() const;
- T* raw_ptr() const;
- };
- template<class T>
- unsigned hash_func(const CHXSmartCPtr<T>& sp, unsigned sz);
- template<class T>
- unsigned hash_func(const CHXSmartPtr<T>& sp, unsigned sz);
- /** CHXSmartCPtr<T> **/
- template<class T>
- inline CHXSmartCPtr<T>::CHXSmartCPtr(const T* p)
- : cptr_(p)
- {
- if (cptr_) ptr_->ref();
- }
- template<class T>
- inline CHXSmartCPtr<T>::CHXSmartCPtr(const CHXSmartCPtr<T>& sp)
- : cptr_(sp.cptr_)
- {
- if (cptr_) ptr_->ref();
- }
- template<class T>
- inline CHXSmartCPtr<T>::~CHXSmartCPtr()
- {
- if (ptr_ && ptr_->unref() == 0) {
- delete ptr_;
- ptr_ = 0;
- }
- }
- template<class T>
- inline CHXSmartCPtr<T>& CHXSmartCPtr<T>::operator=(const CHXSmartCPtr<T>& sp)
- {
- if (sp.ptr_) sp.ptr_->ref();
- if (ptr_ && ptr_->unref() == 0)
- delete ptr_;
- cptr_ = sp.cptr_;
- return *this;
- }
- template<class T>
- inline CHXSmartCPtr<T>& CHXSmartCPtr<T>::operator=(const T* p)
- {
- if (p) ((T*)p)->ref();
- if (ptr_ && ptr_->unref() == 0)
- delete ptr_;
- cptr_ = p;
- return *this;
- }
- template<class T>
- inline int CHXSmartCPtr<T>::operator==(const CHXSmartCPtr<T>& sp) const
- {
- return cptr_ == sp.cptr_;
- }
- template<class T>
- inline int CHXSmartCPtr<T>::operator==(const T* p) const
- {
- return cptr_ == p;
- }
- template<class T>
- inline int CHXSmartCPtr<T>::operator!=(const CHXSmartCPtr<T>& sp) const
- {
- return cptr_ != sp.cptr_;
- }
- template<class T>
- inline int CHXSmartCPtr<T>::operator!=(const T* p) const
- {
- return cptr_ != p;
- }
- template<class T>
- inline const T* CHXSmartCPtr<T>::operator->() const
- {
- return cptr_;
- }
- template<class T>
- inline const T& CHXSmartCPtr<T>::operator*() const
- {
- return *cptr_;
- }
- template<class T>
- inline CHXSmartCPtr<T>::operator bool () const
- {
- return cptr_ != 0;
- }
- template<class T>
- inline const T* CHXSmartCPtr<T>::raw_ptr() const
- {
- return cptr_ ;
- }
- /** CHXSmartPtr<T> **/
- template<class T>
- inline CHXSmartPtr<T>::CHXSmartPtr(T* p)
- : CHXSmartCPtr<T>(p)
- {}
- template<class T>
- inline CHXSmartPtr<T>& CHXSmartPtr<T>::operator=(T* p)
- {
- (void) CHXSmartCPtr<T>::operator=(p);
- return *this;
- }
- template<class T>
- inline T* CHXSmartPtr<T>::operator->() const
- {
- return ptr_;
- }
- template<class T>
- inline T& CHXSmartPtr<T>::operator*() const
- {
- return *ptr_;
- }
- template<class T>
- inline T* CHXSmartPtr<T>::raw_ptr() const
- {
- return ptr_ ;
- }
- template<class T>
- inline unsigned hash_func(const CHXSmartCPtr<T>& sp, unsigned sz)
- {
- return ((unsigned) sp.raw_ptr())%sz;
- }
- template<class T>
- inline unsigned hash_func(const CHXSmartPtr<T>& sp, unsigned sz)
- {
- return ((unsigned) sp.raw_ptr())%sz;
- }
- #endif // _chxsmartptr_h_