Rework Dictionary::TryGet to use std::optional.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
362e4bf59b
commit
96315cb857
|
@ -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
|
/// @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.
|
/// not be touched in that case.
|
||||||
inline bool TryGet(const KeyT& key, ValueT& out) const noexcept {
|
inline std::optional<std::reference_wrapper<const ValueT>> TryGet(const KeyT& key) const noexcept {
|
||||||
const auto& find = _map.find(key);
|
const auto& find = _map.find(key);
|
||||||
if (find == _map.end()) {
|
if (find == _map.end()) {
|
||||||
return false;
|
return {};
|
||||||
}
|
}
|
||||||
out = find->second;
|
return std::ref(find->second);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Removes an item with a certain key from the dictionary
|
/// @brief Removes an item with a certain key from the dictionary
|
||||||
|
|
|
@ -48,16 +48,14 @@ TEST_CASE("Create Dictionary, insert value, then update value") {
|
||||||
TEST_CASE("Create Dictionary, insert value, try get value") {
|
TEST_CASE("Create Dictionary, insert value, try get value") {
|
||||||
auto dic = Dictionary<int, int>(5);
|
auto dic = Dictionary<int, int>(5);
|
||||||
dic.Insert(10, 5);
|
dic.Insert(10, 5);
|
||||||
int result = 0;
|
auto v = dic.TryGet(10);
|
||||||
CHECK(dic.TryGet(10, result));
|
CHECK(v.has_value());
|
||||||
CHECK(result == 5);
|
CHECK(v.value() == 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Create Dictionary, insert value, try get non existing value") {
|
TEST_CASE("Create Dictionary, insert value, try get non existing value") {
|
||||||
auto dic = Dictionary<int, int>(5);
|
auto dic = Dictionary<int, int>(5);
|
||||||
int result = 0;
|
CHECK_FALSE(dic.TryGet(10).has_value());
|
||||||
CHECK_FALSE(dic.TryGet(10, result));
|
|
||||||
CHECK(result == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Create Dictionary, insert value, Has") {
|
TEST_CASE("Create Dictionary, insert value, Has") {
|
||||||
|
|
Loading…
Reference in New Issue