9 #ifndef LIBPMEMOBJ_CPP_DESTROYER_HPP
10 #define LIBPMEMOBJ_CPP_DESTROYER_HPP
13 #include <type_traits>
37 struct if_not_array<T[]>;
42 template <
typename T,
size_t N>
43 struct if_not_array<T[N]>;
55 struct if_size_array<T[]>;
60 template <
typename T,
size_t N>
61 struct if_size_array<T[N]> {
70 template <
typename T,
typename... Args>
72 create(
typename if_not_array<T>::type *ptr, Args &&... args)
74 #if __cpp_lib_is_aggregate
75 if constexpr (std::is_aggregate_v<T>)
76 new (
static_cast<void *
>(ptr)) T{std::forward<Args>(args)...};
78 new (
static_cast<void *
>(ptr)) T(std::forward<Args>(args)...);
80 new (
static_cast<void *
>(ptr)) T(std::forward<Args>(args)...);
87 template <
typename T,
typename... Args>
89 create(
typename if_size_array<T>::type *ptr, Args &&... args)
91 typedef typename detail::pp_array_type<T>::type I;
92 enum { N = pp_array_elems<T>::elems };
94 for (std::size_t i = 0; i < N; ++i)
95 create<I>(&(*ptr)[i], std::forward<Args>(args)...);
103 template <
typename T,
size_t... Indices,
typename Tuple>
105 create_from_tuple(
void *ptr, index_sequence<Indices...>, Tuple tuple)
107 new (ptr) T(std::get<Indices>(std::move(tuple))...);
115 template <
typename T,
typename Tuple,
typename... Args>
117 c_style_construct(
void *ptr,
void *arg)
119 auto *arg_pack =
static_cast<Tuple *
>(arg);
121 typedef typename make_index_sequence<Args...>::type index;
123 create_from_tuple<T>(ptr, index(), std::move(*arg_pack));
134 template <
typename T,
135 typename =
typename std::enable_if<
136 !std::is_trivially_destructible<T>::value>::type>
138 destroy(
typename if_not_array<T>::type &arg)
146 template <
typename T,
typename dummy = void,
147 typename =
typename std::enable_if<
148 std::is_trivially_destructible<T>::value>::type>
150 destroy(
typename if_not_array<T>::type &)
157 template <
typename T>
159 destroy(
typename if_size_array<T>::type &arg)
161 typedef typename detail::pp_array_type<T>::type I;
162 enum { N = pp_array_elems<T>::elems };
164 for (std::size_t i = 0; i < N; ++i)
165 destroy<I>(arg[N - 1 - i]);