Scribus
Open source desktop publishing at your fingertips
scguardedptr.h
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"
11 #include "assert.h"
12 /*
13 A replacement for QPointer
14 Does not rely on QObject, and provides faster destructor
15 */
16 
17 template<typename T>
19 {
20 public:
21 
22  int refs;
23  T* pointer;
24 
25  ScGuardedPtrData(void) { pointer = 0; refs = 0; }
26  ScGuardedPtrData(T* ptr) { pointer = ptr; refs = 0; }
27 };
28 
29 template<typename T>
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;}
42  bool operator!= ( const ScGuardedPtr<T>& p ) const { return !( *this == p ); }
43 
44  bool isNull(void);
45 
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 
50  void deref(void);
51 };
52 
53 template<typename T>
54 class ScGuardedObject : public ScGuardedPtr<T>
55 {
56 public:
57  ScGuardedObject(T* ptr);
58  ScGuardedObject(const ScGuardedObject& gPtr);
59  ~ScGuardedObject();
60 
61  ScGuardedObject& operator=(const ScGuardedObject& gPtr);
62  bool operator==( const ScGuardedObject<T> &p ) const { return (T*)(*this) == (T*) p;}
63  bool operator!= ( const ScGuardedObject<T>& p ) const { return !( *this == p ); }
64 
65  void nullify(void);
66 };
67 
68 template<typename T>
70 {
71  data = new ScGuardedPtrData<T>();
72  ++(data->refs);
73 };
74 
75 template<typename T>
77 {
78  data = new ScGuardedPtrData<T>(ptr);
79  ++(data->refs);
80 };
81 
82 template<typename T>
84 {
85  data = other.data;
86  ++(data->refs);
87 };
88 
89 template<typename T>
91 {
92  deref();
93 };
94 
95 template<typename T>
97 {
98  // We do not copy the owner member
99  if (data != other.data)
100  {
101  ++(other.data->refs);
102  deref();
103  data = other.data;
104  }
105  return *this;
106 };
107 
108 template<typename T>
109 bool ScGuardedPtr<T>::isNull(void)
110 {
111  if (data)
112  return (data->pointer == 0);
113  return true;
114 };
115 
116 template<typename T>
117 void ScGuardedPtr<T>::deref(void)
118 {
119  if (data && --(data->refs) == 0)
120  {
121  delete data;
122  data = 0;
123  }
124 };
125 
126 template<typename T>
128 {
129 };
130 
131 template<typename T>
133 {
134  this->data = 0;
135  // Must never be used
136  assert(false);
137 };
138 
139 template<typename T>
141 {
142  // We do nothing, think about an object being assigned to another
143  // The object being assigned must keep track of its own data
144  return *this;
145 };
146 
147 template<typename T>
149 {
150  if (this->data)
151  this->data->pointer = 0;
152 };
153 
154 template<typename T>
156 {
157  nullify();
158  // deref() is called by ScGuardedPtr<T> dtor
159 };
160 
161 #endif
Definition: scguardedptr.h:18
Definition: scguardedptr.h:30
Definition: scguardedptr.h:54