15#ifndef DOXYGEN_SHOULD_SKIP_THIS
19template <std::size_t... Idxs>
20constexpr auto substring_as_array(std::string_view str,
21 std::index_sequence<Idxs...>) {
22 return std::array{str[Idxs]...};
26constexpr auto type_name_array() {
28 constexpr auto prefix = std::string_view{
"[T = symmetri::"};
29 constexpr auto suffix = std::string_view{
"]"};
30 constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
31#elif defined(__GNUC__)
32 constexpr auto prefix = std::string_view{
"with T = symmetri::"};
33 constexpr auto suffix = std::string_view{
"]"};
34 constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
35#elif defined(_MSC_VER)
36 constexpr auto prefix = std::string_view{
"type_name_array<symmetri::"};
37 constexpr auto suffix = std::string_view{
">(void)"};
38 constexpr auto function = std::string_view{__FUNCSIG__};
40#error Unsupported compiler
43 constexpr auto start = function.find(prefix) + prefix.size();
44 constexpr auto end = function.rfind(suffix);
46 static_assert(start < end);
48 constexpr auto name = function.substr(start, (end - start));
49 return substring_as_array(name, std::make_index_sequence<name.size()>{});
53struct type_name_holder {
54 static inline constexpr auto value = type_name_array<T>();
58constexpr auto type_name() -> std::string_view {
59 constexpr auto& value = type_name_holder<T>::value;
60 return std::string_view{value.data(), value.size()};
68 template <
typename...>
69 friend constexpr auto is_defined(tag) {
74 template <
typename...>
75 friend constexpr auto is_defined(tag);
77 template <
typename Tag = tag,
auto I = (int)is_defined(Tag{})>
78 static constexpr auto exists(
decltype(I)) {
82 static constexpr auto exists(...) {
return generator(),
false; }
85template <
typename T,
auto Id =
int{}>
86constexpr auto unique_id() {
87 if constexpr (counter<Id>::exists(Id)) {
88 return unique_id<T, Id + 1>();
105 const static size_t kMaxTokenColors =
108 inline static std::array<std::string_view, kMaxTokenColors>
120 constexpr Token(T*
const) : idx(unique_id<T>()) {
121 static_assert(unique_id<T>() < v.size(),
122 "There can only be 100 different token-colors.");
123 v[idx] = type_name<T>();
135 : idx([&]() -> size_t {
137 auto it = std::find(v.cbegin(), v.cend(), s);
138 if (it == std::cend(v)) {
142 return std::distance(v.cbegin(), it);
145 if (std::find(v.cbegin(), v.cend(), s) == std::cend(v)) {
146 assert(v[idx].empty() &&
"There can only be 100 different token-colors.");
159 std::vector<std::string_view> colors;
160 colors.reserve(v.size());
161 std::copy_if(v.begin(), v.end(), std::back_inserter(colors),
162 [](
const auto& color) { return not color.empty(); });
165 constexpr bool operator<(
const Token& rhs)
const {
168 constexpr bool operator>(
const Token& rhs)
const {
169 return idx > rhs.toIndex();
176 constexpr size_t toIndex()
const {
return idx; }
183 constexpr const auto&
toString()
const {
return v[idx]; }
184 constexpr bool operator==(
const Token& c)
const {
return idx == c.idx; }
186 constexpr bool operator==(
const T&)
const {
187 return idx == unique_id<T>();
198#define CREATE_CUSTOM_TOKEN(name) \
199 namespace symmetri { \
200 struct name : public Token { \
201 constexpr name() : Token(this) {} \
202 constexpr bool operator==(const Token& c) const { \
203 return toIndex() == c.toIndex(); \
206 static inline name name; \
Tokens are elements that can reside in places. Tokens can have a color which makes them distinguishab...
Definition colors.hpp:104
Token(const char *s)
Construct a new Token object from a string at run-time. A unique id is generated and if it fails it w...
Definition colors.hpp:134
static std::vector< std::string_view > getColors()
Get a list of all the colors.
Definition colors.hpp:158
constexpr const auto & toString() const
returns the string-representation for this color.
Definition colors.hpp:183
constexpr size_t toIndex() const
returns the unique index for this color.
Definition colors.hpp:176
constexpr Token(T *const)
Creates a Token with a unique numerical id and a string representation based on the name of the argum...
Definition colors.hpp:120
#define CREATE_CUSTOM_TOKEN(name)
A macro from which we can create token-colors. Colors ceated this way end up in the symmetri namespac...
Definition colors.hpp:198
Definition colors.hpp:213
Definition colors.hpp:212
Definition colors.hpp:215
Definition colors.hpp:214
Definition colors.hpp:209
Definition colors.hpp:210
Definition colors.hpp:211