Update allocator_try_assign() to match behavior of fstream allocator for non-copyable types

Looks like the choice we have made here previously is to return NULL if
attempting to allocate types that we cannot allocate.
develop
lethosor 2023-05-28 13:08:06 -04:00
parent 6fbb22e912
commit d0250ae426
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
1 changed files with 7 additions and 3 deletions

@ -499,23 +499,27 @@ namespace df
template<class T> template<class T>
typename std::enable_if< typename std::enable_if<
std::is_copy_assignable<T>::value std::is_copy_assignable<T>::value,
void*
>::type allocator_try_assign(void *out, const void *in) { >::type allocator_try_assign(void *out, const void *in) {
*(T*)out = *(const T*)in; *(T*)out = *(const T*)in;
return out;
} }
template<class T> template<class T>
typename std::enable_if< typename std::enable_if<
!std::is_copy_assignable<T>::value !std::is_copy_assignable<T>::value,
void*
>::type allocator_try_assign(void *out, const void *in) { >::type allocator_try_assign(void *out, const void *in) {
// assignment is not possible; do nothing // assignment is not possible; do nothing
return NULL;
} }
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor" #pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
template<class T> template<class T>
void *allocator_fn(void *out, const void *in) { void *allocator_fn(void *out, const void *in) {
if (out) { allocator_try_assign<T>(out, in); return out; } if (out) { return allocator_try_assign<T>(out, in); }
else if (in) { delete (T*)in; return (T*)in; } else if (in) { delete (T*)in; return (T*)in; }
else return new T(); else return new T();
} }