From: Fan You Date: Fri, 13 Nov 2015 11:05:28 +0000 (+0000) Subject: Implement std::experimental::shared_ptr with array support X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=930d560203039875c4da07a01f579f56f5f2c636;p=gcc.git Implement std::experimental::shared_ptr with array support 2015-11-13 Fan You Jonathan Wakely * include/Makefile.am: Add new header. * include/Makefile.in: Regenerate. * include/experimental/bits/shared_ptr.h: New. * include/experimental/memory: Include new header. * testsuite/experimental/memory/shared_ptr/assign/assign.cc: New. * testsuite/experimental/memory/shared_ptr/cast/cast.cc: New. * testsuite/experimental/memory/shared_ptr/comparison/comparison.cc: New. * testsuite/experimental/memory/shared_ptr/cons/alias_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/alloc_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/copy_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/copy_ctor_neg.cc: New. * testsuite/experimental/memory/shared_ptr/cons/default_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/pointer_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc: New. * testsuite/experimental/memory/shared_ptr/dest/dest.cc: New. * testsuite/experimental/memory/shared_ptr/modifiers/reset.cc: New. * testsuite/experimental/memory/shared_ptr/modifiers/swap.cc: New. * testsuite/experimental/memory/shared_ptr/observers/bool_conv.cc: New. * testsuite/experimental/memory/shared_ptr/observers/operators.cc: New. * testsuite/experimental/memory/shared_ptr/observers/owner_before.cc: New. * testsuite/experimental/memory/shared_ptr/observers/use_count.cc: New. Co-Authored-By: Jonathan Wakely From-SVN: r230300 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index bae1b962cc3..fc49f8db682 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,35 @@ +2015-11-13 Fan You + Jonathan Wakely + + * include/Makefile.am: Add new header. + * include/Makefile.in: Regenerate. + * include/experimental/bits/shared_ptr.h: New. + * include/experimental/memory: Include new header. + * testsuite/experimental/memory/shared_ptr/assign/assign.cc: New. + * testsuite/experimental/memory/shared_ptr/cast/cast.cc: New. + * testsuite/experimental/memory/shared_ptr/comparison/comparison.cc: + New. + * testsuite/experimental/memory/shared_ptr/cons/alias_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/alloc_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/copy_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/copy_ctor_neg.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/default_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/pointer_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc: + New. + * testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc: New. + * testsuite/experimental/memory/shared_ptr/dest/dest.cc: New. + * testsuite/experimental/memory/shared_ptr/modifiers/reset.cc: New. + * testsuite/experimental/memory/shared_ptr/modifiers/swap.cc: New. + * testsuite/experimental/memory/shared_ptr/observers/bool_conv.cc: + New. + * testsuite/experimental/memory/shared_ptr/observers/operators.cc: + New. + * testsuite/experimental/memory/shared_ptr/observers/owner_before.cc: + New. + * testsuite/experimental/memory/shared_ptr/observers/use_count.cc: New. + 2015-11-13 Jonathan Wakely * include/Makefile.am: Add new header. diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index 67d83790207..4e2ae181538 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -678,6 +678,7 @@ experimental_bits_srcdir = ${glibcxx_srcdir}/include/experimental/bits experimental_bits_builddir = ./experimental/bits experimental_bits_headers = \ ${experimental_bits_srcdir}/erase_if.h \ + ${experimental_bits_srcdir}/shared_ptr.h \ ${experimental_bits_srcdir}/string_view.tcc \ ${experimental_bits_filesystem_headers} diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in index 519c6be663c..89dfdbe5278 100644 --- a/libstdc++-v3/include/Makefile.in +++ b/libstdc++-v3/include/Makefile.in @@ -967,6 +967,7 @@ experimental_bits_srcdir = ${glibcxx_srcdir}/include/experimental/bits experimental_bits_builddir = ./experimental/bits experimental_bits_headers = \ ${experimental_bits_srcdir}/erase_if.h \ + ${experimental_bits_srcdir}/shared_ptr.h \ ${experimental_bits_srcdir}/string_view.tcc \ ${experimental_bits_filesystem_headers} diff --git a/libstdc++-v3/include/experimental/bits/shared_ptr.h b/libstdc++-v3/include/experimental/bits/shared_ptr.h new file mode 100644 index 00000000000..feba7d7efa1 --- /dev/null +++ b/libstdc++-v3/include/experimental/bits/shared_ptr.h @@ -0,0 +1,1197 @@ +// Experimental shared_ptr with array support -*- C++ -*- + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// . + +/** @file experimental/bits/shared_ptr.h + * This is an internal header file, included by other library headers. + * Do not attempt to use it directly. @headername{memory} + */ + +#ifndef _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H +#define _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H 1 + +#pragma GCC system_header + +#if __cplusplus <= 201103L +# include +#else + +#include +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +namespace experimental +{ +inline namespace fundamentals_v2 +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + template class enable_shared_from_this; +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace fundamentals_v2 +} // namespace experimental + +#define __cpp_lib_experimental_shared_ptr_arrays 201406 + +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + /* + * The specification of std::experimental::shared_ptr is slightly different + * to std::shared_ptr (specifically in terms of pointer "compatibility") so + * to implement std::experimental::shared_ptr without too much duplication + * we make it derive from a partial specialization of std::__shared_ptr + * using a special tag type, __libfund_v1. + * + * There are two partial specializations for the tag type, supporting the + * different interfaces of the array and non-array forms. + */ + + template ::value> + struct __libfund_v1 { using type = _Tp; }; + + // helper for _Compatible + template + struct __sp_compatible + : is_convertible<_From_type*, _To_type*>::type + { }; + + template + struct __sp_compatible<_Tp[_Nm], _Tp[]> + : true_type + { }; + + template + struct __sp_compatible<_Tp[_Nm], const _Tp[]> + : true_type + { }; + + // Partial specialization for base class of experimental::shared_ptr + // (i.e. the non-array form of experimental::shared_ptr) + template + class __shared_ptr<__libfund_v1<_Tp, false>, _Lp> + : private __shared_ptr<_Tp, _Lp> + { + template + using _Compatible + = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>; + + using _Base_type = __shared_ptr<_Tp>; + + _Base_type& _M_get_base() { return *this;} + const _Base_type& _M_get_base() const { return *this;} + + public: + using element_type = _Tp; + + constexpr __shared_ptr() noexcept = default; + + template + explicit __shared_ptr(_Tp1* __p) + : _Base_type(__p) + { } + + template + __shared_ptr(_Tp1* __p, _Deleter __d) + : _Base_type(__p, __d) + { } + + template + __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a) + : _Base_type(__p, __d, __a) + { } + + template + __shared_ptr(nullptr_t __p, _Deleter __d) + : _Base_type(__p, __d) + { } + + template + __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) + : _Base_type(__p, __d, __a) + { } + + template + __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r, + element_type* __p) noexcept + : _Base_type(__r._M_get_base(), __p) + { } + + __shared_ptr(const __shared_ptr&) noexcept = default; + __shared_ptr(__shared_ptr&&) noexcept = default; + __shared_ptr& operator=(const __shared_ptr&) noexcept = default; + __shared_ptr& operator=(__shared_ptr&&) noexcept = default; + ~__shared_ptr() = default; + + template> + __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + : _Base_type(__r._M_get_base()) + { } + + template> + __shared_ptr(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept + : _Base_type(std::move((__r._M_get_base()))) + { } + + template + explicit __shared_ptr(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r) + : _Base_type(__r._M_get_base()) + { } + + template::pointer>>> + __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r) + : _Base_type(std::move(__r)) + { } + +#if _GLIBCXX_USE_DEPRECATED + // Postcondition: use_count() == 1 and __r.get() == 0 + template + __shared_ptr(std::auto_ptr<_Tp1>&& __r) + : _Base_type(std::move(__r)) + { } +#endif + + constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } + + // reset + void + reset() noexcept + { __shared_ptr(nullptr).swap(*this); } + + template + void + reset(_Tp1* __p) + { + _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != get()); + __shared_ptr(__p).swap(*this); + } + + template + void + reset(_Tp1* __p, _Deleter __d) + { __shared_ptr(__p, __d).swap(*this); } + + template + void + reset(_Tp1* __p, _Deleter __d, _Alloc __a) + { __shared_ptr(__p, __d, std::move(__a)).swap(*this); } + + using _Base_type::operator*; + using _Base_type::operator->; + + template + __shared_ptr& + operator=(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + { + _Base_type::operator=(__r._M_get_base()); + return *this; + } + + template + __shared_ptr& + operator=(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept + { + _Base_type::operator=(std::move(__r._M_get_base())); + return *this; + } + + template + __shared_ptr& + operator=(std::unique_ptr<_Tp1>&& __r) + { + _Base_type::operator=(std::move(__r)); + return *this; + } + +#if _GLIBCXX_USE_DEPRECATED + template + __shared_ptr& + operator=(std::auto_ptr<_Tp1>&& __r) + { + _Base_type::operator=(std::move(__r)); + return *this; + } +#endif + + void + swap(__shared_ptr& __other) noexcept + { _Base_type::swap(__other); } + + template + bool + owner_before(__shared_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const + { return _Base_type::owner_before(__rhs._M_get_base()); } + + template + bool + owner_before(__weak_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const + { return _Base_type::owner_before(__rhs._M_get_base()); } + + using _Base_type::operator bool; + using _Base_type::get; + using _Base_type::unique; + using _Base_type::use_count; + + protected: + + // make_shared not yet support for shared_ptr_arrays + //template + // __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, + // _Args&&... __args) + // : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a, + // std::forward<_Args>(__args)...) + // { + // void* __p = _M_refcount._M_get_deleter(typeid(__tag)); + // _M_ptr = static_cast<_Tp*>(__p); + // __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr); + // } + + // __weak_ptr::lock() + __shared_ptr(const __weak_ptr<__libfund_v1<_Tp>, _Lp>& __r, + std::nothrow_t) + : _Base_type(__r._M_get_base(), std::nothrow) + { } + + private: + template friend class __weak_ptr; + template friend class __shared_ptr; + + // TODO + template + friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; + }; + + // Partial specialization for base class of experimental::shared_ptr + // and experimental::shared_ptr (i.e. the array forms). + template + class __shared_ptr<__libfund_v1<_Tp, true>, _Lp> + : private __shared_ptr, _Lp> + { + public: + using element_type = remove_extent_t<_Tp>; + + private: + struct _Array_Deleter + { + void + operator()(element_type const *__p) const + { delete [] __p; } + }; + + struct _Normal_Deleter + { + void + operator()(element_type const *__p) const + { delete __p; } + }; + + template + using _Compatible + = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>; + + using _Deleter_type + = conditional_t::value, _Array_Deleter, _Normal_Deleter>; + + using _Base_type = __shared_ptr; + + _Base_type& _M_get_base() { return *this;} + const _Base_type& _M_get_base() const { return *this;} + + public: + constexpr __shared_ptr() noexcept + : _Base_type() + { } + + template + explicit __shared_ptr(_Tp1* __p) + : _Base_type(__p, _Deleter_type()) + { } + + template + __shared_ptr(_Tp1* __p, _Deleter __d) + : _Base_type(__p, __d) + { } + + template + __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a) + : _Base_type(__p, __d, __a) + { } + + template + __shared_ptr(nullptr_t __p, _Deleter __d) + : _Base_type(__p, __d) + { } + + template + __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) + : _Base_type(__p, __d, __a) + { } + + template + __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r, + element_type* __p) noexcept + : _Base_type(__r._M_get_base(), __p) + { } + + __shared_ptr(const __shared_ptr&) noexcept = default; + __shared_ptr(__shared_ptr&&) noexcept = default; + __shared_ptr& operator=(const __shared_ptr&) noexcept = default; + __shared_ptr& operator=(__shared_ptr&&) noexcept = default; + ~__shared_ptr() = default; + + template> + __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + : _Base_type(__r._M_get_base()) + { } + + template> + __shared_ptr(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept + : _Base_type(std::move((__r._M_get_base()))) + { } + + template + explicit __shared_ptr(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r) + : _Base_type(__r._M_get_base()) + { } + + template::pointer>>> + __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r) + : _Base_type(std::move(__r)) + { } + +#if _GLIBCXX_USE_DEPRECATED + // Postcondition: use_count() == 1 and __r.get() == 0 + template + __shared_ptr(std::auto_ptr<_Tp1>&& __r) + : _Base_type(std::move(__r)) + { } +#endif + + constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } + + // reset + void + reset() noexcept + { __shared_ptr(nullptr).swap(*this); } + + template + void + reset(_Tp1* __p) + { + _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != get()); + __shared_ptr(__p, _Deleter_type()).swap(*this); + } + + template + void + reset(_Tp1* __p, _Deleter __d) + { __shared_ptr(__p, __d).swap(*this); } + + template + void + reset(_Tp1* __p, _Deleter __d, _Alloc __a) + { __shared_ptr(__p, __d, std::move(__a)).swap(*this); } + + element_type& + operator[](ptrdiff_t i) const noexcept + { + _GLIBCXX_DEBUG_ASSERT(get() != 0 && i >= 0); + return get()[i]; + } + + template + __shared_ptr& + operator=(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + { + _Base_type::operator=(__r._M_get_base()); + return *this; + } + + template + __shared_ptr& + operator=(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept + { + _Base_type::operator=(std::move(__r._M_get_base())); + return *this; + } + + template + __shared_ptr& + operator=(std::unique_ptr<_Tp1>&& __r) + { + _Base_type::operator=(std::move(__r)); + return *this; + } + +#if _GLIBCXX_USE_DEPRECATED + template + __shared_ptr& + operator=(std::auto_ptr<_Tp1>&& __r) + { + _Base_type::operator=(std::move(__r)); + return *this; + } +#endif + + void + swap(__shared_ptr& __other) noexcept + { _Base_type::swap(__other); } + + template + bool + owner_before(__shared_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const + { return _Base_type::owner_before(__rhs._M_get_base()); } + + template + bool + owner_before(__weak_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const + { return _Base_type::owner_before(__rhs._M_get_base()); } + + using _Base_type::operator bool; + using _Base_type::get; + using _Base_type::unique; + using _Base_type::use_count; + + protected: + + // make_shared not yet support for shared_ptr_arrays + //template + // __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, + // _Args&&... __args) + // : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a, + // std::forward<_Args>(__args)...) + // { + // void* __p = _M_refcount._M_get_deleter(typeid(__tag)); + // _M_ptr = static_cast<_Tp*>(__p); + // __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr); + // } + + // __weak_ptr::lock() + __shared_ptr(const __weak_ptr<__libfund_v1<_Tp>, _Lp>& __r, + std::nothrow_t) + : _Base_type(__r._M_get_base(), std::nothrow) + { } + + private: + template friend class __weak_ptr; + template friend class __shared_ptr; + + // TODO + template + friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; + }; + + // weak_ptr specialization for __shared_ptr array + template + class __weak_ptr<__libfund_v1<_Tp>, _Lp> + : __weak_ptr, _Lp> + { + template + using _Compatible = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>; + + using _Base_type = __weak_ptr>; + + _Base_type& _M_get_base() { return *this;} + const _Base_type& _M_get_base() const { return *this; } + + public: + using element_type = remove_extent_t<_Tp>; + + constexpr __weak_ptr() noexcept + : _Base_type() + { } + + __weak_ptr(const __weak_ptr&) noexcept = default; + + ~__weak_ptr() = default; + + template> + __weak_ptr(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + : _Base_type(__r._M_get_base()) + { } + + template> + __weak_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + : _Base_type(__r._M_get_base()) + { } + + __weak_ptr(__weak_ptr&& __r) noexcept + : _Base_type(std::move(__r)) + { } + + template> + __weak_ptr(__weak_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept + : _Base_type(std::move(__r._M_get_base())) + { } + + __weak_ptr& + operator=(const __weak_ptr& __r) noexcept = default; + + template + __weak_ptr& + operator=(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept + { + this->_Base_type::operator=(__r._M_get_base()); + return *this; + } + + template + __weak_ptr& + operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept + { + this->_Base_type::operator=(__r._M_get_base()); + return *this; + } + + __weak_ptr& + operator=(__weak_ptr&& __r) noexcept + { + this->_Base_type::operator=(std::move(__r)); + return *this; + } + + template + __weak_ptr& + operator=(__weak_ptr<_Tp1, _Lp>&& __r) noexcept + { + this->_Base_type::operator=(std::move(__r._M_get_base())); + return *this; + } + + void + swap(__weak_ptr& __other) noexcept + { this->_Base_type::swap(__other); } + + template + bool + owner_before(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __rhs) const + { return _Base_type::owner_before(__rhs._M_get_base()); } + + template + bool + owner_before(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __rhs) const + { return _Base_type::owner_before(__rhs._M_get_base()); } + + __shared_ptr<__libfund_v1<_Tp>, _Lp> + lock() const noexcept // should not be element_type + { return __shared_ptr<__libfund_v1<_Tp>, _Lp>(*this, std::nothrow); } + + using _Base_type::use_count; + using _Base_type::expired; + using _Base_type::reset; + + private: + // Used by __enable_shared_from_this. + void + _M_assign(element_type* __ptr, + const __shared_count<_Lp>& __refcount) noexcept + { this->_Base_type::_M_assign(__ptr, __refcount); } + + template friend class __shared_ptr; + template friend class __weak_ptr; + friend class __enable_shared_from_this<_Tp, _Lp>; + friend class experimental::enable_shared_from_this<_Tp>; + friend class enable_shared_from_this<_Tp>; + }; + +_GLIBCXX_END_NAMESPACE_VERSION + +namespace experimental +{ +inline namespace fundamentals_v2 +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + // 8.2.1 + + template class shared_ptr; + template class weak_ptr; + + template + using __shared_ptr = std::__shared_ptr<__libfund_v1<_Tp>, _Lp>; + + template + using __weak_ptr = std::__weak_ptr<__libfund_v1<_Tp>, _Lp>; + + template + class shared_ptr : public __shared_ptr<_Tp> + { + template + using _Compatible = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>; + + using _Base_type = __shared_ptr<_Tp>; + + public: + using element_type = typename _Base_type::element_type; + + // 8.2.1.1, shared_ptr constructors + constexpr shared_ptr() noexcept = default; + + template + explicit shared_ptr(_Tp1* __p) : _Base_type(__p) { } + + template + shared_ptr(_Tp1* __p, _Deleter __d) + : _Base_type(__p, __d) { } + + template + shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a) + : _Base_type(__p, __d, __a) { } + + template + shared_ptr(nullptr_t __p, _Deleter __d) + : _Base_type(__p, __d) { } + + template + shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) + : _Base_type(__p, __d, __a) { } + + template + shared_ptr(const shared_ptr<_Tp1>& __r, element_type* __p) noexcept + : _Base_type(__r, __p) { } + + shared_ptr(const shared_ptr& __r) noexcept + : _Base_type(__r) { } + + template> + shared_ptr(const shared_ptr<_Tp1>& __r) noexcept + : _Base_type(__r) { } + + shared_ptr(const shared_ptr<_Tp>&& __r) noexcept + : _Base_type(std::move(__r)) { } + + template> + shared_ptr(shared_ptr<_Tp1>&& __r) noexcept + : _Base_type(std::move(__r)) { } + + template + explicit shared_ptr(const weak_ptr<_Tp1>& __r) + : _Base_type(__r) { } + +#if _GLIBCXX_USE_DEPRECATED + template + shared_ptr(std::auto_ptr<_Tp1>&& __r) + : _Base_type() { } // TODO +#endif + + template::pointer>>> + shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r) + : _Base_type(std::move(__r)) { } + + constexpr shared_ptr(nullptr_t __p) + : _Base_type(__p) { } + + // C++14 §20.8.2.2 + ~shared_ptr() = default; + + // C++14 §20.8.2.3 + shared_ptr& operator=(const shared_ptr&) noexcept = default; + + template + shared_ptr& + operator=(const shared_ptr<_Tp1>& __r) noexcept + { + _Base_type::operator=(__r); + return *this; + } + + shared_ptr& + operator=(shared_ptr&& __r) noexcept + { + _Base_type::operator=(std::move(__r)); + return *this; + } + + template + shared_ptr& + operator=(shared_ptr<_Tp1>&& __r) noexcept + { + _Base_type::operator=(std::move(__r)); + return *this; + } + +#if _GLIBCXX_USE_DEPRECATED + template + shared_ptr& + operator=(std::auto_ptr<_Tp1>&& __r) + { + __shared_ptr<_Tp>::operator=(std::move(__r)); + return *this; + } +#endif + + template + shared_ptr& + operator=(unique_ptr<_Tp1, _Del>&& __r) + { + _Base_type::operator=(std::move(__r)); + return *this; + } + + // C++14 §20.8.2.2.4 + // swap & reset + // 8.2.1.2 shared_ptr observers + // in __shared_ptr + + private: + template + shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, + _Args&&... __args) + : _Base_type(__tag, __a, std::forward<_Args>(__args)...) + { } + + template + friend shared_ptr<_Tp1> + allocate_shared(const _Alloc& __a, _Args&&... __args); + + shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) + : _Base_type(__r, std::nothrow) { } + + friend class weak_ptr<_Tp>; + }; + + // C++14 §20.8.2.2.7 //DOING + template + bool operator==(const shared_ptr<_Tp1>& __a, + const shared_ptr<_Tp2>& __b) noexcept + { return __a.get() == __b.get(); } + + template + inline bool + operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept + { return !__a; } + + template + inline bool + operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept + { return !__a; } + + template + inline bool + operator!=(const shared_ptr<_Tp1>& __a, + const shared_ptr<_Tp2>& __b) noexcept + { return __a.get() != __b.get(); } + + template + inline bool + operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept + { return (bool)__a; } + + template + inline bool + operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept + { return (bool)__a; } + + template + inline bool + operator<(const shared_ptr<_Tp1>& __a, + const shared_ptr<_Tp2>& __b) noexcept + { + using __elem_t1 = typename shared_ptr<_Tp1>::element_type; + using __elem_t2 = typename shared_ptr<_Tp2>::element_type; + using _CT = common_type_t<__elem_t1*, __elem_t2*>; + return std::less<_CT>()(__a.get(), __b.get()); + } + + template + inline bool + operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return std::less<__elem_t>()(__a.get(), nullptr); + } + + template + inline bool + operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return std::less<__elem_t*>()(nullptr, __a.get()); + } + + template + inline bool + operator<=(const shared_ptr<_Tp1>& __a, + const shared_ptr<_Tp2>& __b) noexcept + { return !(__b < __a); } + + template + inline bool + operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept + { return !(nullptr < __a); } + + template + inline bool + operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept + { return !(__a < nullptr); } + + template + inline bool + operator>(const shared_ptr<_Tp1>& __a, + const shared_ptr<_Tp2>& __b) noexcept + { return (__b < __a); } + + template + inline bool + operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return std::less<__elem_t*>()(nullptr, __a.get()); + } + + template + inline bool + operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return std::less<__elem_t*>()(__a.get(), nullptr); + } + + template + inline bool + operator>=(const shared_ptr<_Tp1>& __a, + const shared_ptr<_Tp2>& __b) noexcept + { return !(__a < __b); } + + template + inline bool + operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept + { return !(__a < nullptr); } + + template + inline bool + operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept + { return !(nullptr < __a); } + + // C++14 §20.8.2.2.8 + template + inline void + swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept + { __a.swap(__b); } + + // 8.2.1.3, shared_ptr casts + template + inline shared_ptr<_Tp> + static_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return shared_ptr<_Tp>(__r, static_cast<__elem_t*>(__r.get())); + } + + template + inline shared_ptr<_Tp> + dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + if (_Tp* __p = dynamic_cast<__elem_t*>(__r.get())) + return shared_ptr<_Tp>(__r, __p); + return shared_ptr<_Tp>(); + } + + template + inline shared_ptr<_Tp> + const_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return shared_ptr<_Tp>(__r, const_cast<__elem_t*>(__r.get())); + } + + template + inline shared_ptr<_Tp> + reinterpret_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept + { + using __elem_t = typename shared_ptr<_Tp>::element_type; + return shared_ptr<_Tp>(__r, reinterpret_cast<__elem_t*>(__r.get())); + } + + // C++14 §20.8.2.3 + template + class weak_ptr : public __weak_ptr<_Tp> + { + template + using _Compatible = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>; + + using _Base_type = __weak_ptr<_Tp>; + + public: + constexpr weak_ptr() noexcept = default; + + template> + weak_ptr(const shared_ptr<_Tp1>& __r) noexcept + : _Base_type(__r) { } + + weak_ptr(const weak_ptr&) noexcept = default; + + template> + weak_ptr(const weak_ptr<_Tp1>& __r) noexcept + : _Base_type(__r) { } + + weak_ptr(weak_ptr&&) noexcept = default; + + template> + weak_ptr(weak_ptr<_Tp1>&& __r) noexcept + : _Base_type(std::move(__r)) { } + + weak_ptr& + operator=(const weak_ptr& __r) noexcept = default; + + template + weak_ptr& + operator=(const weak_ptr<_Tp1>& __r) noexcept + { + this->_Base_type::operator=(__r); + return *this; + } + + template + weak_ptr& + operator=(const shared_ptr<_Tp1>& __r) noexcept + { + this->_Base_type::operator=(__r); + return *this; + } + + weak_ptr& + operator=(weak_ptr&& __r) noexcept = default; + + template + weak_ptr& + operator=(weak_ptr<_Tp1>&& __r) noexcept + { + this->_Base_type::operator=(std::move(__r)); + return *this; + } + + shared_ptr<_Tp> + lock() const noexcept + { return shared_ptr<_Tp>(*this, std::nothrow); } + + friend class enable_shared_from_this<_Tp>; + }; + + // C++14 §20.8.2.3.6 + template + inline void + swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept + { __a.swap(__b); } + + /// C++14 §20.8.2.2.10 + template + inline _Del* + get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept + { return std::get_deleter<_Del>(__p); } + + // C++14 §20.8.2.2.11 + template + inline std::basic_ostream<_Ch, _Tr>& + operator<<(std::basic_ostream<_Ch, _Tr>& __os, + const __shared_ptr<_Tp, _Lp>& __p) + { + __os << __p.get(); + return __os; + } + + // C++14 §20.8.2.4 + template class owner_less; + + /// Partial specialization of owner_less for shared_ptr. + template + struct owner_less> + : public _Sp_owner_less, weak_ptr<_Tp>> + { }; + + /// Partial specialization of owner_less for weak_ptr. + template + struct owner_less> + : public _Sp_owner_less, shared_ptr<_Tp>> + { }; + + template<> + class owner_less + { + template + bool + operator()(shared_ptr<_Tp> const& __lhs, + shared_ptr<_Up> const& __rhs) const + { return __lhs.owner_before(__rhs); } + + template + bool + operator()(shared_ptr<_Tp> const& __lhs, + weak_ptr<_Up> const& __rhs) const + { return __lhs.owner_before(__rhs); } + + template + bool + operator()(weak_ptr<_Tp> const& __lhs, + shared_ptr<_Up> const& __rhs) const + { return __lhs.owner_before(__rhs); } + + template + bool + operator()(weak_ptr<_Tp> const& __lhs, + weak_ptr<_Up> const& __rhs) const + { return __lhs.owner_before(__rhs); } + + typedef void is_transparent; + }; + + // C++14 §20.8.2.6 + template + inline bool + atomic_is_lock_free(const shared_ptr<_Tp>* __p) + { return std::atomic_is_lock_free<_Tp, __default_lock_policy>(__p); } + + template + shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p) + { return std::atomic_load<_Tp>(__p); } + + template + shared_ptr<_Tp> + atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order __mo) + { return std::atomic_load_explicit<_Tp>(__p, __mo); } + + template + void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) + { return std::atomic_store<_Tp>(__p, __r); } + + template + shared_ptr<_Tp> + atomic_store_explicit(const shared_ptr<_Tp>* __p, + shared_ptr<_Tp> __r, + memory_order __mo) + { return std::atomic_store_explicit<_Tp>(__p, __r, __mo); } + + template + void atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) + { return std::atomic_exchange<_Tp>(__p, __r); } + + template + shared_ptr<_Tp> + atomic_exchange_explicit(const shared_ptr<_Tp>* __p, + shared_ptr<_Tp> __r, + memory_order __mo) + { return std::atomic_exchange_explicit<_Tp>(__p, __r, __mo); } + + template + bool atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, + shared_ptr<_Tp>* __v, + shared_ptr<_Tp> __w) + { return std::atomic_compare_exchange_weak<_Tp>(__p, __v, __w); } + + template + bool atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, + shared_ptr<_Tp>* __v, + shared_ptr<_Tp> __w) + { return std::atomic_compare_exchange_strong<_Tp>(__p, __v, __w); } + + template + bool atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, + shared_ptr<_Tp>* __v, + shared_ptr<_Tp> __w, + memory_order __success, + memory_order __failure) + { return std::atomic_compare_exchange_weak_explicit<_Tp>(__p, __v, __w, + __success, + __failure); } + + template + bool atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, + shared_ptr<_Tp>* __v, + shared_ptr<_Tp> __w, + memory_order __success, + memory_order __failure) + { return std::atomic_compare_exchange_strong_explicit<_Tp>(__p, __v, __w, + __success, + __failure); } + + //enable_shared_from_this + template + class enable_shared_from_this + { + protected: + constexpr enable_shared_from_this() noexcept { } + + enable_shared_from_this(const enable_shared_from_this&) noexcept { } + + enable_shared_from_this& + operator=(const enable_shared_from_this&) noexcept + { return *this; } + + ~enable_shared_from_this() { } + + public: + shared_ptr<_Tp> + shared_from_this() + { return shared_ptr<_Tp>(this->_M_weak_this); } + + shared_ptr + shared_from_this() const + { return shared_ptr(this->_M_weak_this); } + + private: + template + void + _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept + { _M_weak_this._M_assign(__p, __n); } + + template + friend void + __enable_shared_from_this_helper(const __shared_count<>& __pn, + const enable_shared_from_this* __pe, + const _Tp1* __px) noexcept + { + if(__pe != 0) + __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); + } + + mutable weak_ptr<_Tp> _M_weak_this; + }; + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace fundamentals_v2 +} // namespace experimental + +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + /// std::hash specialization for shared_ptr. + template + struct hash> + : public __hash_base> + { + size_t + operator()(const experimental::shared_ptr<_Tp>& __s) const noexcept + { return std::hash<_Tp*>()(__s.get()); } + }; + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std + +#endif // __cplusplus <= 201103L + +#endif // _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H diff --git a/libstdc++-v3/include/experimental/memory b/libstdc++-v3/include/experimental/memory index 08a6b33c7c1..b8c7849e11f 100644 --- a/libstdc++-v3/include/experimental/memory +++ b/libstdc++-v3/include/experimental/memory @@ -43,6 +43,7 @@ #include #include #include +#include namespace std _GLIBCXX_VISIBILITY(default) { @@ -247,7 +248,6 @@ template { return hash::type> {}(__t.get()); } - }; } // namespace std diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/assign/assign.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/assign/assign.cc new file mode 100644 index 00000000000..7656c98c327 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/assign/assign.cc @@ -0,0 +1,120 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + + +#include +#include + +struct A +{ + A() { ++ctor_count; } + virtual ~A() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long A::ctor_count = 0; +long A::dtor_count = 0; + +struct B : A +{ + B() { ++ctor_count; } + virtual ~B() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long B::ctor_count = 0; +long B::dtor_count = 0; + +struct reset_count_struct +{ + ~reset_count_struct() + { + A::ctor_count = 0; + A::dtor_count = 0; + B::ctor_count = 0; + B::dtor_count = 0; + } +}; + +// C++14 §20.8.2.2.3 shared_ptr assignment + +void +test01() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a; + std::experimental::shared_ptr a1; + std::experimental::shared_ptr a2; + + a = std::experimental::shared_ptr (); + VERIFY( a.get() == 0 ); + VERIFY( A::ctor_count == 0 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + a = std::experimental::shared_ptr (new A[5]); + VERIFY( a.get() != 0 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + a1 = std::experimental::shared_ptr (new A[5]); + VERIFY( a1.get() != 0 ); + VERIFY( A::ctor_count == 10 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + a2 = std::experimental::shared_ptr (new B[5]); + VERIFY( a2.get() != 0 ); + VERIFY( A::ctor_count == 15 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 5 ); + VERIFY( B::dtor_count == 0 ); +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr p(new A[5]); + std::experimental::shared_ptr p1; + std::experimental::shared_ptr p2; + + p1 = p; + VERIFY( p.get() == p1.get() ); + + p2 = p1; + VERIFY( p1.get() == p2.get() ); +} + +int +main() +{ + test01(); + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cast/cast.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cast/cast.cc new file mode 100644 index 00000000000..18c2ba4a4ec --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cast/cast.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1.3 shared_ptr casts [memory.smartptr.shared.cast] + +#include +#include + +// { dg-do compile } + +struct A { }; + +int +main() +{ + using __gnu_test::check_ret_type; + using std::experimental::shared_ptr; + using std::experimental::static_pointer_cast; + using std::experimental::const_pointer_cast; + using std::experimental::dynamic_pointer_cast; + + shared_ptr spa; + shared_ptr spa1; + + check_ret_type >(static_pointer_cast(spa)); + check_ret_type >(const_pointer_cast(spa1)); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/comparison/comparison.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/comparison/comparison.cc new file mode 100644 index 00000000000..52fc1934ed2 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/comparison/comparison.cc @@ -0,0 +1,84 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + virtual ~A() { } +}; + +struct B : A +{ +}; + +// 20.8.2.2.7 shared_ptr comparison + +int +test01() +{ + bool test __attribute__((unused)) = true; + + // test empty shared_ptrs compare equivalent + std::experimental::shared_ptr p1; + std::experimental::shared_ptr p2; + VERIFY( p1 == p2 ); + VERIFY( !(p1 != p2) ); + VERIFY( !(p1 < p2) && !(p2 < p1) ); + return 0; +} + +int +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr A_default; + + std::experimental::shared_ptr A_from_A(new A[5]); + VERIFY( A_default != A_from_A ); + VERIFY( !(A_default == A_from_A) ); + VERIFY( (A_default < A_from_A) || (A_from_A < A_default) ); + + std::experimental::shared_ptr B_from_B(new B[5]); + VERIFY( B_from_B != A_from_A ); + VERIFY( !(B_from_B == A_from_A) ); + VERIFY( (B_from_B < A_from_A) || (A_from_A < B_from_B) ); + + A_from_A.reset(); + VERIFY( A_default == A_from_A ); + VERIFY( !(A_default != A_from_A) ); + VERIFY( !(A_default < A_from_A) && !(A_from_A < A_default)); + + B_from_B.reset(); + VERIFY( B_from_B == A_from_A ); + VERIFY( !(B_from_B != A_from_A) ); + VERIFY( !(B_from_B < A_from_A) && !(A_from_A < B_from_B) ); + + return 0; +} + +int +main() +{ + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/alias_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/alias_ctor.cc new file mode 100644 index 00000000000..15d1de8e12a --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/alias_ctor.cc @@ -0,0 +1,100 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + A() : i() { } + virtual ~A() { } + int i; +}; + +struct B : A +{ + B() : A(), a() { } + virtual ~B() { } + A a; +}; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Aliasing constructors + +void +test01() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a; + std::experimental::shared_ptr b1(a, &test); + VERIFY( b1.use_count() == 0 ); + VERIFY( a.get() == 0 ); + VERIFY( b1.get() == &test ); + + std::experimental::shared_ptr b2(b1); + VERIFY( b2.use_count() == 0 ); + VERIFY( b1.get() == b2.get() ); +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a(new A[5]); + std::experimental::shared_ptr i1(a, &a[0].i); + VERIFY( i1.use_count() == 2 ); + + std::experimental::shared_ptr i2(i1); + VERIFY( i2.use_count() == 3 ); + VERIFY( i2.get() == &a[0].i ); +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr b(new B); + std::experimental::shared_ptr a1(b, b.get()); + std::experimental::shared_ptr a2(b, &b->a); + VERIFY( a2.use_count() == 3 ); + VERIFY( a1 == b ); + VERIFY( a2 != b ); + VERIFY( a1.get() != a2.get() ); + + std::experimental::shared_ptr a3(a1); + VERIFY( a3 == b ); + + a3 = a2; + VERIFY( a3.get() == &b->a ); +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/alloc_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/alloc_ctor.cc new file mode 100644 index 00000000000..12eb15ccef6 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/alloc_ctor.cc @@ -0,0 +1,73 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include +#include + +using __gnu_test::tracker_allocator_counter; +using __gnu_test::tracker_allocator; + +struct A { }; +void deletefunc(A* p) { delete [] p; } +struct D +{ + void operator()(A* p) { delete [] p; ++delete_count; } + static long delete_count; +}; +long D::delete_count = 0; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Construction with allocator + +int +test01() +{ + bool test __attribute__((unused)) = true; + tracker_allocator_counter::reset(); + + std::experimental::shared_ptr p1(new A[5], deletefunc, tracker_allocator()); + std::size_t const sz = tracker_allocator_counter::get_allocation_count(); + VERIFY( sz > 0 ); + { + std::experimental::shared_ptr p2(p1); + VERIFY( p2.use_count() == 2 ); + VERIFY( tracker_allocator_counter::get_allocation_count() == sz ); + VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 ); + } + VERIFY( p1.use_count() == 1 ); + VERIFY( tracker_allocator_counter::get_allocation_count() == sz); + VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 ); + p1.reset(); + VERIFY( p1.use_count() == 0 ); + VERIFY( tracker_allocator_counter::get_allocation_count() == sz ); + VERIFY( tracker_allocator_counter::get_deallocation_count() == sz ); + + return 0; +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor.cc new file mode 100644 index 00000000000..4c3680ed156 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor.cc @@ -0,0 +1,178 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + A() { ++ctor_count; } + virtual ~A() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long A::ctor_count = 0; +long A::dtor_count = 0; + +struct B : A +{ + B() { ++ctor_count; } + virtual ~B() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long B::ctor_count = 0; +long B::dtor_count = 0; + +void deleter(A* p) { delete [] p; } + +struct reset_count_struct +{ + ~reset_count_struct() + { + A::ctor_count = 0; + A::dtor_count = 0; + B::ctor_count = 0; + B::dtor_count = 0; + } +}; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Copy construction + +int +test01() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1; + std::experimental::shared_ptr a2(a1); + VERIFY( a2.use_count() == 0 ); + VERIFY( A::ctor_count == 0 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + return 0; +} + +int +test02() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1(new A[5]); + std::experimental::shared_ptr a2(a1); + VERIFY( a2.use_count() == 2 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + return 0; +} + +int +test03() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1(new A[5], &deleter); + std::experimental::shared_ptr a2(a1); + VERIFY( a2.use_count() == 2 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + return 0; +} + +int +test04() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1(std::experimental::shared_ptr + (new A[5])); + VERIFY( a1.use_count() == 1 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + return 0; +} + +int +test05() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1(new A[5]); + std::experimental::shared_ptr a2(a1); + + VERIFY( a2.use_count() == 2 ); + VERIFY( a2.get() == a1.get() ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + + return 0; +} + +int +test06() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1(new B); + std::experimental::shared_ptr a2(a1); + + VERIFY( a2.use_count() == 2 ); + VERIFY( a2.get() == a1.get() ); + VERIFY( A::ctor_count == 1 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 1 ); + VERIFY( B::dtor_count == 0 ); + + return 0; +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); + test05(); + test06(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor_neg.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor_neg.cc new file mode 100644 index 00000000000..d3c94cf3405 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/copy_ctor_neg.cc @@ -0,0 +1,59 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + + +#include +#include + + +struct A { virtual ~A() { } }; +struct B : A { }; + + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Copy construction + +void +test01() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a; + a = std::experimental::shared_ptr (new B[3]); // { dg-excess-errors "no matching" } +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a(new A[3]); + std::experimental::shared_ptr spa(a); // { dg-excess-errors "no matching" } +} + +int +main() +{ + test01(); + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/default_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/default_ctor.cc new file mode 100644 index 00000000000..794e86528c0 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/default_ctor.cc @@ -0,0 +1,46 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Default construction +int +test01() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a; + VERIFY( a.get() == 0 ); + + return 0; +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc new file mode 100644 index 00000000000..3c070fe669e --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc @@ -0,0 +1,146 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + A() { ++ctor_count; } + virtual ~A() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long A::ctor_count = 0; +long A::dtor_count = 0; + +struct D +{ + void operator()(A* p) const { delete [] p; ++delete_count; } + static long delete_count; +}; +long D::delete_count = 0; + +struct reset_count_struct +{ + ~reset_count_struct() + { + A::ctor_count = 0; + A::dtor_count = 0; + D::delete_count = 0; + } +}; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Rvalue construction +int test01() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1; + std::experimental::shared_ptr a2(std::move(a1)); + VERIFY( a1.use_count() == 0 ); + VERIFY( a2.use_count() == 0 ); + VERIFY( A::ctor_count == 0 ); + VERIFY( A::dtor_count == 0 ); + + return 0; +} + +int +test02() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a1(new A[5]); + std::experimental::shared_ptr a2(std::move(a1)); + VERIFY( a2.use_count() == 1 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + + return 0; +} + +int +test03() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr b(new A[5], D()); + std::experimental::shared_ptr b1(std::move(b)); + VERIFY( b.use_count() == 0 ); + VERIFY( b1.use_count() == 1 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); + + b1 = std::move(std::experimental::shared_ptr ()); + + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 5 ); + VERIFY( D::delete_count == 1 ); + + return 0; +} + +void +test04() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a(std::move(std::experimental + ::shared_ptr + (new A[5]))); + + VERIFY( a.use_count() == 1 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); +} + +void +test05() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a(std::move(std::experimental + ::shared_ptr + (new A[5]))); + + VERIFY( a.use_count() == 1 ); + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 0 ); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); + test05(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/pointer_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/pointer_ctor.cc new file mode 100644 index 00000000000..d9ae591a41d --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/pointer_ctor.cc @@ -0,0 +1,75 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; +struct B : A { }; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Construction from pointer +int +test01() +{ + bool test __attribute__((unused)) = true; + + A * const a = 0; + std::experimental::shared_ptr p(a); + VERIFY( p.get() == 0 ); + VERIFY( p.use_count() == 1 ); + return 0; +} + +int +test02() +{ + bool test __attribute__((unused)) = true; + + A * const a = new A[5]; + std::experimental::shared_ptr p(a); + VERIFY( p.get() == a ); + VERIFY( p.use_count() == 1 ); + return 0; +} + +int +test03() +{ + bool test __attribute__((unused)) = true; + + B * const b = new B[5]; + std::experimental::shared_ptr p(b); + VERIFY( p.get() == b ); + VERIFY( p.use_count() == 1 ); + + return 0; +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc new file mode 100644 index 00000000000..35fb82f36bf --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc @@ -0,0 +1,60 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +int destroyed = 0; + +struct A : std::experimental::enable_shared_from_this +{ + ~A() { ++destroyed; } +}; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Construction from unique_ptr + +int +test01() +{ + bool test __attribute__((unused)) = true; + + std::unique_ptr up(new A[5]); + std::experimental::shared_ptr sp(std::move(up)); + VERIFY( up.get() == 0 ); + VERIFY( sp.get() != 0 ); + VERIFY( sp.use_count() == 1 ); + + VERIFY( sp->shared_from_this() != nullptr ); + + sp.reset(); + VERIFY( destroyed == 5 ); + + return 0; +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc new file mode 100644 index 00000000000..342b37baa0d --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc @@ -0,0 +1,54 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; + +// 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const] + +// Construction from weak_ptr +int +test01() +{ + bool test __attribute__((unused)) = true; + + A * a = new A[5]; + std::experimental::shared_ptr a1(a); + std::experimental::weak_ptr wa(a1); + std::experimental::shared_ptr a2(wa); + std::experimental::shared_ptr a3 = wa.lock(); + VERIFY( a2.get() == a ); + VERIFY( a3.get() == a ); + VERIFY( a2.use_count() == wa.use_count() ); + VERIFY( a3.use_count() == wa.use_count() ); + + return 0; +} + + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/dest/dest.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/dest/dest.cc new file mode 100644 index 00000000000..989121df909 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/dest/dest.cc @@ -0,0 +1,129 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + A() { ++ctor_count; } + ~A() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long A::ctor_count = 0; +long A::dtor_count = 0; + +struct B : A +{ + B() { ++ctor_count; } + ~B() { ++dtor_count; } + static long ctor_count; + static long dtor_count; +}; +long B::ctor_count = 0; +long B::dtor_count = 0; + +struct D +{ + void operator()(const B* p) { delete [] p; ++delete_count; } + static long delete_count; +}; +long D::delete_count = 0; + +struct reset_count_struct +{ + ~reset_count_struct() + { + A::ctor_count = 0; + A::dtor_count = 0; + B::ctor_count = 0; + B::dtor_count = 0; + D::delete_count = 0; + } +}; + +// 20.8.2.2.2 shared_ptr destructor + +int +test01() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + { + std::experimental::shared_ptr a; + } + VERIFY( A::ctor_count == 0 ); + VERIFY( A::dtor_count == 0 ); + VERIFY( B::ctor_count == 0 ); + VERIFY( B::dtor_count == 0 ); + VERIFY( D::delete_count == 0 ); + + return 0; +} + +int +test02() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + { + std::experimental::shared_ptr a; + a = std::experimental::shared_ptr(new B[5], D()); + } + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 5 ); + VERIFY( B::ctor_count == 5 ); + VERIFY( B::dtor_count == 5 ); + VERIFY( D::delete_count == 1 ); + + return 0; +} + +int +test03() +{ + reset_count_struct __attribute__((unused)) reset; + bool test __attribute__((unused)) = true; + + { + std::experimental::shared_ptr a; + a = std::experimental::shared_ptr(new B[5], D()); + } + VERIFY( A::ctor_count == 5 ); + VERIFY( A::dtor_count == 5 ); + VERIFY( B::ctor_count == 5 ); + VERIFY( B::dtor_count == 5 ); + VERIFY( D::delete_count == 1 ); + + return 0; +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/modifiers/reset.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/modifiers/reset.cc new file mode 100644 index 00000000000..bd1ce1d24b9 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/modifiers/reset.cc @@ -0,0 +1,89 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; +struct B : A { }; +struct D +{ + void operator()(B* p) { delete [] p; ++delete_count; } + static long delete_count; +}; +long D::delete_count = 0; + +// C++14 §20.8.2.2.4 + +// reset +int +test01() +{ + bool test __attribute__((unused)) = true; + + A * const a = new A[5]; + std::experimental::shared_ptr p1(a); + std::experimental::shared_ptr p2(p1); + p1.reset(); + VERIFY( p1.get() == 0 ); + VERIFY( p2.get() == a ); + + return 0; +} + +int +test02() +{ + bool test __attribute__((unused)) = true; + + A * const a = new A[5]; + B * const b = new B[5]; + std::experimental::shared_ptr p1(a); + std::experimental::shared_ptr p2(p1); + p1.reset(b); + VERIFY( p1.get() == b ); + VERIFY( p2.get() == a ); + + return 0; +} + +int +test03() +{ + bool test __attribute__((unused)) = true; + + { + std::experimental::shared_ptr p1; + p1.reset(new B[5], D()); + } + VERIFY( D::delete_count == 1 ); + + return 0; +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/modifiers/swap.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/modifiers/swap.cc new file mode 100644 index 00000000000..758042caf3a --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/modifiers/swap.cc @@ -0,0 +1,53 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; + +// C++14 §20.8.2.2.4 + +// swap +int +test01() +{ + bool test __attribute__((unused)) = true; + + A * const a1 = new A[5]; + A * const a2 = new A[5]; + std::experimental::shared_ptr p1(a1); + std::experimental::shared_ptr p2(a2); + p1.swap(p2); + VERIFY( p1.get() == a2 ); + VERIFY( p2.get() == a1 ); + + return 0; +} + + + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/bool_conv.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/bool_conv.cc new file mode 100644 index 00000000000..7e4c750a3cf --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/bool_conv.cc @@ -0,0 +1,74 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; + +// 8.2.1.2 shared_ptr observers [memory.smartptr.shared.obs] + +// Conversion to bool +void +test01() +{ + bool test __attribute__((unused)) = true; + + const std::experimental::shared_ptr p1; + VERIFY( static_cast(p1) == false ); + const std::experimental::shared_ptr p2(p1); + VERIFY( static_cast(p2) == false ); +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr p1(new A[5]); + VERIFY( static_cast(p1) ); + std::experimental::shared_ptr p2(p1); + VERIFY( static_cast(p2) ); + p1.reset(); + VERIFY( !static_cast(p1) ); + VERIFY( static_cast(p2) ); +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr p1(new A[5]); + std::experimental::shared_ptr p2(p1); + p2.reset(new A[5]); + VERIFY( static_cast(p1) ); + VERIFY( static_cast(p2) ); +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/operators.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/operators.cc new file mode 100644 index 00000000000..d32c89978aa --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/operators.cc @@ -0,0 +1,93 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + A() : i() {} + int i; +}; + +// 8.2.1.2 shared_ptr observers [memory.smartptr.shared.obs] + +// get +void +test01() +{ + bool test __attribute__((unused)) = true; + + A * const a = new A[5]; + const std::experimental::shared_ptr p(a); + VERIFY( p.get() == a ); +} + +// operator [] +int +test02() +{ + A * p = new A[5]; + std::experimental::shared_ptr a(p); + + for(int j = 0; j < 5; j++) + { a[j].i = j; } + + VERIFY(a.get() == p); + VERIFY(a.use_count() == 1); + + for(int j = 0; j < 5; j++) + { VERIFY(a[j].i == j); } + + return 0; +} + +// operator* +void +test03() +{ + bool test __attribute__((unused)) = true; + + A * const a = new A[5]; + const std::experimental::shared_ptr p(a); + VERIFY( p.get() == a ); +} + +// operator-> +void +test04() +{ + bool test __attribute__((unused)) = true; + + A * const a = new A[5]; + const std::experimental::shared_ptr p(a); + VERIFY( &p[0].i == &a[0].i ); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/owner_before.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/owner_before.cc new file mode 100644 index 00000000000..24a658a96f8 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/owner_before.cc @@ -0,0 +1,86 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A +{ + int i; + virtual ~A() { } +}; + +struct B : A { }; + +// 8.2.1.2 shared_ptr observers [memory.smartptr.shared.obs] + +// owner_before +void +test01() +{ + bool test __attribute__((unused)) = true; + + // test empty shared_ptrs compare equivalent + std::experimental::shared_ptr p1; + std::experimental::shared_ptr p2; + VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) ); +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr a0; + + std::experimental::shared_ptr a1(new A[5]); + VERIFY( a1.owner_before(a0) || a0.owner_before(a1) ); + VERIFY( !(a1.owner_before(a0) && a0.owner_before(a1)) ); + + std::experimental::shared_ptr b1(new B[5]); + VERIFY( a1.owner_before(b1) || b1.owner_before(a1) ); + VERIFY( !(a1.owner_before(b1) && b1.owner_before(a1)) ); + + std::experimental::shared_ptr a2(a1); + VERIFY( !a1.owner_before(a2) && !a2.owner_before(a1) ); + + std::experimental::weak_ptr w1(a1); + VERIFY( !a1.owner_before(w1) && !w1.owner_before(a1) ); +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr p1(new A[5]); + std::experimental::shared_ptr p2(p1, &p1[0].i); + VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) ); +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/use_count.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/use_count.cc new file mode 100644 index 00000000000..fc48bf2160d --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/observers/use_count.cc @@ -0,0 +1,73 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 8.2.1 Class template shared_ptr [memory.smartptr.shared] + +#include +#include + +struct A { }; +struct B : A { }; + +// 8.2.1.2 shared_ptr observers [memory.smartptr.shared.obs] + +// use_count +void +test01() +{ + bool test __attribute__((unused)) = true; + + const std::experimental::shared_ptr p1; + VERIFY( p1.use_count() == 0 ); + const std::experimental::shared_ptr p2(p1); + VERIFY( p1.use_count() == 0 ); +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr p1(new A[5]); + std::experimental::shared_ptr p2(p1); + p1.reset(); + VERIFY( p1.use_count() == 0 ); + VERIFY( p2.use_count() == 1 ); +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::experimental::shared_ptr p1(new A[5]); + std::experimental::shared_ptr p2(p1); + p2.reset(new B[5]); + VERIFY( p1.use_count() == 1 ); + VERIFY( p2.use_count() == 1 ); +} + +int +main() +{ + test01(); + test02(); + test03(); + return 0; +}