diff --git a/stringid.cpp b/stringid.cpp index 5e5dbe7a..db237b95 100644 --- a/stringid.cpp +++ b/stringid.cpp @@ -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 diff --git a/stringid.hpp b/stringid.hpp index 98286a6d..0496f056 100644 --- a/stringid.hpp +++ b/stringid.hpp @@ -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;