Add pointer_traits with to_address

This commit is contained in:
Glen Fernandes 2017-05-16 07:11:46 -04:00
parent 80875a19b6
commit 26cab26e52
8 changed files with 650 additions and 0 deletions

View File

@ -0,0 +1,256 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_CORE_POINTER_TRAITS_HPP
#define BOOST_CORE_POINTER_TRAITS_HPP
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#if !defined(BOOST_NO_CXX11_ALLOCATOR) && \
!BOOST_WORKAROUND(BOOST_MSVC, < 1910)
#include <memory>
#else
#include <boost/core/addressof.hpp>
#endif
namespace boost {
template<class T>
struct pointer_traits;
namespace detail {
template<class U>
static typename pointer_traits<U>::element_type*
ptr_traits_address(U v) BOOST_NOEXCEPT
{
return pointer_traits<U>::to_address(v);
}
} /* detail */
#if !defined(BOOST_NO_CXX11_ALLOCATOR) && \
!BOOST_WORKAROUND(BOOST_MSVC, < 1910)
template<class T>
struct pointer_traits
: std::pointer_traits<T> {
template<class U>
struct rebind_to {
typedef typename std::pointer_traits<T>::template rebind<U> type;
};
static typename std::pointer_traits<T>::element_type*
to_address(T v) BOOST_NOEXCEPT {
return detail::ptr_traits_address(v.operator->());
}
};
template<class T>
struct pointer_traits<T*>
: std::pointer_traits<T*> {
template<class U>
struct rebind_to {
typedef U* type;
};
static T* to_address(T* v) BOOST_NOEXCEPT {
return v;
}
};
#else
namespace detail {
struct ptr_traits_none { char first; char second; };
template<class T>
struct ptr_traits_has_element {
private:
template<class U>
static ptr_traits_none call(...);
template<class U>
static char call(typename U::element_type* = 0);
public:
BOOST_STATIC_CONSTEXPR bool value = sizeof(call<T>(0)) == 1;
};
template<class T>
struct ptr_traits_first;
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args>
struct ptr_traits_first<T<U, Args...> > {
typedef U type;
};
#else
template<template<class> class T, class U>
struct ptr_traits_first<T<U> > {
typedef U type;
};
template<template<class, class> class T, class U1, class U2>
struct ptr_traits_first<T<U1, U2> > {
typedef U1 type;
};
template<template<class, class, class> class T, class U1, class U2, class U3>
struct ptr_traits_first<T<U1, U2, U3> > {
typedef U1 type;
};
#endif
template<class T, bool = ptr_traits_has_element<T>::value>
struct ptr_traits_element;
template<class T>
struct ptr_traits_element<T, true> {
typedef typename T::element_type type;
};
template<class T>
struct ptr_traits_element<T, false> {
typedef typename ptr_traits_first<T>::type type;
};
template<class T>
struct ptr_traits_has_difference {
private:
template<class U>
static ptr_traits_none call(...);
template<class U>
static char call(typename U::difference_type* = 0);
public:
BOOST_STATIC_CONSTEXPR bool value = sizeof(call<T>(0)) == 1;
};
template<class T, bool = ptr_traits_has_difference<T>::value>
struct ptr_traits_difference;
template<class T>
struct ptr_traits_difference<T, true> {
typedef typename T::difference_type type;
};
template<class T>
struct ptr_traits_difference<T, false> {
typedef std::ptrdiff_t type;
};
template<class T>
struct ptr_traits_has_rebind {
private:
template<class U>
static ptr_traits_none call(...);
template<class U>
static char call(typename U::template rebind<U>* = 0);
public:
BOOST_STATIC_CONSTEXPR bool value = sizeof(call<T>(0)) == 1;
};
template<class T, class V>
struct ptr_traits_rebind_to;
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args, class V>
struct ptr_traits_rebind_to<T<U, Args...>, V> {
typedef T<V, Args...> type;
};
#else
template<template<class> class T, class U, class V>
struct ptr_traits_rebind_to<T<U>, V> {
typedef T<V> type;
};
template<template<class, class> class T, class U1, class U2, class V>
struct ptr_traits_rebind_to<T<U1, U2>, V> {
typedef T<V, U2> type;
};
template<template<class, class, class> class T,
class U1, class U2, class U3, class V>
struct ptr_traits_rebind_to<T<U1, U2, U3>, V> {
typedef T<V, U2, U3> type;
};
#endif
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class T, class U, bool = ptr_traits_has_rebind<T>::value>
struct ptr_traits_rebind;
template<class T, class U>
struct ptr_traits_rebind<T, U, true> {
typedef typename T::template rebind<U> type;
};
template<class T, class U>
struct ptr_traits_rebind<T, U, false> {
typedef typename ptr_traits_rebind_to<T, U>::type type;
};
#else
template<class T, class U>
struct ptr_traits_rebind {
typedef typename ptr_traits_rebind_to<T, U>::type type;
};
#endif
template<class T>
struct ptr_traits_value {
typedef T type;
};
template<>
struct ptr_traits_value<void> {
typedef struct { } type;
};
} /* detail */
template<class T>
struct pointer_traits {
typedef T pointer;
typedef typename detail::ptr_traits_element<T>::type element_type;
typedef typename detail::ptr_traits_difference<T>::type difference_type;
template<class U>
struct rebind_to {
typedef typename detail::ptr_traits_rebind<T, U>::type type;
};
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class U>
using rebind = typename detail::ptr_traits_rebind<T, U>::type;
#endif
static pointer
pointer_to(typename detail::ptr_traits_value<element_type>::type& v) {
return pointer::pointer_to(v);
}
static element_type* to_address(pointer v) BOOST_NOEXCEPT {
return detail::ptr_traits_address(v.operator->());
}
};
template<class T>
struct pointer_traits<T*> {
typedef T* pointer;
typedef T element_type;
typedef std::ptrdiff_t difference_type;
template<class U>
struct rebind_to {
typedef U* type;
};
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class U>
using rebind = U*;
#endif
static T*
pointer_to(typename detail::ptr_traits_value<T>::type& v) BOOST_NOEXCEPT {
return addressof(v);
}
static T* to_address(T* v) BOOST_NOEXCEPT {
return v;
}
};
#endif
} /* boost */
#endif

