From 9d35200ea54a0729d489dc2c76823a2a13cdcac4 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Apr 2024 08:53:35 +0200 Subject: add find_object specialization for scenery subtypes --- src/world.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/world.cpp') diff --git a/src/world.cpp b/src/world.cpp index 9f7c0cc3..dd6f2ca7 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -196,7 +196,12 @@ void world::set_object_counter(object_id value) void world::throw_on_wrong_object_type(object_id id, object_type actual, object_type expected) { - fm_throw("object '{}' has wrong object type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); + fm_throw("object {} has wrong object type {}, should be {}"_cf, id, (size_t)actual, (size_t)expected); +} + +void world::throw_on_wrong_scenery_type(object_id id, scenery_type actual, scenery_type expected) +{ + fm_throw("object {} has wrong scenery type {}, should be {}"_cf, id, (size_t)actual, (size_t)expected); } void world::throw_on_empty_scenery_proto(object_id id, global_coords pos, Vector2b offset) @@ -285,18 +290,30 @@ std::shared_ptr world::find_object(object_id id) return {}; else if constexpr(std::is_same_v) return ptr; + else if (ptr->type() != object_type_::value) [[unlikely]] + throw_on_wrong_object_type(id, ptr->type(), object_type_::value); + else + return static_pointer_cast(move(ptr)); +} + +template +requires is_strict_base_of +std::shared_ptr world::find_object(object_id id) +{ + if (auto ptr = find_object(id); !ptr) + return {}; + else if (ptr->scenery_type() != scenery_type_::value) [[unlikely]] + throw_on_wrong_scenery_type(id, ptr->scenery_type(), scenery_type_::value); else - { - if (!(ptr->type() == object_type_::value)) [[unlikely]] - throw_on_wrong_object_type(id, ptr->type(), object_type_::value); return static_pointer_cast(move(ptr)); - } } template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::find_object(object_id id); +template std::shared_ptr world::find_object(object_id id); +template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::make_scenery(object_id id, global_coords pos, scenery_proto&& proto) -- cgit v1.2.3