YouCompleteMe/cpp/BoostParts/boost/smart_ptr/shared_array.hpp

291 lines
7.1 KiB
C++
Raw Normal View History

2012-05-10 00:45:30 -04:00
#ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
#define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
//
// shared_array.hpp
//
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
2013-02-13 23:03:52 -05:00
// Copyright (c) 2001, 2002, 2012 Peter Dimov
2012-05-10 00:45:30 -04:00
//
// 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)
//
// See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
//
#include <boost/config.hpp> // for broken compiler workarounds
#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
#include <boost/smart_ptr/detail/shared_array_nmt.hpp>
#else
#include <memory> // TR1 cyclic inclusion fix
#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
2013-02-13 23:03:52 -05:00
#include <boost/smart_ptr/shared_ptr.hpp>
2012-05-10 00:45:30 -04:00
#include <boost/smart_ptr/detail/shared_count.hpp>
2013-02-13 23:03:52 -05:00
#include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
2012-05-10 00:45:30 -04:00
#include <boost/detail/workaround.hpp>
#include <cstddef> // for std::ptrdiff_t
#include <algorithm> // for std::swap
#include <functional> // for std::less
namespace boost
{
//
// shared_array
//
// shared_array extends shared_ptr to arrays.
// The array pointed to is deleted when the last shared_array pointing to it
// is destroyed or reset.
//
template<class T> class shared_array
{
private:
// Borland 5.5.1 specific workarounds
typedef checked_array_deleter<T> deleter;
typedef shared_array<T> this_type;
public:
typedef T element_type;
2013-02-13 23:03:52 -05:00
shared_array() BOOST_NOEXCEPT : px( 0 ), pn()
2012-05-10 00:45:30 -04:00
{
}
2013-02-13 23:03:52 -05:00
template<class Y>
explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
{
boost::detail::sp_assert_convertible< Y[], T[] >();
}
2012-05-10 00:45:30 -04:00
//
// Requirements: D's copy constructor must not throw
//
// shared_array will release p by calling d(p)
//
2013-02-13 23:03:52 -05:00
template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
2012-05-10 00:45:30 -04:00
{
2013-02-13 23:03:52 -05:00
boost::detail::sp_assert_convertible< Y[], T[] >();
}
// As above, but with allocator. A's copy constructor shall not throw.
template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
{
boost::detail::sp_assert_convertible< Y[], T[] >();
2012-05-10 00:45:30 -04:00
}
// generated copy constructor, destructor are fine...
2013-02-13 23:03:52 -05:00
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
2012-05-10 00:45:30 -04:00
// ... except in C++0x, move disables the implicit copy
2013-02-13 23:03:52 -05:00
shared_array( shared_array const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
2012-05-10 00:45:30 -04:00
{
}
2013-02-13 23:03:52 -05:00
shared_array( shared_array && r ) BOOST_NOEXCEPT : px( r.px ), pn()
{
pn.swap( r.pn );
r.px = 0;
}
#endif
// conversion
template<class Y>
#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
#else
shared_array( shared_array<Y> const & r )
2012-05-10 00:45:30 -04:00
#endif
2013-02-13 23:03:52 -05:00
BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) // never throws
{
boost::detail::sp_assert_convertible< Y[], T[] >();
}
// aliasing
template< class Y >
shared_array( shared_array<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
{
}
2012-05-10 00:45:30 -04:00
// assignment
2013-02-13 23:03:52 -05:00
shared_array & operator=( shared_array const & r ) BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
this_type( r ).swap( *this );
return *this;
}
2013-02-13 23:03:52 -05:00
#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
template<class Y>
shared_array & operator=( shared_array<Y> const & r ) BOOST_NOEXCEPT
{
this_type( r ).swap( *this );
return *this;
}
#endif
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
shared_array & operator=( shared_array && r ) BOOST_NOEXCEPT
{
this_type( static_cast< shared_array && >( r ) ).swap( *this );
return *this;
}
template<class Y>
shared_array & operator=( shared_array<Y> && r ) BOOST_NOEXCEPT
{
this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
return *this;
}
#endif
void reset() BOOST_NOEXCEPT
{
this_type().swap( *this );
}
template<class Y> void reset( Y * p ) // Y must be complete
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type( p ).swap( *this );
}
template<class Y, class D> void reset( Y * p, D d )
{
this_type( p, d ).swap( *this );
}
template<class Y, class D, class A> void reset( Y * p, D d, A a )
2012-05-10 00:45:30 -04:00
{
2013-02-13 23:03:52 -05:00
this_type( p, d, a ).swap( *this );
2012-05-10 00:45:30 -04:00
}
2013-02-13 23:03:52 -05:00
template<class Y> void reset( shared_array<Y> const & r, element_type * p )
2012-05-10 00:45:30 -04:00
{
2013-02-13 23:03:52 -05:00
this_type( r, p ).swap( *this );
2012-05-10 00:45:30 -04:00
}
2013-02-13 23:03:52 -05:00
T & operator[] (std::ptrdiff_t i) const // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
2012-05-10 00:45:30 -04:00
{
BOOST_ASSERT(px != 0);
BOOST_ASSERT(i >= 0);
return px[i];
}
2013-02-13 23:03:52 -05:00
T * get() const BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
return px;
}
// implicit conversion to "bool"
#include <boost/smart_ptr/detail/operator_bool.hpp>
2013-02-13 23:03:52 -05:00
bool unique() const BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
return pn.unique();
}
2013-02-13 23:03:52 -05:00
long use_count() const BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
return pn.use_count();
}
2013-02-13 23:03:52 -05:00
void swap(shared_array<T> & other) BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
std::swap(px, other.px);
pn.swap(other.pn);
}
void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const
{
return pn.get_deleter( ti );
}
private:
2013-02-13 23:03:52 -05:00
template<class Y> friend class shared_array;
2012-05-10 00:45:30 -04:00
T * px; // contained pointer
detail::shared_count pn; // reference counter
}; // shared_array
2013-02-13 23:03:52 -05:00
template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
return a.get() == b.get();
}
2013-02-13 23:03:52 -05:00
template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
return a.get() != b.get();
}
2013-02-13 23:03:52 -05:00
#if !defined( BOOST_NO_CXX11_NULLPTR )
template<class T> inline bool operator==( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
{
return p.get() == 0;
}
template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
{
return p.get() == 0;
}
template<class T> inline bool operator!=( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
{
return p.get() != 0;
}
template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
{
return p.get() != 0;
}
#endif
template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
return std::less<T*>()(a.get(), b.get());
}
2013-02-13 23:03:52 -05:00
template<class T> void swap(shared_array<T> & a, shared_array<T> & b) BOOST_NOEXCEPT
2012-05-10 00:45:30 -04:00
{
a.swap(b);
}
template< class D, class T > D * get_deleter( shared_array<T> const & p )
{
return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID(D) ) );
}
} // namespace boost
#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
#endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED