#ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED
#define BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED

//  make_shared_object.hpp
//
//  Copyright (c) 2007, 2008, 2012 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
//
//  See http://www.boost.org/libs/smart_ptr/make_shared.html
//  for documentation.

#include <boost/config.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/sp_forward.hpp>
#include <boost/type_traits/type_with_alignment.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include <cstddef>
#include <new>

namespace pdalboost
{

namespace detail
{

template< std::size_t N, std::size_t A > struct sp_aligned_storage
{
    union type
    {
        char data_[ N ];
        typename pdalboost::type_with_alignment< A >::type align_;
    };
};

template< class T > class sp_ms_deleter
{
private:

    typedef typename sp_aligned_storage< sizeof( T ), ::pdalboost::alignment_of< T >::value >::type storage_type;

    bool initialized_;
    storage_type storage_;

private:

    void destroy()
    {
        if( initialized_ )
        {
#if defined( __GNUC__ )

            // fixes incorrect aliasing warning
            T * p = reinterpret_cast< T* >( storage_.data_ );
            p->~T();

#else

            reinterpret_cast< T* >( storage_.data_ )->~T();

#endif

            initialized_ = false;
        }
    }

public:

    sp_ms_deleter() BOOST_NOEXCEPT : initialized_( false )
    {
    }

    template<class A> explicit sp_ms_deleter( A const & ) BOOST_NOEXCEPT : initialized_( false )
    {
    }

    // optimization: do not copy storage_
    sp_ms_deleter( sp_ms_deleter const & ) BOOST_NOEXCEPT : initialized_( false )
    {
    }

    ~sp_ms_deleter()
    {
        destroy();
    }

    void operator()( T * )
    {
        destroy();
    }

    static void operator_fn( T* ) // operator() can't be static
    {
    }

    void * address() BOOST_NOEXCEPT
    {
        return storage_.data_;
    }

    void set_initialized() BOOST_NOEXCEPT
    {
        initialized_ = true;
    }
};

template< class T, class A > class sp_as_deleter
{
private:

    typedef typename sp_aligned_storage< sizeof( T ), ::pdalboost::alignment_of< T >::value >::type storage_type;

    storage_type storage_;
    A a_;
    bool initialized_;

private:

    void destroy()
    {
        if( initialized_ )
        {
            T * p = reinterpret_cast< T* >( storage_.data_ );

#if !defined( BOOST_NO_CXX11_ALLOCATOR )

            std::allocator_traits<A>::destroy( a_, p );

#else

            p->~T();

#endif

            initialized_ = false;
        }
    }

public:

    sp_as_deleter( A const & a ) BOOST_NOEXCEPT : a_( a ), initialized_( false )
    {
    }

    // optimization: do not copy storage_
    sp_as_deleter( sp_as_deleter const & r ) BOOST_NOEXCEPT : a_( r.a_), initialized_( false )
    {
    }

    ~sp_as_deleter()
    {
        destroy();
    }

    void operator()( T * )
    {
        destroy();
    }

    static void operator_fn( T* ) // operator() can't be static
    {
    }

    void * address() BOOST_NOEXCEPT
    {
        return storage_.data_;
    }

    void set_initialized() BOOST_NOEXCEPT
    {
        initialized_ = true;
    }
};

template< class T > struct sp_if_not_array
{
    typedef pdalboost::shared_ptr< T > type;
};

#if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )

template< class T > struct sp_if_not_array< T[] >
{
};

#if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )

template< class T, std::size_t N > struct sp_if_not_array< T[N] >
{
};

#endif

#endif

} // namespace detail

#if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
# define BOOST_SP_MSD( T ) pdalboost::detail::sp_inplace_tag< pdalboost::detail::sp_ms_deleter< T > >()
#else
# define BOOST_SP_MSD( T ) pdalboost::detail::sp_ms_deleter< T >()
#endif

// _noinit versions

template< class T > typename pdalboost::detail::sp_if_not_array< T >::type make_shared_noinit()
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T;
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A > typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared_noinit( A const & a )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T;
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

#if !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

// Variadic templates, rvalue reference

template< class T, class... Args > typename pdalboost::detail::sp_if_not_array< T >::type make_shared( Args && ... args )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( pdalboost::detail::sp_forward<Args>( args )... );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class... Args > typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, Args && ... args )
{
#if !defined( BOOST_NO_CXX11_ALLOCATOR )

    typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
    A2 a2( a );

    typedef pdalboost::detail::sp_as_deleter< T, A2 > D;

    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), pdalboost::detail::sp_inplace_tag<D>(), a2 );

