stringid: add the `key` query

This commit is contained in:
Danny Robson 2022-05-13 10:03:48 +10:00
parent 2b7dd99a68
commit 2d18241e61
2 changed files with 21 additions and 0 deletions

View File

@ -52,6 +52,18 @@ stringid::find (std::string_view const &key) const
}
///////////////////////////////////////////////////////////////////////////////
std::string const&
stringid::key (id_t id) const&
{
for (auto const &[key, idx]: m_map)
if (idx == id)
return key;
throw std::out_of_range ("stringid");
}
///////////////////////////////////////////////////////////////////////////////
void
stringid::clear (void) noexcept

View File

@ -63,6 +63,15 @@ namespace cruft {
}
///////////////////////////////////////////////////////////////////////
/// Returns the underlying string that an id maps to.
///
/// If there is no such id then a std::out_of_range exception is
/// thrown.
std::string const&
key (id_t id) const&;
///////////////////////////////////////////////////////////////////////
void clear (void) noexcept;