Pure clang-format --style=file -i change.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-12 10:28:17 -08:00
parent cfc848a137
commit e7aea026bb
2 changed files with 340 additions and 379 deletions

View File

@ -48,27 +48,27 @@ namespace memory {
template <typename T>
struct guarded_builtin_delete {
bool* flag_ptr;
explicit guarded_builtin_delete(bool* guard_flag_ptr)
: flag_ptr{guard_flag_ptr} {}
void operator()(T* raw_ptr) {
if (*flag_ptr) delete raw_ptr;
bool *flag_ptr;
explicit guarded_builtin_delete(bool *guard_flag_ptr) : flag_ptr{guard_flag_ptr} {}
void operator()(T *raw_ptr) {
if (*flag_ptr)
delete raw_ptr;
}
};
template <typename T, typename D>
struct guarded_custom_deleter {
bool* flag_ptr;
explicit guarded_custom_deleter(bool* guard_flag_ptr)
: flag_ptr{guard_flag_ptr} {}
void operator()(T* raw_ptr) {
if (*flag_ptr) D()(raw_ptr);
bool *flag_ptr;
explicit guarded_custom_deleter(bool *guard_flag_ptr) : flag_ptr{guard_flag_ptr} {}
void operator()(T *raw_ptr) {
if (*flag_ptr)
D()(raw_ptr);
}
};
struct smart_holder {
const std::type_info* rtti_held;
const std::type_info* rtti_uqp_del;
const std::type_info *rtti_held;
const std::type_info *rtti_uqp_del;
std::shared_ptr<void> vptr;
bool vptr_deleter_guard_flag;
bool vptr_is_using_noop_deleter : 1;
@ -76,138 +76,126 @@ struct smart_holder {
bool vptr_is_external_shared_ptr : 1;
smart_holder()
: rtti_held{nullptr},
rtti_uqp_del{nullptr},
vptr_deleter_guard_flag{false},
vptr_is_using_noop_deleter{false},
vptr_is_using_builtin_delete{false},
: rtti_held{nullptr}, rtti_uqp_del{nullptr}, vptr_deleter_guard_flag{false},
vptr_is_using_noop_deleter{false}, vptr_is_using_builtin_delete{false},
vptr_is_external_shared_ptr{false} {}
bool has_pointee() const { return vptr.get() != nullptr; }
template <typename T>
void ensure_compatible_rtti_held(const char* context) const {
void ensure_compatible_rtti_held(const char *context) const {
if (!rtti_held) {
throw std::runtime_error(std::string("Unpopulated holder (") + context +
").");
throw std::runtime_error(std::string("Unpopulated holder (") + context + ").");
}
const std::type_info* rtti_requested = &typeid(T);
const std::type_info *rtti_requested = &typeid(T);
if (!(*rtti_requested == *rtti_held)) {
throw std::runtime_error(std::string("Incompatible type (") + context +
").");
throw std::runtime_error(std::string("Incompatible type (") + context + ").");
}
}
template <typename D>
void ensure_compatible_rtti_uqp_del(const char* context) const {
void ensure_compatible_rtti_uqp_del(const char *context) const {
if (!rtti_uqp_del) {
throw std::runtime_error(std::string("Missing unique_ptr deleter (") +
context + ").");
throw std::runtime_error(std::string("Missing unique_ptr deleter (") + context + ").");
}
const std::type_info* rtti_requested = &typeid(D);
const std::type_info *rtti_requested = &typeid(D);
if (!(*rtti_requested == *rtti_uqp_del)) {
throw std::runtime_error(
std::string("Incompatible unique_ptr deleter (") + context + ").");
throw std::runtime_error(std::string("Incompatible unique_ptr deleter (") + context
+ ").");
}
}
void ensure_has_pointee(const char* context) const {
void ensure_has_pointee(const char *context) const {
if (!has_pointee()) {
throw std::runtime_error(std::string("Disowned holder (") + context +
").");
throw std::runtime_error(std::string("Disowned holder (") + context + ").");
}
}
void ensure_vptr_is_using_builtin_delete(const char* context) const {
void ensure_vptr_is_using_builtin_delete(const char *context) const {
if (vptr_is_external_shared_ptr) {
throw std::runtime_error(
std::string("Cannot disown external shared_ptr (") + context + ").");
throw std::runtime_error(std::string("Cannot disown external shared_ptr (") + context
+ ").");
}
if (vptr_is_using_noop_deleter) {
throw std::runtime_error(
std::string("Cannot disown non-owning holder (") + context + ").");
throw std::runtime_error(std::string("Cannot disown non-owning holder (") + context
+ ").");
}
if (!vptr_is_using_builtin_delete) {
throw std::runtime_error(std::string("Cannot disown custom deleter (") +
context + ").");
throw std::runtime_error(std::string("Cannot disown custom deleter (") + context
+ ").");
}
}
void ensure_use_count_1(const char* context) const {
void ensure_use_count_1(const char *context) const {
if (vptr.get() == nullptr) {
throw std::runtime_error(std::string("Cannot disown nullptr (") +
context + ").");
throw std::runtime_error(std::string("Cannot disown nullptr (") + context + ").");
}
if (vptr.use_count() != 1) {
throw std::runtime_error(std::string("Cannot disown use_count != 1 (") +
context + ").");
throw std::runtime_error(std::string("Cannot disown use_count != 1 (") + context
+ ").");
}
}
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;
hld.rtti_held = &typeid(T);
hld.vptr_is_using_noop_deleter = true;
hld.vptr.reset(raw_ptr,
guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
hld.vptr.reset(raw_ptr, guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
return hld;
}
template <typename T>
T* as_raw_ptr_unowned() const {
static const char* context = "as_raw_ptr_unowned";
T *as_raw_ptr_unowned() const {
static const char *context = "as_raw_ptr_unowned";
ensure_compatible_rtti_held<T>(context);
return static_cast<T*>(vptr.get());
return static_cast<T *>(vptr.get());
}
template <typename T>
T& lvalue_ref() const {
static const char* context = "lvalue_ref";
T &lvalue_ref() const {
static const char *context = "lvalue_ref";
ensure_compatible_rtti_held<T>(context);
ensure_has_pointee(context);
return *static_cast<T*>(vptr.get());
return *static_cast<T *>(vptr.get());
}
template <typename T>
T&& rvalue_ref() const {
static const char* context = "rvalue_ref";
T &&rvalue_ref() const {
static const char *context = "rvalue_ref";
ensure_compatible_rtti_held<T>(context);
ensure_has_pointee(context);
return std::move(*static_cast<T*>(vptr.get()));
return std::move(*static_cast<T *>(vptr.get()));
}
template <typename T>
static smart_holder from_raw_ptr_take_ownership(T* raw_ptr) {
static smart_holder from_raw_ptr_take_ownership(T *raw_ptr) {
smart_holder hld;
hld.rtti_held = &typeid(T);
hld.vptr_deleter_guard_flag = true;
hld.vptr_is_using_builtin_delete = true;
hld.vptr.reset(raw_ptr,
guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
hld.vptr.reset(raw_ptr, guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
return hld;
}
template <typename T>
T* as_raw_ptr_release_ownership(
const char* context = "as_raw_ptr_release_ownership") {
T *as_raw_ptr_release_ownership(const char *context = "as_raw_ptr_release_ownership") {
ensure_compatible_rtti_held<T>(context);
ensure_vptr_is_using_builtin_delete(context);
ensure_use_count_1(context);
T* raw_ptr = static_cast<T*>(vptr.get());
T *raw_ptr = static_cast<T *>(vptr.get());
vptr_deleter_guard_flag = false;
vptr.reset();
return raw_ptr;
}
template <typename T>
static smart_holder from_unique_ptr(std::unique_ptr<T>&& unq_ptr) {
static smart_holder from_unique_ptr(std::unique_ptr<T> &&unq_ptr) {
smart_holder hld;
hld.rtti_held = &typeid(T);
hld.vptr_deleter_guard_flag = true;
hld.vptr_is_using_builtin_delete = true;
hld.vptr.reset(unq_ptr.get(),
guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
hld.vptr.reset(unq_ptr.get(), guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
unq_ptr.release();
return hld;
}
@ -218,25 +206,23 @@ struct smart_holder {
}
template <typename T, typename D>
static smart_holder from_unique_ptr_with_deleter(
std::unique_ptr<T, D>&& unq_ptr) {
static smart_holder from_unique_ptr_with_deleter(std::unique_ptr<T, D> &&unq_ptr) {
smart_holder hld;
hld.rtti_held = &typeid(T);
hld.rtti_uqp_del = &typeid(D);
hld.vptr_deleter_guard_flag = true;
hld.vptr.reset(unq_ptr.get(),
guarded_custom_deleter<T, D>(&hld.vptr_deleter_guard_flag));
hld.vptr.reset(unq_ptr.get(), guarded_custom_deleter<T, D>(&hld.vptr_deleter_guard_flag));
unq_ptr.release();
return hld;
}
template <typename T, typename D>
std::unique_ptr<T, D> as_unique_ptr_with_deleter() {
static const char* context = "as_unique_ptr_with_deleter";
static const char *context = "as_unique_ptr_with_deleter";
ensure_compatible_rtti_held<T>(context);
ensure_compatible_rtti_uqp_del<D>(context);
ensure_use_count_1(context);
T* raw_ptr = static_cast<T*>(vptr.get());
T *raw_ptr = static_cast<T *>(vptr.get());
vptr_deleter_guard_flag = false;
vptr.reset();
return std::unique_ptr<T, D>(raw_ptr);
@ -253,7 +239,7 @@ struct smart_holder {
template <typename T>
std::shared_ptr<T> as_shared_ptr() const {
static const char* context = "as_shared_ptr";
static const char *context = "as_shared_ptr";
ensure_compatible_rtti_held<T>(context);
return std::static_pointer_cast<T>(vptr);
}

View File

@ -10,7 +10,7 @@ namespace helpers {
struct movable_int {
int valu;
movable_int(int v) : valu{v} {}
movable_int(movable_int&& other) {
movable_int(movable_int &&other) {
valu = other.valu;
other.valu = 91;
}
@ -18,7 +18,7 @@ struct movable_int {
template <typename T>
struct functor_builtin_delete {
void operator()(T* ptr) { delete ptr; }
void operator()(T *ptr) { delete ptr; }
};
template <typename T>
@ -51,8 +51,7 @@ TEST_CASE("from_raw_ptr_unowned+rvalue_ref", "[S]") {
TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_release_ownership", "[E]") {
static int value = 19;
auto hld = smart_holder::from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(),
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown non-owning holder (as_raw_ptr_release_ownership).");
}
@ -66,11 +65,10 @@ TEST_CASE("from_raw_ptr_unowned+as_unique_ptr", "[E]") {
TEST_CASE("from_raw_ptr_unowned+as_unique_ptr_with_deleter", "[E]") {
static int value = 19;
auto hld = smart_holder::from_raw_ptr_unowned(&value);
auto condense_for_macro = [](smart_holder& hld) {
auto condense_for_macro = [](smart_holder &hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
};
REQUIRE_THROWS_WITH(
condense_for_macro(hld),
REQUIRE_THROWS_WITH(condense_for_macro(hld),
"Missing unique_ptr deleter (as_unique_ptr_with_deleter).");
}
@ -88,8 +86,7 @@ TEST_CASE("from_raw_ptr_take_ownership+lvalue_ref", "[S]") {
TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
auto new_owner =
std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>());
auto new_owner = std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>());
REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19);
}
@ -97,8 +94,7 @@ TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") {
TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership2", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(),
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown use_count != 1 (as_raw_ptr_release_ownership).");
}
@ -112,17 +108,15 @@ TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") {
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr2", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown use_count != 1 (as_unique_ptr).");
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), "Cannot disown use_count != 1 (as_unique_ptr).");
}
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr_with_deleter", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
auto condense_for_macro = [](smart_holder& hld) {
auto condense_for_macro = [](smart_holder &hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
};
REQUIRE_THROWS_WITH(
condense_for_macro(hld),
REQUIRE_THROWS_WITH(condense_for_macro(hld),
"Missing unique_ptr deleter (as_unique_ptr_with_deleter).");
}
@ -144,8 +138,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership1", "[S]") {
std::unique_ptr<int> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
auto new_owner =
std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>());
auto new_owner = std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>());
REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19);
}
@ -155,8 +148,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") {
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(),
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown use_count != 1 (as_raw_ptr_release_ownership).");
}
@ -174,19 +166,17 @@ TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") {
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown use_count != 1 (as_unique_ptr).");
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), "Cannot disown use_count != 1 (as_unique_ptr).");
}
TEST_CASE("from_unique_ptr+as_unique_ptr_with_deleter", "[E]") {
std::unique_ptr<int> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
auto condense_for_macro = [](smart_holder& hld) {
auto condense_for_macro = [](smart_holder &hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
};
REQUIRE_THROWS_WITH(
condense_for_macro(hld),
REQUIRE_THROWS_WITH(condense_for_macro(hld),
"Missing unique_ptr deleter (as_unique_ptr_with_deleter).");
}
@ -200,60 +190,50 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") {
}
TEST_CASE("from_unique_ptr_with_deleter+lvalue_ref", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19));
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
REQUIRE(hld.lvalue_ref<int>() == 19);
}
TEST_CASE("from_unique_ptr_with_deleter+as_raw_ptr_release_ownership", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19));
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(),
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown custom deleter (as_raw_ptr_release_ownership).");
}
TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19));
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown custom deleter (as_unique_ptr).");
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), "Cannot disown custom deleter (as_unique_ptr).");
}
TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19));
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
std::unique_ptr<int, helpers::functor_builtin_delete<int>> new_owner =
hld.as_unique_ptr_with_deleter<int,
helpers::functor_builtin_delete<int>>();
std::unique_ptr<int, helpers::functor_builtin_delete<int>> new_owner
= hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19);
}
TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19));
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
auto condense_for_macro = [](smart_holder& hld) {
auto condense_for_macro = [](smart_holder &hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_other_delete<int>>();
};
REQUIRE_THROWS_WITH(
condense_for_macro(hld),
REQUIRE_THROWS_WITH(condense_for_macro(hld),
"Incompatible unique_ptr deleter (as_unique_ptr_with_deleter).");
}
TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19));
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr);
std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>();
@ -270,8 +250,7 @@ TEST_CASE("from_shared_ptr+lvalue_ref", "[S]") {
TEST_CASE("from_shared_ptr+as_raw_ptr_release_ownership", "[E]") {
std::shared_ptr<int> orig_owner(new int(19));
auto hld = smart_holder::from_shared_ptr(orig_owner);
REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(),
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown external shared_ptr (as_raw_ptr_release_ownership).");
}
@ -285,11 +264,10 @@ TEST_CASE("from_shared_ptr+as_unique_ptr", "[E]") {
TEST_CASE("from_shared_ptr+as_unique_ptr_with_deleter", "[E]") {
std::shared_ptr<int> orig_owner(new int(19));
auto hld = smart_holder::from_shared_ptr(orig_owner);
auto condense_for_macro = [](smart_holder& hld) {
auto condense_for_macro = [](smart_holder &hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
};
REQUIRE_THROWS_WITH(
condense_for_macro(hld),
REQUIRE_THROWS_WITH(condense_for_macro(hld),
"Missing unique_ptr deleter (as_unique_ptr_with_deleter).");
}
@ -301,15 +279,13 @@ TEST_CASE("from_shared_ptr+as_shared_ptr", "[S]") {
TEST_CASE("error_unpopulated_holder", "[E]") {
smart_holder hld;
REQUIRE_THROWS_WITH(hld.as_raw_ptr_unowned<int>(),
"Unpopulated holder (as_raw_ptr_unowned).");
REQUIRE_THROWS_WITH(hld.as_raw_ptr_unowned<int>(), "Unpopulated holder (as_raw_ptr_unowned).");
}
TEST_CASE("error_incompatible_type", "[E]") {
static int value = 19;
auto hld = smart_holder::from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<std::string>(),
"Incompatible type (as_unique_ptr).");
REQUIRE_THROWS_WITH(hld.as_unique_ptr<std::string>(), "Incompatible type (as_unique_ptr).");
}
TEST_CASE("error_disowned_holder", "[E]") {
@ -321,6 +297,5 @@ TEST_CASE("error_disowned_holder", "[E]") {
TEST_CASE("error_cannot_disown_nullptr", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.as_unique_ptr<int>();
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown nullptr (as_unique_ptr).");
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), "Cannot disown nullptr (as_unique_ptr).");
}