1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

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

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

@ -0,0 +1,279 @@
/*=============================================================================
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_EXEMPT_PTR_HPP
#define SPROUT_MEMORY_EXEMPT_PTR_HPP
#include <functional>
#include <iterator>
#include <type_traits>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/functional/hash.hpp>
namespace sprout {
//
// exempt_ptr
//
template<typename T>
class exempt_ptr {
public:
typedef T value_type;
typedef T element_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef T& reference;
typedef T const& const_reference;
typedef std::ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category;
private:
pointer p_;
private:
template<typename P>
static SPROUT_CONSTEXPR bool is_compat() {
return std::is_convertible<P*, pointer>::value;
}
public:
SPROUT_CONSTEXPR exempt_ptr() SPROUT_NOEXCEPT
: p_()
{}
SPROUT_CONSTEXPR exempt_ptr(std::nullptr_t) SPROUT_NOEXCEPT
: exempt_ptr()
{}
explicit SPROUT_CONSTEXPR exempt_ptr(pointer other) SPROUT_NOEXCEPT
: p_(other)
{}
template<
typename U,
typename sprout::enabler_if<std::is_convertible<U*, pointer>::value>::type = sprout::enabler
>
explicit SPROUT_CONSTEXPR exempt_ptr(U* other) SPROUT_NOEXCEPT
: p_(other)
{}
template<
typename U,
typename sprout::enabler_if<std::is_convertible<U*, pointer>::value>::type = sprout::enabler
>
SPROUT_CONSTEXPR exempt_ptr(sprout::exempt_ptr<U> const& other) SPROUT_NOEXCEPT
: p_(other.get())
{}
SPROUT_CXX14_CONSTEXPR exempt_ptr&
operator=(std::nullptr_t) SPROUT_NOEXCEPT {
reset();
return *this;
}
template<
typename U,
typename sprout::enabler_if<std::is_convertible<U*, pointer>::value>::type = sprout::enabler
>
SPROUT_CXX14_CONSTEXPR exempt_ptr&
operator=(U* other) SPROUT_NOEXCEPT {
reset(other);
return *this;
}
template<
typename U,
typename sprout::enabler_if<std::is_convertible<U*, pointer>::value>::type = sprout::enabler
>
SPROUT_CXX14_CONSTEXPR exempt_ptr&
operator=(exempt_ptr<U> const& other) SPROUT_NOEXCEPT {
reset(other.get());
return *this;
}
SPROUT_CONSTEXPR pointer
get() const SPROUT_NOEXCEPT {
return p_;
}
SPROUT_CONSTEXPR reference
operator*() const SPROUT_NOEXCEPT {
return *get();
}
SPROUT_CONSTEXPR pointer
operator->() const SPROUT_NOEXCEPT {
return get();
}
SPROUT_EXPLICIT_CONVERSION SPROUT_CONSTEXPR operator bool() const SPROUT_NOEXCEPT {
return get();
}
SPROUT_CONSTEXPR operator pointer() SPROUT_NOEXCEPT {
return get();
}
SPROUT_CONSTEXPR operator const_pointer() const SPROUT_NOEXCEPT {
return get();
}
SPROUT_CXX14_CONSTEXPR pointer
release() SPROUT_NOEXCEPT {
pointer old = get();
reset();
return old;
}
SPROUT_CXX14_CONSTEXPR void
reset(pointer t = 0) SPROUT_NOEXCEPT {
p_ = t;
}
SPROUT_CXX14_CONSTEXPR void
swap(exempt_ptr& other) SPROUT_NOEXCEPT {
swap(p_, other.p_);
}
SPROUT_CXX14_CONSTEXPR exempt_ptr&
operator++() {
++p_;
return *this;
}
SPROUT_CXX14_CONSTEXPR exempt_ptr&
operator--() {
--p_;
return *this;
}
SPROUT_CXX14_CONSTEXPR exempt_ptr operator++(int) {
pointer tmp = p_; ++p_; return exempt_ptr{tmp};
}
SPROUT_CXX14_CONSTEXPR exempt_ptr operator--(int) {
pointer tmp = p_; --p_; return exempt_ptr{tmp};
}
SPROUT_CXX14_CONSTEXPR exempt_ptr
operator+() const {
return *this; }
SPROUT_CXX14_CONSTEXPR exempt_ptr
operator+(std::ptrdiff_t d) const {
return exempt_ptr{p_+d};
}
SPROUT_CXX14_CONSTEXPR exempt_ptr
operator-(std::ptrdiff_t d) const {
return exempt_ptr{p_-d};
}
SPROUT_CXX14_CONSTEXPR value_type&
operator[](std::ptrdiff_t k) {
return p_[k];
}
SPROUT_CXX14_CONSTEXPR value_type const&
operator[](std::ptrdiff_t k) const {
return p_[k];
}
};
//
// swap
//
template<typename T>
inline SPROUT_CXX14_CONSTEXPR void
swap(sprout::exempt_ptr<T>& x, sprout::exempt_ptr<T>& y) SPROUT_NOEXCEPT {
x.swap(y);
}
//
// make_exempt
//
template<typename T>
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
make_exempt(T* p) SPROUT_NOEXCEPT {
return sprout::exempt_ptr<T>(p);
}
//
// operator==
// operator!=
//
template<typename T, typename U>
inline SPROUT_CONSTEXPR bool
operator==(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) SPROUT_NOEXCEPT {
return x.get() == y.get();
}
template<typename T, typename U>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) SPROUT_NOEXCEPT {
return !(x == y);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator==(sprout::exempt_ptr<T> const& x, std::nullptr_t y) SPROUT_NOEXCEPT {
return x.get() == y;
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::exempt_ptr<T> const& x, std::nullptr_t y) SPROUT_NOEXCEPT {
return !(x == y);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator==(std::nullptr_t x, sprout::exempt_ptr<T> const& y) SPROUT_NOEXCEPT {
return x == y.get();
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator!=(std::nullptr_t x, sprout::exempt_ptr<T> const& y) SPROUT_NOEXCEPT {
return !(x == y);
}
//
// operator<
// operator>
// operator=<
// operator=>
//
template<typename T, typename U>
inline SPROUT_CONSTEXPR bool
operator<(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) {
return x.get() < y.get();
}
template<typename T, typename U>
inline SPROUT_CONSTEXPR bool
operator>(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) {
return y < x;
}
template<typename T, typename U>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) {
return !(y < x);
}
template<typename T, typename U>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) {
return !(x < y);
}
//
// operator+
// operator-
//
template<typename T>
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
operator+(std::ptrdiff_t d, sprout::exempt_ptr<T> const& p) {
return p + d;
}
template<typename T>
inline SPROUT_CONSTEXPR std::ptrdiff_t
operator-(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<T> const& y) {
return x.get() - y.get();
}
} // namespace sprout
namespace sprout {
//
// hash_value
//
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::exempt_ptr<T> const& v) {
return sprout::hash_value(v.get());
}
} // namespace sprout
namespace std {
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmismatched-tags"
#endif
//
// hash
//
template<typename T>
struct hash<sprout::exempt_ptr<T> >
: public sprout::hash<sprout::exempt_ptr<T> >
{};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
} // namespace std
#endif // #ifndef SPROUT_MEMORY_EXEMPT_PTR_HPP