Subversion Repositories Scribus

Rev

Rev 20976 | Rev 23210 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7906 jghali 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
#ifndef SCGUARDEDPTR_H
8
#define SCGUARDEDPTR_H
9
 
10
#include "scribusapi.h"
10601 mrdocs 11
#include "assert.h"
7906 jghali 12
/*
10030 cbradney 13
A replacement for QPointer
7906 jghali 14
Does not rely on QObject, and provides faster destructor
15
*/
16
 
17
template<typename T>
18
class ScGuardedPtrData
19
{
20
public:
21
 
22
        int refs;
23
        T* pointer;
24
 
16546 jghali 25
        ScGuardedPtrData(void) { pointer = 0; refs = 0; }
7906 jghali 26
        ScGuardedPtrData(T* ptr) { pointer = ptr; refs = 0; }
27
};
28
 
29
template<typename T>
30
class ScGuardedPtr
31
{
32
protected:
33
        ScGuardedPtrData<T> *data;
34
public:
35
        ScGuardedPtr(void);
36
        ScGuardedPtr(T* ptr);
37
        ScGuardedPtr(const ScGuardedPtr& gPtr);
38
        ~ScGuardedPtr();
39
 
40
        ScGuardedPtr& operator=(const ScGuardedPtr& gPtr);
41
        bool operator==( const ScGuardedPtr<T> &p ) const { return (T*)(*this) == (T*) p;}
7908 cbradney 42
        bool operator!= ( const ScGuardedPtr<T>& p ) const { return !( *this == p ); }
7906 jghali 43
 
22794 jghali 44
        bool isNull(void) const;
16546 jghali 45
 
7906 jghali 46
        T* operator->() const { return (T*)(data ? data->pointer : 0); }
47
        T& operator*() const { return *((T*)(data ? data->pointer : 0)); }
48
        operator T*() const { return (T*)(data ? data->pointer : 0); }
49
 
20976 jghali 50
        int refCount(void) const;
7906 jghali 51
        void deref(void);
52
};
53
 
54
template<typename T>
55
class ScGuardedObject : public ScGuardedPtr<T>
56
{
57
public:
58
        ScGuardedObject(T* ptr);
59
        ScGuardedObject(const ScGuardedObject& gPtr);
60
        ~ScGuardedObject();
61
 
62
        ScGuardedObject& operator=(const ScGuardedObject& gPtr);
63
        bool operator==( const ScGuardedObject<T> &p ) const { return (T*)(*this) == (T*) p;}
7908 cbradney 64
        bool operator!= ( const ScGuardedObject<T>& p ) const { return !( *this == p ); }
7906 jghali 65
 
66
        void nullify(void);
67
};
68
 
69
template<typename T>
70
ScGuardedPtr<T>::ScGuardedPtr(void)
71
{
72
        data = new ScGuardedPtrData<T>();
73
        ++(data->refs);
74
};
75
 
76
template<typename T>
77
ScGuardedPtr<T>::ScGuardedPtr(T* ptr)
78
{
79
        data = new ScGuardedPtrData<T>(ptr);
80
        ++(data->refs);
81
};
82
 
83
template<typename T>
84
ScGuardedPtr<T>::ScGuardedPtr(const ScGuardedPtr& other)
85
{
86
        data = other.data;
87
        ++(data->refs);
88
};
89
 
90
template<typename T>
91
ScGuardedPtr<T>::~ScGuardedPtr()
92
{
93
        deref();
94
};
95
 
96
template<typename T>
97
ScGuardedPtr<T>& ScGuardedPtr<T>::operator=(const ScGuardedPtr& other)
98
{
99
        // We do not copy the owner member
100
        if (data != other.data)
101
        {
102
                ++(other.data->refs);
103
                deref();
104
                data = other.data;
105
        }
106
        return *this;
107
};
108
 
109
template<typename T>
22794 jghali 110
bool ScGuardedPtr<T>::isNull(void) const
16546 jghali 111
{
112
        if (data)
113
                return (data->pointer == 0);
114
        return true;
115
};
116
 
117
template<typename T>
20976 jghali 118
int ScGuardedPtr<T>::refCount(void) const
119
{
120
        if (data)
121
                return data->refs;
122
        return 0;
123
};
124
 
125
template<typename T>
7906 jghali 126
void ScGuardedPtr<T>::deref(void)
127
{
128
        if (data && --(data->refs) == 0)
129
        {
130
                delete data;
16546 jghali 131
                data = 0;
7906 jghali 132
        }
133
};
134
 
135
template<typename T>
7907 jghali 136
ScGuardedObject<T>::ScGuardedObject(T* ptr) : ScGuardedPtr<T>(ptr)
7906 jghali 137
{
138
};
139
 
140
template<typename T>
7908 cbradney 141
ScGuardedObject<T>::ScGuardedObject(const ScGuardedObject& other)
7906 jghali 142
{
16546 jghali 143
        this->data = 0;
7906 jghali 144
        // Must never be used
145
        assert(false);
146
};
147
 
148
template<typename T>
149
ScGuardedObject<T>& ScGuardedObject<T>::operator=(const ScGuardedObject& other)
150
{
151
        // We do nothing, think about an object being assigned to another
152
        // The object being assigned must keep track of its own data
153
        return *this;
154
};
155
 
156
template<typename T>
157
void ScGuardedObject<T>::nullify(void)
158
{
7908 cbradney 159
        if (this->data)
16546 jghali 160
                this->data->pointer = 0;
7906 jghali 161
};
162
 
163
template<typename T>
164
ScGuardedObject<T>::~ScGuardedObject()
165
{
166
        nullify();
167
        // deref() is called by ScGuardedPtr<T> dtor
168
};
169
 
170
#endif