1 #ifndef ZSERIO_ALLOCATOR_PROPAGATING_COPY_H_INC
2 #define ZSERIO_ALLOCATOR_PROPAGATING_COPY_H_INC
30 template <
typename T,
typename ALLOC>
33 template <
typename T,
typename ALLOC,
typename ALLOC2>
36 template <
typename T,
typename ALLOC>
39 template <
typename T,
typename ALLOC,
typename ALLOC2>
47 template <
typename CharT,
typename Traits,
typename ALLOC1,
typename ALLOC2>
48 std::basic_string<CharT, Traits, ALLOC1> allocatorPropagatingCopyDefault(
49 std::true_type,
const std::basic_string<CharT, Traits, ALLOC1>& source,
const ALLOC2& allocator)
51 return std::basic_string<CharT, Traits, ALLOC1>(source.c_str(), source.length(), allocator);
56 template <
typename ALLOC,
typename ALLOC2>
57 std::vector<bool, ALLOC> allocatorPropagatingCopyVec(
58 std::false_type,
const std::vector<bool, ALLOC>& source,
const ALLOC2& allocator)
60 std::vector<bool, ALLOC> ret(allocator);
61 ret.reserve(source.size());
62 ret.assign(source.begin(), source.end());
67 template <
typename T,
typename ALLOC>
68 T allocatorPropagatingCopyDefault(std::true_type,
const T& source,
const ALLOC& allocator)
70 return T(source, allocator);
74 template <
typename T,
typename ALLOC>
75 T allocatorPropagatingCopyDefault(std::false_type,
const T& source,
const ALLOC&)
81 template <
typename T,
typename ALLOC>
82 T allocatorPropagatingCopyPropagating(std::true_type,
const T& source,
const ALLOC& allocator)
88 template <
typename T,
typename ALLOC>
89 T allocatorPropagatingCopyPropagating(std::true_type, NoInitT,
const T& source,
const ALLOC& allocator)
95 template <
typename T,
typename ALLOC>
96 T allocatorPropagatingCopyPropagating(std::false_type,
const T& source,
const ALLOC& allocator)
98 return allocatorPropagatingCopyDefault(std::uses_allocator<T, ALLOC>(), source, allocator);
102 template <
typename T,
typename ALLOC,
typename ALLOC2>
103 std::vector<T, ALLOC> allocatorPropagatingCopyVec(
104 std::true_type,
const std::vector<T, ALLOC>& source,
const ALLOC2& allocator)
106 std::vector<T, ALLOC> result(allocator);
107 result.reserve(source.size());
108 (void)std::transform(source.begin(), source.end(), std::back_inserter(result), [&](
const T& value) {
115 template <
typename T,
typename ALLOC,
typename ALLOC2>
116 std::vector<T, ALLOC> allocatorPropagatingCopyVec(
117 std::true_type, NoInitT,
const std::vector<T, ALLOC>& source,
const ALLOC2& allocator)
119 std::vector<T, ALLOC> result(allocator);
120 result.reserve(source.size());
121 (void)std::transform(source.begin(), source.end(), std::back_inserter(result), [&](
const T& value) {
128 template <
typename T,
typename ALLOC,
typename ALLOC2>
129 std::vector<T, ALLOC> allocatorPropagatingCopyVec(
130 std::false_type,
const std::vector<T, ALLOC>& source,
const ALLOC2& allocator)
132 return std::vector<T, ALLOC>(source, allocator);
135 template <
typename T,
typename ALLOC>
136 T allocatorPropagatingCopyImpl(
const T& source,
const ALLOC& allocator)
138 return allocatorPropagatingCopyPropagating(
139 std::is_constructible<T, PropagateAllocatorT, T, ALLOC>(), source, allocator);
142 template <
typename T,
typename ALLOC>
143 T allocatorPropagatingCopyImpl(NoInitT,
const T& source,
const ALLOC& allocator)
145 static_assert(std::is_constructible<T, NoInitT, T>::value,
"Can be used only for parameterized compounds!");
147 return allocatorPropagatingCopyPropagating(
148 std::is_constructible<T, PropagateAllocatorT, NoInitT, T, ALLOC>(),
NoInit, source, allocator);
151 template <
typename T,
typename ALLOC,
typename ALLOC2>
152 HeapOptionalHolder<T, ALLOC> allocatorPropagatingCopyImpl(
153 const HeapOptionalHolder<T, ALLOC>& source,
const ALLOC2& allocator)
155 if (source.hasValue())
161 return HeapOptionalHolder<T, ALLOC>(allocator);
165 template <
typename T,
typename ALLOC,
typename ALLOC2>
166 HeapOptionalHolder<T, ALLOC> allocatorPropagatingCopyImpl(
167 NoInitT,
const HeapOptionalHolder<T, ALLOC>& source,
const ALLOC2& allocator)
169 static_assert(std::is_constructible<T, NoInitT, T>::value,
"Can be used only for parameterized compounds!");
171 if (source.hasValue())
173 return HeapOptionalHolder<T, ALLOC>(
178 return HeapOptionalHolder<T, ALLOC>(allocator);
182 template <
typename T,
typename ALLOC>
183 InplaceOptionalHolder<T> allocatorPropagatingCopyImpl(
184 const InplaceOptionalHolder<T>& source,
const ALLOC& allocator)
186 if (source.hasValue())
192 return InplaceOptionalHolder<T>();
196 template <
typename T,
typename ALLOC>
197 InplaceOptionalHolder<T> allocatorPropagatingCopyImpl(
198 NoInitT,
const InplaceOptionalHolder<T>& source,
const ALLOC& allocator)
200 static_assert(std::is_constructible<T, NoInitT, T>::value,
"Can be used only for parameterized compounds!");
202 if (source.hasValue())
208 return InplaceOptionalHolder<T>();
212 template <
typename T,
typename ALLOC,
typename ALLOC2>
213 AnyHolder<ALLOC> allocatorPropagatingCopyImpl(
const AnyHolder<ALLOC>& source,
const ALLOC2& allocator)
215 if (source.hasValue())
221 return AnyHolder<ALLOC>(allocator);
225 template <
typename T,
typename ALLOC,
typename ALLOC2>
226 AnyHolder<ALLOC> allocatorPropagatingCopyImpl(NoInitT,
const AnyHolder<ALLOC>& source,
const ALLOC2& allocator)
228 if (source.hasValue())
230 return AnyHolder<ALLOC>(
235 return AnyHolder<ALLOC>(allocator);
239 template <
typename T,
typename ALLOC,
typename ALLOC2>
240 std::vector<T, ALLOC> allocatorPropagatingCopyImpl(
const std::vector<T, ALLOC>& source,
const ALLOC2& allocator)
242 return allocatorPropagatingCopyVec(std::uses_allocator<T, ALLOC>(), source, allocator);
245 template <
typename T,
typename ALLOC,
typename ALLOC2>
246 std::vector<T, ALLOC> allocatorPropagatingCopyImpl(
247 NoInitT,
const std::vector<T, ALLOC>& source,
const ALLOC2& allocator)
249 static_assert(std::is_constructible<T, NoInitT, T>::value,
"Can be used only for parameterized compounds!");
251 return allocatorPropagatingCopyVec(std::uses_allocator<T, ALLOC>(),
NoInit, source, allocator);
264 template <
typename T,
typename ALLOC>
267 static_assert(!std::is_same<
AnyHolder<ALLOC>, T>::value,
"Cannot be used for AnyHolder!");
269 return detail::allocatorPropagatingCopyImpl(source, allocator);
280 template <
typename T,
typename ALLOC>
283 static_assert(!std::is_same<
AnyHolder<ALLOC>, T>::value,
"Cannot be used for AnyHolder!");
285 return detail::allocatorPropagatingCopyImpl(
NoInit, source, allocator);
296 template <
typename T,
typename ALLOC,
typename ALLOC2>
299 return detail::allocatorPropagatingCopyImpl<T>(source, allocator);
310 template <
typename T,
typename ALLOC,
typename ALLOC2>
313 static_assert(std::is_constructible<T, NoInitT, T>::value,
"Can be used only for parameterized compounds!");
315 return detail::allocatorPropagatingCopyImpl<T>(
NoInit, source, allocator);
constexpr PropagateAllocatorT PropagateAllocator
T allocatorPropagatingCopy(const T &source, const ALLOC &allocator)
constexpr PropagateAllocatorT()=default