Skip to content

Commit 5139616

Browse files
Add optional_lookup utility function
This provides a more ergonomic interface over map::find
1 parent c752d9d commit 5139616

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/util/optional_utils.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************\
2+
3+
Module: functions that are useful with optionalt
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_OPTIONAL_UTILS_H
10+
#define CPROVER_UTIL_OPTIONAL_UTILS_H
11+
12+
#include "optional.h"
13+
14+
#include <iterator>
15+
#include <map>
16+
17+
/// Lookup a key in a map, if found return the associated value,
18+
/// nullopt otherwise
19+
template <typename map_like_collectiont, typename keyt>
20+
auto optional_lookup(const map_like_collectiont &map, const keyt &key)
21+
-> optionalt<decltype(map.find(key)->second)>
22+
{
23+
auto const it = map.find(key);
24+
if(it != map.end())
25+
{
26+
return it->second;
27+
}
28+
return nullopt;
29+
}
30+
31+
#endif // CPROVER_UTIL_OPTIONAL_UTILS_H

0 commit comments

Comments
 (0)