diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-16 06:12:29 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-16 06:12:29 +0100 |
commit | cb0069f839db3d8f4748dd8e39f0d3830bfdf352 (patch) | |
tree | 1b7cd5486935f5d478f714f34656a9feec864b05 /src | |
parent | 8cf6c38140e505d787d2db12edbd638a91fa2dfc (diff) |
c
Diffstat (limited to 'src')
-rw-r--r-- | src/world.cpp | 26 | ||||
-rw-r--r-- | src/world.hpp | 21 |
2 files changed, 23 insertions, 24 deletions
diff --git a/src/world.cpp b/src/world.cpp index e057ffe7..319892ad 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -4,6 +4,7 @@ #include "compat/int-hash.hpp" #include "compat/exception.hpp" #include <Corrade/Containers/GrowableArray.h> +#include <tsl/robin_map.h> using namespace floormat; @@ -20,6 +21,11 @@ size_t world::chunk_coords_hasher::operator()(const chunk_coords_& coord) const namespace floormat { +struct world::robin_map_wrapper final : tsl::robin_map<object_id, std::weak_ptr<object>, object_id_hasher> +{ + using tsl::robin_map<object_id, std::weak_ptr<object>, object_id_hasher>::robin_map; +}; + world::world(world&& w) noexcept = default; world::world(std::unordered_map<chunk_coords_, chunk>&& chunks) : @@ -70,15 +76,15 @@ world::~world() noexcept } _last_chunk = {}; _chunks.clear(); - _objects.clear(); + _objects->clear(); } world::world(size_t capacity) : _chunks{capacity} { _chunks.max_load_factor(max_load_factor); _chunks.reserve(initial_capacity); - _objects.max_load_factor(max_load_factor); - _objects.reserve(initial_capacity); + _objects->max_load_factor(max_load_factor); + _objects->reserve(initial_capacity); } chunk& world::operator[](chunk_coords_ coord) noexcept @@ -117,8 +123,8 @@ void world::clear() _last_collection = 0; _chunks.clear(); _chunks.rehash(initial_capacity); - _objects.clear(); - _objects.rehash(initial_capacity); + _objects->clear(); + _objects->rehash(initial_capacity); _collect_every = initial_collect_every; _object_counter = object_counter_init; auto& [c, pos] = _last_chunk; @@ -157,10 +163,10 @@ void world::do_make_object(const std::shared_ptr<object>& e, global_coords pos, { fm_assert(e->id > 0); fm_debug_assert(_unique_id && e->c->world()._unique_id == _unique_id); - fm_assert(!_objects.contains(e->id)); + fm_assert(!_objects->contains(e->id)); fm_assert(e->type() != object_type::none); const_cast<global_coords&>(e->coord) = pos; - _objects[e->id] = e; + (*_objects)[e->id] = e; if (sorted) e->c->add_object(e); else @@ -170,14 +176,14 @@ void world::do_make_object(const std::shared_ptr<object>& e, global_coords pos, void world::do_kill_object(object_id id) { fm_debug_assert(id > 0); - auto cnt = _objects.erase(id); + auto cnt = _objects->erase(id); fm_debug_assert(cnt > 0); } std::shared_ptr<object> world::find_object_(object_id id) { - auto it = _objects.find(id); - auto ret = it == _objects.end() ? nullptr : it->second.lock(); + auto it = _objects->find(id); + auto ret = it == _objects->end() ? nullptr : it->second.lock(); fm_debug_assert(!ret || &ret->c->world() == this); return ret; } diff --git a/src/world.hpp b/src/world.hpp index 55331f49..4ffac90d 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -1,11 +1,10 @@ #pragma once -#include "compat/defs.hpp" +#include "compat/safe-ptr.hpp" #include "chunk.hpp" #include "global-coords.hpp" #include "object-type.hpp" #include <memory> #include <unordered_map> -#include <tsl/robin_map.h> #include <Corrade/Utility/Move.h> namespace floormat { @@ -22,23 +21,19 @@ public: static constexpr size_t initial_collect_every = 64; private: - struct chunk_tuple final { + struct chunk_tuple + { static constexpr chunk_coords_ invalid_coords = { -1 << 15, -1 << 15, chunk_z_min }; chunk* c = nullptr; chunk_coords_ pos = invalid_coords; } _last_chunk; - struct object_id_hasher - { - size_t operator()(object_id id) const noexcept; - }; - - struct chunk_coords_hasher { - size_t operator()(const chunk_coords_& coord) const noexcept; - }; + struct object_id_hasher { size_t operator()(object_id id) const noexcept; }; + struct chunk_coords_hasher { size_t operator()(const chunk_coords_& coord) const noexcept; }; + struct robin_map_wrapper; std::unordered_map<chunk_coords_, chunk, chunk_coords_hasher> _chunks; - tsl::robin_map<object_id, std::weak_ptr<object>, object_id_hasher> _objects; + safe_ptr<robin_map_wrapper> _objects; size_t _last_collection = 0; size_t _collect_every = initial_collect_every; std::shared_ptr<char> _unique_id = std::make_shared<char>('A'); @@ -109,8 +104,6 @@ public: world& operator=(world&& w) noexcept; world(world&& w) noexcept; - - fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world); }; template<typename T> |