mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-23 18:10:39 +00:00
shared_ptr: add const support for lw_shared_ptr
This commit is contained in:
@@ -112,6 +112,7 @@ protected:
|
||||
enable_lw_shared_from_this& operator=(enable_lw_shared_from_this&&) { return *this; }
|
||||
public:
|
||||
lw_shared_ptr<T> shared_from_this();
|
||||
lw_shared_ptr<const T> shared_from_this() const;
|
||||
template <typename X>
|
||||
friend class lw_shared_ptr;
|
||||
};
|
||||
@@ -136,9 +137,9 @@ struct shared_ptr_no_esft {
|
||||
template <typename T>
|
||||
using shared_ptr_impl
|
||||
= std::conditional_t<
|
||||
std::is_base_of<enable_lw_shared_from_this<T>, T>::value,
|
||||
enable_lw_shared_from_this<T>,
|
||||
shared_ptr_no_esft<T>
|
||||
std::is_base_of<enable_lw_shared_from_this<std::remove_const_t<T>>, T>::value,
|
||||
enable_lw_shared_from_this<std::remove_const_t<T>>,
|
||||
shared_ptr_no_esft<std::remove_const_t<T>>
|
||||
>;
|
||||
|
||||
template <typename T>
|
||||
@@ -219,6 +220,25 @@ public:
|
||||
return _p->_count == 1;
|
||||
}
|
||||
|
||||
bool operator==(const lw_shared_ptr<const T>& x) const {
|
||||
return _p == x._p;
|
||||
}
|
||||
|
||||
bool operator!=(const lw_shared_ptr<const T>& x) const {
|
||||
return !operator==(x);
|
||||
}
|
||||
|
||||
bool operator==(const lw_shared_ptr<std::remove_const_t<T>>& x) const {
|
||||
return _p == x._p;
|
||||
}
|
||||
|
||||
bool operator!=(const lw_shared_ptr<std::remove_const_t<T>>& x) const {
|
||||
return !operator==(x);
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
friend class lw_shared_ptr;
|
||||
|
||||
template <typename X, typename... A>
|
||||
friend lw_shared_ptr<X> make_lw_shared(A&&...);
|
||||
|
||||
@@ -257,6 +277,13 @@ enable_lw_shared_from_this<T>::shared_from_this() {
|
||||
return lw_shared_ptr<T>(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline
|
||||
lw_shared_ptr<const T>
|
||||
enable_lw_shared_from_this<T>::shared_from_this() const {
|
||||
return lw_shared_ptr<const T>(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline
|
||||
std::ostream& operator<<(std::ostream& out, const lw_shared_ptr<T>& p) {
|
||||
|
||||
@@ -64,3 +64,21 @@ BOOST_AUTO_TEST_CASE(test_const_ptr) {
|
||||
shared_ptr<const C> cca = ca->get();
|
||||
BOOST_REQUIRE(cca == ca);
|
||||
}
|
||||
|
||||
struct D {};
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_lw_const_ptr_1) {
|
||||
auto pd1 = make_lw_shared<const D>(D());
|
||||
auto pd2 = make_lw_shared(D());
|
||||
lw_shared_ptr<const D> pd3 = pd2;
|
||||
BOOST_REQUIRE(pd2 == pd3);
|
||||
}
|
||||
|
||||
struct E : enable_lw_shared_from_this<E> {};
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_lw_const_ptr_2) {
|
||||
auto pe1 = make_lw_shared<const E>();
|
||||
auto pe2 = make_lw_shared<E>();
|
||||
lw_shared_ptr<const E> pe3 = pe2;
|
||||
BOOST_REQUIRE(pe2 == pe3);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user