Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=f35e3a39c956fcfe2ffcefe0594375f8d70bb1ec

commit f35e3a39c956fcfe2ffcefe0594375f8d70bb1ec
Author: Michel Hermier <[email protected]>
Date:   Mon Nov 3 08:00:39 2014 +0100

libpacman: Make refcounted_ptr a base class of refcounted_shared_ptr.

diff --git a/lib/libpacman/kernel/frefcounted.h 
b/lib/libpacman/kernel/frefcounted.h
index 4340196..2412b5d 100644
--- a/lib/libpacman/kernel/frefcounted.h
+++ b/lib/libpacman/kernel/frefcounted.h
@@ -65,33 +65,90 @@ namespace flib
};

template <class T>
+       class refcounted_ptr
+       {
+       protected:
+               refcounted_ptr()
+                       : m_refcounted_ptr(nullptr)
+               { }
+
+               /* Manipulators */
+               template <class Y>
+               void reset(Y *refcounted_ptr)
+               {
+                       m_refcounted_ptr = refcounted_ptr;
+               }
+
+       public:
+               /* Accessors */
+               T *get() const
+               {
+                       return m_refcounted_ptr;
+               }
+
+               T &operator * () const
+               {
+                       return *operator -> ();
+               }
+
+               T *operator -> () const
+               {
+                       assert(m_refcounted_ptr != nullptr);
+                       return get();
+               }
+
+               explicit operator T * () const
+               {
+                       return get();
+               }
+
+               operator bool () const
+               {
+                       return m_refcounted_ptr != nullptr;
+               }
+
+       protected:
+               void swap(refcounted_ptr<T> &o)
+               {
+                       std::swap(m_refcounted_ptr, o.m_refcounted_ptr);
+               }
+
+       private:
+               refcounted_ptr(const refcounted_ptr &o);
+               refcounted_ptr &operator = (const refcounted_ptr &o);
+
+               T *m_refcounted_ptr;
+       };
+
+       template <class T>
class refcounted_shared_ptr
+               : public refcounted_ptr<T>
{
public:
typedef T element_type;
+               typedef refcounted_ptr<T> super_type;

constexpr refcounted_shared_ptr()
: refcounted_shared_ptr(nullptr)
{ }

constexpr refcounted_shared_ptr(std::nullptr_t)
-                       : m_refcounted_ptr(nullptr)
{ }

template <class Y>
-               explicit refcounted_shared_ptr(Y *ptr)
+               explicit refcounted_shared_ptr(Y *refcounted_ptr)
: refcounted_shared_ptr()
{
-                       reset(ptr);
+                       reset(refcounted_ptr);
}

template <class Y>
refcounted_shared_ptr(const refcounted_shared_ptr<Y> &o)
-                       : refcounted_shared_ptr(o.m_refcounted_ptr)
+                       : refcounted_shared_ptr(o.get())
{ }

refcounted_shared_ptr(const refcounted_shared_ptr &o)
-                       : refcounted_shared_ptr(o.m_refcounted_ptr)
+                       : refcounted_shared_ptr(o.get())
{ }

refcounted_shared_ptr(refcounted_shared_ptr &&o)
@@ -108,13 +165,13 @@ namespace flib
template <class Y>
refcounted_shared_ptr &operator = (const refcounted_shared_ptr<Y> &o)
{
-                       reset(o.m_refcounted_ptr);
+                       reset(o.get());
return *this;
}

refcounted_shared_ptr &operator = (const refcounted_shared_ptr &o)
{
-                       reset(o.m_refcounted_ptr);
+                       reset(o.get());
return *this;
}

@@ -133,71 +190,41 @@ namespace flib
template <class Y>
void reset(Y *ptr)
{
-                       if(m_refcounted_ptr != nullptr) {
-                               m_refcounted_ptr->release();
+                       if(super_type::get() != nullptr) {
+                               super_type::get()->release();
}
if(ptr != nullptr) {
ptr->acquire();
}
-                       m_refcounted_ptr = ptr;
+                       super_type::reset(ptr);
}

void swap(refcounted_shared_ptr<T> &o)
{
-                       std::swap(m_refcounted_ptr, o.m_refcounted_ptr);
-               }
-
-               /* Accessors */
-               T *get() const
-               {
-                       return m_refcounted_ptr;
-               }
-
-               T &operator * () const
-               {
-                       return *operator -> ();
-               }
-
-               T *operator -> () const
-               {
-                       assert(m_refcounted_ptr != nullptr);
-                       return get();
-               }
-
-               explicit operator T * () const
-               {
-                       return get();
-               }
-
-               operator bool () const
-               {
-                       return m_refcounted_ptr != nullptr;
+                       super_type::swap(o);
}
-
-       private:
-               T *m_refcounted_ptr;
};

template <class T, class U>
-       bool operator == (const refcounted_shared_ptr<T> &lhs, const 
refcounted_shared_ptr<U> &rhs)
+       bool operator == (const refcounted_ptr<T> &lhs, const refcounted_ptr<U> 
&rhs)
{
return lhs.get() == rhs.get();
}

template <class T>
-       bool operator == (const refcounted_shared_ptr<T> &lhs, std::nullptr_t 
rhs)
+       bool operator == (const refcounted_ptr<T> &lhs, std::nullptr_t rhs)
{
return lhs.get() == rhs;
}

template <class T, class U>
-       bool operator != (const refcounted_shared_ptr<T> &lhs, const 
refcounted_shared_ptr<U> &rhs)
+       bool operator != (const refcounted_ptr<T> &lhs, const refcounted_ptr<U> 
&rhs)
{
return lhs.get() != rhs.get();
}

template <class T>
-       bool operator != (const refcounted_shared_ptr<T> &lhs, std::nullptr_t 
rhs)
+       bool operator != (const refcounted_ptr<T> &lhs, std::nullptr_t rhs)
{
return lhs.get() != rhs;
}
@@ -206,13 +233,13 @@ namespace flib
namespace std
{
template <class T>
-       bool operator == (nullptr_t lhs, const flib::refcounted_shared_ptr<T> 
&rhs)
+       bool operator == (nullptr_t lhs, const flib::refcounted_ptr<T> &rhs)
{
return lhs == rhs.get();
}

template <class T>
-       bool operator != (nullptr_t lhs, const flib::refcounted_shared_ptr<T> 
&rhs)
+       bool operator != (nullptr_t lhs, const flib::refcounted_ptr<T> &rhs)
{
return lhs != rhs.get();
}
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to