// Copyright David Abrahams 2001. // 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) #ifndef FORWARD_DWA20011215_HPP # define FORWARD_DWA20011215_HPP # include # include # include # include # include # include # include # if BOOST_WORKAROUND(BOOST_MSVC, < 1300) # include # include # include # else # include # endif namespace boost { namespace python { namespace objects { // Very much like boost::reference_wrapper, except that in this // case T can be a reference already without causing a // reference-to-reference error. template struct reference_to_value { typedef typename add_reference::type>::type reference; reference_to_value(reference x) : m_value(x) {} reference get() const { return m_value; } private: reference m_value; }; // A little metaprogram which selects the type to pass through an // intermediate forwarding function when the destination argument type // is T. template struct forward : mpl::if_< # if BOOST_WORKAROUND(BOOST_MSVC, < 1300) // vc6 chokes on unforwarding enums nested in classes mpl::and_< is_scalar , mpl::not_< is_enum > > # else mpl::or_, is_scalar > # endif , T , reference_to_value > { }; # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION template struct unforward { typedef typename unwrap_reference::type& type; }; template struct unforward > { typedef T type; }; template struct unforward_cref : python::detail::value_arg< typename unwrap_reference::type > { }; template struct unforward_cref > : add_reference::type> { }; # else // no partial specialization namespace detail { typedef char (&yes_reference_to_value_t)[1]; typedef char (&no_reference_to_value_t)[2]; no_reference_to_value_t is_reference_to_value_test(...); template yes_reference_to_value_t is_reference_to_value_test(boost::type< reference_to_value >); template struct unforwarder { template struct apply { typedef typename unwrap_reference::type& type; }; }; template<> struct unforwarder { template struct apply { typedef typename T::reference type; }; }; template struct cref_unforwarder { template struct apply : python::detail::value_arg< typename unwrap_reference::type > { }; }; template<> struct cref_unforwarder { template struct apply : python::detail::value_arg< typename T::reference > { }; }; template struct is_reference_to_value { BOOST_STATIC_CONSTANT( bool, value = ( sizeof(is_reference_to_value_test(boost::type())) == sizeof(yes_reference_to_value_t))); typedef mpl::bool_ type; }; } template struct unforward : public detail::unforwarder< detail::is_reference_to_value::value >::template apply {}; template struct unforward_cref : public detail::cref_unforwarder< detail::is_reference_to_value::value >::template apply {}; # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION template typename reference_to_value::reference do_unforward(reference_to_value const& x, int) { return x.get(); } template typename reference_wrapper::type& do_unforward(reference_wrapper const& x, int) { return x.get(); } template T const& do_unforward(T const& x, ...) { return x; } }}} // namespace boost::python::objects #endif // FORWARD_DWA20011215_HPP