name_translator.hpp 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #ifndef SOPHIAR2_NAME_TRANSLATOR_HPP
  2. #define SOPHIAR2_NAME_TRANSLATOR_HPP
  3. #include "utility/string_map.hpp"
  4. #include <string>
  5. #include <unordered_map>
  6. namespace sophiar {
  7. template<typename IndexType>
  8. class name_translator {
  9. public:
  10. void register_item(std::string_view name, IndexType index) {
  11. assert(!name_map.contains(name));
  12. assert(!index_map.contains(index));
  13. name_map.insert(name, index);
  14. index_map.emplace(index, name);
  15. }
  16. IndexType translate(std::string_view name) const {
  17. assert(name_map.contains(name));
  18. return name_map.query(name);
  19. }
  20. std::string_view translate(IndexType index) const {
  21. assert(index_map.contains(index));
  22. return index_map.at(index);
  23. }
  24. private:
  25. string_map<IndexType> name_map;
  26. std::unordered_map<IndexType, std::string> index_map;
  27. };
  28. }
  29. #endif //SOPHIAR2_NAME_TRANSLATOR_HPP