Simplify alloc_construct using allocator_access

This commit is contained in:
Glen Fernandes
2020-05-24 00:03:14 -04:00
parent 9f2dbba2fc
commit 27715ed01a
2 changed files with 14 additions and 90 deletions

View File

@ -12,12 +12,11 @@ Distributed under the Boost Software License, Version 1.0.
namespace boost { namespace boost {
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class A, class T> template<class A, class T>
inline void inline void
alloc_destroy(A& a, T* p) alloc_destroy(A& a, T* p)
{ {
std::allocator_traits<A>::destroy(a, p); boost::allocator_destroy(a, p);
} }
template<class A, class T> template<class A, class T>
@ -25,26 +24,25 @@ inline void
alloc_destroy_n(A& a, T* p, std::size_t n) alloc_destroy_n(A& a, T* p, std::size_t n)
{ {
while (n > 0) { while (n > 0) {
std::allocator_traits<A>::destroy(a, p + --n); boost::allocator_destroy(a, p + --n);
} }
} }
#else
template<class A, class T> template<class A, class T>
inline void inline void
alloc_destroy(A&, T* p) alloc_destroy(noinit_adaptor<A>&, T* p)
{ {
p->~T(); p->~T();
} }
template<class A, class T> template<class A, class T>
inline void inline void
alloc_destroy_n(A&, T* p, std::size_t n) alloc_destroy_n(noinit_adaptor<A>&, T* p, std::size_t n)
{ {
while (n > 0) { while (n > 0) {
p[--n].~T(); p[--n].~T();
} }
} }
#endif
namespace detail { namespace detail {
@ -75,12 +73,11 @@ private:
} /* detail */ } /* detail */
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class A, class T> template<class A, class T>
inline void inline void
alloc_construct(A& a, T* p) alloc_construct(A& a, T* p)
{ {
std::allocator_traits<A>::construct(a, p); boost::allocator_construct(a, p);
} }
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
@ -89,7 +86,7 @@ template<class A, class T, class U, class... V>
inline void inline void
alloc_construct(A& a, T* p, U&& u, V&&... v) alloc_construct(A& a, T* p, U&& u, V&&... v)
{ {
std::allocator_traits<A>::construct(a, p, std::forward<U>(u), boost::allocator_construct(a, p, std::forward<U>(u),
std::forward<V>(v)...); std::forward<V>(v)...);
} }
#else #else
@ -97,7 +94,7 @@ template<class A, class T, class U>
inline void inline void
alloc_construct(A& a, T* p, U&& u) alloc_construct(A& a, T* p, U&& u)
{ {
std::allocator_traits<A>::construct(a, p, std::forward<U>(u)); boost::allocator_construct(a, p, std::forward<U>(u));
} }
#endif #endif
#else #else
@ -105,14 +102,14 @@ template<class A, class T, class U>
inline void inline void
alloc_construct(A& a, T* p, const U& u) alloc_construct(A& a, T* p, const U& u)
{ {
std::allocator_traits<A>::construct(a, p, u); boost::allocator_construct(a, p, u);
} }
template<class A, class T, class U> template<class A, class T, class U>
inline void inline void
alloc_construct(A& a, T* p, U& u) alloc_construct(A& a, T* p, U& u)
{ {
std::allocator_traits<A>::construct(a, p, u); boost::allocator_construct(a, p, u);
} }
#endif #endif
@ -122,7 +119,7 @@ alloc_construct_n(A& a, T* p, std::size_t n)
{ {
detail::alloc_destroyer<A, T> hold(a, p); detail::alloc_destroyer<A, T> hold(a, p);
for (std::size_t& i = hold.size(); i < n; ++i) { for (std::size_t& i = hold.size(); i < n; ++i) {
std::allocator_traits<A>::construct(a, p + i); boost::allocator_construct(a, p + i);
} }
hold.size() = 0; hold.size() = 0;
} }
@ -133,7 +130,7 @@ alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m)
{ {
detail::alloc_destroyer<A, T> hold(a, p); detail::alloc_destroyer<A, T> hold(a, p);
for (std::size_t& i = hold.size(); i < n; ++i) { for (std::size_t& i = hold.size(); i < n; ++i) {
std::allocator_traits<A>::construct(a, p + i, l[i % m]); boost::allocator_construct(a, p + i, l[i % m]);
} }
hold.size() = 0; hold.size() = 0;
} }
@ -144,17 +141,10 @@ alloc_construct_n(A& a, T* p, std::size_t n, I b)
{ {
detail::alloc_destroyer<A, T> hold(a, p); detail::alloc_destroyer<A, T> hold(a, p);
for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) { for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) {
std::allocator_traits<A>::construct(a, p + i, *b); boost::allocator_construct(a, p + i, *b);
} }
hold.size() = 0; hold.size() = 0;
} }
#else
template<class A, class T>
inline void
alloc_construct(A&, T* p)
{
::new(static_cast<void*>(p)) T();
}
template<class A, class T> template<class A, class T>
inline void inline void
@ -163,49 +153,6 @@ alloc_construct(noinit_adaptor<A>&, T* p)
::new(static_cast<void*>(p)) T; ::new(static_cast<void*>(p)) T;
} }
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class A, class T, class U, class... V>
inline void
alloc_construct(A&, T* p, U&& u, V&&... v)
{
::new(static_cast<void*>(p)) T(std::forward<U>(u), std::forward<V>(v)...);
}
#else
template<class A, class T, class U>
inline void
alloc_construct(A& a, T* p, U&& u)
{
::new(static_cast<void*>(p)) T(std::forward<U>(u));
}
#endif
#else
template<class A, class T, class U>
inline void
alloc_construct(A&, T* p, const U& u)
{
::new(static_cast<void*>(p)) T(u);
}
template<class A, class T, class U>
inline void
alloc_construct(A&, T* p, U& u)
{
::new(static_cast<void*>(p)) T(u);
}
#endif
template<class A, class T>
inline void
alloc_construct_n(A& a, T* p, std::size_t n)
{
detail::alloc_destroyer<A, T> hold(a, p);
for (std::size_t& i = hold.size(); i < n; ++i) {
::new(static_cast<void*>(p + i)) T();
}
hold.size() = 0;
}
template<class A, class T> template<class A, class T>
inline void inline void
alloc_construct_n(noinit_adaptor<A>& a, T* p, std::size_t n) alloc_construct_n(noinit_adaptor<A>& a, T* p, std::size_t n)
@ -217,29 +164,6 @@ alloc_construct_n(noinit_adaptor<A>& a, T* p, std::size_t n)
hold.size() = 0; hold.size() = 0;
} }
template<class A, class T>
inline void
alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m)
{
detail::alloc_destroyer<A, T> hold(a, p);
for (std::size_t& i = hold.size(); i < n; ++i) {
::new(static_cast<void*>(p + i)) T(l[i % m]);
}
hold.size() = 0;
}
template<class A, class T, class I>
inline void
alloc_construct_n(A& a, T* p, std::size_t n, I b)
{
detail::alloc_destroyer<A, T> hold(a, p);
for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) {
::new(static_cast<void*>(p + i)) T(*b);
}
hold.size() = 0;
}
#endif
} /* boost */ } /* boost */
#endif #endif

View File

@ -6,7 +6,7 @@ Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt) (http://www.boost.org/LICENSE_1_0.txt)
*/ */
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if defined(BOOST_CORE_ALLOCATOR_DETECTION)
#include <boost/core/alloc_construct.hpp> #include <boost/core/alloc_construct.hpp>
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>