Inserting const_cast for std::get_deleter return, to keep Ubuntu 20 GCC 6.3.0 happy.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-06-16 14:07:18 -07:00 committed by Ralf W. Grosse-Kunstleve
parent b04a70b5cc
commit 275aaf977a

View File

@ -61,10 +61,6 @@ struct guarded_operator_call {
explicit guarded_operator_call(bool armed_flag) : armed_flag{armed_flag} {} explicit guarded_operator_call(bool armed_flag) : armed_flag{armed_flag} {}
virtual void operator()(void *) = 0; virtual void operator()(void *) = 0;
virtual ~guarded_operator_call() = default; virtual ~guarded_operator_call() = default;
guarded_operator_call(guarded_operator_call &&) = default;
guarded_operator_call(guarded_operator_call &) = default;
guarded_operator_call &operator=(guarded_operator_call &&) = delete;
guarded_operator_call &operator=(const guarded_operator_call &) = delete;
}; };
template <typename T> template <typename T>
@ -214,20 +210,25 @@ struct smart_holder {
} }
} }
template <typename T, typename D>
void vptr_reset(T *raw_ptr, const D &del) {
vptr.reset(raw_ptr, del); // Copies del.
// The const_cast is only for certain compilers (Ubuntu 20 GCC 6.3.0 being one).
vptr_del = const_cast<D *>(std::get_deleter<D>(vptr)); // Pointer to copy of del.
}
void reset_vptr_deleter_armed_flag(bool armed_flag) const { void reset_vptr_deleter_armed_flag(bool armed_flag) const {
auto vptr_del_ptr = std::get_deleter<guarded_delete>(vptr); if (vptr_del == nullptr) {
if (vptr_del_ptr == nullptr) {
throw std::runtime_error( throw std::runtime_error(
"smart_holder::reset_vptr_deleter_armed_flag() called in an invalid context."); "smart_holder::reset_vptr_deleter_armed_flag() called in an invalid context.");
} }
vptr_del_ptr->armed_flag = armed_flag; vptr_del->armed_flag = armed_flag;
} }
template <typename T> template <typename T>
static smart_holder from_raw_ptr_unowned(T *raw_ptr) { static smart_holder from_raw_ptr_unowned(T *raw_ptr) {
smart_holder hld; smart_holder hld;
hld.vptr.reset(raw_ptr, guarded_builtin_delete<T>(false)); hld.vptr_reset(raw_ptr, guarded_builtin_delete<T>(false));
hld.vptr_del = std::get_deleter<guarded_builtin_delete<T>>(hld.vptr);
hld.vptr_is_using_noop_deleter = true; hld.vptr_is_using_noop_deleter = true;
hld.is_populated = true; hld.is_populated = true;
return hld; return hld;
@ -258,21 +259,12 @@ struct smart_holder {
static smart_holder from_raw_ptr_take_ownership(T *raw_ptr) { static smart_holder from_raw_ptr_take_ownership(T *raw_ptr) {
ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership"); ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership");
smart_holder hld; smart_holder hld;
hld.vptr.reset(raw_ptr, guarded_builtin_delete<T>(true)); hld.vptr_reset(raw_ptr, guarded_builtin_delete<T>(true));
hld.vptr_del = std::get_deleter<guarded_builtin_delete<T>>(hld.vptr);
hld.vptr_is_using_builtin_delete = true; hld.vptr_is_using_builtin_delete = true;
hld.is_populated = true; hld.is_populated = true;
return hld; return hld;
} }
void reset_vptr_deleter_armed_flag(bool armed_flag) {
if (vptr_del == nullptr) {
throw std::runtime_error(
"smart_holder::reset_vptr_deleter_armed_flag() called in an invalid context.");
}
vptr_del->armed_flag = armed_flag;
}
// Caller is responsible for ensuring preconditions (SMART_HOLDER_WIP: details). // Caller is responsible for ensuring preconditions (SMART_HOLDER_WIP: details).
void disown() { void disown() {
reset_vptr_deleter_armed_flag(false); reset_vptr_deleter_armed_flag(false);
@ -318,11 +310,9 @@ struct smart_holder {
hld.rtti_uqp_del = &typeid(D); hld.rtti_uqp_del = &typeid(D);
hld.vptr_is_using_builtin_delete = is_std_default_delete<T>(*hld.rtti_uqp_del); hld.vptr_is_using_builtin_delete = is_std_default_delete<T>(*hld.rtti_uqp_del);
if (hld.vptr_is_using_builtin_delete) { if (hld.vptr_is_using_builtin_delete) {
hld.vptr.reset(unq_ptr.get(), guarded_builtin_delete<T>(true)); hld.vptr_reset(unq_ptr.get(), guarded_builtin_delete<T>(true));
hld.vptr_del = std::get_deleter<guarded_builtin_delete<T>>(hld.vptr);
} else { } else {
hld.vptr.reset(unq_ptr.get(), guarded_custom_deleter<T, D>(true)); hld.vptr_reset(unq_ptr.get(), guarded_custom_deleter<T, D>(true));
hld.vptr_del = std::get_deleter<guarded_custom_deleter<T, D>>(hld.vptr);
} }
unq_ptr.release(); unq_ptr.release();
hld.is_populated = true; hld.is_populated = true;