From ab875e84ded5ae113359748d766a8774759c0250 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Tue, 12 Aug 2014 21:33:22 +0900 Subject: [PATCH] add sprout::addressof --- sprout/memory.hpp | 15 +++ sprout/memory/addressof.hpp | 154 +++++++++++++++++++++++++++++ sprout/{ => memory}/exempt_ptr.hpp | 6 +- testspr/header_all.hpp | 2 +- 4 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 sprout/memory.hpp create mode 100644 sprout/memory/addressof.hpp rename sprout/{ => memory}/exempt_ptr.hpp (94%) diff --git a/sprout/memory.hpp b/sprout/memory.hpp new file mode 100644 index 00000000..0cfa73e5 --- /dev/null +++ b/sprout/memory.hpp @@ -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 +#include +#include + +#endif // #ifndef SPROUT_MEMORY_HPP diff --git a/sprout/memory/addressof.hpp b/sprout/memory/addressof.hpp new file mode 100644 index 00000000..2d8531c5 --- /dev/null +++ b/sprout/memory/addressof.hpp @@ -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 +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + struct has_mem_address_op_test { + public: + template< + typename U = T, + typename = typename sprout::identity().operator&())>::type + > + static sprout::true_type test(int); + static sprout::false_type test(...); + }; +#if defined(_MSC_VER) + template::test(0))>::type> + struct has_mem_address_op + : public Base_ + {}; +#else + template + struct has_mem_address_op + : public sprout::identity::test(0))>::type + {}; +#endif + + template + struct has_nonmem_address_op_test { + public: + template< + typename U = T, + typename = typename sprout::identity()))>::type + > + static sprout::true_type test(int); + static sprout::false_type test(...); + }; +#if defined(_MSC_VER) + template::test(0))>::type> + struct has_nonmem_address_op + : public Base_ + {}; +#else + template + struct has_nonmem_address_op + : public sprout::identity::test(0))>::type + {}; +#endif + + template + struct has_address_op + : public sprout::integral_constant< + bool, + sprout::detail::has_mem_address_op::value || sprout::detail::has_nonmem_address_op::value + > + {}; + + template::value> + struct addressof_traits_default; + template + struct addressof_traits_default { + public: + template + static U* + get_addressof(U& t) SPROUT_NOEXCEPT { + return std::addressof(t); + } + }; + template + struct addressof_traits_default { + public: + template + static SPROUT_CONSTEXPR U* + get_addressof(U& t) SPROUT_NOEXCEPT { + return &t; + } + }; + } // namespace detail + + // + // address_traits + // + template + struct address_traits + : public sprout::detail::addressof_traits_default + {}; + template + struct address_traits + : public sprout::address_traits + {}; + template + struct address_traits + : public sprout::address_traits + {}; + template + struct address_traits + : public sprout::address_traits + {}; +} // namespace sprout + +namespace sprout_adl { + sprout::not_found_via_adl get_addressof(...); +} // namespace sprout_adl + +namespace sprout_addressof_detail { + template + inline SPROUT_CONSTEXPR T* + get_addressof(T& t) SPROUT_NOEXCEPT { + return sprout::address_traits::get_addressof(t); + } + + template + 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::get_addressof(t) + // [default] + // no overloaded operator&() -> &t + // otherwise -> std::addressof(t) + // + template + 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 diff --git a/sprout/exempt_ptr.hpp b/sprout/memory/exempt_ptr.hpp similarity index 94% rename from sprout/exempt_ptr.hpp rename to sprout/memory/exempt_ptr.hpp index a4f2be72..a0e472a4 100644 --- a/sprout/exempt_ptr.hpp +++ b/sprout/memory/exempt_ptr.hpp @@ -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 #include @@ -276,4 +276,4 @@ namespace std { #endif } // namespace std -#endif // #ifndef SPROUT_EXEMPT_PTR_HPP +#endif // #ifndef SPROUT_MEMORY_EXEMPT_PTR_HPP diff --git a/testspr/header_all.hpp b/testspr/header_all.hpp index 0c907bf5..83a20f72 100644 --- a/testspr/header_all.hpp +++ b/testspr/header_all.hpp @@ -36,7 +36,6 @@ #include #include #include -#include #include #include #include @@ -47,6 +46,7 @@ #include #include #include +#include #include #include #include