diff --git a/src/Collections/Dictionary.hpp b/src/Collections/Dictionary.hpp index 9aab849..2ddb486 100644 --- a/src/Collections/Dictionary.hpp +++ b/src/Collections/Dictionary.hpp @@ -46,13 +46,12 @@ namespace ArbUt { /// @brief Try to get an item from the dictionary using a key. Returns false if no item is found, and out will /// not be touched in that case. - inline bool TryGet(const KeyT& key, ValueT& out) const noexcept { + inline std::optional> TryGet(const KeyT& key) const noexcept { const auto& find = _map.find(key); if (find == _map.end()) { - return false; + return {}; } - out = find->second; - return true; + return std::ref(find->second); } /// @brief Removes an item with a certain key from the dictionary diff --git a/tests/DictionaryTests.cpp b/tests/DictionaryTests.cpp index c7af9bd..bb3cee7 100644 --- a/tests/DictionaryTests.cpp +++ b/tests/DictionaryTests.cpp @@ -48,16 +48,14 @@ TEST_CASE("Create Dictionary, insert value, then update value") { TEST_CASE("Create Dictionary, insert value, try get value") { auto dic = Dictionary(5); dic.Insert(10, 5); - int result = 0; - CHECK(dic.TryGet(10, result)); - CHECK(result == 5); + auto v = dic.TryGet(10); + CHECK(v.has_value()); + CHECK(v.value() == 5); } TEST_CASE("Create Dictionary, insert value, try get non existing value") { auto dic = Dictionary(5); - int result = 0; - CHECK_FALSE(dic.TryGet(10, result)); - CHECK(result == 0); + CHECK_FALSE(dic.TryGet(10).has_value()); } TEST_CASE("Create Dictionary, insert value, Has") {