2017-05-29 17:21:11 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-05-29 17:21:11 +10:00
|
|
|
*
|
2019-05-30 10:43:05 +10:00
|
|
|
* Copyright 2017-2019 Danny Robson <danny@nerdcruft.net>
|
2017-05-29 17:21:11 +10:00
|
|
|
*/
|
|
|
|
|
2019-05-30 10:43:05 +10:00
|
|
|
#pragma once
|
2017-05-29 17:21:11 +10:00
|
|
|
|
2021-01-19 13:17:23 +11:00
|
|
|
#include "introspection/name.hpp"
|
|
|
|
|
2019-09-04 08:06:29 +10:00
|
|
|
#include <atomic>
|
2020-08-12 10:18:23 +10:00
|
|
|
#include <variant>
|
2021-01-19 13:17:23 +11:00
|
|
|
#include <vector>
|
2021-03-25 13:02:00 +11:00
|
|
|
#include <string_view>
|
2021-01-19 13:17:23 +11:00
|
|
|
|
|
|
|
#include <cassert>
|
2019-09-04 08:06:29 +10:00
|
|
|
|
2017-05-29 17:21:11 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2019-09-04 08:06:29 +10:00
|
|
|
namespace detail {
|
|
|
|
template <typename TagT>
|
|
|
|
int typeidx_next (void)
|
|
|
|
{
|
2021-03-25 13:02:00 +11:00
|
|
|
if constexpr (std::is_same_v<TagT, void>) {
|
|
|
|
// This must match the highest offset of the default set of names.
|
|
|
|
static std::atomic<int> counter = 17;
|
|
|
|
return ++counter;
|
|
|
|
} else {
|
|
|
|
static std::atomic<int> counter = 0;
|
|
|
|
return ++counter;
|
|
|
|
}
|
2019-09-04 08:06:29 +10:00
|
|
|
}
|
2021-01-19 13:17:23 +11:00
|
|
|
|
|
|
|
template <typename TagT>
|
2021-03-25 13:02:00 +11:00
|
|
|
std::vector<std::string_view>&
|
2021-01-19 13:17:23 +11:00
|
|
|
name (void)
|
|
|
|
{
|
2021-03-25 13:02:00 +11:00
|
|
|
if constexpr (std::is_same_v<TagT, void>) {
|
|
|
|
// This list is a propulated form of the specialisations for
|
|
|
|
// index queries that appears a little below. The order of this
|
|
|
|
// list _must_ be _exactly_ the same in both locations so that
|
|
|
|
// the indexes match.
|
|
|
|
static std::vector<std::string_view> s_names ({
|
|
|
|
"",
|
|
|
|
"void",
|
|
|
|
"nullptr_t",
|
|
|
|
"bool",
|
|
|
|
"char",
|
|
|
|
"signed char",
|
|
|
|
"unsigned char",
|
|
|
|
"signed short",
|
|
|
|
"unsigned short",
|
|
|
|
"signed int",
|
|
|
|
"unsigned int",
|
|
|
|
"signed long",
|
|
|
|
"unsigned long",
|
|
|
|
"signed long long",
|
|
|
|
"unsigned long long",
|
|
|
|
"float",
|
|
|
|
"double",
|
|
|
|
"long double",
|
|
|
|
});
|
|
|
|
return s_names;
|
|
|
|
} else {
|
|
|
|
static std::vector<std::string_view> s_names (1, "");
|
|
|
|
return s_names;
|
|
|
|
}
|
2021-01-19 13:17:23 +11:00
|
|
|
}
|
2021-03-25 13:02:00 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename TagT = void>
|
|
|
|
struct queryidx {
|
|
|
|
static int get (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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SPECIALISE(KLASS, IDX) \
|
|
|
|
template <> \
|
|
|
|
struct queryidx<KLASS, void> { \
|
|
|
|
static consteval int get (void) { \
|
|
|
|
return IDX; \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
SPECIALISE(void, 1)
|
|
|
|
SPECIALISE(std::nullptr_t, 2)
|
|
|
|
|
|
|
|
SPECIALISE(bool, 3)
|
|
|
|
|
|
|
|
SPECIALISE(char, 4)
|
|
|
|
SPECIALISE(signed char, 5)
|
|
|
|
SPECIALISE(unsigned char, 6)
|
|
|
|
|
|
|
|
SPECIALISE(signed short, 7)
|
|
|
|
SPECIALISE(unsigned short, 8)
|
|
|
|
SPECIALISE(signed int, 9)
|
|
|
|
SPECIALISE(unsigned int, 10)
|
|
|
|
SPECIALISE(signed long, 11)
|
|
|
|
SPECIALISE(unsigned long, 12)
|
|
|
|
SPECIALISE(signed long long, 13)
|
|
|
|
SPECIALISE(unsigned long long, 14)
|
|
|
|
|
|
|
|
SPECIALISE(float, 15)
|
|
|
|
SPECIALISE(double, 16)
|
|
|
|
SPECIALISE(long double, 17)
|
|
|
|
|
|
|
|
|
|
|
|
#undef SPECIALISE
|
2019-09-04 08:06:29 +10:00
|
|
|
}
|
2017-05-29 17:21:11 +10:00
|
|
|
|
2021-03-25 13:02:00 +11:00
|
|
|
|
2019-09-04 08:06:29 +10:00
|
|
|
/// Return a globally unique runtime ID for a given type (namespaced by a
|
|
|
|
/// tag type).
|
2017-05-29 17:21:11 +10:00
|
|
|
///
|
2019-05-30 10:43:05 +10:00
|
|
|
/// This is intended to be used as a lightweight type check for variants
|
2017-05-29 17:21:11 +10:00
|
|
|
/// and such without requiring RTTI.
|
|
|
|
///
|
2019-05-30 10:43:05 +10:00
|
|
|
/// The identifier is constructed at runtime and is not guaranteed to be
|
2017-05-29 17:21:11 +10:00
|
|
|
/// stable across executions (particularly in the case of threads and
|
2019-05-30 10:43:05 +10:00
|
|
|
/// other non-determinism). However it _is_ threadsafe to call this.
|
2017-05-29 17:21:11 +10:00
|
|
|
///
|
2019-05-30 10:43:05 +10:00
|
|
|
/// The value 0 will never be returned from this function and can be used
|
|
|
|
/// to indicate an 'unknown' state.
|
|
|
|
///
|
|
|
|
/// The range of identifiers is _probably_ contiguous. This should not
|
|
|
|
/// be relied upon for correctness, but may be used for performance
|
|
|
|
/// concerns.
|
2019-09-04 08:06:29 +10:00
|
|
|
///
|
|
|
|
/// \tparam T The type to register-and-retrieve the ID for
|
|
|
|
/// \tparam TagT A namespacing type; different tags will have different
|
|
|
|
/// sets of IDs. The default Tag is void.
|
|
|
|
/// \return The unique ID of the type
|
|
|
|
template <typename T, typename TagT = void>
|
2021-03-25 13:02:00 +11:00
|
|
|
constexpr int
|
2017-05-29 17:21:11 +10:00
|
|
|
typeidx (void)
|
|
|
|
{
|
2021-03-25 13:02:00 +11:00
|
|
|
return detail::queryidx<T,TagT>::get ();
|
2017-05-29 17:21:11 +10:00
|
|
|
}
|
2020-08-12 10:18:23 +10:00
|
|
|
|
|
|
|
|
2021-01-19 13:17:23 +11:00
|
|
|
template <typename TagT = void>
|
2021-03-25 13:02:00 +11:00
|
|
|
std::string_view
|
2021-01-19 13:17:23 +11:00
|
|
|
typeidx_name (int idx)
|
|
|
|
{
|
|
|
|
return detail::name<TagT> ()[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 10:18:23 +10:00
|
|
|
/// Returns the typeidx of the contained value within a variant.
|
|
|
|
///
|
|
|
|
/// May throw std::bad_variant_access if the variant is
|
|
|
|
/// valueless_by_exception.
|
|
|
|
template <typename TagT=void, typename ...ComponentT>
|
|
|
|
int
|
|
|
|
value_typeidx (std::variant<ComponentT...> const &val)
|
|
|
|
{
|
|
|
|
return std::visit (
|
|
|
|
[] <typename ValueT> (ValueT const &) {
|
|
|
|
return typeidx<ValueT> ();
|
|
|
|
},
|
|
|
|
val
|
|
|
|
);
|
|
|
|
}
|
2017-05-29 17:21:11 +10:00
|
|
|
}
|