Make allocator_fn compile for non-copy-assignable types like std::mutex

develop
lethosor 2023-05-28 02:51:06 -04:00
parent e5bc4b3221
commit 7c4d9364a2
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
1 changed files with 15 additions and 1 deletions

@ -497,11 +497,25 @@ namespace df
using DFHack::DfLinkedList; using DFHack::DfLinkedList;
using DFHack::DfOtherVectors; using DFHack::DfOtherVectors;
template<class T>
typename std::enable_if<
std::is_copy_assignable<T>::value
>::type allocator_try_assign(void *out, const void *in) {
*(T*)out = *(const T*)in;
}
template<class T>
typename std::enable_if<
!std::is_copy_assignable<T>::value
>::type allocator_try_assign(void *out, const void *in) {
// assignment is not possible; do nothing
}
#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) { *(T*)out = *(const T*)in; return out; } if (out) { allocator_try_assign<T>(out, in); return out; }
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();
} }