Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=77c00a5b5f279dd4f911450b080f225ae88158d0
commit 77c00a5b5f279dd4f911450b080f225ae88158d0 Author: Michel Hermier <herm...@frugalware.org> Date: Fri Oct 17 11:26:09 2014 +0200 libpacman: Add flib::refcounted_ptr. diff --git a/lib/libpacman/kernel/frefcounted.h b/lib/libpacman/kernel/frefcounted.h index 51c26fc..64f95ba 100644 --- a/lib/libpacman/kernel/frefcounted.h +++ b/lib/libpacman/kernel/frefcounted.h @@ -23,9 +23,10 @@ #include "kernel/fsignal.h" +#include <assert.h> + namespace flib { - class refcounted { public: @@ -62,6 +63,80 @@ namespace flib mutable unsigned m_reference_counter; }; + + template <class T> + class refcounted_ptr + { + public: + typedef T element_type; + + constexpr refcounted_ptr() + : m_refcounted_ptr(nullptr) + { } + + template <class Y> + explicit refcounted_ptr(Y *ptr) + : m_refcounted_ptr(ptr) + { + if(m_refcounted_ptr != nullptr) { + m_refcounted_ptr->acquire(); + } + } + + ~refcounted_ptr() + { + if(m_refcounted_ptr != nullptr) { + m_refcounted_ptr->release(); + } + } + + /* Manipulators */ + void reset() + { + reset<T>(*this, nullptr); + } + + template <class Y> + void reset(Y *ptr) + { + swap(*this, refcounted_ptr<T>(ptr)); + } + + void swap(refcounted_ptr &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(*this); + return get(); + } + + operator T * () const + { + return get(); + } + + operator bool () const + { + return m_refcounted_ptr != nullptr; + } + + private: + T *m_refcounted_ptr; + }; } #endif /* FREFCOUNTED_H */ diff --git a/lib/libpacman/package.h b/lib/libpacman/package.h index 8c24f35..5aecd25 100644 --- a/lib/libpacman/package.h +++ b/lib/libpacman/package.h @@ -206,6 +206,7 @@ private: }; class package_node + : public flib::refcounted { public: package_node(const char *name); @@ -225,7 +226,7 @@ private: }; class package_graph - : flib::set<libpacman::package_node *, package_node_less> + : flib::set<flib::refcounted_ptr<libpacman::package_node>, package_node_less> { public: using set::set; _______________________________________________ Frugalware-git mailing list Frugalware-git@frugalware.org http://frugalware.org/mailman/listinfo/frugalware-git