Skip to content

Commit

Permalink
find_in_map_required: overload with error message callback
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkvdb committed May 6, 2024
1 parent 0626f56 commit b050e39
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/infra/algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ auto find_in_map_required(MapType&& m, const typename std::decay_t<MapType>::key
return iter->second;
}

template <typename MapType, typename NotFoundMessageCallback>
auto find_in_map_required(MapType&& m, const typename std::decay_t<MapType>::key_type& key, NotFoundMessageCallback&& cb)
{
auto iter = m.find(key);
if (iter == m.end()) {
throw RuntimeError(cb(key));
}

return iter->second;
}

/* Search for an entry in the map that matches the predicate
* The entry is returned as a pointer (nullptr when not found)
* This funtion never throws
Expand Down

0 comments on commit b050e39

Please sign in to comment.