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/scenery-proto.hpp | 3 +++ src/scenery-type.hpp | 2 ++ src/scenery.hpp | 3 +++ src/world.cpp | 27 ++++++++++++++++++++++----- src/world.hpp | 4 ++++ 5 files changed, 34 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/scenery-proto.hpp b/src/scenery-proto.hpp index c5889b04..44c8fe6f 100644 --- a/src/scenery-proto.hpp +++ b/src/scenery-proto.hpp @@ -41,4 +41,7 @@ struct scenery_proto : object_proto scenery_proto& operator=(scenery_proto&&) noexcept; }; +template<> struct scenery_type_ : std::integral_constant {}; +template<> struct scenery_type_ : std::integral_constant {}; + } // namespace floormat diff --git a/src/scenery-type.hpp b/src/scenery-type.hpp index 3879e225..98cb9b10 100644 --- a/src/scenery-type.hpp +++ b/src/scenery-type.hpp @@ -6,4 +6,6 @@ enum class scenery_type : unsigned char { none, generic, door, COUNT, }; +template struct scenery_type_; + } // namespace floormat diff --git a/src/scenery.hpp b/src/scenery.hpp index 6699fe2f..beb06db5 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -66,4 +66,7 @@ template<> struct object_type_ : std::integral_constant struct object_type_ : std::integral_constant {}; template<> struct object_type_ : std::integral_constant {}; +template<> struct scenery_type_ : std::integral_constant {}; +template<> struct scenery_type_ : std::integral_constant {}; + } // namespace floormat 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) diff --git a/src/world.hpp b/src/world.hpp index e92979db..52cb1f64 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -1,8 +1,10 @@ #pragma once #include "compat/safe-ptr.hpp" +#include "compat/base-of.hpp" #include "chunk.hpp" #include "global-coords.hpp" #include "object-type.hpp" +#include "scenery-type.hpp" #include "loader/policy.hpp" #include #include @@ -50,6 +52,7 @@ private: std::shared_ptr find_object_(object_id id); [[noreturn]] static void throw_on_wrong_object_type(object_id id, object_type actual, object_type expected); + [[noreturn]] static void throw_on_wrong_scenery_type(object_id id, scenery_type actual, scenery_type expected); [[noreturn]] static void throw_on_empty_scenery_proto(object_id id, global_coords pos, Vector2b offset); friend struct object; @@ -91,6 +94,7 @@ public: } template std::shared_ptr make_scenery(object_id id, global_coords pos, scenery_proto&& proto); template std::shared_ptr find_object(object_id id); + template requires is_strict_base_of std::shared_ptr find_object(object_id id); shared_ptr_wrapper ensure_player_character(object_id& id, critter_proto p); shared_ptr_wrapper ensure_player_character(object_id& id); -- cgit v1.2.3