diff options
author | Murray Cumming <murrayc@murrayc.com> | 2016-04-15 17:16:15 +0200 |
---|---|---|
committer | Murray Cumming <murrayc@murrayc.com> | 2016-04-23 21:29:42 +0200 |
commit | 46b9dff3274ce9520e28182c42ca5a6df55c73d7 (patch) | |
tree | c48ee0100f527e20c6463b6a194bafee9b16d3a1 /sigc++/signal_base.h | |
parent | 3527ebb4d52a58a491571d9c0bdfccd0a0c0ef2c (diff) | |
download | sigc++-46b9dff3274ce9520e28182c42ca5a6df55c73d7.tar.gz |
signal_impl: Trying to do the ref-counting with std::shared_ptr.
Bug #764935
Diffstat (limited to 'sigc++/signal_base.h')
-rw-r--r-- | sigc++/signal_base.h | 36 |
1 files changed, 10 insertions, 26 deletions
diff --git a/sigc++/signal_base.h b/sigc++/signal_base.h index b9ccaa1..75a390e 100644 --- a/sigc++/signal_base.h +++ b/sigc++/signal_base.h @@ -21,6 +21,7 @@ #include <cstddef> #include <list> +#include <memory> //For std::shared_ptr<> #include <sigc++config.h> #include <sigc++/type_traits.h> #include <sigc++/trackable.h> @@ -42,7 +43,9 @@ namespace internal * erase() to sweep() when the signal is being emitted. sweep() removes all * invalid slots from the list. */ -struct SIGC_API signal_impl : public notifiable +struct SIGC_API signal_impl + : public notifiable, + public std::enable_shared_from_this<signal_impl> { using size_type = std::size_t; using slot_list = std::list<slot_base>; @@ -64,34 +67,19 @@ struct SIGC_API signal_impl : public notifiable void operator delete(void* p); #endif - /// Increments the reference counter. - inline void reference() noexcept { ++ref_count_; } - /// Increments the reference and execution counter. inline void reference_exec() noexcept { - ++ref_count_; ++exec_count_; } - /** Decrements the reference counter. - * The object is deleted when the reference counter reaches zero. - */ - inline void unreference() - { - if (!(--ref_count_)) - delete this; - } - /** Decrements the reference and execution counter. * Invokes sweep() if the execution counter reaches zero and the * removal of one or more slots has been deferred. */ inline void unreference_exec() { - if (!(--ref_count_)) - delete this; - else if (!(--exec_count_) && deferred_) + if (!(--exec_count_) && deferred_) sweep(); } @@ -184,11 +172,6 @@ public: std::list<slot_base> slots_; private: - /** Reference counter. - * The object is destroyed when @em ref_count_ reaches zero. - */ - short ref_count_; - /** Execution counter. * Indicates whether the signal is being emitted. */ @@ -204,7 +187,8 @@ struct SIGC_API signal_exec /** Increments the reference and execution counter of the parent sigc::signal_impl object. * @param sig The parent sigc::signal_impl object. */ - inline signal_exec(const signal_impl* sig) noexcept : sig_(const_cast<signal_impl*>(sig)) + inline signal_exec(const std::shared_ptr<signal_impl>& sig) noexcept + : sig_(sig) { sig_->reference_exec(); } @@ -220,7 +204,7 @@ struct SIGC_API signal_exec protected: /// The parent sigc::signal_impl object. - signal_impl* sig_; + const std::shared_ptr<signal_impl> sig_; }; } /* namespace internal */ @@ -388,10 +372,10 @@ protected: /** Returns the signal_impl object encapsulating the list of slots. * @return The signal_impl object encapsulating the list of slots. */ - internal::signal_impl* impl() const; + std::shared_ptr<internal::signal_impl> impl() const; /// The signal_impl object encapsulating the slot list. - mutable internal::signal_impl* impl_; + mutable std::shared_ptr<internal::signal_impl> impl_; }; } // namespace sigc |