aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/FbTk/RefCount.hh57
1 files changed, 18 insertions, 39 deletions
diff --git a/src/FbTk/RefCount.hh b/src/FbTk/RefCount.hh
index ab4c663..adeebf0 100644
--- a/src/FbTk/RefCount.hh
+++ b/src/FbTk/RefCount.hh
@@ -25,7 +25,6 @@
25namespace FbTk { 25namespace FbTk {
26 26
27/// holds a pointer with reference counting, similar to std:auto_ptr 27/// holds a pointer with reference counting, similar to std:auto_ptr
28
29template <typename Pointer> 28template <typename Pointer>
30class RefCount { 29class RefCount {
31public: 30public:
@@ -40,7 +39,7 @@ public:
40 Pointer *operator -> () const { return get(); } 39 Pointer *operator -> () const { return get(); }
41 Pointer *get() const { return m_data; } 40 Pointer *get() const { return m_data; }
42#ifdef NOT_USED 41#ifdef NOT_USED
43 /// @return number of referenses 42 /// @return number of references
44 unsigned int usedBy() const { return (m_refcount != 0 ? *m_refcount : 0); } 43 unsigned int usedBy() const { return (m_refcount != 0 ? *m_refcount : 0); }
45#endif 44#endif
46private: 45private:
@@ -48,43 +47,33 @@ private:
48 void incRefCount(); 47 void incRefCount();
49 /// decrease reference count 48 /// decrease reference count
50 void decRefCount(); 49 void decRefCount();
51 /// decrease refcount count
52 void decRefCountCount();
53 Pointer *m_data; ///< data holder 50 Pointer *m_data; ///< data holder
54 mutable unsigned int *m_refcount; ///< holds reference counting 51 mutable unsigned int *m_refcount; ///< holds reference counting
55
56 // This one counts the number of active references pointing to the m_refcount data!
57 // when it reaches zero, *then* we can delete it, otherwise someone else might check it.
58 mutable unsigned int *m_refcount_count; ///< holds reference counting
59}; 52};
60 53
61// implementation 54// implementation
62 55
63template <typename Pointer> 56template <typename Pointer>
64RefCount<Pointer>::RefCount():m_data(0), m_refcount(new unsigned int(0)), m_refcount_count(new unsigned int(1)) { 57RefCount<Pointer>::RefCount():m_data(0), m_refcount(new unsigned int(0)) {
65 58 incRefCount(); // it really counts how many things are storing m_refcount
66} 59}
67 60
68template <typename Pointer> 61template <typename Pointer>
69RefCount<Pointer>::RefCount(RefCount<Pointer> &copy): 62RefCount<Pointer>::RefCount(RefCount<Pointer> &copy):
70 m_data(copy.m_data), 63 m_data(copy.m_data),
71 m_refcount(copy.m_refcount), 64 m_refcount(copy.m_refcount) {
72 m_refcount_count(copy.m_refcount_count) {
73 (*m_refcount_count)++;
74 incRefCount(); 65 incRefCount();
75} 66}
76 67
77template <typename Pointer> 68template <typename Pointer>
78RefCount<Pointer>::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)), m_refcount_count(new unsigned int(1)) { 69RefCount<Pointer>::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)) {
79 incRefCount(); 70 incRefCount();
80} 71}
81 72
82template <typename Pointer> 73template <typename Pointer>
83RefCount<Pointer>::RefCount(const RefCount<Pointer> &copy): 74RefCount<Pointer>::RefCount(const RefCount<Pointer> &copy):
84 m_data(copy.m_data), 75 m_data(copy.m_data),
85 m_refcount(copy.m_refcount), 76 m_refcount(copy.m_refcount) {
86 m_refcount_count(copy.m_refcount_count) {
87 (*m_refcount_count)++;
88 incRefCount(); 77 incRefCount();
89} 78}
90 79
@@ -97,8 +86,6 @@ template <typename Pointer>
97RefCount<Pointer> &RefCount<Pointer>::operator = (const RefCount<Pointer> &copy) { 86RefCount<Pointer> &RefCount<Pointer>::operator = (const RefCount<Pointer> &copy) {
98 decRefCount(); // dec current ref count 87 decRefCount(); // dec current ref count
99 m_refcount = copy.m_refcount; // set new ref count 88 m_refcount = copy.m_refcount; // set new ref count
100 m_refcount_count = copy.m_refcount_count;
101 (*m_refcount_count)++;
102 m_data = copy.m_data; // set new data pointer 89 m_data = copy.m_data; // set new data pointer
103 incRefCount(); // inc new ref count 90 incRefCount(); // inc new ref count
104 return *this; 91 return *this;
@@ -109,35 +96,27 @@ RefCount<Pointer> &RefCount<Pointer>::operator = (Pointer *p) {
109 decRefCount(); 96 decRefCount();
110 m_data = p; // set data pointer 97 m_data = p; // set data pointer
111 m_refcount = new unsigned int(0); // create new counter 98 m_refcount = new unsigned int(0); // create new counter
112 m_refcount_count = new unsigned int(1);
113 incRefCount(); 99 incRefCount();
114 return *this; 100 return *this;
115} 101}
116 102
117template <typename Pointer> 103template <typename Pointer>
118void RefCount<Pointer>::decRefCount() { 104void RefCount<Pointer>::decRefCount() {
119 if (m_refcount != 0) { 105 if (m_refcount == 0)
120 (*m_refcount)--; 106 return;
121 if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this 107 if (*m_refcount == 0) { // already zero, then delete refcount
122 if (m_data != 0) 108 delete m_refcount;
123 delete m_data; 109 m_refcount = 0;
124 m_data = 0; 110 return;
125 }
126 } 111 }
127 decRefCountCount(); 112 (*m_refcount)--;
128} 113 if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this
129 114 if (m_data != 0)
130template <typename Pointer> 115 delete m_data;
131void RefCount<Pointer>::decRefCountCount() { 116 m_data = 0;
132 if (*m_refcount_count == 0)
133 return; // shouldnt happen
134 (*m_refcount_count)--;
135 if (*m_refcount_count == 0) {
136 delete m_refcount; 117 delete m_refcount;
137 delete m_refcount_count; 118 m_refcount = 0;
138 } 119 }
139 m_refcount = 0;
140 m_refcount_count = 0;
141} 120}
142 121
143template <typename Pointer> 122template <typename Pointer>