typeidx: add name registration

This commit is contained in:
Danny Robson 2021-01-19 12:17:23 +10:00
parent 957c4d169f
commit 393ecede5a
2 changed files with 33 additions and 0 deletions

View File

@ -7,3 +7,5 @@
*/
#include "typeidx.hpp"
#include <string>

View File

@ -8,8 +8,13 @@
#pragma once
#include "introspection/name.hpp"
#include <atomic>
#include <variant>
#include <vector>
#include <cassert>
namespace cruft {
@ -20,6 +25,14 @@ namespace cruft {
static std::atomic<int> counter;
return ++counter;
}
template <typename TagT>
std::vector<std::string>&
name (void)
{
static std::vector<std::string> s_names (1, "");
return s_names;
}
}
/// Return a globally unique runtime ID for a given type (namespaced by a
@ -48,10 +61,28 @@ namespace cruft {
typeidx (void)
{
static auto id = detail::typeidx_next<TagT> ();
static bool s_done = false;
if (!s_done) {
auto &name = detail::name<TagT> ();
assert (name.size () == std::size_t (id));
name.resize (name.size () + 1);
name[id] = cruft::introspection::name::full<T> ();
s_done = true;
}
return id;
}
template <typename TagT = void>
std::string const&
typeidx_name (int idx)
{
return detail::name<TagT> ()[idx];
}
/// Returns the typeidx of the contained value within a variant.
///
/// May throw std::bad_variant_access if the variant is