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

155 lines
4.3 KiB
C++
Raw Normal View History

2014-08-12 12:33:22 +00:00
/*=============================================================================
Copyright (c) 2011-2017 Bolero MURAKAMI
2014-08-12 12:33:22 +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)
=============================================================================*/
#ifndef SPROUT_MEMORY_ADDRESSOF_HPP
#define SPROUT_MEMORY_ADDRESSOF_HPP
#include <utility>
#include <memory>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/adl/not_found.hpp>
2016-04-14 14:16:21 +00:00
namespace sprout_adl {
sprout::not_found_via_adl get_addressof(...);
} // namespace sprout_adl
2014-08-12 12:33:22 +00:00
namespace sprout {
namespace detail {
2015-03-04 14:01:10 +00:00
struct address_op_helper {};
void operator&(sprout::detail::address_op_helper const&);
2014-08-12 12:33:22 +00:00
template<typename T>
struct has_mem_address_op_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(std::declval<U>().operator&())>::type
>
static sprout::true_type test(int);
static sprout::false_type test(...);
};
2015-12-09 10:54:35 +00:00
#if defined(_MSC_VER) && (_MSC_VER > 1900)
2014-08-12 12:33:22 +00:00
template<typename T, typename Base_ = typename sprout::identity<decltype(sprout::detail::has_mem_address_op_test<T>::test(0))>::type>
struct has_mem_address_op
: public Base_
{};
#else
template<typename T>
struct has_mem_address_op
: public sprout::identity<decltype(sprout::detail::has_mem_address_op_test<T>::test(0))>::type
{};
#endif
template<typename T>
struct has_nonmem_address_op_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(operator&(std::declval<U>()))>::type
>
static sprout::true_type test(int);
static sprout::false_type test(...);
};
2015-12-09 10:54:35 +00:00
#if defined(_MSC_VER) && (_MSC_VER > 1900)
2014-08-12 12:33:22 +00:00
template<typename T, typename Base_ = typename sprout::identity<decltype(sprout::detail::has_nonmem_address_op_test<T>::test(0))>::type>
struct has_nonmem_address_op
: public Base_
{};
#else
template<typename T>
struct has_nonmem_address_op
: public sprout::identity<decltype(sprout::detail::has_nonmem_address_op_test<T>::test(0))>::type
{};
#endif
template<typename T>
struct has_address_op
2014-12-10 10:54:12 +00:00
: public sprout::bool_constant<
2014-08-12 12:33:22 +00:00
sprout::detail::has_mem_address_op<T>::value || sprout::detail::has_nonmem_address_op<T>::value
>
{};
template<typename T>
2016-04-14 14:16:21 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::detail::has_address_op<T>::value,
T*
>::type
get_addressof_impl(T& t) SPROUT_NOEXCEPT {
return std::addressof(t);
}
2014-08-12 12:33:22 +00:00
template<typename T>
2016-04-14 14:16:21 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
!sprout::detail::has_address_op<T>::value,
T*
>::type
get_addressof_impl(T& t) SPROUT_NOEXCEPT {
return &t;
}
2014-08-12 12:33:22 +00:00
} // namespace detail
2016-04-14 14:16:21 +00:00
} // namespace sprout
2014-08-12 12:33:22 +00:00
2016-04-14 14:16:21 +00:00
namespace sprout_addressof_detail {
template<typename T>
inline SPROUT_CONSTEXPR T*
get_addressof(T& t) SPROUT_NOEXCEPT {
return sprout::detail::get_addressof_impl(t);
}
} // namespace sprout_addressof_detail
namespace sprout {
2014-08-12 12:33:22 +00:00
//
// address_traits
//
template<typename T>
2016-04-14 14:16:21 +00:00
struct address_traits {
public:
template<typename U>
static SPROUT_CONSTEXPR U*
get_addressof(U& t) SPROUT_NOEXCEPT {
using sprout_addressof_detail::get_addressof;
using sprout_adl::get_addressof;
return get_addressof(t);
}
};
2014-08-12 12:33:22 +00:00
template<typename T>
struct address_traits<T const>
: public sprout::address_traits<T>
{};
template<typename T>
struct address_traits<T volatile>
: public sprout::address_traits<T>
{};
template<typename T>
struct address_traits<T const volatile>
: public sprout::address_traits<T>
{};
} // namespace sprout
namespace sprout {
//
// addressof
//
// effect:
2016-04-14 14:16:21 +00:00
// sprout::address_traits<T>::get_addressof(t)
2014-08-12 12:33:22 +00:00
// [default]
2016-04-14 14:16:21 +00:00
// ADL callable get_addressof(t) -> get_addressof(t)
// [default]
// no overloaded operator&() -> &t
// otherwise -> std::addressof(t)
2014-08-12 12:33:22 +00:00
//
template<typename T>
inline SPROUT_CONSTEXPR T*
addressof(T& t) SPROUT_NOEXCEPT {
2016-04-14 14:16:21 +00:00
return sprout::address_traits<T>::get_addressof(t);
2014-08-12 12:33:22 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_MEMORY_ADDRESSOF_HPP