Skip to content

Commit

Permalink
Revert "[libcxx] shared_ptr changes from library fundamentals (P0414R…
Browse files Browse the repository at this point in the history
…2)."

This reverts commit e8c13c1.
  • Loading branch information
zoecarver committed May 12, 2020
1 parent f98709a commit 5eb5548
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 485 deletions.
128 changes: 45 additions & 83 deletions libcxx/include/memory
Expand Up @@ -3702,25 +3702,15 @@ public:

template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;

template<class _Tp, class _Up>
struct __compatible_with
#if _LIBCPP_STD_VER > 14
: is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
#else
: is_convertible<_Tp*, _Up*> {};
#endif // _LIBCPP_STD_VER > 14

template<class _Tp>
class _LIBCPP_TEMPLATE_VIS shared_ptr
{
public:
typedef _Tp element_type;

#if _LIBCPP_STD_VER > 14
typedef weak_ptr<_Tp> weak_type;
typedef remove_extent_t<_Tp> element_type;
#else
typedef _Tp element_type;
#endif

private:
element_type* __ptr_;
__shared_weak_count* __cntrl_;
Expand All @@ -3733,13 +3723,13 @@ public:
_LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
template<class _Yp>
explicit shared_ptr(_Yp* __p,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
template<class _Yp, class _Dp>
shared_ptr(_Yp* __p, _Dp __d,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
template<class _Yp, class _Dp, class _Alloc>
shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
Expand All @@ -3748,13 +3738,13 @@ public:
template<class _Yp>
_LIBCPP_INLINE_VISIBILITY
shared_ptr(const shared_ptr<_Yp>& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
_NOEXCEPT;
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
_LIBCPP_INLINE_VISIBILITY
shared_ptr(shared_ptr&& __r) _NOEXCEPT;
template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
_NOEXCEPT;
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Expand Down Expand Up @@ -3817,7 +3807,7 @@ public:
template<class _Yp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
is_convertible<_Yp*, element_type*>::value,
shared_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
Expand All @@ -3828,8 +3818,8 @@ public:
template<class _Yp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
shared_ptr&
is_convertible<_Yp*, element_type*>::value,
shared_ptr<_Tp>&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(shared_ptr<_Yp>&& __r);
Expand Down Expand Up @@ -3879,23 +3869,23 @@ public:
template<class _Yp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
is_convertible<_Yp*, element_type*>::value,
void
>::type
_LIBCPP_INLINE_VISIBILITY
reset(_Yp* __p);
template<class _Yp, class _Dp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
is_convertible<_Yp*, element_type*>::value,
void
>::type
_LIBCPP_INLINE_VISIBILITY
reset(_Yp* __p, _Dp __d);
template<class _Yp, class _Dp, class _Alloc>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
is_convertible<_Yp*, element_type*>::value,
void
>::type
_LIBCPP_INLINE_VISIBILITY
Expand All @@ -3907,12 +3897,7 @@ public:
typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
{return *__ptr_;}
_LIBCPP_INLINE_VISIBILITY
element_type* operator->() const _NOEXCEPT
{
static_assert(!_VSTD::is_array<_Tp>::value,
"std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
return __ptr_;
}
element_type* operator->() const _NOEXCEPT {return __ptr_;}
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
_LIBCPP_INLINE_VISIBILITY
Expand All @@ -3932,17 +3917,6 @@ public:
__owner_equivalent(const shared_ptr& __p) const
{return __cntrl_ == __p.__cntrl_;}

#if _LIBCPP_STD_VER > 14
typename add_lvalue_reference<element_type>::type
_LIBCPP_INLINE_VISIBILITY
operator[](ptrdiff_t __i) const
{
static_assert(_VSTD::is_array<_Tp>::value,
"std::shared_ptr<T>::operator[] is only valid when T is an array type.");
return __ptr_[__i];
}
#endif

#ifndef _LIBCPP_NO_RTTI
template <class _Dp>
_LIBCPP_INLINE_VISIBILITY
Expand Down Expand Up @@ -3995,18 +3969,6 @@ private:

_LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}

template <class, class _Yp>
struct __shared_ptr_default_delete
: default_delete<_Yp> {};

template <class _Yp, class _Un, size_t _Sz>
struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
: default_delete<_Yp[]> {};

template <class _Yp, class _Un>
struct __shared_ptr_default_delete<_Yp[], _Un>
: default_delete<_Yp[]> {};

template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
};
Expand Down Expand Up @@ -4039,21 +4001,21 @@ shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
template<class _Tp>
template<class _Yp>
shared_ptr<_Tp>::shared_ptr(_Yp* __p,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
: __ptr_(__p)
{
unique_ptr<_Yp> __hold(__p);
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
__hold.release();
__enable_weak_this(__p, __p);
}

template<class _Tp>
template<class _Yp, class _Dp>
shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
: __ptr_(__p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
Expand Down Expand Up @@ -4099,7 +4061,7 @@ shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
template<class _Tp>
template<class _Yp, class _Dp, class _Alloc>
shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
: __ptr_(__p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
Expand Down Expand Up @@ -4177,7 +4139,7 @@ template<class _Tp>
template<class _Yp>
inline
shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
_NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
Expand All @@ -4202,7 +4164,7 @@ template<class _Tp>
template<class _Yp>
inline
shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
_NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
Expand Down Expand Up @@ -4314,7 +4276,7 @@ template<class _Yp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
shared_ptr<_Tp>&
>::type
shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Expand All @@ -4339,7 +4301,7 @@ template<class _Yp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
shared_ptr<_Tp>&
>::type
shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
Expand Down Expand Up @@ -4440,7 +4402,7 @@ template<class _Yp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
void
>::type
shared_ptr<_Tp>::reset(_Yp* __p)
Expand All @@ -4453,7 +4415,7 @@ template<class _Yp, class _Dp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
void
>::type
shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
Expand All @@ -4466,7 +4428,7 @@ template<class _Yp, class _Dp, class _Alloc>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
void
>::type
shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
Expand Down Expand Up @@ -4680,41 +4642,41 @@ swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT

template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp>
typename enable_if
<
!is_array<_Tp>::value && !is_array<_Up>::value,
shared_ptr<_Tp>
>::type
static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
{
return shared_ptr<_Tp>(__r,
static_cast<
typename shared_ptr<_Tp>::element_type*>(__r.get()));
return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
}

template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp>
typename enable_if
<
!is_array<_Tp>::value && !is_array<_Up>::value,
shared_ptr<_Tp>
>::type
dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
{
typedef typename shared_ptr<_Tp>::element_type _ET;
_ET* __p = dynamic_cast<_ET*>(__r.get());
_Tp* __p = dynamic_cast<_Tp*>(__r.get());
return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
}

template<class _Tp, class _Up>
shared_ptr<_Tp>
typename enable_if
<
is_array<_Tp>::value == is_array<_Up>::value,
shared_ptr<_Tp>
>::type
const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
{
typedef typename shared_ptr<_Tp>::element_type _RTp;
typedef typename remove_extent<_Tp>::type _RTp;
return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
}

template<class _Tp, class _Up>
shared_ptr<_Tp>
reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
{
return shared_ptr<_Tp>(__r,
reinterpret_cast<
typename shared_ptr<_Tp>::element_type*>(__r.get()));
}

#ifndef _LIBCPP_NO_RTTI

template<class _Dp, class _Tp>
Expand Down Expand Up @@ -5118,7 +5080,7 @@ struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
_LIBCPP_INLINE_VISIBILITY
result_type operator()(const argument_type& __ptr) const _NOEXCEPT
{
return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
return hash<_Tp*>()(__ptr.get());
}
};

Expand Down
Expand Up @@ -20,42 +20,14 @@

#include "test_macros.h"

#if TEST_STD_VER > 14
template <typename T, typename = std::void_t<> >
struct has_less : std::false_type {};

template <typename T>
struct has_less<T,
std::void_t<decltype(std::declval<T>() < std::declval<T>())> >
: std::true_type {};
#endif

struct A; // purposefully incomplete
struct B {
int x;
B() = default;
};

template <class T>
void test() {
ASSERT_SAME_TYPE(typename std::shared_ptr<T>::element_type, T);
int main(int, char**)
{
static_assert((std::is_same<std::shared_ptr<A>::element_type, A>::value), "");
#if TEST_STD_VER > 14
ASSERT_SAME_TYPE(typename std::shared_ptr<T>::weak_type, std::weak_ptr<T>);
static_assert(std::is_copy_constructible<std::shared_ptr<T> >::value, "");
static_assert(std::is_copy_assignable<std::shared_ptr<T> >::value, "");
static_assert(has_less<std::shared_ptr<T> >::value);
static_assert(
std::is_same<typename std::shared_ptr<T[]>::element_type, T>::value, "");
static_assert(
std::is_same<typename std::shared_ptr<T[8]>::element_type, T>::value, "");
static_assert((std::is_same<std::shared_ptr<A>::weak_type, std::weak_ptr<A>>::value), "");
#endif
}

int main(int, char**) {
test<A>();
test<B>();
test<int>();
test<char*>();

return 0;
}
Expand Up @@ -55,14 +55,6 @@ int main(int, char**)
assert(pB.get() == pA.get());
assert(!pB.owner_before(pA) && !pA.owner_before(pB));
}
#if TEST_STD_VER > 14
{
const std::shared_ptr<const A[8]> pA;
std::shared_ptr<A[8]> pB = std::const_pointer_cast<A[8]>(pA);
assert(pB.get() == pA.get());
assert(!pB.owner_before(pA) && !pA.owner_before(pB));
}
#endif // TEST_STD_VER > 14

return 0;
return 0;
}

0 comments on commit 5eb5548

Please sign in to comment.