[haiku-commits] haiku: hrev48723 - headers/os/support src/kits/network

  • From: pulkomandy@xxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 26 Jan 2015 10:51:39 +0100 (CET)

hrev48723 adds 2 changesets to branch 'master'
old head: c861dfdb7e58022b877c94a6f4ff3144854a240b
new head: e9b82428687037c1998e7140661a249710dd93c8
overview: 
http://cgit.haiku-os.org/haiku/log/?qt=range&q=e9b824286870+%5Ec861dfdb7e58

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

2ba13ecd938f: getifaddrs: actually iterate over interfaces.

e9b824286870: Add a BConstReference class.
  
  This is a BReference that allows only const access to the referenced
  object. This was not easily possible with the existing BReference for
  two reasons:
  * BReference<const Type> would not work, as BReference needs to change
  the reference count of the referenced object. Adding mutable and casting
  where appropriate wouldwork but,
  * It is now also possible to assign a BReference to a BConstReference
  (to the same type, of course). The reverse is not allowed, making it
  more difficult to "const cast" the referenced object (it's still
  possible to "get" the object pointer and cast that).
  
  BConstReference can be used to provide shared read-only access to an
  object, for example this can be used to cache non-copiable or
  expansive to create objects.

                                 [ Adrien Destugues <pulkomandy@xxxxxxxxx> ]

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

2 files changed, 50 insertions(+), 12 deletions(-)
headers/os/support/Referenceable.h | 59 +++++++++++++++++++++++++++-------
src/kits/network/getifaddrs.cpp    |  3 ++

############################################################################

Commit:      2ba13ecd938f4d9a876c6130bede6331a58fe2d0
URL:         http://cgit.haiku-os.org/haiku/commit/?id=2ba13ecd938f
Author:      Adrien Destugues <pulkomandy@xxxxxxxxx>
Date:        Wed Jan 21 16:00:43 2015 UTC

getifaddrs: actually iterate over interfaces.

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

diff --git a/src/kits/network/getifaddrs.cpp b/src/kits/network/getifaddrs.cpp
index 436ab2f..9462735 100644
--- a/src/kits/network/getifaddrs.cpp
+++ b/src/kits/network/getifaddrs.cpp
@@ -119,6 +119,9 @@ getifaddrs(struct ifaddrs **ifap)
                        current->ifa_next = previous;
                        previous = current;
                }
+
+               interfaces = (ifreq*)((uint8*)interfaces
+                       + _SIZEOF_ADDR_IFREQ(interfaces[0]));
        }
 
        *ifap = current;

############################################################################

Revision:    hrev48723
Commit:      e9b82428687037c1998e7140661a249710dd93c8
URL:         http://cgit.haiku-os.org/haiku/commit/?id=e9b824286870
Author:      Adrien Destugues <pulkomandy@xxxxxxxxx>
Date:        Thu Jan 22 13:36:40 2015 UTC

Add a BConstReference class.

This is a BReference that allows only const access to the referenced
object. This was not easily possible with the existing BReference for
two reasons:
* BReference<const Type> would not work, as BReference needs to change
the reference count of the referenced object. Adding mutable and casting
where appropriate wouldwork but,
* It is now also possible to assign a BReference to a BConstReference
(to the same type, of course). The reverse is not allowed, making it
more difficult to "const cast" the referenced object (it's still
possible to "get" the object pointer and cast that).

BConstReference can be used to provide shared read-only access to an
object, for example this can be used to cache non-copiable or
expansive to create objects.

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

diff --git a/headers/os/support/Referenceable.h 
b/headers/os/support/Referenceable.h
index 0c7b298..a9af8c6 100644
--- a/headers/os/support/Referenceable.h
+++ b/headers/os/support/Referenceable.h
@@ -37,7 +37,7 @@ protected:
 // #pragma mark - BReference
 
 
-template<typename Type = BReferenceable>
+template<typename Type = BReferenceable, typename ConstType = Type>
 class BReference {
 public:
        BReference()
@@ -57,7 +57,7 @@ public:
                :
                fObject(NULL)
        {
-               SetTo(other.fObject);
+               SetTo(other.Get());
        }
 
        
@@ -92,34 +92,34 @@ public:
                }
        }
 
-       Type* Get() const
+       ConstType* Get() const
        {
                return fObject;
        }
 
-       Type* Detach()
+       ConstType* Detach()
        {
                Type* object = fObject;
                fObject = NULL;
                return object;
        }
 
-       Type& operator*() const
+       ConstType& operator*() const
        {
                return *fObject;
        }
 
-       Type* operator->() const
+       ConstType* operator->() const
        {
                return fObject;
        }
 
-       operator Type*() const
+       operator ConstType*() const
        {
                return fObject;
        }
 
-       BReference& operator=(const BReference<Type>& other)
+       BReference& operator=(const BReference<Type, ConstType>& other)
        {
                SetTo(other.fObject);
                return *this;
@@ -131,14 +131,14 @@ public:
                return *this;
        }
 
-       template<typename OtherType>
-       BReference& operator=(const BReference<OtherType>& other)
+       template<typename OtherType, typename OtherConstType>
+       BReference& operator=(const BReference<OtherType, OtherConstType>& 
other)
        {
                SetTo(other.Get());
                return *this;
        }
 
-       bool operator==(const BReference<Type>& other) const
+       bool operator==(const BReference<Type, ConstType>& other) const
        {
                return fObject == other.fObject;
        }
@@ -148,7 +148,7 @@ public:
                return fObject == other;
        }
 
-       bool operator!=(const BReference<Type>& other) const
+       bool operator!=(const BReference<Type, ConstType>& other) const
        {
                return fObject != other.fObject;
        }
@@ -163,4 +163,39 @@ private:
 };
 
 
+// #pragma mark - BReference
+
+
+template<typename Type = BReferenceable>
+class BConstReference: public BReference<Type, const Type> {
+public:
+       BConstReference()
+               :
+               BReference<Type, const Type>()
+       {
+       }
+
+       BConstReference(Type* object, bool alreadyHasReference = false)
+               :
+               BReference<Type, const Type>(object, alreadyHasReference)
+       {
+       }
+
+       BConstReference(const BReference<Type>& other)
+               :
+               BReference<Type, const Type>(other)
+       {
+       }
+
+       // Allow assignment of a const reference from a mutable one (but not the
+       // reverse).
+       BConstReference& operator=(const BReference<Type, Type>& other)
+       {
+               SetTo(other.Get());
+               return *this;
+       }
+
+};
+
+
 #endif // _REFERENCEABLE_H


Other related posts: