Commit aaf0ca6f by Jonathan Wakely Committed by Jonathan Wakely

boost_shared_ptr.h: Add support for allocators, aliasing, make_shared and rvalue-references.

2007-12-15  Jonathan Wakely  <jwakely-gcc@gmail.com>

	* include/tr1_impl/boost_shared_ptr.h: Add support for allocators,
	aliasing, make_shared and rvalue-references. Move __shared_count
	and _Sp_counted_* classes to new headers.
	* include/tr1_impl/boost_sp_counted_base.h: New.
	* include/bits/boost_sp_shared_count.h: New.
	* include/tr1/boost_sp_shared_count.h: New.
	* include/std/memory, include/tr1/memory: Include new headers.
	* include/Makefile.am: Adjust.
	* include/Makefile.in: Regenerate.
	* docs/html/documentation.html: Link to shared_ptr notes.
	* docs/html/20_util/shared_ptr.html: New.
	* docs/html/17_intro/c++0x_status.html: Update shared_ptr status.
	* testsuite/20_util/shared_ptr/cons/alias.cc: New.
	* testsuite/20_util/shared_ptr/cons/alloc.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/move.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/move.cc: Likewise.
	* testsuite/20_util/shared_ptr/creation/alloc.cc: Likewise.
	* testsuite/20_util/shared_ptr/creation/make.cc: Likewise.
	* testsuite/20_util/shared_ptr/creation/dr402.cc: Likewise.
	* testsuite/20_util/shared_ptr/modifiers/reset_alloc.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/assign.cc: Duplicate tr1 test.
	* testsuite/20_util/shared_ptr/assign/auto_ptr.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/auto_ptr_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/auto_ptr_rvalue_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/dr541.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/shared_ptr.cc: Likewise.
	* testsuite/20_util/shared_ptr/assign/shared_ptr_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/casts/1.cc: Likewise.
	* testsuite/20_util/shared_ptr/comparison/cmp.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/auto_ptr.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/auto_ptr_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/copy.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/default.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/pointer.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/weak_ptr.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/weak_ptr_expired.cc: Likewise.
	* testsuite/20_util/shared_ptr/dest/dest.cc: Likewise.
	* testsuite/20_util/shared_ptr/misc/24595.cc: Likewise.
	* testsuite/20_util/shared_ptr/misc/io.cc: Likewise.
	* testsuite/20_util/shared_ptr/misc/swap.cc: Likewise.
	* testsuite/20_util/shared_ptr/modifiers/24805.cc: Likewise.
	* testsuite/20_util/shared_ptr/modifiers/reset.cc: Likewise.
	* testsuite/20_util/shared_ptr/modifiers/reset_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/modifiers/swap.cc: Likewise.
	* testsuite/20_util/shared_ptr/modifiers/swap_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/observers/bool_conv.cc: Likewise.
	* testsuite/20_util/shared_ptr/observers/get.cc: Likewise.
	* testsuite/20_util/shared_ptr/observers/unique.cc: Likewise.
	* testsuite/20_util/shared_ptr/observers/use_count.cc: Likewise.
	* testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc:
	Likewise.
	* testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc: Likewise.
	* testsuite/20_util/shared_ptr/requirements/explicit_instantiation/
	1.cc: Likewise.
	* testsuite/20_util/shared_ptr/requirements/explicit_instantiation/
	2.cc: Likewise.
	* testsuite/20_util/shared_ptr/requirements/explicit_instantiation.cc:
	Remove.
	* testsuite/20_util/weak_ptr/lock/1.cc: Duplicate tr1 test.
	* testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc:
	Likewise.
	* testsuite/20_util/weak_ptr/requirements/explicit_instantiation/2.cc:
	Likewise.
	* testsuite/20_util/weak_ptr/requirements/explicit_instantiation.cc:
	Remove.

