From 211c77bf52af4ba9ad059da59fbb315d2e69720c Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Wed, 6 May 2015 11:41:33 +0300 Subject: [PATCH] shared_ptr: add const support for lw_shared_ptr --- core/shared_ptr.hh | 33 ++++++++++++++++++++++++++++++--- tests/shared_ptr_test.cc | 18 ++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/core/shared_ptr.hh b/core/shared_ptr.hh index f72779413c..0d9a5980b5 100644 --- a/core/shared_ptr.hh +++ b/core/shared_ptr.hh @@ -112,6 +112,7 @@ protected: enable_lw_shared_from_this& operator=(enable_lw_shared_from_this&&) { return *this; } public: lw_shared_ptr shared_from_this(); + lw_shared_ptr shared_from_this() const; template friend class lw_shared_ptr; }; @@ -136,9 +137,9 @@ struct shared_ptr_no_esft { template using shared_ptr_impl = std::conditional_t< - std::is_base_of, T>::value, - enable_lw_shared_from_this, - shared_ptr_no_esft + std::is_base_of>, T>::value, + enable_lw_shared_from_this>, + shared_ptr_no_esft> >; template @@ -219,6 +220,25 @@ public: return _p->_count == 1; } + bool operator==(const lw_shared_ptr& x) const { + return _p == x._p; + } + + bool operator!=(const lw_shared_ptr& x) const { + return !operator==(x); + } + + bool operator==(const lw_shared_ptr>& x) const { + return _p == x._p; + } + + bool operator!=(const lw_shared_ptr>& x) const { + return !operator==(x); + } + + template + friend class lw_shared_ptr; + template friend lw_shared_ptr make_lw_shared(A&&...); @@ -257,6 +277,13 @@ enable_lw_shared_from_this::shared_from_this() { return lw_shared_ptr(this); } +template +inline +lw_shared_ptr +enable_lw_shared_from_this::shared_from_this() const { + return lw_shared_ptr(this); +} + template static inline std::ostream& operator<<(std::ostream& out, const lw_shared_ptr& p) { diff --git a/tests/shared_ptr_test.cc b/tests/shared_ptr_test.cc index c9088385d2..6b3dc3316a 100644 --- a/tests/shared_ptr_test.cc +++ b/tests/shared_ptr_test.cc @@ -64,3 +64,21 @@ BOOST_AUTO_TEST_CASE(test_const_ptr) { shared_ptr cca = ca->get(); BOOST_REQUIRE(cca == ca); } + +struct D {}; + +BOOST_AUTO_TEST_CASE(test_lw_const_ptr_1) { + auto pd1 = make_lw_shared(D()); + auto pd2 = make_lw_shared(D()); + lw_shared_ptr pd3 = pd2; + BOOST_REQUIRE(pd2 == pd3); +} + +struct E : enable_lw_shared_from_this {}; + +BOOST_AUTO_TEST_CASE(test_lw_const_ptr_2) { + auto pe1 = make_lw_shared(); + auto pe2 = make_lw_shared(); + lw_shared_ptr pe3 = pe2; + BOOST_REQUIRE(pe2 == pe3); +}