add sprout::addressof

This commit is contained in:
bolero-MURAKAMI 2014-08-12 21:33:22 +09:00
parent 22b1ac3f62
commit ab875e84de
4 changed files with 173 additions and 4 deletions

15
sprout/memory.hpp Normal file
View file

@ -0,0 +1,15 @@
/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
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_HPP
#define SPROUT_MEMORY_HPP
#include <sprout/config.hpp>
#include <sprout/memory/addressof.hpp>
#include <sprout/memory/exempt_ptr.hpp>
#endif // #ifndef SPROUT_MEMORY_HPP

154
sprout/memory/addressof.hpp Normal file
View file

@ -0,0 +1,154 @@
/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
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>
namespace sprout {
namespace detail {
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(...);
};
#if defined(_MSC_VER)
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(...);
};
#if defined(_MSC_VER)
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
: public sprout::integral_constant<
bool,
sprout::detail::has_mem_address_op<T>::value || sprout::detail::has_nonmem_address_op<T>::value
>
{};
template<typename T, bool IsOverloaded = sprout::detail::has_address_op<T>::value>
struct addressof_traits_default;
template<typename T>
struct addressof_traits_default<T, true> {
public:
template<typename U>
static U*
get_addressof(U& t) SPROUT_NOEXCEPT {
return std::addressof(t);
}
};
template<typename T>
struct addressof_traits_default<T, false> {
public:
template<typename U>
static SPROUT_CONSTEXPR U*
get_addressof(U& t) SPROUT_NOEXCEPT {
return &t;
}
};
} // namespace detail
//
// address_traits
//
template<typename T>
struct address_traits
: public sprout::detail::addressof_traits_default<T>
{};
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_adl {
sprout::not_found_via_adl get_addressof(...);
} // namespace sprout_adl
namespace sprout_addressof_detail {
template<typename T>
inline SPROUT_CONSTEXPR T*
get_addressof(T& t) SPROUT_NOEXCEPT {
return sprout::address_traits<T>::get_addressof(t);
}
template<typename T>
inline SPROUT_CONSTEXPR T*
call_get_addressof(T& t) SPROUT_NOEXCEPT {
using sprout_adl::get_addressof;
using sprout_addressof_detail::get_addressof;
return get_addressof(t);
}
} // namespace sprout_addressof_detail
namespace sprout {
//
// addressof
//
// effect:
// ADL callable get_addressof(t) -> get_addressof(t)
// otherwise -> sprout::address_traits<T>::get_addressof(t)
// [default]
// no overloaded operator&() -> &t
// otherwise -> std::addressof(t)
//
template<typename T>
inline SPROUT_CONSTEXPR T*
addressof(T& t) SPROUT_NOEXCEPT {
return sprout_addressof_detail::call_get_addressof(t);
}
} // namespace sprout
#endif // #ifndef SPROUT_MEMORY_ADDRESSOF_HPP

View file

@ -5,8 +5,8 @@
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_EXEMPT_PTR_HPP
#define SPROUT_EXEMPT_PTR_HPP
#ifndef SPROUT_MEMORY_EXEMPT_PTR_HPP
#define SPROUT_MEMORY_EXEMPT_PTR_HPP
#include <functional>
#include <iterator>
@ -276,4 +276,4 @@ namespace std {
#endif
} // namespace std
#endif // #ifndef SPROUT_EXEMPT_PTR_HPP
#endif // #ifndef SPROUT_MEMORY_EXEMPT_PTR_HPP

View file

@ -36,7 +36,6 @@
#include <sprout/cwctype.hpp>
#include <sprout/darkroom.hpp>
#include <sprout/endian_traits.hpp>
#include <sprout/exempt_ptr.hpp>
#include <sprout/forward_clist.hpp>
#include <sprout/functional.hpp>
#include <sprout/generator.hpp>
@ -47,6 +46,7 @@
#include <sprout/limits.hpp>
#include <sprout/logic.hpp>
#include <sprout/math.hpp>
#include <sprout/memory.hpp>
#include <sprout/net.hpp>
#include <sprout/none.hpp>
#include <sprout/numeric.hpp>