From-SVN: r130977
parent 5f0686bc
......@@ -567,7 +567,7 @@ particular release.
<td>done</td>
<td></td>
<td></td>
<td><a href="tr1.html#1">1</a></td>
<td><a href="#1">1</a></td>
</tr>
<tr>
<td>20.6.6.2.1</td>
......@@ -611,6 +611,16 @@ particular release.
</tr>
<tr>
<td>20.6.6.2.6</td>
<td><code>shared_ptr</code> creation</td>
<td>done</td>
<td></td>
<td></td>
<td>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2351.htm">N2351</a>
</td>
</tr>
<tr>
<td>20.6.6.2.7</td>
<td><code>shared_ptr</code> comparison</td>
<td>done</td>
<td></td>
......@@ -618,7 +628,7 @@ particular release.
<td></td>
</tr>
<tr>
<td>20.6.6.2.7</td>
<td>20.6.6.2.8</td>
<td><code>shared_ptr</code> I/O</td>
<td>done</td>
<td></td>
......@@ -626,7 +636,7 @@ particular release.
<td></td>
</tr>
<tr>
<td>20.6.6.2.8</td>
<td>20.6.6.2.9</td>
<td><code>shared_ptr</code> specialized algorithms</td>
<td>done</td>
<td></td>
......@@ -634,7 +644,7 @@ particular release.
<td></td>
</tr>
<tr>
<td>20.6.6.2.9</td>
<td>20.6.6.2.10</td>
<td><code>shared_ptr</code> casts</td>
<td>done</td>
<td></td>
......@@ -642,7 +652,7 @@ particular release.
<td></td>
</tr>
<tr>
<td>20.6.6.2.10</td>
<td>20.6.6.2.11</td>
<td><code>get_deleter</code></td>
<td>done</td>
<td></td>
......
......@@ -143,6 +143,12 @@ href="http://gcc.gnu.org/svn.html">web</a>.
<li><a href="20_util/howto.html#2"><code>auto_ptr</code> inside container classes</a></li>
</ul>
</li>
<li>shared_ptr
<ul>
<li><a href="20_util/shared_ptr.html">Notes on the <code>shared_ptr</code>
implementation</a></li>
</ul>
</li>
</ul>
</li>
</ul>
......@@ -330,7 +336,7 @@ href="http://gcc.gnu.org/svn.html">web</a>.
<hr />
<br />
<h2><a name="7" href="faq/index.html">Frequently Asked Questions</a></h2
<h2><a name="7" href="faq/index.html">Frequently Asked Questions</a></h2>
<hr />
<br />
......
......@@ -77,6 +77,7 @@ bits_headers = \
${bits_srcdir}/basic_string.h \
${bits_srcdir}/basic_string.tcc \
${bits_srcdir}/boost_concept_check.h \
${bits_srcdir}/boost_sp_shared_count.h \
${bits_srcdir}/char_traits.h \
${bits_srcdir}/codecvt.h \
${bits_srcdir}/concept_check.h \
......@@ -515,6 +516,7 @@ tr1_headers = \
${tr1_srcdir}/array \
${tr1_srcdir}/bessel_function.tcc \
${tr1_srcdir}/beta_function.tcc \
${tr1_srcdir}/boost_sp_shared_count.h \
${tr1_srcdir}/ccomplex \
${tr1_srcdir}/cctype \
${tr1_srcdir}/cfenv \
......@@ -575,6 +577,7 @@ tr1_impl_builddir = ./tr1_impl
tr1_impl_headers = \
${tr1_impl_srcdir}/array \
${tr1_impl_srcdir}/boost_shared_ptr.h \
${tr1_impl_srcdir}/boost_sp_counted_base.h \
${tr1_impl_srcdir}/cctype \
${tr1_impl_srcdir}/cfenv \
${tr1_impl_srcdir}/cinttypes \
......
......@@ -326,6 +326,7 @@ bits_headers = \
${bits_srcdir}/basic_string.h \
${bits_srcdir}/basic_string.tcc \
${bits_srcdir}/boost_concept_check.h \
${bits_srcdir}/boost_sp_shared_count.h \
${bits_srcdir}/char_traits.h \
${bits_srcdir}/codecvt.h \
${bits_srcdir}/concept_check.h \
......@@ -762,6 +763,7 @@ tr1_headers = \
${tr1_srcdir}/array \
${tr1_srcdir}/bessel_function.tcc \
${tr1_srcdir}/beta_function.tcc \
${tr1_srcdir}/boost_sp_shared_count.h \
${tr1_srcdir}/ccomplex \
${tr1_srcdir}/cctype \
${tr1_srcdir}/cfenv \
......@@ -821,6 +823,7 @@ tr1_impl_builddir = ./tr1_impl
tr1_impl_headers = \
${tr1_impl_srcdir}/array \
${tr1_impl_srcdir}/boost_shared_ptr.h \
${tr1_impl_srcdir}/boost_sp_counted_base.h \
${tr1_impl_srcdir}/cctype \
${tr1_impl_srcdir}/cfenv \
${tr1_impl_srcdir}/cinttypes \
......
......@@ -75,12 +75,16 @@
# include <backward/auto_ptr.h>
# endif
# if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
# include <tr1_impl/boost_sp_counted_base.h>
# include <bits/boost_sp_shared_count.h>
# include <tr1_impl/boost_shared_ptr.h>
# else
# define _GLIBCXX_INCLUDE_AS_CXX0X
# define _GLIBCXX_BEGIN_NAMESPACE_TR1
# define _GLIBCXX_END_NAMESPACE_TR1
# define _GLIBCXX_TR1
# include <tr1_impl/boost_sp_counted_base.h>
# include <bits/boost_sp_shared_count.h>
# include <tr1_impl/boost_shared_ptr.h>
# undef _GLIBCXX_TR1
# undef _GLIBCXX_END_NAMESPACE_TR1
......
// <tr1/boost_sp_shared_count.h> -*- C++ -*-
// Copyright (C) 2007 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
// shared_count.hpp
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
// shared_ptr.hpp
// Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
// Copyright (C) 2001, 2002, 2003 Peter Dimov
// weak_ptr.hpp
// Copyright (C) 2001, 2002, 2003 Peter Dimov
// enable_shared_from_this.hpp
// Copyright (C) 2002 Peter Dimov
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// GCC Note: based on version 1.32.0 of the Boost library.
/** @file tr1/boost_sp_shared_count.h
* This is an internal header file, included by other library headers.
* You should not attempt to use it directly.
*/
#if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
# error TR1 header cannot be included from C++0x header
#endif
namespace std
{
namespace tr1
{
template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
class _Sp_counted_base_impl
: public _Sp_counted_base<_Lp>
{
public:
/**
* @brief
* @pre __d(__p) must not throw.
*/
_Sp_counted_base_impl(_Ptr __p, _Deleter __d)
: _M_ptr(__p), _M_del(__d) { }
virtual void
_M_dispose() // nothrow
{ _M_del(_M_ptr); }
virtual void*
_M_get_deleter(const std::type_info& __ti)
{ return __ti == typeid(_Deleter) ? &_M_del : 0; }
private:
_Sp_counted_base_impl(const _Sp_counted_base_impl&);
_Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
_Ptr _M_ptr; // copy constructor must not throw
_Deleter _M_del; // copy constructor must not throw
};
template<_Lock_policy _Lp = __default_lock_policy>
class __weak_count;
template<typename _Tp>
struct _Sp_deleter
{
typedef void result_type;
typedef _Tp* argument_type;
void operator()(_Tp* __p) const { delete __p; }
};
template<_Lock_policy _Lp = __default_lock_policy>
class __shared_count
{
public:
__shared_count()
: _M_pi(0) // nothrow
{ }
template<typename _Ptr>
__shared_count(_Ptr __p) : _M_pi(0)
{
try
{
typedef typename std::tr1::remove_pointer<_Ptr>::type _Tp;
_M_pi = new _Sp_counted_base_impl<_Ptr, _Sp_deleter<_Tp>, _Lp>(
__p, _Sp_deleter<_Tp>());
}
catch(...)
{
delete __p;
__throw_exception_again;
}
}
template<typename _Ptr, typename _Deleter>
__shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
{
try
{
_M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
}
catch(...)
{
__d(__p); // Call _Deleter on __p.
__throw_exception_again;
}
}
// Special case for auto_ptr<_Tp> to provide the strong guarantee.
template<typename _Tp>
explicit
__shared_count(std::auto_ptr<_Tp>& __r)
: _M_pi(new _Sp_counted_base_impl<_Tp*,
_Sp_deleter<_Tp>, _Lp >(__r.get(), _Sp_deleter<_Tp>()))
{ __r.release(); }
// Throw bad_weak_ptr when __r._M_get_use_count() == 0.
explicit
__shared_count(const __weak_count<_Lp>& __r);
~__shared_count() // nothrow
{
if (_M_pi != 0)
_M_pi->_M_release();
}
__shared_count(const __shared_count& __r)
: _M_pi(__r._M_pi) // nothrow
{
if (_M_pi != 0)
_M_pi->_M_add_ref_copy();
}
__shared_count&
operator=(const __shared_count& __r) // nothrow
{
_Sp_counted_base<_Lp>* __tmp = __r._M_pi;
if (__tmp != _M_pi)
{
if (__tmp != 0)
__tmp->_M_add_ref_copy();
if (_M_pi != 0)
_M_pi->_M_release();
_M_pi = __tmp;
}
return *this;
}
void
_M_swap(__shared_count& __r) // nothrow
{
_Sp_counted_base<_Lp>* __tmp = __r._M_pi;
__r._M_pi = _M_pi;
_M_pi = __tmp;
}
long
_M_get_use_count() const // nothrow
{ return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
bool
_M_unique() const // nothrow
{ return this->_M_get_use_count() == 1; }
friend inline bool
operator==(const __shared_count& __a, const __shared_count& __b)
{ return __a._M_pi == __b._M_pi; }
friend inline bool
operator<(const __shared_count& __a, const __shared_count& __b)
{ return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
void*
_M_get_deleter(const std::type_info& __ti) const
{ return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
private:
friend class __weak_count<_Lp>;
_Sp_counted_base<_Lp>* _M_pi;
};
}
}
......@@ -54,12 +54,16 @@
#include <tr1/type_traits>
#if defined(_GLIBCXX_INCLUDE_AS_TR1)
# include <tr1_impl/boost_sp_counted_base.h>
# include <tr1/boost_sp_shared_count.h>
# include <tr1_impl/boost_shared_ptr.h>
#else
# define _GLIBCXX_INCLUDE_AS_TR1
# define _GLIBCXX_BEGIN_NAMESPACE_TR1 namespace tr1 {
# define _GLIBCXX_END_NAMESPACE_TR1 }
# define _GLIBCXX_TR1 tr1::
# include <tr1_impl/boost_sp_counted_base.h>
# include <tr1/boost_sp_shared_count.h>
# include <tr1_impl/boost_shared_ptr.h>
# undef _GLIBCXX_TR1
# undef _GLIBCXX_END_NAMESPACE_TR1
......
// <tr1_impl/boost_sp_counted_base.h> -*- C++ -*-
// Copyright (C) 2007 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
// shared_count.hpp
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
// shared_ptr.hpp
// Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
// Copyright (C) 2001, 2002, 2003 Peter Dimov
// weak_ptr.hpp
// Copyright (C) 2001, 2002, 2003 Peter Dimov
// enable_shared_from_this.hpp
// Copyright (C) 2002 Peter Dimov
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// GCC Note: based on version 1.32.0 of the Boost library.
/** @file tr1_impl/boost_sp_counted_base.h
* This is an internal header file, included by other library headers.
* You should not attempt to use it directly.
*/
namespace std
{
_GLIBCXX_BEGIN_NAMESPACE_TR1
class bad_weak_ptr : public std::exception
{
public:
virtual char const*
what() const throw()
#ifdef _GLIBCXX_INCLUDE_AS_CXX0X
{ return "std::bad_weak_ptr"; }
#else
{ return "tr1::bad_weak_ptr"; }
#endif
};
// Substitute for bad_weak_ptr object in the case of -fno-exceptions.
inline void
__throw_bad_weak_ptr()
{
#if __EXCEPTIONS
throw bad_weak_ptr();
#else
__builtin_abort();
#endif
}
using __gnu_cxx::_Lock_policy;
using __gnu_cxx::__default_lock_policy;
using __gnu_cxx::_S_single;
using __gnu_cxx::_S_mutex;
using __gnu_cxx::_S_atomic;
// Empty helper class except when the template argument is _S_mutex.
template<_Lock_policy _Lp>
class _Mutex_base
{
protected:
// The atomic policy uses fully-fenced builtins, single doesn't care.
enum { _S_need_barriers = 0 };
};
template<>
class _Mutex_base<_S_mutex>
: public __gnu_cxx::__mutex
{
protected:
// This policy is used when atomic builtins are not available.
// The replacement atomic operations might not have the necessary
// memory barriers.
enum { _S_need_barriers = 1 };
};
template<_Lock_policy _Lp = __default_lock_policy>
class _Sp_counted_base
: public _Mutex_base<_Lp>
{
public:
_Sp_counted_base()
: _M_use_count(1), _M_weak_count(1) { }
virtual
~_Sp_counted_base() // nothrow
{ }
// Called when _M_use_count drops to zero, to release the resources
// managed by *this.
virtual void
_M_dispose() = 0; // nothrow
// Called when _M_weak_count drops to zero.
virtual void
_M_destroy() // nothrow
{ delete this; }
virtual void*
_M_get_deleter(const std::type_info&) = 0;
void
_M_add_ref_copy()
{ __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
void
_M_add_ref_lock();
void
_M_release() // nothrow
{
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
{
_M_dispose();
// There must be a memory barrier between dispose() and destroy()
// to ensure that the effects of dispose() are observed in the
// thread that runs destroy().
// See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
if (_Mutex_base<_Lp>::_S_need_barriers)
{
_GLIBCXX_READ_MEM_BARRIER;
_GLIBCXX_WRITE_MEM_BARRIER;
}
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
-1) == 1)
_M_destroy();
}
}
void
_M_weak_add_ref() // nothrow
{ __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
void
_M_weak_release() // nothrow
{
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
{
if (_Mutex_base<_Lp>::_S_need_barriers)
{
// See _M_release(),
// destroy() must observe results of dispose()
_GLIBCXX_READ_MEM_BARRIER;
_GLIBCXX_WRITE_MEM_BARRIER;
}
_M_destroy();
}
}
long
_M_get_use_count() const // nothrow
{
// No memory barrier is used here so there is no synchronization
// with other threads.
return const_cast<const volatile _Atomic_word&>(_M_use_count);
}
private:
_Sp_counted_base(_Sp_counted_base const&);
_Sp_counted_base& operator=(_Sp_counted_base const&);
_Atomic_word _M_use_count; // #shared
_Atomic_word _M_weak_count; // #weak + (#shared != 0)
};
template<>
inline void
_Sp_counted_base<_S_single>::
_M_add_ref_lock()
{
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
{
_M_use_count = 0;
__throw_bad_weak_ptr();
}
}
template<>
inline void
_Sp_counted_base<_S_mutex>::
_M_add_ref_lock()
{
__gnu_cxx::__scoped_lock sentry(*this);
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
{
_M_use_count = 0;
__throw_bad_weak_ptr();
}
}
template<>
inline void
_Sp_counted_base<_S_atomic>::
_M_add_ref_lock()
{
// Perform lock-free add-if-not-zero operation.
_Atomic_word __count;
do
{
__count = _M_use_count;
if (__count == 0)
__throw_bad_weak_ptr();
// Replace the current counter value with the old value + 1, as
// long as it's not changed meanwhile.
}
while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
__count + 1));
}
_GLIBCXX_END_NAMESPACE_TR1
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
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 reset_count_struct
{
~reset_count_struct()
{
A::ctor_count = 0;
A::dtor_count = 0;
}
};
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
// Assignment from shared_ptr<Y>
void
test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
a = std::shared_ptr<A>(new A);
VERIFY( a.get() != 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
a = std::shared_ptr<A>();
VERIFY( a.get() == 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 1 );
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
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;
}
};
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
// Assignment from auto_ptr<Y>
int
test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a(new A);
std::auto_ptr<B> b(new B);
a = b;
VERIFY( a.get() != 0 );
VERIFY( b.get() == 0 );
VERIFY( A::ctor_count == 2 );
VERIFY( A::dtor_count == 1 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
struct B { };
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
// Assignment from incompatible auto_ptr<Y>
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
std::auto_ptr<B> b;
a = b; // { dg-error "here" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-excess-errors "In constructor" }
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
std::auto_ptr<A> source() { return std::auto_ptr<A>(); }
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
// Assignment from rvalue auto_ptr
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
a = source(); // { dg-error "no match" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-excess-errors "candidates are" }
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2007 Free Software Foundation
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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
......@@ -14,11 +14,18 @@
// 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 COPYING. If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
template class std::weak_ptr<int>;
// DR 541. shared_ptr template assignment and void
void test01()
{
std::shared_ptr<void> p;
p.operator=<void>(p);
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <utility>
#include <testsuite_hooks.h>
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;
}
};
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Rvalue assignment from shared_ptr
void
test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a1;
std::shared_ptr<A> a2(new A);
a1 = std::move(a2);
VERIFY( a1.get() != 0 );
VERIFY( a2.get() == 0 );
VERIFY( a1.use_count() == 1 );
VERIFY( a2.use_count() == 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
a1 = std::move(std::shared_ptr<A>());
VERIFY( a1.get() == 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 1 );
}
// Rvalue assignment from shared_ptr<Y>
void
test02()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
std::shared_ptr<B> b(new B);
a = std::move(b);
VERIFY( a.get() != 0 );
VERIFY( b.get() == 0 );
VERIFY( a.use_count() == 1 );
VERIFY( b.use_count() == 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
a = std::move(std::shared_ptr<A>());
VERIFY( a.get() == 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 1 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 1 );
}
int
main()
{
test01();
test02();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
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;
}
};
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
// Assignment from shared_ptr<Y>
void
test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
a = std::shared_ptr<A>();
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::shared_ptr<A>(new A);
VERIFY( a.get() != 0 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
VERIFY( B::ctor_count == 0 );
VERIFY( B::dtor_count == 0 );
a = std::shared_ptr<B>(new B);
VERIFY( a.get() != 0 );
VERIFY( A::ctor_count == 2 );
VERIFY( A::dtor_count == 1 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
struct B { };
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
// Assignment from incompatible shared_ptr<Y>
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
std::shared_ptr<B> b;
a = b; // { dg-error "here" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-error "In member function" "" { target *-*-* } 0 }
// { dg-error "cannot convert" "" { target *-*-* } 0 }
// { dg-error "instantiated from" "" { target *-*-* } 0 }
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2.10 shared_ptr casts [util.smartptr.shared.cast]
#include <memory>
#include <testsuite_tr1.h>
// { dg-do compile }
struct MyP { virtual ~MyP() { }; };
struct MyDP : MyP { };
int main()
{
using __gnu_test::check_ret_type;
using std::shared_ptr;
using std::static_pointer_cast;
using std::const_pointer_cast;
using std::dynamic_pointer_cast;
shared_ptr<double> spd;
shared_ptr<const int> spci;
shared_ptr<MyP> spa;
check_ret_type<shared_ptr<void> >(static_pointer_cast<void>(spd));
check_ret_type<shared_ptr<int> >(const_pointer_cast<int>(spci));
check_ret_type<shared_ptr<MyDP> >(static_pointer_cast<MyDP>(spa));
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A
{
virtual ~A() { }
};
struct B : A
{
};
// 20.6.6.2.6 shared_ptr comparison [util.smartptr.shared.cmp]
int
test01()
{
// test empty shared_ptrs compare equivalent
std::shared_ptr<A> p1;
std::shared_ptr<B> p2;
VERIFY( p1 == p2 );
VERIFY( !(p1 != p2) );
VERIFY( !(p1 < p2) && !(p2 < p1) );
return 0;
}
// Construction from pointer
int
test02()
{
std::shared_ptr<A> A_default;
std::shared_ptr<A> A_from_A(new A);
VERIFY( A_default != A_from_A );
VERIFY( !(A_default == A_from_A) );
VERIFY( (A_default < A_from_A) || (A_from_A < A_default) );
std::shared_ptr<B> B_from_B(new B);
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()
{
test01();
test02();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A
{
A() : i() { }
virtual ~A() { }
int i;
};
struct B : A
{
B() : A(), a() { }
virtual ~B() { }
A a;
};
void deletefunc(A* p) { delete p; }
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Aliasing constructors
int test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
std::shared_ptr<bool> b1(a, &test);
VERIFY( b1.use_count() == 0 );
VERIFY( a.get() == 0 );
VERIFY( b1.get() == &test );
std::shared_ptr<bool> b2(b1);
VERIFY( b2.use_count() == 0 );
VERIFY( b1.get() == b2.get() );
return 0;
}
int
test02()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a(new A);
std::shared_ptr<int> i1(a, &a->i);
VERIFY( i1.use_count() == 2 );
std::shared_ptr<int> i2(i1);
VERIFY( i2.use_count() == 3 );
VERIFY( i2.get() == &a->i );
return 0;
}
int
test03()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<B> b(new B);
std::shared_ptr<A> a1(b, b.get());
std::shared_ptr<A> a2(b, &b->a);
VERIFY( a2.use_count() == 3 );
VERIFY( a1 == b );
VERIFY( a2 != b );
VERIFY( a1.get() != a2.get() );
std::shared_ptr<A> a3(a1);
VERIFY( a3 == b );
a3 = a2;
VERIFY( a3.get() == &b->a );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
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;
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction with allocator
int
test01()
{
bool test __attribute__((unused)) = true;
tracker_allocator_counter::reset();
std::shared_ptr<A> p1(new A, deletefunc, tracker_allocator<A>());
std::size_t const sz = tracker_allocator_counter::get_allocation_count();
VERIFY( sz > 0 );
{
std::shared_ptr<A> 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;
}
// Construction with allocator
int
test02()
{
bool test __attribute__((unused)) = true;
tracker_allocator_counter::reset();
std::shared_ptr<A> p1(new A, deletefunc, tracker_allocator<A>());
std::size_t const sz1 = tracker_allocator_counter::get_allocation_count();
VERIFY( sz1 > 0 );
std::shared_ptr<A> p2(new A, D(), tracker_allocator<A>());
std::size_t const sz2 = tracker_allocator_counter::get_allocation_count();
VERIFY( sz2 > sz1 );
VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
p1 = p2;
VERIFY( p2.use_count() == 2 );
VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1 );
p1.reset();
VERIFY( p2.use_count() == 1 );
VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1 );
p2.reset();
VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
VERIFY( tracker_allocator_counter::get_deallocation_count() == sz2 );
VERIFY( D::delete_count == 1 );
return 0;
}
int
main()
{
test01();
test02();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction from auto_ptr
int
test01()
{
bool test __attribute__((unused)) = true;
std::auto_ptr<A> a(new A);
std::shared_ptr<A> a2(a);
VERIFY( a.get() == 0 );
VERIFY( a2.get() != 0 );
VERIFY( a2.use_count() == 1 );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.const]
// Construction from const auto_ptr
int
test01()
{
bool test __attribute__((unused)) = true;
const std::auto_ptr<A> a;
std::shared_ptr<A> p(a); // { dg-error "no match" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-excess-errors "candidates are" }
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
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;
}
};
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Copy construction
int test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a1;
std::shared_ptr<A> 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::shared_ptr<A> a1(new A);
std::shared_ptr<A> a2(a1);
VERIFY( a2.use_count() == 2 );
VERIFY( A::ctor_count == 1 );
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::shared_ptr<B> b(new B);
std::shared_ptr<A> a(b);
VERIFY( a.use_count() == 2 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
return 0;
}
int
test04()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<B> b(new B, &deleter);
std::shared_ptr<A> a(b);
VERIFY( a.use_count() == 2 );
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();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Default construction
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
VERIFY( a.get() == 0 );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// TR1 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <utility>
#include <testsuite_hooks.h>
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 D
{
void operator()(B* 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;
B::ctor_count = 0;
B::dtor_count = 0;
D::delete_count = 0;
}
};
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Rvalue construction
int test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a1;
std::shared_ptr<A> a2(std::move(a1));
VERIFY( a1.use_count() == 0 );
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::shared_ptr<A> a1(new A);
std::shared_ptr<A> a2(std::move(a1));
VERIFY( a1.use_count() == 0 );
VERIFY( a2.use_count() == 1 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
return 0;
}
int
test03()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<B> b(new B);
std::shared_ptr<A> a(std::move(b));
VERIFY( b.use_count() == 0 );
VERIFY( a.use_count() == 1 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
return 0;
}
int
test04()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<B> b(new B, D());
std::shared_ptr<A> a(std::move(b));
VERIFY( b.use_count() == 0 );
VERIFY( a.use_count() == 1 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
a = std::move(std::shared_ptr<A>());
VERIFY( D::delete_count == 1 );
VERIFY( B::dtor_count == 1 );
return 0;
}
int
test05()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a(std::move(std::shared_ptr<A>(new A)));
VERIFY( a.use_count() == 1 );
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
return 0;
}
int
main()
{
test01();
test02();
test03();
test04();
test05();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
struct B : A { };
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction from pointer
int
test01()
{
bool test __attribute__((unused)) = true;
A * const a = 0;
std::shared_ptr<A> 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;
std::shared_ptr<A> 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;
std::shared_ptr<A> p(b);
VERIFY( p.get() == b );
VERIFY( p.use_count() == 1 );
return 0;
}
int
main()
{
test01();
test02();
test02();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction from weak_ptr
int
test01()
{
bool test __attribute__((unused)) = true;
A * const a = new A;
std::shared_ptr<A> a1(a);
std::weak_ptr<A> wa(a1);
std::shared_ptr<A> a2(wa);
VERIFY( a2.get() == a );
VERIFY( a2.use_count() == wa.use_count() );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do run { xfail *-*-* } }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction from expired weak_ptr
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a1(new A);
std::weak_ptr<A> wa(a1);
a1.reset();
VERIFY( wa.expired() );
try
{
std::shared_ptr<A> a2(wa);
}
catch (const std::bad_weak_ptr&)
{
// Expected.
__throw_exception_again;
}
catch (...)
{
// Failed.
}
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
using __gnu_test::tracker_allocator_counter;
using __gnu_test::tracker_allocator;
struct A
{
A(int i, double d, char c = '\0') : i(i), d(d), c(c) { ++ctor_count; }
explicit A(int i) : i(i), d(), c() { ++ctor_count; }
A() : i(), d(), c() { ++ctor_count; }
~A() { ++dtor_count; }
int i;
double d;
char c;
static int ctor_count;
static int dtor_count;
};
int A::ctor_count = 0;
int A::dtor_count = 0;
struct reset_count_struct
{
~reset_count_struct()
{
A::ctor_count = 0;
A::dtor_count = 0;
tracker_allocator_counter::reset();
}
};
// 20.6.6.2.6 shared_ptr creation [util.smartptr.shared.create]
int
test01()
{
bool test __attribute__((unused)) = true;
reset_count_struct __attribute__((unused)) reset;
{
std::shared_ptr<A> p1 = std::allocate_shared<A>(tracker_allocator<A>());
VERIFY( p1.get() != 0 );
VERIFY( p1.use_count() == 1 );
VERIFY( A::ctor_count == 1 );
VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
}
VERIFY( A::ctor_count == A::dtor_count );
VERIFY( tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count() );
}
int
test02()
{
bool test __attribute__((unused)) = true;
reset_count_struct __attribute__((unused)) reset;
std::shared_ptr<A> p1;
p1 = std::allocate_shared<A>(tracker_allocator<A>(), 1);
VERIFY( A::ctor_count == 1 );
VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
p1 = std::allocate_shared<A>(tracker_allocator<A>(), 1, 2.0);
VERIFY( A::ctor_count == 2 );
VERIFY( A::dtor_count == 1 );
VERIFY( tracker_allocator_counter::get_deallocation_count() > 0 );
p1 = std::allocate_shared<A>(tracker_allocator<A>(), 1, 2.0, '3');
VERIFY( A::ctor_count == 3 );
VERIFY( A::dtor_count == 2 );
VERIFY( p1->i == 1 );
VERIFY( p1->d == 2.0 );
VERIFY( p1->c == '3' );
p1 = std::shared_ptr<A>();
VERIFY( A::ctor_count == A::dtor_count );
VERIFY( tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count() );
return 0;
}
int
main()
{
test01();
test02();
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <new>
#include <testsuite_hooks.h>
struct A
{
void* operator new(size_t n) { return new char[sizeof(A)]; }
void operator delete(void* p, size_t) { delete (char*)p; }
};
// 20.6.6.2.6 shared_ptr creation [util.smartptr.shared.create]
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p = std::make_shared<A>();
}
int
main()
{
test01();
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A
{
A(int i, double d, char c = '\0') : i(i), d(d), c(c) { ++ctor_count; }
explicit A(int i) : i(i), d(), c() { ++ctor_count; }
A() : i(), d(), c() { ++ctor_count; }
~A() { ++dtor_count; }
int i;
double d;
char c;
static int ctor_count;
static int dtor_count;
};
int A::ctor_count = 0;
int A::dtor_count = 0;
struct reset_count_struct
{
~reset_count_struct()
{
A::ctor_count = 0;
A::dtor_count = 0;
}
};
// 20.6.6.2.6 shared_ptr creation [util.smartptr.shared.create]
int
test01()
{
bool test __attribute__((unused)) = true;
reset_count_struct __attribute__((unused)) reset;
{
std::shared_ptr<A> p1 = std::make_shared<A>();
VERIFY( p1.get() != 0 );
VERIFY( p1.use_count() == 1 );
VERIFY( A::ctor_count == 1 );
}
VERIFY( A::ctor_count == A::dtor_count );
}
int
test02()
{
bool test __attribute__((unused)) = true;
reset_count_struct __attribute__((unused)) reset;
std::shared_ptr<A> p1;
p1 = std::make_shared<A>(1);
VERIFY( A::ctor_count == 1 );
p1 = std::make_shared<A>(1, 2.0);
VERIFY( A::ctor_count == 2 );
VERIFY( A::dtor_count == 1 );
p1 = std::make_shared<A>(1, 2.0, '3');
VERIFY( A::ctor_count == 3 );
VERIFY( A::dtor_count == 2 );
VERIFY( p1->i == 1 );
VERIFY( p1->d == 2.0 );
VERIFY( p1->c == '3' );
p1 = std::shared_ptr<A>();
VERIFY( A::ctor_count == A::dtor_count );
return 0;
}
int
main()
{
test01();
test02();
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
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.6.6.2.2 shared_ptr destructor [util.smartptr.shared.dest]
// empty shared_ptr
int
test01()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
{
std::shared_ptr<A> 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;
}
// shared ownership
int
test02()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
std::shared_ptr<A> a;
{
a = std::shared_ptr<A>(new B, D());
}
VERIFY( A::ctor_count == 1 );
VERIFY( A::dtor_count == 0 );
VERIFY( B::ctor_count == 1 );
VERIFY( B::dtor_count == 0 );
VERIFY( D::delete_count == 0 );
return 0;
}
// exclusive ownership
int
test03()
{
reset_count_struct __attribute__((unused)) reset;
bool test __attribute__((unused)) = true;
{
std::shared_ptr<A> a1(new B);
std::shared_ptr<A> a2(new B, D());
}
VERIFY( A::ctor_count == 2 );
VERIFY( A::dtor_count == 2 );
VERIFY( B::ctor_count == 2 );
VERIFY( B::dtor_count == 2 );
VERIFY( D::delete_count == 1 );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
using std::get_deleter;
// libstdc++/24595
void test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<int> sp;
VERIFY( !get_deleter<void(*)(int*)>(sp) );
}
int main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <sstream>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.8 shared_ptr I/O [util.smartptr.shared.io]
// operator<<
int
test01()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p(new A);
std::ostringstream buf;
buf << p;
const std::string s = buf.str();
buf.str("");
buf << p.get();
VERIFY( s == buf.str() );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.9 shared_ptr specialized algorithms [util.smartptr.shared.spec]
// std::swap
int
test01()
{
bool test __attribute__((unused)) = true;
A * const a1 = new A;
A * const a2 = new A;
std::shared_ptr<A> p1(a1);
std::shared_ptr<A> p2(a2);
std::swap(p1, p2);
VERIFY( p1.get() == a2 );
VERIFY( p2.get() == a1 );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2007 Free Software Foundation
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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
......@@ -14,11 +14,18 @@
// 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 COPYING. If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
template class std::shared_ptr<int>;
// 20.6.6.2.4 shared_ptr modifiers [util.smartptr.shared.mod]
// swap
// libstdc++/24805
using std::swap;
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
struct B : A { };
struct D
{
void operator()(B* p) { delete p; ++delete_count; }
static long delete_count;
};
long D::delete_count = 0;
// 20.6.6.2.4 shared_ptr modifiers [util.smartptr.shared.mod]
// reset
int
test01()
{
bool test __attribute__((unused)) = true;
A * const a = new A;
std::shared_ptr<A> p1(a);
std::shared_ptr<A> 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;
B * const b = new B;
std::shared_ptr<A> p1(a);
std::shared_ptr<A> p2(p1);
p1.reset(b);
VERIFY( p1.get() == b );
VERIFY( p2.get() == a );
return 0;
}
int
test03()
{
bool test __attribute__((unused)) = true;
{
std::shared_ptr<A> p1;
p1.reset(new B, D());
}
VERIFY( D::delete_count == 1 );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
using __gnu_test::tracker_allocator_counter;
using __gnu_test::tracker_allocator;
struct A { };
struct B : A { };
struct D
{
void operator()(B* p) { delete p; ++delete_count; }
static long delete_count;
};
long D::delete_count = 0;
// 20.6.6.2.4 shared_ptr modifiers [util.smartptr.shared.mod]
// Reset with allocator
int
test01()
{
bool test __attribute__((unused)) = true;
tracker_allocator_counter::reset();
{
std::shared_ptr<A> p1;
p1.reset(new B, D(), tracker_allocator<B>());
VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
}
VERIFY( D::delete_count == 1 );
VERIFY( tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count() );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.4 shared_ptr modifiers [util.smartptr.shared.mod]
// reset
int
test01()
{
bool test __attribute__((unused)) = true;
const std::shared_ptr<A> p1(new A);
p1.reset(); // { dg-error "discards qualifiers" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.4 shared_ptr modifiers [util.smartptr.shared.mod]
// swap
int
test01()
{
bool test __attribute__((unused)) = true;
A * const a1 = new A;
A * const a2 = new A;
std::shared_ptr<A> p1(a1);
std::shared_ptr<A> p2(a2);
p1.swap(p2);
VERIFY( p1.get() == a2 );
VERIFY( p2.get() == a1 );
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.4 shared_ptr modifiers [util.smartptr.shared.mod]
// swap
int
test01()
{
bool test __attribute__((unused)) = true;
const std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(new A);
p1.swap(p2); // { dg-error "discards qualifiers" }
return 0;
}
int
main()
{
test01();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// conversion to bool
int
test01()
{
bool test __attribute__((unused)) = true;
const std::shared_ptr<A> p1;
VERIFY( p1 == false );
const std::shared_ptr<A> p2(p1);
VERIFY( p2 == false );
return 0;
}
int
test02()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A);
VERIFY( p1 );
std::shared_ptr<A> p2(p1);
VERIFY( p2 );
p1.reset();
VERIFY( !p1 );
VERIFY( p2 );
return 0;
}
int
test03()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(p1);
p2.reset(new A);
VERIFY( p1 );
VERIFY( p2 );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A
{
A() : i() {}
int i;
};
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// get
int
test01()
{
bool test __attribute__((unused)) = true;
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( p.get() == a );
return 0;
}
// operator*
int
test02()
{
bool test __attribute__((unused)) = true;
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &*p == a );
return 0;
}
// operator->
int
test03()
{
bool test __attribute__((unused)) = true;
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &p->i == &a->i );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// unique
int
test01()
{
bool test __attribute__((unused)) = true;
const std::shared_ptr<A> p1;
VERIFY( !p1.unique() );
const std::shared_ptr<A> p2(p1);
VERIFY( !p1.unique() );
return 0;
}
int
test02()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A);
VERIFY( p1.unique() );
std::shared_ptr<A> p2(p1);
VERIFY( !p1.unique() );
p1.reset();
VERIFY( !p1.unique() );
VERIFY( p2.unique() );
return 0;
}
int
test03()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(p1);
p2.reset(new A);
VERIFY( p1.unique() );
VERIFY( p2.unique() );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// Copyright (C) 2005, 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_hooks.h>
struct A { };
struct B : A { };
// 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
// use_count
int
test01()
{
bool test __attribute__((unused)) = true;
const std::shared_ptr<A> p1;
VERIFY( p1.use_count() == 0 );
const std::shared_ptr<A> p2(p1);
VERIFY( p1.use_count() == 0 );
return 0;
}
int
test02()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(p1);
p1.reset();
VERIFY( p1.use_count() == 0 );
VERIFY( p2.use_count() == 1 );
return 0;
}
int
test03()
{
bool test __attribute__((unused)) = true;
std::shared_ptr<A> p1(new A);
std::shared_ptr<A> p2(p1);
p2.reset(new B);
VERIFY( p1.use_count() == 1 );
VERIFY( p2.use_count() == 1 );
return 0;
}
int
main()
{
test01();
test02();
test03();
return 0;
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_tr1.h>
using namespace __gnu_test;
using std::shared_ptr;
template class shared_ptr<int>;
template class shared_ptr<void>;
template class shared_ptr<ClassType>;
template class shared_ptr<IncompleteClass>;
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_tr1.h>
// Check the _S_single lock policy can be instantiated. For a thread-enabled
// library this checks the templates can be instantiated for non-default
// lock policy, for a single-threaded lib this is redundant but harmless.
using namespace __gnu_test;
using std::__shared_ptr;
using std::_S_single;
template class __shared_ptr<int, _S_single>;
template class __shared_ptr<ClassType, _S_single>;
template class __shared_ptr<IncompleteClass, _S_single>;
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options "-pthread -std=gnu++0x" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options "-pthreads -std=gnu++0x" { target *-*-solaris* } }
#include <memory>
#include <random>
#include <vector>
#include <testsuite_hooks.h>
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#ifdef _GLIBCXX_HAVE_UNISTD_H
#include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
#endif
/* This (brute-force) tests the atomicity and thus thread safety of the
* shared_ptr <- weak_ptr
* assignment operation by allocating a test object, retrieving a weak
* reference to it, and letting a number of threads repeatedly create strong
* references from the weak reference.
* Specifically, this tests the function _Sp_counted_base<true>::add_ref_lock()
*/
const unsigned int HAMMER_MAX_THREADS = 10;
const unsigned int POOL_SIZE = 1000;
const unsigned long HAMMER_REPEAT = 100000;
const unsigned long KILL_ONE_IN = 1000;
struct A
{
static _Atomic_word counter;
A()
{
__gnu_cxx::__atomic_add(&counter, 1);
}
~A()
{
__gnu_cxx::__atomic_add(&counter, -1);
}
};
_Atomic_word A::counter = 0;
typedef std::shared_ptr<A> sp_A_t;
typedef std::weak_ptr<A> wp_A_t;
typedef std::vector<sp_A_t> sp_vector_t;
typedef std::vector<wp_A_t> wp_vector_t;
struct shared_and_weak_pools
{
sp_vector_t& shared_pool;
wp_vector_t& weak_pool;
shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
: shared_pool(_shared_pool), weak_pool(_weak_pool)
{ }
};
void* thread_hammer_and_kill(void* opaque_pools)
{
shared_and_weak_pools& pools = *static_cast<shared_and_weak_pools*>(opaque_pools);
// Using the same parameters as in the RNG test cases.
std::mersenne_twister<
unsigned long, 32, 624, 397, 31,
0x9908b0dful, 11, 7,
0x9d2c5680ul, 15,
0xefc60000ul, 18> rng;
sp_vector_t::iterator cur_shared = pools.shared_pool.begin();
wp_vector_t::iterator cur_weak = pools.weak_pool.begin();
for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
{
try
{
sp_A_t strong(*cur_weak);
}
catch (std::bad_weak_ptr& exception)
{
++cur_weak;
if (cur_weak == pools.weak_pool.end())
break;
}
if (rng() % KILL_ONE_IN == 0)
{
cur_shared->reset();
++cur_shared;
}
}
return 0;
}
void* thread_hammer(void* opaque_weak)
{
wp_vector_t& weak_pool = *static_cast<wp_vector_t*>(opaque_weak);
// Using the same parameters as in the RNG test cases.
std::mersenne_twister<
unsigned long, 32, 624, 397, 31,
0x9908b0dful, 11, 7,
0x9d2c5680ul, 15,
0xefc60000ul, 18> rng;
wp_vector_t::iterator cur_weak = weak_pool.begin();
for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
{
try
{
sp_A_t strong(*cur_weak);
}
catch (std::bad_weak_ptr& exception)
{
++cur_weak;
if (cur_weak == weak_pool.end())
break;
}
}
return 0;
}
int
test01()
{
bool test __attribute__((unused)) = true;
sp_vector_t obj_pool(POOL_SIZE);
for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur)
{
cur->reset(new A);
}
// Obtain weak references.
std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
// Launch threads with pointer to weak reference.
pthread_t threads[HAMMER_MAX_THREADS];
#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
pthread_setconcurrency (HAMMER_MAX_THREADS);
#endif
pthread_attr_t tattr;
int ret = pthread_attr_init(&tattr);
shared_and_weak_pools pools(obj_pool, weak_pool[0]);
pthread_create(threads, &tattr, thread_hammer_and_kill, static_cast<void*>(&pools));
for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
{
if (pthread_create(&threads[worker], &tattr,
thread_hammer, static_cast<void*>(&weak_pool[worker])))
std::abort();
}
// Wait for threads to complete, then check integrity of reference.
void* status;
for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++)
{
if (pthread_join(threads[worker], &status))
std::abort();
}
obj_pool.clear();
VERIFY( A::counter == 0 );
return 0;
}
int
main()
{
test01();
return 0;
}
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
#include <memory>
#include <random>
#include <vector>
#include <testsuite_hooks.h>
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#ifdef _GLIBCXX_HAVE_UNISTD_H
#include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
#endif
/* This (brute-force) tests the atomicity and thus thread safety of the
* shared_ptr <- weak_ptr
* assignment operation by allocating a test object, retrieving a weak
* reference to it, and letting a number of threads repeatedly create strong
* references from the weak reference.
* Specifically, this tests the function _Sp_counted_base<true>::add_ref_lock()
*/
const unsigned int HAMMER_MAX_THREADS = 10;
const unsigned int POOL_SIZE = 1000;
const unsigned long HAMMER_REPEAT = 100000;
const unsigned long KILL_ONE_IN = 1000;
struct A
{
static _Atomic_word counter;
A()
{
__gnu_cxx::__atomic_add(&counter, 1);
}
~A()
{
__gnu_cxx::__atomic_add(&counter, -1);
}
};
_Atomic_word A::counter = 0;
using std::_S_mutex;
typedef std::__shared_ptr<A, _S_mutex> sp_A_t;
typedef std::__weak_ptr<A, _S_mutex> wp_A_t;
typedef std::vector<sp_A_t> sp_vector_t;
typedef std::vector<wp_A_t> wp_vector_t;
struct shared_and_weak_pools
{
sp_vector_t& shared_pool;
wp_vector_t& weak_pool;
shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
: shared_pool(_shared_pool), weak_pool(_weak_pool)
{ }
};
void* thread_hammer_and_kill(void* opaque_pools)
{
shared_and_weak_pools& pools = *static_cast<shared_and_weak_pools*>(opaque_pools);
// Using the same parameters as in the RNG test cases.
std::mersenne_twister<
unsigned long, 32, 624, 397, 31,
0x9908b0dful, 11, 7,
0x9d2c5680ul, 15,
0xefc60000ul, 18> rng;
sp_vector_t::iterator cur_shared = pools.shared_pool.begin();
wp_vector_t::iterator cur_weak = pools.weak_pool.begin();
for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
{
try
{
sp_A_t strong(*cur_weak);
}
catch (std::bad_weak_ptr& exception)
{
++cur_weak;
if (cur_weak == pools.weak_pool.end())
break;
}
if (rng() % KILL_ONE_IN == 0)
{
cur_shared->reset();
++cur_shared;
}
}
return 0;
}
void* thread_hammer(void* opaque_weak)
{
wp_vector_t& weak_pool = *static_cast<wp_vector_t*>(opaque_weak);
// Using the same parameters as in the RNG test cases.
std::mersenne_twister<
unsigned long, 32, 624, 397, 31,
0x9908b0dful, 11, 7,
0x9d2c5680ul, 15,
0xefc60000ul, 18> rng;
wp_vector_t::iterator cur_weak = weak_pool.begin();
for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
{
try
{
sp_A_t strong(*cur_weak);
}
catch (std::bad_weak_ptr& exception)
{
++cur_weak;
if (cur_weak == weak_pool.end())
break;
}
}
return 0;
}
int
test01()
{
bool test __attribute__((unused)) = true;
sp_vector_t obj_pool(POOL_SIZE);
for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur)
{
cur->reset(new A);
}
// Obtain weak references.
std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
// Launch threads with pointer to weak reference.
pthread_t threads[HAMMER_MAX_THREADS];
#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
pthread_setconcurrency (HAMMER_MAX_THREADS);
#endif
pthread_attr_t tattr;
int ret = pthread_attr_init(&tattr);
shared_and_weak_pools pools(obj_pool, weak_pool[0]);
pthread_create(threads, &tattr, thread_hammer_and_kill, static_cast<void*>(&pools));
for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
{
if (pthread_create(&threads[worker], &tattr,
thread_hammer, static_cast<void*>(&weak_pool[worker])))
std::abort();
}
// Wait for threads to complete, then check integrity of reference.
void* status;
for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++)
{
if (pthread_join(threads[worker], &status))
std::abort();
}
obj_pool.clear();
VERIFY( A::counter == 0 );
return 0;
}
int
main()
{
test01();
return 0;
}
// 2006-09-24 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.3 Template class weak_ptr [util.smartptr.weak]
#include <memory>
#include <testsuite_tr1.h>
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
int main()
{
using __gnu_test::check_ret_type;
using std::weak_ptr;
using std::shared_ptr;
weak_ptr<int> wp;
check_ret_type<shared_ptr<int> >(wp.lock());
}
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2006, 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_tr1.h>
using namespace __gnu_test;
using std::weak_ptr;
template class weak_ptr<int>;
template class weak_ptr<void>;
template class weak_ptr<ClassType>;
template class weak_ptr<IncompleteClass>;
// { dg-options "-std=gnu++0x" }
// { dg-do compile }
// Copyright (C) 2007 Free Software Foundation
//
// 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
#include <memory>
#include <testsuite_tr1.h>
// Check the _S_single lock policy can be instantiated. For a thread-enabled
// library this checks the templates can be instantiated for non-default
// lock policy, for a single-threaded lib this is redundant but harmless.
using namespace __gnu_test;
using std::__weak_ptr;
using std::_S_single;
template class __weak_ptr<int, _S_single>;
template class __weak_ptr<void, _S_single>;
template class __weak_ptr<ClassType, _S_single>;
template class __weak_ptr<IncompleteClass, _S_single>;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment