Files
boost_bind/include/boost/bind/mem_fn.hpp
T

254 lines
5.5 KiB
C++
Raw Normal View History

#ifndef BOOST_BIND_MEM_FN_HPP_INCLUDED
#define BOOST_BIND_MEM_FN_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
2024-01-04 19:51:50 +02:00
// mem_fn.hpp - a generalization of std::mem_fun[_ref]
//
2024-01-04 19:51:50 +02:00
// Copyright 2001-2005, 2024 Peter Dimov
// Copyright 2001 David Abrahams
//
// 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)
//
2024-01-04 19:51:50 +02:00
// See http://www.boost.org/libs/bind/mem_fn.html for documentation.
//
#include <boost/get_pointer.hpp>
2023-03-04 19:58:19 +02:00
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
2024-01-04 18:38:45 +02:00
#include <type_traits>
namespace boost
{
namespace _mfi
{
2024-01-04 18:38:45 +02:00
template<class T> struct remove_cvref: std::remove_cv< typename std::remove_reference<T>::type >
{
};
2024-01-04 19:51:50 +02:00
template<class Pm, class R, class T, class... A> class mf
2024-01-04 18:38:45 +02:00
{
2024-01-04 19:51:50 +02:00
public:
2024-01-04 18:38:45 +02:00
2024-01-04 19:51:50 +02:00
typedef R result_type;
2024-01-04 19:51:50 +02:00
private:
2024-01-04 19:51:50 +02:00
Pm pm_;
2024-01-04 19:51:50 +02:00
public:
2024-01-04 19:51:50 +02:00
mf( Pm pm ): pm_( pm ) {}
2024-01-04 19:51:50 +02:00
template<class U,
class Ud = typename _mfi::remove_cvref<U>::type,
class En = typename std::enable_if<
std::is_same<T, Ud>::value || std::is_base_of<T, Ud>::value
>::type
>
2024-01-04 19:51:50 +02:00
R operator()( U&& u, A... a ) const
{
return (std::forward<U>( u ).*pm_)( std::forward<A>( a )... );
}
2024-01-04 19:51:50 +02:00
template<class U,
class Ud = typename _mfi::remove_cvref<U>::type,
class E1 = void,
class En = typename std::enable_if<
!(std::is_same<T, Ud>::value || std::is_base_of<T, Ud>::value)
>::type
>
2024-01-04 19:51:50 +02:00
R operator()( U&& u, A... a ) const
{
return (get_pointer( std::forward<U>( u ) )->*pm_)( std::forward<A>( a )... );
}
2024-01-04 19:51:50 +02:00
bool operator==( mf const & rhs ) const
{
return pm_ == rhs.pm_;
}
2024-01-04 19:51:50 +02:00
bool operator!=( mf const & rhs ) const
{
return pm_ != rhs.pm_;
}
};
} // namespace _mfi
//
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (T::*pmf) (A...) ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (T::*pmf) (A...) const ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
#if defined( __cpp_noexcept_function_type ) || defined( _NOEXCEPT_TYPES_SUPPORTED )
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (T::*pmf) (A...) noexcept ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
template<class R, class T, class... A>
auto mem_fn( R (T::*pmf) (A...) const noexcept ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
#endif // #if defined( __cpp_noexcept_function_type ) || defined( _NOEXCEPT_TYPES_SUPPORTED )
#if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (__cdecl T::*pmf) (A...) ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (__cdecl T::*pmf) (A...) const ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
#endif // #if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
#if defined(BOOST_MEM_FN_ENABLE_STDCALL) && !defined(_M_X64)
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (__stdcall T::*pmf) (A...) ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (__stdcall T::*pmf) (A...) const ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
#endif // #if defined(BOOST_MEM_FN_ENABLE_STDCALL) && !defined(_M_X64)
#if defined(BOOST_MEM_FN_ENABLE_FASTCALL) && !defined(_M_X64)
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (__fastcall T::*pmf) (A...) ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
template<class R, class T, class... A>
auto mem_fn( R (__fastcall T::*pmf) (A...) const ) -> _mfi::mf<decltype(pmf), R, T, A...>
{
return pmf;
}
2024-01-04 20:07:02 +02:00
#endif // #if defined(BOOST_MEM_FN_ENABLE_FASTCALL) && !defined(_M_X64)
// data member support
namespace _mfi
{
template<class R, class T> class dm
{
public:
typedef R const & result_type;
typedef T const * argument_type;
private:
2024-01-04 19:51:50 +02:00
2024-01-04 18:38:45 +02:00
typedef R (T::*Pm);
Pm pm_;
public:
2024-01-04 19:51:50 +02:00
dm( Pm pm ): pm_( pm ) {}
2024-01-04 18:38:45 +02:00
template<class U,
class Ud = typename _mfi::remove_cvref<U>::type,
class En = typename std::enable_if<
std::is_same<T, Ud>::value || std::is_base_of<T, Ud>::value
>::type
>
auto operator()( U&& u ) const -> decltype( std::forward<U>( u ).*pm_ )
{
2024-01-04 18:38:45 +02:00
return std::forward<U>( u ).*pm_;
}
2024-01-04 18:38:45 +02:00
template<class U,
class Ud = typename _mfi::remove_cvref<U>::type,
class E1 = void,
class En = typename std::enable_if<
!(std::is_same<T, Ud>::value || std::is_base_of<T, Ud>::value)
>::type
>
auto operator()( U&& u ) const -> decltype( get_pointer( std::forward<U>( u ) )->*pm_ )
{
2024-01-04 18:38:45 +02:00
return get_pointer( std::forward<U>( u ) )->*pm_;
}
2024-01-04 18:38:45 +02:00
#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
template<class U>
R& operator()( U* u ) const
{
2024-01-04 18:38:45 +02:00
return u->*pm_;
}
2024-01-04 18:38:45 +02:00
template<class U>
R const& operator()( U const* u ) const
{
2024-01-04 18:38:45 +02:00
return u->*pm_;
}
#endif
2024-01-04 18:38:45 +02:00
bool operator==( dm const & rhs ) const
{
2024-01-04 18:38:45 +02:00
return pm_ == rhs.pm_;
}
2024-01-04 18:38:45 +02:00
bool operator!=( dm const & rhs ) const
{
2024-01-04 18:38:45 +02:00
return pm_ != rhs.pm_;
}
};
} // namespace _mfi
2024-01-04 19:51:50 +02:00
template<class R, class T,
class E = typename std::enable_if< !std::is_function<R>::value >::type
>
_mfi::dm<R, T> mem_fn( R T::*pm )
{
2024-01-04 19:51:50 +02:00
return pm;
}
} // namespace boost
#endif // #ifndef BOOST_BIND_MEM_FN_HPP_INCLUDED