libostd/ostd/memory.hh

769 lines
20 KiB
C++
Raw Normal View History

/* Memory utilities for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_MEMORY_HH
#define OSTD_MEMORY_HH
2015-05-13 21:56:36 +02:00
#include <stddef.h>
2017-01-14 15:09:27 +01:00
#include <new>
2017-01-29 15:16:13 +01:00
#include <memory>
2015-05-13 21:56:36 +02:00
2015-07-13 21:08:55 +02:00
#include "ostd/utility.hh"
#include "ostd/type_traits.hh"
2015-04-27 19:56:17 +02:00
2015-07-13 21:07:14 +02:00
namespace ostd {
2015-06-04 03:20:20 +02:00
/* address of */
2015-04-27 19:56:17 +02:00
2016-09-11 20:46:34 +02:00
template<typename T> constexpr T *address_of(T &v) noexcept {
return reinterpret_cast<T *>(
&const_cast<char &>(reinterpret_cast<char const volatile &>(v))
);
2015-06-04 03:20:20 +02:00
}
2015-04-27 19:56:17 +02:00
2015-06-04 03:20:20 +02:00
/* pointer traits */
2015-04-27 19:56:17 +02:00
2015-06-04 03:20:20 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:20:20 +02:00
struct HasElement {
template<typename U>
static int test(...);
template<typename U>
static char test(typename U::Element * = 0);
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T, bool = HasElement<T>::value>
2015-06-04 03:20:20 +02:00
struct PointerElementBase;
2015-04-27 19:56:17 +02:00
template<typename T>
struct PointerElementBase<T, true> {
2015-06-08 01:55:08 +02:00
using Type = typename T::Element;
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<template<typename, typename...> class T, typename U, typename ...A>
struct PointerElementBase<T<U, A...>, true> {
2015-06-08 01:55:08 +02:00
using Type = typename T<U, A...>::Element;
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<template<typename, typename...> class T, typename U, typename ...A>
struct PointerElementBase<T<U, A...>, false> {
2015-06-08 01:55:08 +02:00
using Type = U;
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:20:20 +02:00
struct PointerElementType {
2015-06-08 01:55:08 +02:00
using Type = typename PointerElementBase<T>::Type;
2015-06-04 03:20:20 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
struct PointerElementType<T *> {
2015-06-08 01:55:08 +02:00
using Type = T;
2015-06-04 03:20:20 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:20:20 +02:00
struct HasDifference {
template<typename U>
static int test(...);
template<typename U>
static char test(typename U::Difference * = 0);
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T, bool = HasDifference<T>::value>
2015-06-04 03:20:20 +02:00
struct PointerDifferenceBase {
using Type = Ptrdiff;
2015-04-27 19:56:17 +02:00
};
template<typename T>
struct PointerDifferenceBase<T, true> {
2015-06-08 01:55:08 +02:00
using Type = typename T::Difference;
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:20:20 +02:00
struct PointerDifferenceType {
2015-06-08 01:55:08 +02:00
using Type = typename PointerDifferenceBase<T>::Type;
2015-06-04 03:20:20 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
struct PointerDifferenceType<T *> {
using Type = Ptrdiff;
2015-06-04 03:20:20 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
2015-06-04 03:20:20 +02:00
struct HasRebind {
template<typename V>
static int test(...);
template<typename V>
static char test(typename V::template Rebind<U> * = 0);
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T, typename U, bool = HasRebind<T, U>::value>
2015-06-04 03:20:20 +02:00
struct PointerRebindBase {
2015-06-08 01:55:08 +02:00
using Type = typename T::template Rebind<U>;
2015-04-27 19:56:17 +02:00
};
template<
template<typename, typename...> class T, typename U,
2015-06-04 23:57:06 +02:00
typename ...A, typename V
2015-04-27 19:56:17 +02:00
>
2015-06-04 23:57:06 +02:00
struct PointerRebindBase<T<U, A...>, V, true> {
2015-06-08 01:55:08 +02:00
using Type = typename T<U, A...>::template Rebind<V>;
2015-04-27 19:56:17 +02:00
};
template<
template<typename, typename...> class T, typename U,
2015-06-04 23:57:06 +02:00
typename ...A, typename V
2015-04-27 19:56:17 +02:00
>
2015-06-04 23:57:06 +02:00
struct PointerRebindBase<T<U, A...>, V, false> {
2015-06-08 01:55:08 +02:00
using Type = T<V, A...>;
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
2015-06-04 03:20:20 +02:00
struct PointerRebindType {
2016-09-18 19:22:54 +02:00
using Type = typename PointerRebindBase<T, U>::Type;
};
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
struct PointerRebindType<T *, U> {
2016-09-18 19:22:54 +02:00
using Type = U *;
};
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:20:20 +02:00
struct PointerPointer {
2015-06-08 01:55:08 +02:00
using Type = T;
2015-04-27 19:56:17 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
struct PointerPointer<T *> {
2015-06-08 01:55:08 +02:00
using Type = T *;
};
2015-06-04 03:20:20 +02:00
} /*namespace detail */
2015-06-04 23:57:06 +02:00
template<typename T>
using Pointer = typename detail::PointerPointer<T>::Type;
2015-06-04 23:57:06 +02:00
template<typename T>
using PointerElement = typename detail::PointerElementType<T>::Type;
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
using PointerDifference = typename detail::PointerDifferenceType<T>::Type;
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
using PointerRebind = typename detail::PointerRebindType<T, U>::Type;
2015-06-04 03:20:20 +02:00
/* pointer to */
2015-06-04 03:20:20 +02:00
namespace detail {
struct PointerToNat {};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:20:20 +02:00
struct PointerTo {
static T pointer_to(
Conditional<
IsVoid<PointerElement<T>>, PointerToNat, PointerElement<T>
> &r
) {
2015-06-04 23:57:06 +02:00
return T::pointer_to(r);
}
};
2015-06-04 23:57:06 +02:00
template<typename T>
struct PointerTo<T *> {
static T pointer_to(Conditional<IsVoid<T>, PointerToNat, T> &r) {
return address_of(r);
2015-04-27 19:56:17 +02:00
}
};
2015-06-04 03:20:20 +02:00
}
2015-04-27 19:56:17 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
static T pointer_to(
Conditional<
IsVoid<PointerElement<T>>, detail::PointerToNat, PointerElement<T>
> &r
) {
return detail::PointerTo<T>::pointer_to(r);
2015-06-04 03:20:20 +02:00
}
2015-06-04 03:09:07 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 03:09:07 +02:00
static int ptr_test(...);
2015-06-04 23:57:06 +02:00
template<typename T>
static char ptr_test(typename T::Pointer * = 0);
2015-04-27 19:56:17 +02:00
template<typename T>
constexpr bool HasPtr = sizeof(ptr_test<T>(0)) == 1;
2015-04-27 19:56:17 +02:00
2016-01-20 19:09:44 +01:00
template<typename T, typename D, bool = HasPtr<D>>
2015-06-04 03:09:07 +02:00
struct PointerBase {
2015-06-08 01:55:08 +02:00
using Type = typename D::Pointer;
2015-04-27 19:56:17 +02:00
};
template<typename T, typename D>
struct PointerBase<T, D, false> {
2015-06-08 01:55:08 +02:00
using Type = T *;
2015-04-27 19:56:17 +02:00
};
template<typename T, typename D>
struct PointerType {
using Type = typename PointerBase<T, RemoveReference<D>>::Type;
2015-04-27 19:56:17 +02:00
};
2015-06-04 03:09:07 +02:00
} /* namespace detail */
2015-04-27 19:56:17 +02:00
2015-06-04 02:56:04 +02:00
/* allocator */
2015-05-13 21:56:36 +02:00
template<typename T>
struct Allocator {
2015-06-08 01:55:08 +02:00
using Value = T;
2015-05-13 21:56:36 +02:00
2016-09-11 20:46:34 +02:00
Allocator() noexcept {}
template<typename U>
2016-09-11 20:46:34 +02:00
Allocator(Allocator<U> const &) noexcept {}
2015-06-14 03:36:20 +02:00
2016-09-18 19:22:54 +02:00
Value *allocate(Size n) {
return reinterpret_cast<Value *>(::new byte[n * sizeof(T)]);
2015-06-04 02:56:04 +02:00
}
2015-05-13 21:56:36 +02:00
2016-09-18 19:22:54 +02:00
void deallocate(Value *p, Size) noexcept {
2016-09-11 20:46:34 +02:00
::delete[] reinterpret_cast<byte *>(p);
}
2015-06-04 02:56:04 +02:00
};
2015-05-13 21:56:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
2016-09-11 20:46:34 +02:00
bool operator==(Allocator<T> const &, Allocator<U> const &) noexcept {
2015-06-04 02:56:04 +02:00
return true;
}
2015-05-13 21:56:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
2016-09-11 20:46:34 +02:00
bool operator!=(Allocator<T> const &, Allocator<U> const &) noexcept {
2015-06-04 02:56:04 +02:00
return false;
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator traits - modeled after libc++ */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct ConstPtrTest {
template<typename U>
static char test(typename U::ConstPointer * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
template<typename T, typename P, typename A, bool = ConstPtrTest<A>::value>
2015-06-04 02:56:04 +02:00
struct ConstPointer {
2015-06-08 01:55:08 +02:00
using Type = typename A::ConstPointer;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T, typename P, typename A>
struct ConstPointer<T, P, A, false> {
2016-06-23 20:18:35 +02:00
using Type = PointerRebind<P, T const>;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct VoidPtrTest {
template<typename U>
static char test(typename U::VoidPointer * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename P, typename A, bool = VoidPtrTest<A>::value>
2015-06-04 02:56:04 +02:00
struct VoidPointer {
2015-06-08 01:55:08 +02:00
using Type = typename A::VoidPointer;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename P, typename A>
struct VoidPointer<P, A, false> {
2015-06-08 01:55:08 +02:00
using Type = PointerRebind<P, void>;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct ConstVoidPtrTest {
2015-06-04 23:57:06 +02:00
template<typename U> static char test(
typename U::ConstVoidPointer * = 0);
template<typename U> static int test(...);
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename P, typename A, bool = ConstVoidPtrTest<A>::value>
2015-06-04 02:56:04 +02:00
struct ConstVoidPointer {
2015-06-08 01:55:08 +02:00
using Type = typename A::ConstVoidPointer;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename P, typename A>
struct ConstVoidPointer<P, A, false> {
2016-06-23 20:18:35 +02:00
using Type = PointerRebind<P, void const>;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct SizeTest {
template<typename U>
static char test(typename U::Size * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename A, typename D, bool = SizeTest<A>::value>
2015-06-04 02:56:04 +02:00
struct SizeBase {
using Type = MakeUnsigned<D>;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename A, typename D>
struct SizeBase<A, D, true> {
2015-06-08 01:55:08 +02:00
using Type = typename A::Size;
2015-06-03 20:47:36 +02:00
};
2015-06-04 02:56:04 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator type traits */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorType = A;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorValue = typename AllocatorType<A>::Value;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorPointer = typename detail::PointerType<
AllocatorValue<A>, AllocatorType<A>
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorConstPointer = typename detail::ConstPointer<
2015-06-04 23:57:06 +02:00
AllocatorValue<A>, AllocatorPointer<A>, AllocatorType<A>
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorVoidPointer = typename detail::VoidPointer<
2015-06-04 23:57:06 +02:00
AllocatorPointer<A>, AllocatorType<A>
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorConstVoidPointer = typename detail::ConstVoidPointer<
2015-06-04 23:57:06 +02:00
AllocatorPointer<A>, AllocatorType<A>
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator difference */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct DiffTest {
template<typename U>
static char test(typename U::Difference * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename A, typename P, bool = DiffTest<A>::value>
2015-06-04 02:56:04 +02:00
struct AllocDifference {
2015-06-08 01:55:08 +02:00
using Type = PointerDifference<P>;
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename A, typename P>
struct AllocDifference<A, P, true> {
2015-06-08 01:55:08 +02:00
using Type = typename A::Difference;
2015-06-03 20:47:36 +02:00
};
2015-06-04 02:56:04 +02:00
}
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorDifference = typename detail::AllocDifference<
2015-06-04 23:57:06 +02:00
A, AllocatorPointer<A>
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator size */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
using AllocatorSize = typename detail::SizeBase<
2015-06-04 23:57:06 +02:00
A, AllocatorDifference<A>
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator rebind */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
template<typename T, typename U, bool = detail::HasRebind<T, U>::value>
2015-06-04 02:56:04 +02:00
struct AllocTraitsRebindType {
2015-06-08 01:55:08 +02:00
using Type = typename T::template Rebind<U>;
2015-06-03 20:47:36 +02:00
};
template<
template<typename, typename...> class A, typename T,
2015-06-04 23:57:06 +02:00
typename ...Args, typename U
2015-06-03 20:47:36 +02:00
>
2015-06-04 23:57:06 +02:00
struct AllocTraitsRebindType<A<T, Args...>, U, true> {
2015-06-08 01:55:08 +02:00
using Type = typename A<T, Args...>::template Rebind<U>;
2015-06-03 20:47:36 +02:00
};
template<
template<typename, typename...> class A, typename T,
2015-06-04 23:57:06 +02:00
typename ...Args, typename U
2015-06-03 20:47:36 +02:00
>
2015-06-04 23:57:06 +02:00
struct AllocTraitsRebindType<A<T, Args...>, U, false> {
2015-06-08 01:55:08 +02:00
using Type = A<U, Args...>;
2015-06-03 20:47:36 +02:00
};
2015-06-04 02:56:04 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T>
using AllocatorRebind = typename detail::AllocTraitsRebindType<
2015-06-04 23:57:06 +02:00
AllocatorType<A>, T
2015-06-04 02:56:04 +02:00
>::Type;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator propagate on container copy assignment */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct PropagateOnContainerCopyAssignmentTest {
template<typename U>
static char test(decltype(U::PropagateOnContainerCopyAssignment) * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
template<typename A, bool =
PropagateOnContainerCopyAssignmentTest<A>::value
>
constexpr bool PropagateOnContainerCopyAssignmentBase = false;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool PropagateOnContainerCopyAssignmentBase<A, true> =
A::PropagateOnContainerCopyAssignment;
2015-06-04 02:56:04 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool AllocatorPropagateOnContainerCopyAssignment =
detail::PropagateOnContainerCopyAssignmentBase<A>;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator propagate on container move assignment */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct PropagateOnContainerMoveAssignmentTest {
template<typename U>
static char test(decltype(U::PropagateOnContainerMoveAssignment) * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
template<typename A, bool =
PropagateOnContainerMoveAssignmentTest<A>::value
>
constexpr bool PropagateOnContainerMoveAssignmentBase = false;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool PropagateOnContainerMoveAssignmentBase<A, true> =
A::PropagateOnContainerMoveAssignment;
2015-06-04 02:56:04 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool AllocatorPropagateOnContainerMoveAssignment =
detail::PropagateOnContainerMoveAssignmentBase<A>;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator propagate on container swap */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct PropagateOnContainerSwapTest {
template<typename U>
static char test(decltype(U::PropagateOnContainerSwap) * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename A, bool = PropagateOnContainerSwapTest<A>::value>
constexpr bool PropagateOnContainerSwapBase = false;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool PropagateOnContainerSwapBase<A, true> =
A::PropagateOnContainerSwap;
2015-06-04 02:56:04 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool AllocatorPropagateOnContainerSwap =
detail::PropagateOnContainerSwapBase<A>;
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
/* allocator is always equal */
2015-06-03 20:47:36 +02:00
2015-06-04 02:56:04 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 02:56:04 +02:00
struct IsAlwaysEqualTest {
template<typename U>
static char test(decltype(U::IsAlwaysEqual) * = 0);
template<typename U>
static int test(...);
2015-06-04 23:57:06 +02:00
static constexpr bool value = (sizeof(test<T>(0)) == 1);
2015-06-03 20:47:36 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename A, bool = IsAlwaysEqualTest<A>::value>
constexpr bool IsAlwaysEqualBase = IsEmpty<A>;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool IsAlwaysEqualBase<A, true> = A::IsAlwaysEqual;
2015-06-04 02:56:04 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool AllocatorIsAlwaysEqual = detail::IsAlwaysEqualBase<A>;
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
/* allocator allocate */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
inline AllocatorPointer<A> allocator_allocate(A &a, AllocatorSize<A> n) {
2015-06-04 02:45:30 +02:00
return a.allocate(n);
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename A, typename S, typename CVP>
auto allocate_hint_test(A &&a, S &&sz, CVP &&p) ->
decltype(a.allocate(sz, p), True());
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename S, typename CVP>
auto allocate_hint_test(A const &, S &&, CVP &&) -> False;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename S, typename CVP>
constexpr bool AllocateHintTest =
IsSame<
decltype(allocate_hint_test(
2017-01-29 15:29:11 +01:00
std::declval<A>(), std::declval<S>(), std::declval<CVP>()
)), True
>;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
inline AllocatorPointer<A> allocate(
A &a, AllocatorSize<A> n, AllocatorConstVoidPointer<A> h, True
) {
2015-06-04 02:45:30 +02:00
return a.allocate(n, h);
2015-06-03 20:47:36 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename A>
inline AllocatorPointer<A> allocate(
A &a, AllocatorSize<A> n, AllocatorConstVoidPointer<A>, False
) {
2015-06-04 02:45:30 +02:00
return a.allocate(n);
2015-06-03 20:47:36 +02:00
}
2015-06-04 02:45:30 +02:00
} /* namespace detail */
2015-06-04 23:57:06 +02:00
template<typename A>
inline AllocatorPointer<A> allocator_allocate(
A &a, AllocatorSize<A> n, AllocatorConstVoidPointer<A> h
) {
return detail::allocate(
a, n, h, BoolConstant<detail::AllocateHintTest<
2015-06-04 23:57:06 +02:00
A, AllocatorSize<A>, AllocatorConstVoidPointer<A>
>>()
);
2015-06-04 02:45:30 +02:00
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
/* allocator deallocate */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
inline void allocator_deallocate(
A &a, AllocatorPointer<A> p, AllocatorSize<A> n
2016-09-11 20:46:34 +02:00
) noexcept {
2015-06-04 02:45:30 +02:00
a.deallocate(p, n);
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
/* allocator construct */
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename A, typename T, typename ...Args>
auto construct_test(A &&a, T *p, Args &&...args) ->
2017-01-25 01:44:22 +01:00
decltype(a.construct(p, std::forward<Args>(args)...), True());
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T, typename ...Args>
auto construct_test(A const &, T *, Args &&...) -> False;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T, typename ...Args>
constexpr bool ConstructTest =
IsSame<
decltype(construct_test(
2017-01-29 15:29:11 +01:00
std::declval<A>(), std::declval<T>(), std::declval<Args>()...
)), True
>;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T, typename ...Args>
inline void construct(True, A &a, T *p, Args &&...args) {
2017-01-25 01:44:22 +01:00
a.construct(p, std::forward<Args>(args)...);
2015-06-03 20:47:36 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename A, typename T, typename ...Args>
inline void construct(False, A &, T *p, Args &&...args) {
2017-01-25 01:44:22 +01:00
::new (p) T(std::forward<Args>(args)...);
2015-06-03 20:47:36 +02:00
}
2015-06-04 02:45:30 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T, typename ...Args>
inline void allocator_construct(A &a, T *p, Args &&...args) {
detail::construct(
BoolConstant<detail::ConstructTest<A, T *, Args...>>(),
2017-01-25 01:44:22 +01:00
a, p, std::forward<Args>(args)...
);
2015-06-04 02:45:30 +02:00
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
/* allocator destroy */
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename A, typename P>
auto destroy_test(A &&a, P &&p) -> decltype(a.destroy(p), True());
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename P>
2016-06-23 20:18:35 +02:00
auto destroy_test(A const &, P &&) -> False;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename P>
2017-01-29 15:29:11 +01:00
constexpr bool DestroyTest = IsSame<
decltype(destroy_test(std::declval<A>(), std::declval<P>())), True
>;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T>
inline void destroy(True, A &a, T *p) {
2015-06-04 02:45:30 +02:00
a.destroy(p);
2015-06-03 20:47:36 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename A, typename T>
inline void destroy(False, A &, T *p) {
2015-06-04 23:57:06 +02:00
p->~T();
2015-06-03 20:47:36 +02:00
}
2015-06-04 02:45:30 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A, typename T>
2016-09-11 20:46:34 +02:00
inline void allocator_destroy(A &a, T *p) noexcept {
detail::destroy(BoolConstant<detail::DestroyTest<A, T *>>(), a, p);
2015-06-04 02:45:30 +02:00
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
/* allocator max size */
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename A>
auto alloc_max_size_test(A &&a) -> decltype(a.max_size(), True());
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
auto alloc_max_size_test(A const &) -> False;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool AllocMaxSizeTest =
2017-01-29 15:29:11 +01:00
IsSame<decltype(alloc_max_size_test(std::declval<A &>())), True>;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
inline AllocatorSize<A> alloc_max_size(True, A const &a) {
2015-06-04 02:45:30 +02:00
return a.max_size();
2015-06-03 20:47:36 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
inline AllocatorSize<A> alloc_max_size(False, A const &) {
2015-06-04 23:57:06 +02:00
return AllocatorSize<A>(~0);
2015-06-03 20:47:36 +02:00
}
2015-06-04 02:45:30 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
2016-09-11 20:46:34 +02:00
inline AllocatorSize<A> allocator_max_size(A const &a) noexcept {
return detail::alloc_max_size(
BoolConstant<detail::AllocMaxSizeTest<A const>>(), a
);
2015-06-04 02:45:30 +02:00
}
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
/* allocator container copy */
2015-06-03 20:47:36 +02:00
2015-06-04 02:45:30 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename A>
auto alloc_copy_test(A &&a) -> decltype(a.container_copy(), True());
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
auto alloc_copy_test(A const &) -> False;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
constexpr bool AllocCopyTest =
2017-01-29 15:29:11 +01:00
IsSame<decltype(alloc_copy_test(std::declval<A &>())), True>;
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
inline AllocatorType<A> alloc_container_copy(True, A const &a) {
2015-06-04 02:45:30 +02:00
return a.container_copy();
2015-06-03 20:47:36 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
inline AllocatorType<A> alloc_container_copy(False, A const &a) {
2015-06-04 02:45:30 +02:00
return a;
2015-06-03 20:47:36 +02:00
}
2015-06-04 02:45:30 +02:00
} /* namespace detail */
2015-06-03 20:47:36 +02:00
2015-06-04 23:57:06 +02:00
template<typename A>
2016-06-23 20:18:35 +02:00
inline AllocatorType<A> allocator_container_copy(A const &a) {
return detail::alloc_container_copy(
BoolConstant<detail::AllocCopyTest<A const>>(), a
);
2015-06-04 02:45:30 +02:00
}
2015-06-07 02:14:04 +02:00
/* allocator arg */
2015-06-07 02:14:04 +02:00
struct AllocatorArg {};
constexpr AllocatorArg allocator_arg = AllocatorArg();
/* uses allocator */
namespace detail {
template<typename T>
struct HasAllocatorType {
template<typename U>
static char test(typename U::Allocator *);
template<typename U>
static int test(...);
static constexpr bool value = (sizeof(test<T>(0)) == 1);
};
template<typename T, typename A, bool = HasAllocatorType<T>::value>
constexpr bool UsesAllocatorBase = IsConvertible<A, typename T::Allocator>;
template<typename T, typename A>
constexpr bool UsesAllocatorBase<T, A, false> = false;
}
template<typename T, typename A>
constexpr bool UsesAllocator = detail::UsesAllocatorBase<T, A>;
/* uses allocator ctor */
namespace detail {
template<typename T, typename A, typename ...Args>
struct UsesAllocCtor {
static constexpr bool ua = UsesAllocator<T, A>;
static constexpr bool ic = IsConstructible<
T, AllocatorArg, A, Args...
2016-01-12 23:09:40 +01:00
>;
static constexpr int value = ua ? (2 - ic) : 0;
};
}
template<typename T, typename A, typename ...Args>
constexpr int UsesAllocatorConstructor =
detail::UsesAllocCtor<T, A, Args...>::value;
/* util for other classes */
namespace detail {
template<typename A>
2017-01-09 17:58:28 +01:00
struct AllocatorDestructor {
using Pointer = AllocatorPointer<A>;
using Size = ostd::Size;
AllocatorDestructor(A &a, Size s) noexcept: p_alloc(a), p_size(s) {}
void operator()(Pointer p) noexcept {
allocator_deallocate(p_alloc, p, p_size);
}
2017-01-09 17:58:28 +01:00
private:
A &p_alloc;
Size p_size;
};
}
2015-07-13 21:07:14 +02:00
} /* namespace ostd */
2016-02-07 22:17:15 +01:00
#endif