[haiku-commits] haiku: hrev43625 - headers/private/shared

  • From: clemens.zeidler@xxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 5 Jan 2012 01:43:36 +0100 (CET)

hrev43625 adds 1 changeset to branch 'master'
old head: dafbb164060463db6c906f729c05975c246bae7f
new head: 32951c4e632ef88d5a2db417806eb5d0a0f4ec43

----------------------------------------------------------------------------

32951c4: Make BReference and BWeakReference behave more like a normal pointer.
  * Casts like BReference<Derived> to BReference<Base> are now possible.
  * This cast for BWeakReference is, because of the underlying structure, not 
automatically type safe. I used a simple hack to make the compiler complain if 
the cast
  is not type safe. Please take a look if that can be done better.
  * Smaller style and bug fixes.

                                     [ czeidler <haiku@xxxxxxxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev43625
Commit:      32951c4e632ef88d5a2db417806eb5d0a0f4ec43
URL:         http://cgit.haiku-os.org/haiku/commit/?id=32951c4
Author:      czeidler <haiku@xxxxxxxxxxxxxxxxxx>
Date:        Thu Jan  5 00:29:47 2012 UTC

----------------------------------------------------------------------------

2 files changed, 81 insertions(+), 9 deletions(-)
headers/private/shared/Referenceable.h     |   31 +++++++++++-
headers/private/shared/WeakReferenceable.h |   59 +++++++++++++++++++++--

----------------------------------------------------------------------------

diff --git a/headers/private/shared/Referenceable.h 
b/headers/private/shared/Referenceable.h
index e1f0a6e..d6d9c31 100644
--- a/headers/private/shared/Referenceable.h
+++ b/headers/private/shared/Referenceable.h
@@ -41,22 +41,34 @@ template<typename Type = BReferenceable>
 class BReference {
 public:
        BReference()
-               : fObject(NULL)
+               :
+               fObject(NULL)
        {
        }
 
        BReference(Type* object, bool alreadyHasReference = false)
-               : fObject(NULL)
+               :
+               fObject(NULL)
        {
                SetTo(object, alreadyHasReference);
        }
 
        BReference(const BReference<Type>& other)
-               : fObject(NULL)
+               :
+               fObject(NULL)
        {
                SetTo(other.fObject);
        }
 
+       
+       template <typename OtherType>
+       BReference(const BReference<OtherType>& other)
+               :
+               fObject(NULL)
+       {
+               SetTo(other.Get());
+       }
+
        ~BReference()
        {
                Unset();
@@ -113,6 +125,19 @@ public:
                return *this;
        }
 
+       BReference& operator=(Type* other)
+       {
+               SetTo(other);
+               return *this;
+       }
+
+       template <typename OtherType>
+       BReference& operator=(const BReference<OtherType>& other)
+       {
+               SetTo(other.Get());
+               return *this;
+       }
+
        bool operator==(const BReference<Type>& other) const
        {
                return (fObject == other.fObject);
diff --git a/headers/private/shared/WeakReferenceable.h 
b/headers/private/shared/WeakReferenceable.h
index 22d4181..376ae29 100644
--- a/headers/private/shared/WeakReferenceable.h
+++ b/headers/private/shared/WeakReferenceable.h
@@ -87,6 +87,22 @@ public:
                SetTo(other);
        }
 
+       template <typename OtherType>
+       BWeakReference(const BReference<OtherType>& other)
+               :
+               fPointer(NULL)
+       {
+               SetTo(other.Get());
+       }
+
+       template <typename OtherType>
+       BWeakReference(const BWeakReference<OtherType>& other)
+               :
+               fPointer(NULL)
+       {
+               SetTo(other);
+       }
+
        ~BWeakReference()
        {
                Unset();
@@ -110,6 +126,22 @@ public:
                }
        }
 
+       template <typename OtherType>
+       void SetTo(const BWeakReference<OtherType>& other)
+       {
+               // Just a compiler check if the types are compatible.
+               OtherType* otherDummy = NULL;
+               Type* dummy = otherDummy;
+               dummy = NULL;
+
+               Unset();
+
+               if (other.Get()) {
+                       fPointer = const_cast<WeakPointer*>(other.Get());
+                       fPointer->AcquireReference();
+               }
+       }
+
        void SetTo(const BReference<Type>& other)
        {
                SetTo(other.Get());
@@ -140,33 +172,48 @@ public:
                return BReference<Type>(object, true);
        }
 
+       /*! Do not use this if you do not know what you are doing. The 
WeakPointer
+       is for internal use only. */
+       const WeakPointer* Get() const
+       {
+               return fPointer;
+       }
+
        BWeakReference& operator=(const BWeakReference<Type>& other)
        {
                if (this == &other)
                        return *this;
 
-               SetTo(other.fPointer);
+               SetTo(other);
                return *this;
        }
 
-       BWeakReference& operator=(const Type& other)
+       BWeakReference& operator=(Type* other)
        {
-               SetTo(&other);
+               SetTo(other);
                return *this;
        }
 
-       BWeakReference& operator=(Type* other)
+       BWeakReference& operator=(const BReference<Type>& other)
        {
-               SetTo(other);
+               SetTo(other.Get());
                return *this;
        }
 
-       BWeakReference& operator=(const BReference<Type>& other)
+       template <typename OtherType>
+       BWeakReference& operator=(const BReference<OtherType>& other)
        {
                SetTo(other.Get());
                return *this;
        }
 
+       template <typename OtherType>
+       BWeakReference& operator=(const BWeakReference<OtherType>& other)
+       {
+               SetTo(other);
+               return *this;
+       }
+
        bool operator==(const BWeakReference<Type>& other) const
        {
                return fPointer == other.fPointer;


Other related posts: