Back to index

scribus-ng  1.3.4.dfsg+svn20071115
scguardedptr.h
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #ifndef SCGUARDEDPTR_H
00008 #define SCGUARDEDPTR_H
00009 
00010 #include "scribusapi.h"
00011 
00012 /*
00013 A replacement for QGuardedPtr
00014 Does not rely on QObject, and provides faster destructor
00015 */ 
00016 
00017 template<typename T>
00018 class ScGuardedPtrData
00019 {
00020 public:
00021 
00022        int refs;
00023        T* pointer;
00024 
00025        ScGuardedPtrData(void) { pointer = NULL; refs = 0; }
00026        ScGuardedPtrData(T* ptr) { pointer = ptr; refs = 0; }
00027 };
00028 
00029 template<typename T>
00030 class ScGuardedPtr
00031 {
00032 protected:
00033        ScGuardedPtrData<T> *data;
00034 public:
00035        ScGuardedPtr(void);
00036        ScGuardedPtr(T* ptr);
00037        ScGuardedPtr(const ScGuardedPtr& gPtr);
00038        ~ScGuardedPtr();
00039 
00040        ScGuardedPtr& operator=(const ScGuardedPtr& gPtr);
00041        bool operator==( const ScGuardedPtr<T> &p ) const { return (T*)(*this) == (T*) p;}
00042        bool operator!= ( const ScGuardedPtr<T>& p ) const { return !( *this == p ); }
00043 
00044        T* operator->() const { return (T*)(data ? data->pointer : 0); }
00045        T& operator*() const { return *((T*)(data ? data->pointer : 0)); }
00046        operator T*() const { return (T*)(data ? data->pointer : 0); }
00047 
00048        void deref(void);
00049 };
00050 
00051 template<typename T>
00052 class ScGuardedObject : public ScGuardedPtr<T>
00053 {
00054 public:
00055        ScGuardedObject(T* ptr);
00056        ScGuardedObject(const ScGuardedObject& gPtr);
00057        ~ScGuardedObject();
00058 
00059        ScGuardedObject& operator=(const ScGuardedObject& gPtr);
00060        bool operator==( const ScGuardedObject<T> &p ) const { return (T*)(*this) == (T*) p;}
00061        bool operator!= ( const ScGuardedObject<T>& p ) const { return !( *this == p ); }
00062 
00063        void nullify(void);
00064 };
00065 
00066 template<typename T>
00067 ScGuardedPtr<T>::ScGuardedPtr(void)
00068 {
00069        data = new ScGuardedPtrData<T>();
00070        ++(data->refs);
00071 };
00072 
00073 template<typename T>
00074 ScGuardedPtr<T>::ScGuardedPtr(T* ptr)
00075 {
00076        data = new ScGuardedPtrData<T>(ptr);
00077        ++(data->refs);
00078 };
00079 
00080 template<typename T>
00081 ScGuardedPtr<T>::ScGuardedPtr(const ScGuardedPtr& other)
00082 {
00083        data = other.data;
00084        ++(data->refs);
00085 };
00086 
00087 template<typename T>
00088 ScGuardedPtr<T>::~ScGuardedPtr()
00089 {
00090        deref();
00091 };
00092 
00093 template<typename T>
00094 ScGuardedPtr<T>& ScGuardedPtr<T>::operator=(const ScGuardedPtr& other)
00095 {
00096        // We do not copy the owner member
00097        if (data != other.data)
00098        {
00099               ++(other.data->refs);
00100               deref();
00101               data = other.data;
00102        }
00103        return *this;
00104 };
00105 
00106 template<typename T>
00107 void ScGuardedPtr<T>::deref(void)
00108 {
00109        if (data && --(data->refs) == 0)
00110        {
00111               delete data;
00112               data = NULL;
00113        }
00114 };
00115 
00116 template<typename T>
00117 ScGuardedObject<T>::ScGuardedObject(T* ptr) : ScGuardedPtr<T>(ptr)
00118 {
00119 };
00120 
00121 template<typename T>
00122 ScGuardedObject<T>::ScGuardedObject(const ScGuardedObject& other)
00123 {
00124        this->data=NULL;
00125        // Must never be used
00126        assert(false);
00127 };
00128 
00129 template<typename T>
00130 ScGuardedObject<T>& ScGuardedObject<T>::operator=(const ScGuardedObject& other)
00131 {
00132        // We do nothing, think about an object being assigned to another
00133        // The object being assigned must keep track of its own data
00134        return *this;
00135 };
00136 
00137 template<typename T>
00138 void ScGuardedObject<T>::nullify(void)
00139 {
00140        if (this->data)
00141               this->data->pointer = NULL;
00142 };
00143 
00144 template<typename T>
00145 ScGuardedObject<T>::~ScGuardedObject()
00146 {
00147        nullify();
00148        // deref() is called by ScGuardedPtr<T> dtor
00149 };
00150 
00151 #endif