View File

@ -105,5 +105,12 @@ compile-fail scoped_enum_compile_fail_conv_to_int.cpp ;
run underlying_type.cpp ;
run pointer_traits_to_address_test.cpp ;
run pointer_traits_pointer_test.cpp ;
run pointer_traits_element_type_test.cpp ;
run pointer_traits_difference_type_test.cpp ;
run pointer_traits_rebind_test.cpp ;
run pointer_traits_pointer_to_test.cpp ;
use-project /boost/core/swap : ./swap ;
build-project ./swap ;

View File

@ -0,0 +1,41 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct P { };
template<class T>
struct E {
typedef long difference_type;
};
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::pointer_traits<int*>::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::pointer_traits<P<int> >::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<long,
boost::pointer_traits<E<int> >::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::pointer_traits<void*>::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::pointer_traits<P<void> >::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<long,
boost::pointer_traits<E<void> >::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::pointer_traits<const int*>::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<std::ptrdiff_t,
boost::pointer_traits<P<const int> >::difference_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<long,
boost::pointer_traits<E<const int> >::difference_type>));
return boost::report_errors();
}

View File

@ -0,0 +1,83 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct P1 { };
template<class T1, class T2>
struct P2 { };
template<class T1, class T2, class T3>
struct P3 { };
template<class T>
struct E1 {
typedef bool element_type;
};
template<class T1, class T2>
struct E2 {
typedef bool element_type;
};
template<class T1, class T2, class T3>
struct E3 {
typedef bool element_type;
};
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class T, class... U>
struct P { };
template<class T, class... U>
struct E {
typedef bool element_type;
};
#endif
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::pointer_traits<int*>::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::pointer_traits<P1<int> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::pointer_traits<P2<int, char> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::pointer_traits<P3<int, char, char> >::element_type>));
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int,
boost::pointer_traits<P<int, char, char, char> >::element_type>));
#endif
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool,
boost::pointer_traits<E1<int> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool,
boost::pointer_traits<E2<int, int> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool,
boost::pointer_traits<E3<int, int, int> >::element_type>));
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool,
boost::pointer_traits<E<int, int, int, int> >::element_type>));
#endif
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<void,
boost::pointer_traits<void*>::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<void,
boost::pointer_traits<P1<void> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool,
boost::pointer_traits<E1<void> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const int,
boost::pointer_traits<const int*>::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const int,
boost::pointer_traits<P1<const int> >::element_type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<bool,
boost::pointer_traits<E1<const int> >::element_type>));
return boost::report_errors();
}

View File

@ -0,0 +1,30 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct P { };
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::pointer_traits<int*>::pointer>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P<int>,
boost::pointer_traits<P<int> >::pointer>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<void*,
boost::pointer_traits<void*>::pointer>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P<void>,
boost::pointer_traits<P<void> >::pointer>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const int*,
boost::pointer_traits<const int*>::pointer>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P<const int>,
boost::pointer_traits<P<const int> >::pointer>));
return boost::report_errors();
}

