You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/03/01 18:47:06 UTC
svn commit: r1295703 - in
/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu:
include/capu/util/SmartPointer.h test/util/SmartPointerTest.cpp
Author: veithm
Date: Thu Mar 1 17:47:05 2012
New Revision: 1295703
URL: http://svn.apache.org/viewvc?rev=1295703&view=rev
Log:
ETCH-149 SmartPointer
Enabled assignment of different, but castable type
Added equals operator
Change-Id: I876a21d1f39d0a7392981889f7311c799f15431c
Modified:
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/util/SmartPointer.h
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/util/SmartPointerTest.cpp
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/util/SmartPointer.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/util/SmartPointer.h?rev=1295703&r1=1295702&r2=1295703&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/util/SmartPointer.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/util/SmartPointer.h Thu Mar 1 17:47:05 2012
@@ -38,10 +38,18 @@ namespace capu {
* @param pointer to object
*/
SmartPointer(T* ptr);
-
+
+ /**
+ * Copy constructor for different, but castable type
+ * @param reference to smartPointer
+ */
+ template<class X> friend class SmartPointer;
+ template<class X>
+ SmartPointer(const SmartPointer<X>& smartPointer);
+
/**
* Copy constructor
- * @param reference to object
+ * @param reference to smartPointer
*/
SmartPointer(const SmartPointer& smartPointer);
@@ -49,14 +57,22 @@ namespace capu {
* Deconstructor
*/
~SmartPointer();
-
+
/**
* Overload assignment operator to be able to decrement the
* reference count if another object gets assigned to this smart pointer
+ * @param reference to smartPointer
*/
SmartPointer& operator= (const SmartPointer& smartPointer);
/**
+ * Overload assignment operator for castable, but different type
+ * @param reference to smartPointer
+ */
+ template <class X>
+ SmartPointer<T>& operator= (const SmartPointer<X>& smartPointer);
+
+ /**
* Overload assignment operator to be able to decrement the
* reference count if another object gets assigned to this smart pointer
*/
@@ -75,6 +91,18 @@ namespace capu {
T& operator*() const;
/**
+ * Returns true if two smart pointer are equal to each other
+ * false otherwise
+ */
+ capu::bool_t operator==(const SmartPointer<T>& x) const;
+
+ /**
+ * Returns true if two smart pointer aren't equal to each other
+ * false otherwise
+ */
+ capu::bool_t operator!=(const SmartPointer<T>& x) const;
+
+ /**
* Returns the object stored by the smartPointer
*/
T* get() const;
@@ -92,7 +120,6 @@ namespace capu {
*/
capu::uint32_t getRefCount();
- protected:
private:
T* m_data;
capu::uint32_t* m_referenceCount;
@@ -121,13 +148,21 @@ namespace capu {
{
m_data = ptr;
m_referenceCount = new capu::uint32_t(0);
-
incRefCount();
-
}
}
template<class T>
+ template<class X>
+ inline
+ SmartPointer<T>::SmartPointer(const SmartPointer<X>& smartPointer)
+ : m_data(static_cast<X*> (smartPointer.m_data))
+ , m_referenceCount(smartPointer.m_referenceCount)
+ {
+ incRefCount();
+ }
+
+ template<class T>
inline
SmartPointer<T>::SmartPointer(const SmartPointer<T>& smartPointer)
: m_data(smartPointer.m_data)
@@ -176,6 +211,19 @@ namespace capu {
return *this;
}
+ template <class T>
+ template <class X>
+ inline
+ SmartPointer<T>& SmartPointer<T>::operator= (const SmartPointer<X>& smartPointer) {
+ if (*this != smartPointer) {
+ decRefCount();
+ m_data = static_cast<X*> (smartPointer.m_data);
+ m_referenceCount = smartPointer.m_referenceCount;
+ incRefCount();
+ }
+ return *this;
+ }
+
template<class T>
inline
T* SmartPointer<T>::operator->() const
@@ -191,6 +239,16 @@ namespace capu {
}
template<class T>
+ capu::bool_t SmartPointer<T>::operator==(const SmartPointer<T>& x) const {
+ return ((x.m_data == this->m_data) && (x.m_referenceCount == this->m_referenceCount));
+ }
+
+ template<class T>
+ capu::bool_t SmartPointer<T>::operator!=(const SmartPointer<T>& x) const {
+ return ((x.m_data != this->m_data) || (x.m_referenceCount != this->m_referenceCount));
+ }
+
+ template<class T>
inline
void SmartPointer<T>::incRefCount()
{
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/util/SmartPointerTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/util/SmartPointerTest.cpp?rev=1295703&r1=1295702&r2=1295703&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/util/SmartPointerTest.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/util/SmartPointerTest.cpp Thu Mar 1 17:47:05 2012
@@ -30,7 +30,7 @@ public:
static capu::int32_t mRefCount;
public:
- DummyClass() {
+ DummyClass() {
mValue = 5;
mRefCount++;
}
@@ -43,6 +43,13 @@ public:
};
};
+class ChildDummyClass : public DummyClass {
+public:
+ ChildDummyClass() {
+
+ }
+};
+
capu::int32_t DummyClass::mRefCount = 0;
@@ -62,6 +69,12 @@ TEST(SmartPointer,Constructors) {
}
EXPECT_EQ(0,DummyClass::mRefCount);
+
+ //constructor for castable type
+ capu::SmartPointer<ChildDummyClass> childPtr(new ChildDummyClass());
+ capu::SmartPointer<DummyClass> parentPtr(childPtr);
+ EXPECT_EQ((capu::uint32_t)2,childPtr.getRefCount());
+ EXPECT_EQ((capu::uint32_t)2,parentPtr.getRefCount());
}
@@ -82,6 +95,21 @@ TEST(SmartPointer,AssignmentOperator) {
ptr3 = new DummyClass();
EXPECT_EQ((capu::uint32_t)1,ptr3.getRefCount());
EXPECT_EQ((capu::uint32_t)1,ptr4.getRefCount());
+
+ //assignment of castable type
+ capu::SmartPointer<ChildDummyClass> childPtr(new ChildDummyClass());
+ capu::SmartPointer<DummyClass> parentPtr(new DummyClass());
+ capu::SmartPointer<DummyClass> parentPtrCopy(parentPtr);
+
+ EXPECT_EQ((capu::uint32_t)2,parentPtr.getRefCount());
+ EXPECT_EQ((capu::uint32_t)2,parentPtrCopy.getRefCount());
+
+ //assign
+ parentPtr = childPtr;
+ EXPECT_EQ((capu::uint32_t)2,childPtr.getRefCount());
+ EXPECT_EQ((capu::uint32_t)2,parentPtr.getRefCount());
+ EXPECT_EQ((capu::uint32_t)1,parentPtrCopy.getRefCount());
+
}
TEST(SmartPointer,FileOperator) {
@@ -121,3 +149,23 @@ TEST(SmartPointer,getRefCount) {
ptr.~SmartPointer();
EXPECT_EQ((capu::uint32_t)0, ptr.getRefCount());
}
+
+TEST(SmartPointer, operatorTest) {
+ capu::SmartPointer<DummyClass> ptr, ptr2;
+ ptr = new DummyClass();
+ EXPECT_EQ((capu::uint32_t)1, ptr.getRefCount());
+ EXPECT_FALSE(ptr == ptr2);
+ EXPECT_TRUE(ptr != ptr2);
+ ptr2 = ptr;
+ EXPECT_TRUE(ptr == ptr2);
+ EXPECT_FALSE(ptr != ptr2);
+}
+
+TEST(SmartPointer,castTest) {
+ capu::SmartPointer<DummyClass> ptr2;
+ capu::SmartPointer<ChildDummyClass> ptr;
+ ptr = new ChildDummyClass();
+ EXPECT_EQ((capu::uint32_t)1, ptr.getRefCount());
+ capu::SmartPointer<DummyClass> gecastet = ptr;
+ EXPECT_EQ((capu::uint32_t)2, ptr.getRefCount());
+}