77 lines
2.1 KiB
C++
77 lines
2.1 KiB
C++
namespace pmr {
|
|
using NameTable_t = std::pmr::unordered_map<size_t,const std::pmr::string>;
|
|
struct NameTable {
|
|
static const std::pmr::string& Find(size_t id);
|
|
template<typename T>
|
|
static std::string_view MakePair(size_t id, T&& str);
|
|
static NameTable_t BuildNameTable() {
|
|
static FrameAllocatorPool MemPool;
|
|
return NameTable_t(&MemPool);
|
|
}
|
|
inline static NameTable_t Table = BuildNameTable();
|
|
};
|
|
template<typename T>
|
|
inline std::string_view NameTable::MakePair(size_t id, T&& str)
|
|
{
|
|
auto it = Table.find(id);
|
|
if (it == Table.end()) {
|
|
auto it2 = Table.emplace(std::make_pair(id, std::pmr::string(str, Table.get_allocator())));
|
|
if (it2.second) {
|
|
return it2.first->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
return it->second;
|
|
}
|
|
inline const std::pmr::string& NameTable::Find(size_t id)
|
|
{
|
|
auto it = Table.find(id);
|
|
if (it == Table.end()) {
|
|
static std::pmr::string empty("", Table.get_allocator());
|
|
return empty;
|
|
}
|
|
return it->second;
|
|
}
|
|
inline std::string NameID::ToString() const
|
|
{
|
|
return std::string(NameTable::Find(hash));
|
|
}
|
|
inline const std::pmr::string& NameID::ToStringRef() const
|
|
{
|
|
return NameTable::Find(hash);
|
|
}
|
|
#ifdef API_DEBUG
|
|
#define MAKE_NAME_PAIR(hash, str) value = NameTable::MakePair(hash, str)
|
|
#define NAME_TO_STRING value
|
|
#else
|
|
#define MAKE_NAME_PAIR(hash, str) NameTable::MakePair(hash, str)
|
|
#define NAME_TO_STRING NameTable::Find(hash)
|
|
#endif
|
|
inline std::string Name::ToString() const
|
|
{
|
|
return std::string(NAME_TO_STRING);
|
|
}
|
|
inline const std::string_view Name::ToStringView() const
|
|
{
|
|
return std::string_view(NAME_TO_STRING);
|
|
}
|
|
inline Name::Name(const char* str) noexcept: hash(string_hash(str))
|
|
{
|
|
MAKE_NAME_PAIR(hash, str);
|
|
}
|
|
template<std::size_t N>
|
|
inline constexpr Name::Name(const char(&str)[N]) noexcept : hash(string_hash(str))
|
|
{
|
|
MAKE_NAME_PAIR(hash, str);
|
|
}
|
|
inline Name::Name(const std::string& str) noexcept : hash(string_hash(str))
|
|
{
|
|
MAKE_NAME_PAIR(hash, str);
|
|
}
|
|
inline Name::Name(std::string_view str) noexcept : hash(string_hash(str))
|
|
{
|
|
MAKE_NAME_PAIR(hash, str);
|
|
}
|
|
#undef MAKE_NAME_PAIR
|
|
#undef NAME_TO_STRING
|
|
} |