View File

@ -0,0 +1,63 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
class pointer {
public:
typedef typename boost::pointer_traits<T>::element_type element_type;
pointer(T value)
: value_(value) { }
T get() const BOOST_NOEXCEPT {
return value_;
}
static pointer<T> pointer_to(element_type& value) {
return pointer<T>(&value);
}
private:
T value_;
};
template<class T>
inline bool
operator==(const pointer<T>& lhs, const pointer<T>& rhs) BOOST_NOEXCEPT
{
return lhs.get() == rhs.get();
}
int main()
{
int i = 0;
{
typedef int* type;
type p = &i;
BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
}
{
typedef pointer<int*> type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
}
{
typedef pointer<pointer<int*> > type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
}
{
typedef const int* type;
type p = &i;
BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
}
{
typedef pointer<const int*> type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
}
return boost::report_errors();
}

View File

@ -0,0 +1,107 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>
template<class T>
struct P1 { };
template<class T1, class T2>
struct P2 { };
template<class T1, class T2, class T3>
struct P3 { };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class T>
struct E1 {
template<class U>
using rebind = E1<bool>;
};
template<class T1, class T2>
struct E2 {
template<class U>
using rebind = E2<bool, T2>;
};
template<class T1, class T2, class T3>
struct E3 {
template<class U>
using rebind = E3<bool, T2, T3>;
};
#endif
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class T, class... U>
struct P { };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class T, class... U>
struct E {
template<class V>
using rebind = E<bool, U...>;
};
#endif
#endif
struct R { };
int main()
{
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<char*,
boost::pointer_traits<R*>::rebind_to<char>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P1<char>,
boost::pointer_traits<P1<R> >::rebind_to<char>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P2<char, R>,
boost::pointer_traits<P2<R, R> >::rebind_to<char>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P3<char, R, R>,
boost::pointer_traits<P3<R, R, R> >::rebind_to<char>::type>));
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P<char, R, R, R>,
boost::pointer_traits<P<R, R, R, R> >::rebind_to<char>::type>));
#endif
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<void*,
boost::pointer_traits<R*>::rebind_to<void>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P1<void>,
boost::pointer_traits<P1<R> >::rebind_to<void>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<R*,
boost::pointer_traits<void*>::rebind_to<R>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P1<R>,
boost::pointer_traits<P1<void> >::rebind_to<R>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<const int*,
boost::pointer_traits<R*>::rebind_to<const int>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P1<const int>,
boost::pointer_traits<P1<R> >::rebind_to<const int>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<int*,
boost::pointer_traits<const R*>::rebind_to<int>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<P1<int>,
boost::pointer_traits<P1<const R> >::rebind_to<int>::type>));
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E1<bool>,
boost::pointer_traits<E1<R> >::rebind_to<char>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E2<bool, R>,
boost::pointer_traits<E2<R, R> >::rebind_to<char>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E3<bool, R, R>,
boost::pointer_traits<E3<R, R, R> >::rebind_to<char>::type>));
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E<bool, R, R, R>,
boost::pointer_traits<E<R, R, R, R> >::rebind_to<char>::type>));
#endif
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E1<bool>,
boost::pointer_traits<E1<R> >::rebind_to<void>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E1<bool>,
boost::pointer_traits<E1<void> >::rebind_to<R>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E1<bool>,
boost::pointer_traits<E1<R> >::rebind_to<const int>::type>));
BOOST_TEST_TRAIT_TRUE((boost::core::is_same<E1<bool>,
boost::pointer_traits<E1<const R> >::rebind_to<int>::type>));
#endif
return boost::report_errors();
}

View File

@ -0,0 +1,63 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
class pointer {
public:
typedef typename boost::pointer_traits<T>::element_type element_type;
pointer(T value)
: value_(value) { }
T operator->() const BOOST_NOEXCEPT {
return value_;
}
private:
T value_;
};
int main()
{
int i = 0;
{
typedef int* type;
type p = &i;
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
{
typedef pointer<int*> type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
{
typedef pointer<pointer<int*> > type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
{
typedef void* type;
type p = &i;
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
{
typedef pointer<void*> type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
{
typedef const int* type;
type p = &i;
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
{
typedef pointer<const int*> type;
type p(&i);
BOOST_TEST(boost::pointer_traits<type>::to_address(p) == &i);
}
return boost::report_errors();
}