HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
tag.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "strings.hpp"
8#include "fixed_string.hpp"
9#include <string>
10#include <string_view>
11#include <exception>
12#include <cstdint>
13#include <typeinfo>
14#include <typeindex>
15
16namespace tt {
17
18template<basic_fixed_string Head, basic_fixed_string... Tail>
19std::string tag_at_index_impl(size_t index) noexcept
20{
21 if constexpr (sizeof...(Tail) > 0) {
22 return index == 0 ? Head : tag_at_index_impl<Tail...>(index - 1);
23 } else {
24 return index == 0 ? Head : std::string{};
25 }
26}
27
31template<basic_fixed_string... Tags>
32std::string tag_at_index(size_t index) noexcept
33{
34 if constexpr (sizeof...(Tags) > 0) {
35 return tag_at_index_impl<Tags...>(index);
36 } else {
37 return {};
38 }
39}
40
41template<basic_fixed_string Head, basic_fixed_string... Tail>
42size_t index_of_tag_impl(std::string tag, size_t index) noexcept
43{
44 if constexpr (sizeof...(Tail) > 0) {
45 return tag == Head ? index : index_of_tag_impl<Tail...>(tag, index + 1);
46 } else {
47 return tag == Head ? index : index + 1;
48 }
49}
50
54template<basic_fixed_string... Tags>
55size_t index_of_tag(std::string tag) noexcept
56{
57 if constexpr (sizeof...(Tags) > 0) {
58 return index_of_tag_impl<Tags...>(tag, 0);
59 } else {
60 return 1;
61 }
62}
63
64template<basic_fixed_string Needle, basic_fixed_string Head, basic_fixed_string... Tail>
65constexpr size_t index_of_tag_impl(size_t index) noexcept
66{
67 if constexpr (sizeof...(Tail) > 0) {
68 return Needle == Head ? index : index_of_tag_impl<Needle,Tail...>(index + 1);
69 } else {
70 return Needle == Head ? index : index + 1;
71 }
72}
73
77template<basic_fixed_string Needle, basic_fixed_string... Haystack>
78constexpr size_t index_of_tag() noexcept
79{
80 if constexpr (sizeof...(Haystack) > 0) {
81 return index_of_tag_impl<Needle, Haystack...>(0);
82 } else {
83 return 1;
84 }
85}
86
87template<basic_fixed_string Needle, basic_fixed_string... Haystack>
88constexpr bool has_tag() noexcept {
89 return index_of_tag<Needle, Haystack...>() < sizeof...(Haystack);
90}
91
92template<basic_fixed_string... Haystack>
93bool has_tag(std::string needle) noexcept {
94 return index_of_tag<Haystack...>(needle) < sizeof...(Haystack);
95}
96
97
98}