diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-18 16:44:39 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-18 16:44:39 +0100 |
commit | 27dd8b268fbe37fa6eed0c0fc0f013134e5e2dc9 (patch) | |
tree | 7f04c83b09aa1d0706e832d16fa19ee31cd12f31 /src/world.hpp | |
parent | f82e612070b3edc963e787f121a58a2821ffa2a5 (diff) |
editor/app: don't duplicate characters on load
Diffstat (limited to 'src/world.hpp')
-rw-r--r-- | src/world.hpp | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/world.hpp b/src/world.hpp index f29a83e2..29d5a8dd 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -77,7 +77,7 @@ public: return ret; } - template<typename T = entity, typename U = entity> std::shared_ptr<T> find_entity(std::uint64_t id); + template<typename T = entity> std::shared_ptr<T> find_entity(std::uint64_t id); bool is_teardown() const { return _teardown; } std::uint64_t entity_counter() const { return _entity_counter; } @@ -98,15 +98,21 @@ world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks operator[](coord) = std::move(c); } -template<typename T, typename U> +template<typename T> std::shared_ptr<T> world::find_entity(std::uint64_t id) { static_assert(std::is_base_of_v<entity, T>); - std::shared_ptr<U> ptr = find_entity_(id); - if (!ptr) - return nullptr; - fm_assert(ptr->type == entity_type_<T>::value); - return std::static_pointer_cast<T>(ptr); + // make it a dependent name so that including "src/entity.hpp" isn't needed + using U = std::conditional_t<std::is_same_v<T, entity>, T, entity>; + if (std::shared_ptr<U> ptr = find_entity_(id); !ptr) + return {}; + else if constexpr(std::is_same_v<T, entity>) + return ptr; + else + { + fm_assert(ptr->type == entity_type_<T>::value); + return std::static_pointer_cast<T>(std::move(ptr)); + } } } // namespace floormat |