summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMurray Cumming <murrayc@murrayc.com>2007-07-14 12:10:18 -0500
committerJonathon Jongsma <jjongsma@gnome.org>2007-07-14 12:10:18 -0500
commitca21cd6fee4dbbd81d5257003184708daca40a65 (patch)
treee709e86b6fab9b3466dcd2c99a94ba516f3c4c7c
parent76f5ca4d67e0455c24c69d80562f5854ad7522c1 (diff)
* cairomm/refptr.h: Added RefPtr(object, refcount) constructor
for use in cast_*(), so that the casted RefPtr shares the same refcount, avoiding an early deletion. I do not like making this constructor public, but I do not see another way.
-rw-r--r--ChangeLog7
-rw-r--r--cairomm/refptr.h32
2 files changed, 27 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index a7ea467..f059e4a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-06-14 Murray Cumming <murrayc@murrayc.com>
+
+ * cairomm/refptr.h: Added RefPtr(object, refcount) constructor
+ for use in cast_*(), so that the casted RefPtr shares the same
+ refcount, avoiding an early deletion. I do not like making
+ this constructor public, but I do not see another way.
+
=== 1.4.0 ===
2007-07-12 Jonathon Jongsma <jjongsma@gnome.org>
diff --git a/cairomm/refptr.h b/cairomm/refptr.h
index 8e49fe2..90b8240 100644
--- a/cairomm/refptr.h
+++ b/cairomm/refptr.h
@@ -64,6 +64,9 @@ public:
*/
explicit inline RefPtr(T_CppObject* pCppObject);
+ /// For use only in the internal implementation of sharedptr.
+ explicit inline RefPtr(T_CppObject* pCppObject, int* refcount);
+
/** Copy constructor
*
* This increments the shared reference count.
@@ -227,6 +230,17 @@ RefPtr<T_CppObject>::RefPtr(T_CppObject* pCppObject)
}
}
+//Used by cast_*() implementations:
+template <class T_CppObject> inline
+RefPtr<T_CppObject>::RefPtr(T_CppObject* pCppObject, int* refcount)
+:
+ pCppObject_(pCppObject),
+ pCppRefcount_(refcount)
+{
+ if(pCppObject_ && pCppRefcount_)
+ ++(*pCppRefcount_);
+}
+
template <class T_CppObject> inline
RefPtr<T_CppObject>::RefPtr(const RefPtr<T_CppObject>& src)
:
@@ -342,10 +356,10 @@ RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_dynamic(const RefPtr<T_CastFrom>&
{
T_CppObject *const pCppObject = dynamic_cast<T_CppObject*>(src.operator->());
- if(pCppObject && src.refcount_())
- ++(*(src.refcount_()));
-
- return RefPtr<T_CppObject>(pCppObject); //TODO: Does an unnecessary extra reference() on the C object.
+ if(pCppObject) //Check whether dynamic_cast<> succeeded so we don't pass a null object with a used refcount:
+ return RefPtr<T_CppObject>(pCppObject, src.refcount_());
+ else
+ return RefPtr<T_CppObject>();
}
template <class T_CppObject>
@@ -355,10 +369,7 @@ RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_static(const RefPtr<T_CastFrom>& s
{
T_CppObject *const pCppObject = static_cast<T_CppObject*>(src.operator->());
- if(pCppObject && src.refcount_())
- ++(*(src.refcount_()));
-
- return RefPtr<T_CppObject>(pCppObject); //TODO: Does an unnecessary extra reference() on the C object.
+ return RefPtr<T_CppObject>(pCppObject, src.refcount_());
}
template <class T_CppObject>
@@ -368,10 +379,7 @@ RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_const(const RefPtr<T_CastFrom>& sr
{
T_CppObject *const pCppObject = const_cast<T_CppObject*>(src.operator->());
- if(pCppObject && src.refcount_())
- ++(*(src.refcount_()));
-
- return RefPtr<T_CppObject>(pCppObject); //TODO: Does an unnecessary extra reference() on the C object.
+ return RefPtr<T_CppObject>(pCppObject, src.refcount_());
}
#endif /* DOXYGEN_IGNORE_THIS */