diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-05-01 14:47:53 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-05-10 11:00:45 (GMT) |
commit | 4b47675441b76620519b0204497686b09113daaa (patch) | |
tree | c3494c61a82b84418761000d5fc7fc5d12188204 /src/FbTk/RefCount.hh | |
parent | fa15400cc24ddcfd6e361bd068ae1986b9f9e561 (diff) | |
download | fluxbox-4b47675441b76620519b0204497686b09113daaa.zip fluxbox-4b47675441b76620519b0204497686b09113daaa.tar.bz2 |
Make RefCount<> more sensible
the previous version of operator*() made no sense. E.g., it violated the invariant
(*ptr).foo <=> ptr->foo. The dereferencing operator now returns a reference to the pointed-to
object, rather than a pointer to it.
I also added a bool conversion operator, which can be used in testing the NULL-ness of the
pointer. Anyone wondering if that could be done in a simpler way is encouraged to read
<http://www.artima.com/cppsource/safebool.html>.
And, finally, I removed the mutable flag from the m_data member, since it does not need it.
Diffstat (limited to 'src/FbTk/RefCount.hh')
-rw-r--r-- | src/FbTk/RefCount.hh | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/FbTk/RefCount.hh b/src/FbTk/RefCount.hh index 6d1b9b7..597f847 100644 --- a/src/FbTk/RefCount.hh +++ b/src/FbTk/RefCount.hh | |||
@@ -27,6 +27,8 @@ namespace FbTk { | |||
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 | template <typename Pointer> | 28 | template <typename Pointer> |
29 | class RefCount { | 29 | class RefCount { |
30 | typedef Pointer* RefCount::*bool_type; | ||
31 | |||
30 | public: | 32 | public: |
31 | RefCount(); | 33 | RefCount(); |
32 | explicit RefCount(Pointer *p); | 34 | explicit RefCount(Pointer *p); |
@@ -35,9 +37,11 @@ public: | |||
35 | ~RefCount(); | 37 | ~RefCount(); |
36 | RefCount<Pointer> &operator = (const RefCount<Pointer> ©); | 38 | RefCount<Pointer> &operator = (const RefCount<Pointer> ©); |
37 | RefCount<Pointer> &operator = (Pointer *p); | 39 | RefCount<Pointer> &operator = (Pointer *p); |
38 | Pointer *operator * () const { return get(); } | 40 | Pointer &operator * () const { return *get(); } |
39 | Pointer *operator -> () const { return get(); } | 41 | Pointer *operator -> () const { return get(); } |
40 | Pointer *get() const { return m_data; } | 42 | Pointer *get() const { return m_data; } |
43 | /// conversion to "bool" | ||
44 | operator bool_type() const { return m_data ? &RefCount::m_data : 0; } | ||
41 | 45 | ||
42 | private: | 46 | private: |
43 | /// increase reference count | 47 | /// increase reference count |
@@ -45,7 +49,7 @@ private: | |||
45 | /// decrease reference count | 49 | /// decrease reference count |
46 | void decRefCount(); | 50 | void decRefCount(); |
47 | Pointer *m_data; ///< data holder | 51 | Pointer *m_data; ///< data holder |
48 | mutable unsigned int *m_refcount; ///< holds reference counting | 52 | unsigned int *m_refcount; ///< holds reference counting |
49 | }; | 53 | }; |
50 | 54 | ||
51 | // implementation | 55 | // implementation |