[haiku-commits] r33879 - in haiku/trunk: headers/private/shared src/kits/support

  • From: ingo_weinhold@xxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 4 Nov 2009 16:08:53 +0100 (CET)

Author: bonefish
Date: 2009-11-04 16:08:53 +0100 (Wed, 04 Nov 2009)
New Revision: 33879
Changeset: http://dev.haiku-os.org/changeset/33879/haiku

Modified:
   haiku/trunk/headers/private/shared/Referenceable.h
   haiku/trunk/src/kits/support/Referenceable.cpp
Log:
* Made BReferenceable the class implementation and BPrivate::Referenceable the
  typedef, so it's clearer which one is the preferred one.
* Added BReference, a clone of BPrivate::Reference.

BPrivate::{Referenceable,Reference} are being phased out. Only the B* versions
should be used.


Modified: haiku/trunk/headers/private/shared/Referenceable.h
===================================================================
--- haiku/trunk/headers/private/shared/Referenceable.h  2009-11-04 14:08:16 UTC 
(rev 33878)
+++ haiku/trunk/headers/private/shared/Referenceable.h  2009-11-04 15:08:53 UTC 
(rev 33879)
@@ -9,16 +9,14 @@
 #include <SupportDefs.h>
 
 
-namespace BPrivate {
-
-class Referenceable {
+class BReferenceable {
 public:
-                                                               Referenceable(
+                                                               BReferenceable(
                                                                        bool 
deleteWhenUnreferenced = true);
                                                                                
// TODO: The parameter is deprecated.
                                                                                
// Override LastReferenceReleased()
                                                                                
// instead!
-       virtual                                         ~Referenceable();
+       virtual                                         ~BReferenceable();
 
                        void                            AcquireReference();
                        bool                            ReleaseReference();
@@ -42,21 +40,116 @@
 
 
 void
-Referenceable::AddReference()
+BReferenceable::AddReference()
 {
        AcquireReference();
 }
 
 
 bool
-Referenceable::RemoveReference()
+BReferenceable::RemoveReference()
 {
        return ReleaseReference();
 }
 
 
+// BReference
+template<typename Type = BReferenceable>
+class BReference {
+public:
+       BReference()
+               : fObject(NULL)
+       {
+       }
+
+       BReference(Type* object, bool alreadyHasReference = false)
+               : fObject(NULL)
+       {
+               SetTo(object, alreadyHasReference);
+       }
+
+       BReference(const BReference<Type>& other)
+               : fObject(NULL)
+       {
+               SetTo(other.fObject);
+       }
+
+       ~BReference()
+       {
+               Unset();
+       }
+
+       void SetTo(Type* object, bool alreadyHasReference = false)
+       {
+               if (object != NULL && !alreadyHasReference)
+                       object->AddReference();
+
+               Unset();
+
+               fObject = object;
+       }
+
+       void Unset()
+       {
+               if (fObject) {
+                       fObject->RemoveReference();
+                       fObject = NULL;
+               }
+       }
+
+       Type* Get() const
+       {
+               return fObject;
+       }
+
+       Type* Detach()
+       {
+               Type* object = fObject;
+               fObject = NULL;
+               return object;
+       }
+
+       Type& operator*() const
+       {
+               return *fObject;
+       }
+
+       Type* operator->() const
+       {
+               return fObject;
+       }
+
+       BReference& operator=(const BReference<Type>& other)
+       {
+               SetTo(other.fObject);
+               return *this;
+       }
+
+       bool operator==(const BReference<Type>& other) const
+       {
+               return (fObject == other.fObject);
+       }
+
+       bool operator!=(const BReference<Type>& other) const
+       {
+               return (fObject != other.fObject);
+       }
+
+private:
+       Type*   fObject;
+};
+
+
+// #pragma mark Obsolete API
+
+// TODO: To be phased out!
+
+
+namespace BPrivate {
+
+
 // Reference
-template<typename Type = BPrivate::Referenceable>
+template<typename Type = BReferenceable>
 class Reference {
 public:
        Reference()
@@ -141,12 +234,13 @@
        Type*   fObject;
 };
 
+
+typedef BReferenceable Referenceable;
+
 }      // namespace BPrivate
 
 using BPrivate::Referenceable;
 using BPrivate::Reference;
 
-typedef BPrivate::Referenceable BReferenceable;
 
-
 #endif // _REFERENCEABLE_H

Modified: haiku/trunk/src/kits/support/Referenceable.cpp
===================================================================
--- haiku/trunk/src/kits/support/Referenceable.cpp      2009-11-04 14:08:16 UTC 
(rev 33878)
+++ haiku/trunk/src/kits/support/Referenceable.cpp      2009-11-04 15:08:53 UTC 
(rev 33879)
@@ -6,20 +6,20 @@
 #include <Referenceable.h>
 
 
-Referenceable::Referenceable(bool deleteWhenUnreferenced)
+BReferenceable::BReferenceable(bool deleteWhenUnreferenced)
        : fReferenceCount(1),
          fDeleteWhenUnreferenced(deleteWhenUnreferenced)
 {
 }
 
 
-Referenceable::~Referenceable()
+BReferenceable::~BReferenceable()
 {
 }
 
 
 void
-Referenceable::AcquireReference()
+BReferenceable::AcquireReference()
 {
        if (atomic_add(&fReferenceCount, 1) == 0)
                FirstReferenceAcquired();
@@ -27,7 +27,7 @@
 
 
 bool
-Referenceable::ReleaseReference()
+BReferenceable::ReleaseReference()
 {
        bool unreferenced = (atomic_add(&fReferenceCount, -1) == 1);
        if (unreferenced)
@@ -37,13 +37,13 @@
 
 
 void
-Referenceable::FirstReferenceAcquired()
+BReferenceable::FirstReferenceAcquired()
 {
 }
 
 
 void
-Referenceable::LastReferenceReleased()
+BReferenceable::LastReferenceReleased()
 {
        if (fDeleteWhenUnreferenced)
                delete this;


Other related posts:

  • » [haiku-commits] r33879 - in haiku/trunk: headers/private/shared src/kits/support - ingo_weinhold