Skip to content

Add optional_lookup utility function #3473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/util/optional_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************\

Module: functions that are useful with optionalt

Author: Diffblue Ltd.

\*******************************************************************/

#ifndef CPROVER_UTIL_OPTIONAL_UTILS_H
#define CPROVER_UTIL_OPTIONAL_UTILS_H

#include "optional.h"

/// Lookup a key in a map, if found return the associated value,
/// nullopt otherwise
template <typename map_like_collectiont, typename keyt>
auto optional_lookup(const map_like_collectiont &map, const keyt &key)
-> optionalt<decltype(map.find(key)->second)>
{
auto const it = map.find(key);
if(it != map.end())
{
return it->second;
}
return nullopt;
}

#endif // CPROVER_UTIL_OPTIONAL_UTILS_H
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ SRC += analyses/ai/ai.cpp \
util/irep_sharing.cpp \
util/message.cpp \
util/optional.cpp \
util/optional_utils.cpp \
util/pointer_offset_size.cpp \
util/range.cpp \
util/replace_symbol.cpp \
Expand Down
44 changes: 44 additions & 0 deletions unit/util/optional_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************\

Module: optional_utils unit tests

Author: Diffblue Ltd.

\*******************************************************************/

#include <testing-utils/catch.hpp>

#include <util/optional_utils.h>

#include <map>
#include <string>
#include <unordered_map>

namespace
{
template <typename map_like_collectiont>
void do_optional_lookup_test(map_like_collectiont &map)
{
map.insert({"hello", "world"});
map.insert({"pwd", "/home"});
auto const hello_result = optional_lookup(map, "hello");
REQUIRE(hello_result.has_value());
REQUIRE(hello_result.value() == "world");
auto const pwd_result = optional_lookup(map, "pwd");
REQUIRE(pwd_result.has_value());
REQUIRE(pwd_result.value() == "/home");
REQUIRE_FALSE(optional_lookup(map, "does not exit").has_value());
}
} // namespace

TEST_CASE("Using optional_lookup with std::map")
{
auto map = std::map<std::string, std::string>{};
do_optional_lookup_test(map);
}

TEST_CASE("Using optional_lookup with std::unordered_map")
{
auto map = std::unordered_map<std::string, std::string>{};
do_optional_lookup_test(map);
}