From f2ebad511585acc3709bf78bf218b2451461fa2b Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 6 Mar 2021 01:49:15 +0100 Subject: [PATCH] Return cached element when set existing --- .../detournavigator/navmeshtilescache.cpp | 9 ++++----- components/detournavigator/makenavmesh.cpp | 13 ++----------- components/detournavigator/navmeshtilescache.cpp | 6 ++++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/apps/openmw_test_suite/detournavigator/navmeshtilescache.cpp b/apps/openmw_test_suite/detournavigator/navmeshtilescache.cpp index 5bc7af646..5d4b1602f 100644 --- a/apps/openmw_test_suite/detournavigator/navmeshtilescache.cpp +++ b/apps/openmw_test_suite/detournavigator/navmeshtilescache.cpp @@ -77,7 +77,7 @@ namespace EXPECT_EQ(result.get(), (NavMeshDataRef {mData, 1})); } - TEST_F(DetourNavigatorNavMeshTilesCacheTest, set_existing_element_should_throw_exception) + TEST_F(DetourNavigatorNavMeshTilesCacheTest, set_existing_element_should_return_cached_element) { const std::size_t navMeshDataSize = 1; const std::size_t navMeshKeySize = cRecastMeshKeySize; @@ -87,10 +87,9 @@ namespace NavMeshData anotherNavMeshData {anotherData, 1}; cache.set(mAgentHalfExtents, mTilePosition, mRecastMesh, mOffMeshConnections, std::move(mNavMeshData)); - EXPECT_THROW( - cache.set(mAgentHalfExtents, mTilePosition, mRecastMesh, mOffMeshConnections, std::move(anotherNavMeshData)), - InvalidArgument - ); + const auto result = cache.set(mAgentHalfExtents, mTilePosition, mRecastMesh, mOffMeshConnections, std::move(anotherNavMeshData)); + ASSERT_TRUE(result); + EXPECT_EQ(result.get(), (NavMeshDataRef {mData, 1})); } TEST_F(DetourNavigatorNavMeshTilesCacheTest, get_should_return_cached_value) diff --git a/components/detournavigator/makenavmesh.cpp b/components/detournavigator/makenavmesh.cpp index 7c7dcf186..9701b4950 100644 --- a/components/detournavigator/makenavmesh.cpp +++ b/components/detournavigator/makenavmesh.cpp @@ -576,17 +576,8 @@ namespace DetourNavigator return navMeshCacheItem->lock()->removeTile(changedTile); } - try - { - cachedNavMeshData = navMeshTilesCache.set(agentHalfExtents, changedTile, *recastMesh, - offMeshConnections, std::move(navMeshData)); - } - catch (const InvalidArgument&) - { - cachedNavMeshData = navMeshTilesCache.get(agentHalfExtents, changedTile, *recastMesh, - offMeshConnections); - cached = static_cast(cachedNavMeshData); - } + cachedNavMeshData = navMeshTilesCache.set(agentHalfExtents, changedTile, *recastMesh, + offMeshConnections, std::move(navMeshData)); if (!cachedNavMeshData) { diff --git a/components/detournavigator/navmeshtilescache.cpp b/components/detournavigator/navmeshtilescache.cpp index 6434fb263..8dc554d79 100644 --- a/components/detournavigator/navmeshtilescache.cpp +++ b/components/detournavigator/navmeshtilescache.cpp @@ -1,5 +1,4 @@ #include "navmeshtilescache.hpp" -#include "exceptions.hpp" #include @@ -68,7 +67,10 @@ namespace DetourNavigator if (!emplaced.second) { mFreeItems.erase(iterator); - throw InvalidArgument("Set existing cache value"); + acquireItemUnsafe(emplaced.first->second); + ++mGetCount; + ++mHitCount; + return Value(*this, emplaced.first->second); } iterator->mNavMeshData = std::move(value);