#else

    typedef pdalboost::detail::sp_ms_deleter< T > D;

    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), pdalboost::detail::sp_inplace_tag<D>(), a );

#endif

    D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
    void * pv = pd->address();

#if !defined( BOOST_NO_CXX11_ALLOCATOR )

    std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), pdalboost::detail::sp_forward<Args>( args )... );

#else

    ::new( pv ) T( pdalboost::detail::sp_forward<Args>( args )... );

#endif

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

#else // !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

// Common zero-argument versions

template< class T > typename pdalboost::detail::sp_if_not_array< T >::type make_shared()
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T();
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A > typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T();
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

// For example MSVC 10.0

template< class T, class A1 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3, A4 && a4 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3, A4 && a4 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6, class A7 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6, A7 && a7 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 ), 
        pdalboost::detail::sp_forward<A7>( a7 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6, A7 && a7 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 ), 
        pdalboost::detail::sp_forward<A7>( a7 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6, A7 && a7, A8 && a8 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 ), 
        pdalboost::detail::sp_forward<A7>( a7 ), 
        pdalboost::detail::sp_forward<A8>( a8 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6, A7 && a7, A8 && a8 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 ), 
        pdalboost::detail::sp_forward<A7>( a7 ), 
        pdalboost::detail::sp_forward<A8>( a8 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6, A7 && a7, A8 && a8, A9 && a9 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T(
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 ), 
        pdalboost::detail::sp_forward<A7>( a7 ), 
        pdalboost::detail::sp_forward<A8>( a8 ), 
        pdalboost::detail::sp_forward<A9>( a9 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 && a1, A2 && a2, A3 && a3, A4 && a4, A5 && a5, A6 && a6, A7 && a7, A8 && a8, A9 && a9 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( 
        pdalboost::detail::sp_forward<A1>( a1 ), 
        pdalboost::detail::sp_forward<A2>( a2 ), 
        pdalboost::detail::sp_forward<A3>( a3 ), 
        pdalboost::detail::sp_forward<A4>( a4 ), 
        pdalboost::detail::sp_forward<A5>( a5 ), 
        pdalboost::detail::sp_forward<A6>( a6 ), 
        pdalboost::detail::sp_forward<A7>( a7 ), 
        pdalboost::detail::sp_forward<A8>( a8 ), 
        pdalboost::detail::sp_forward<A9>( a9 )
        );

    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

#else // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

// C++03 version

template< class T, class A1 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6, class A7 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6, a7 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6, a7 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7, A8 const & a8 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6, a7, a8 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7, A8 const & a8 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6, a7, a8 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9 >
typename pdalboost::detail::sp_if_not_array< T >::type make_shared( A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7, A8 const & a8, A9 const & a9 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6, a7, a8, a9 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

template< class T, class A, class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9 >
typename pdalboost::detail::sp_if_not_array< T >::type allocate_shared( A const & a, A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7, A8 const & a8, A9 const & a9 )
{
    pdalboost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ), a );

    pdalboost::detail::sp_ms_deleter< T > * pd = static_cast<pdalboost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( a1, a2, a3, a4, a5, a6, a7, a8, a9 );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    pdalboost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return pdalboost::shared_ptr< T >( pt, pt2 );
}

#endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

#endif // !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

#undef BOOST_SP_MSD

} // namespace pdalboost

#endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED
