1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/functional/ref.hpp

326 lines
9.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
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)
=============================================================================*/
2012-04-09 16:38:55 +00:00
#ifndef SPROUT_FUNCTIONAL_REF_HPP
#define SPROUT_FUNCTIONAL_REF_HPP
#include <type_traits>
2013-04-12 10:17:16 +00:00
#include <functional>
2012-04-09 16:38:55 +00:00
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/type_traits/integral_constant.hpp>
2012-04-09 16:38:55 +00:00
#include <sprout/type_traits/has_xxx.hpp>
#include <sprout/type_traits/inherit_if_xxx.hpp>
#include <sprout/functional/base.hpp>
2012-11-15 02:54:57 +00:00
#include <sprout/functional/type_traits/weak_result_type.hpp>
2012-12-13 03:18:19 +00:00
#include <sprout/functional/type_traits/has_type.hpp>
2012-04-09 16:38:55 +00:00
namespace sprout {
// 20.8.3 reference_wrapper
namespace detail {
template<bool Unary, bool Binary, typename T>
struct reference_wrapper_base_impl;
template<typename T>
struct reference_wrapper_base_impl<false, false, T>
: public sprout::weak_result_type<T>
{};
template<typename T>
struct reference_wrapper_base_impl<true, false, T>
: public sprout::weak_result_type<T>
{
public:
typedef typename T::argument_type argument_type;
};
template<typename T>
struct reference_wrapper_base_impl<false, true, T>
: public sprout::weak_result_type<T>
{
public:
typedef typename T::first_argument_type first_argument_type;
typedef typename T::second_argument_type second_argument_type;
};
template<typename T>
2012-04-09 16:38:55 +00:00
struct reference_wrapper_base_impl<true, true, T>
: public sprout::weak_result_type<T>
{
public:
typedef typename T::argument_type argument_type;
typedef typename T::first_argument_type first_argument_type;
typedef typename T::second_argument_type second_argument_type;
};
template<typename T>
struct reference_wrapper_base
: public sprout::detail::reference_wrapper_base_impl<
2012-11-15 02:54:57 +00:00
sprout::has_argument_type<T>::value,
sprout::has_first_argument_type<T>::value
&& sprout::has_second_argument_type<T>::value
2012-04-09 16:38:55 +00:00
,
T
>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res(T1)>
: public sprout::unary_function<T1, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res(T1) const>
: public sprout::unary_function<T1, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res(T1) volatile>
: public sprout::unary_function<T1, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res(T1) const volatile>
: public sprout::unary_function<T1, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res(T1, T2)>
: public sprout::binary_function<T1, T2, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res(T1, T2) const>
: public sprout::binary_function<T1, T2, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res(T1, T2) volatile>
: public sprout::binary_function<T1, T2, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res(T1, T2) const volatile>
: public sprout::binary_function<T1, T2, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res(*)(T1)>
: public sprout::unary_function<T1, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res(*)(T1, T2)>
: public sprout::binary_function<T1, T2, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res (T1::*)()>
: public sprout::unary_function<T1*, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res (T1::*)(T2)>
: public sprout::binary_function<T1*, T2, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res (T1::*)() const>
: public sprout::unary_function<T1 const*, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res (T1::*)(T2) const>
: public sprout::binary_function<T1 const*, T2, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res (T1::*)() volatile>
: public sprout::unary_function<T1 volatile*, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res (T1::*)(T2) volatile>
: public sprout::binary_function<T1 volatile*, T2, Res>
{};
template<typename Res, typename T1>
struct reference_wrapper_base<Res (T1::*)() const volatile>
: public sprout::unary_function<T1 const volatile*, Res>
{};
template<typename Res, typename T1, typename T2>
struct reference_wrapper_base<Res (T1::*)(T2) const volatile>
: public sprout::binary_function<T1 const volatile*, T2, Res>
{};
} // namespace detail
template<typename T>
class reference_wrapper
: public sprout::detail::reference_wrapper_base<typename std::remove_cv<T>::type>
{
public:
// types
typedef T type;
private:
T* t_;
2012-12-17 14:10:23 +00:00
private:
reference_wrapper(T&&) SPROUT_DELETED_FUNCTION_DECL
2012-04-09 16:38:55 +00:00
public:
// construct/copy/destroy
SPROUT_CONSTEXPR reference_wrapper(T& t) SPROUT_NOEXCEPT
: t_(&t)
{}
2013-10-31 03:37:38 +00:00
reference_wrapper(reference_wrapper const&) = default;
2012-04-09 16:38:55 +00:00
// assignment
2013-10-31 03:37:38 +00:00
SPROUT_CXX14_CONSTEXPR reference_wrapper& operator=(reference_wrapper const& rhs) {
t_ = rhs.t_;
return *this;
}
2012-04-09 16:38:55 +00:00
// access
SPROUT_CONSTEXPR operator T& () const SPROUT_NOEXCEPT {
return *t_;
}
SPROUT_CONSTEXPR T& get() const SPROUT_NOEXCEPT {
return *t_;
}
SPROUT_CONSTEXPR T* get_pointer() const SPROUT_NOEXCEPT {
return t_;
}
2012-04-09 16:38:55 +00:00
// invocation
template<typename... Args>
SPROUT_CONSTEXPR typename std::result_of<T& (Args&&...)>::type
operator()(Args&&... args) const {
return (*t_)(SPROUT_FORWARD(Args, args)...);
2012-04-09 16:38:55 +00:00
}
};
//
// ref
// cref
//
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR sprout::reference_wrapper<T>
ref(T& t) SPROUT_NOEXCEPT {
2012-04-09 16:38:55 +00:00
return sprout::reference_wrapper<T>(t);
}
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR sprout::reference_wrapper<T const>
cref(T const& t) SPROUT_NOEXCEPT {
2012-04-09 16:38:55 +00:00
return sprout::reference_wrapper<T const>(t);
}
template<typename T>
2012-12-17 14:10:23 +00:00
void ref(T const&&) SPROUT_DELETED_FUNCTION_DECL
2012-04-09 16:38:55 +00:00
template<typename T>
2012-12-17 14:10:23 +00:00
void cref(T const&&) SPROUT_DELETED_FUNCTION_DECL
2013-10-17 07:44:31 +00:00
2012-04-09 16:38:55 +00:00
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR sprout::reference_wrapper<T>
ref(sprout::reference_wrapper<T> t) SPROUT_NOEXCEPT {
2012-04-09 16:38:55 +00:00
return t;
}
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR sprout::reference_wrapper<T const>
2013-10-17 07:44:31 +00:00
cref(sprout::reference_wrapper<T> t) SPROUT_NOEXCEPT {
2012-04-09 16:38:55 +00:00
return sprout::reference_wrapper<T const>(t.get());
}
//
// is_reference_wrapper
//
template<typename T>
struct is_reference_wrapper
: public sprout::false_type
2012-04-09 16:38:55 +00:00
{};
template<typename T>
struct is_reference_wrapper<T const>
: public sprout::is_reference_wrapper<T>
{};
template<typename T>
struct is_reference_wrapper<T volatile>
: public sprout::is_reference_wrapper<T>
{};
template<typename T>
struct is_reference_wrapper<T const volatile>
: public sprout::is_reference_wrapper<T>
{};
2012-04-13 12:23:49 +00:00
template<typename T>
struct is_reference_wrapper<sprout::reference_wrapper<T> >
: public sprout::true_type
2012-04-13 12:23:49 +00:00
{};
2012-04-09 16:38:55 +00:00
//
// unwrap_reference
//
template<typename T>
struct unwrap_reference {
public:
typedef T type;
};
template<typename T>
struct unwrap_reference<sprout::reference_wrapper<T> > {
public:
typedef T type;
};
template<typename T>
2013-04-12 10:17:16 +00:00
struct unwrap_reference<std::reference_wrapper<T> > {
public:
typedef T type;
};
template<typename T>
2012-04-09 16:38:55 +00:00
struct unwrap_reference<T const>
: public sprout::unwrap_reference<T>
{};
template<typename T>
struct unwrap_reference<T volatile>
: public sprout::unwrap_reference<T>
{};
template<typename T>
struct unwrap_reference<T const volatile>
: public sprout::unwrap_reference<T>
{};
2012-04-12 14:41:06 +00:00
//
// strip_reference
//
template<typename T>
struct strip_reference {
public:
typedef T type;
};
template<typename T>
struct strip_reference<sprout::reference_wrapper<T> > {
public:
typedef T& type;
};
template<typename T>
2013-04-12 10:17:16 +00:00
struct strip_reference<std::reference_wrapper<T> > {
public:
typedef T& type;
};
template<typename T>
2012-04-12 14:41:06 +00:00
struct strip_reference<T const>
: public sprout::strip_reference<T>
{};
template<typename T>
struct strip_reference<T volatile>
: public sprout::strip_reference<T>
{};
template<typename T>
struct strip_reference<T const volatile>
: public sprout::strip_reference<T>
{};
2012-04-09 16:38:55 +00:00
//
// unwrap_ref
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::unwrap_reference<T>::type&
2012-10-06 04:53:07 +00:00
unwrap_ref(T& t) {
2012-04-09 16:38:55 +00:00
return t;
}
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR typename sprout::unwrap_reference<T const>::type&
unwrap_ref(T const& t) {
2012-04-09 16:38:55 +00:00
return t;
}
//
// get_pointer
//
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR T*
get_pointer(sprout::reference_wrapper<T> const& r) {
2012-04-09 16:38:55 +00:00
return r.get_pointer();
}
2013-04-12 10:17:16 +00:00
template<typename T>
inline SPROUT_CONSTEXPR T*
get_pointer(std::reference_wrapper<T> const& r) {
return &r.get();
}
2012-04-09 16:38:55 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_REF_HPP