| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | RABERU - Fancy Parameters Library | ||
| 4 | Copyright : RABERU Project Contributors | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <array> | ||
| 11 | #include <cstddef> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <ostream> | ||
| 14 | #include <string_view> | ||
| 15 | #include <utility> | ||
| 16 | |||
| 17 | //====================================================================================================================== | ||
| 18 | //! @defgroup utility Helper types and function | ||
| 19 | //! @brief Tools for interacting with Raberu components | ||
| 20 | //====================================================================================================================== | ||
| 21 | |||
| 22 | //====================================================================================================================== | ||
| 23 | //! @ingroup utility | ||
| 24 | //! @{ | ||
| 25 | //! @defgroup udls User-defined Literal operators | ||
| 26 | //! @brief UDL operators | ||
| 27 | //! @} | ||
| 28 | //====================================================================================================================== | ||
| 29 | namespace rbr::_ | ||
| 30 | { | ||
| 31 | // Lightweight container of value in alternatives | ||
| 32 | template<concepts::keyword T, typename V> struct type_or_ | ||
| 33 | { | ||
| 34 | V value; | ||
| 35 | |||
| 36 | 1 | template<concepts::option... Os> constexpr decltype(auto) operator()(Os&&... os) const | |
| 37 | { | ||
| 38 | 1 | return fetch(*this, RBR_FWD(os)...); | |
| 39 | } | ||
| 40 | }; | ||
| 41 | |||
| 42 | // Type -> String converter | ||
| 43 | template<typename T> constexpr auto typer() noexcept | ||
| 44 | { | ||
| 45 | #if defined(__clang__) | ||
| 46 | constexpr auto pfx = std::string_view("auto rbr::_::typer() [T = ").size(); | ||
| 47 | constexpr auto sfx = std::string_view("]").size(); | ||
| 48 | constexpr auto raw = std::string_view(__PRETTY_FUNCTION__); | ||
| 49 | #elif defined(__GNUC__) | ||
| 50 | constexpr auto pfx = std::string_view("constexpr auto rbr::_::typer() [with " | ||
| 51 | "T = ") | ||
| 52 | .size(); | ||
| 53 | constexpr auto sfx = std::string_view("]").size(); | ||
| 54 | constexpr auto raw = std::string_view(__PRETTY_FUNCTION__); | ||
| 55 | #elif defined(_MSC_VER) | ||
| 56 | constexpr auto pfx = std::string_view("auto __cdecl rbr::_::typer<").size(); | ||
| 57 | constexpr auto sfx = std::string_view(">(void)").size(); | ||
| 58 | constexpr auto raw = std::string_view(__FUNCSIG__); | ||
| 59 | #endif | ||
| 60 | auto value = raw; | ||
| 61 | value.remove_prefix(pfx); | ||
| 62 | value.remove_suffix(sfx); | ||
| 63 | |||
| 64 | constexpr auto size = raw.size() - (pfx + sfx); | ||
| 65 | auto fn = [&]<std::size_t... Is>(std::index_sequence<Is...>) { | ||
| 66 | return std::array<char const, sizeof...(Is) + 1>{value[Is]...}; | ||
| 67 | }; | ||
| 68 | |||
| 69 | return fn(std::make_index_sequence<size>{}); | ||
| 70 | } | ||
| 71 | |||
| 72 | template<typename T> inline constexpr auto type_array = typer<T>(); | ||
| 73 | |||
| 74 | template<typename T, auto ID = type_array<T>> struct type_t | ||
| 75 | { | ||
| 76 | 34 | static constexpr auto name() { return std::string_view(ID.data(), ID.size()); } | |
| 77 | }; | ||
| 78 | |||
| 79 | template<typename T> inline constexpr auto type = type_t<T>{}; | ||
| 80 | } | ||
| 81 | |||
| 82 | #if !defined(RBR_MAX_LITERAL_SIZE) | ||
| 83 | #define RBR_MAX_LITERAL_SIZE 32 | ||
| 84 | #endif | ||
| 85 | |||
| 86 | namespace rbr | ||
| 87 | { | ||
| 88 | //==================================================================================================================== | ||
| 89 | //! @namespace rbr::literals | ||
| 90 | //! @brief Raberu literals namespace | ||
| 91 | //==================================================================================================================== | ||
| 92 | namespace literals | ||
| 93 | { | ||
| 94 | //================================================================================================================== | ||
| 95 | //! @ingroup utility | ||
| 96 | //! @brief Compile-time static string | ||
| 97 | //================================================================================================================== | ||
| 98 | struct str | ||
| 99 | { | ||
| 100 | static constexpr std::size_t max_size = RBR_MAX_LITERAL_SIZE; | ||
| 101 | |||
| 102 | char data_[max_size + 1]; | ||
| 103 | std::uint8_t size_; | ||
| 104 | |||
| 105 | template<std::size_t N, std::size_t... Is> | ||
| 106 | requires _::fits<N, max_size> | ||
| 107 | constexpr str(char const (&s)[N], std::index_sequence<Is...>) : data_{s[Is]...}, size_(N) | ||
| 108 | { | ||
| 109 | } | ||
| 110 | |||
| 111 | template<std::size_t N> | ||
| 112 | requires _::fits<N, max_size> | ||
| 113 | constexpr str(char const (&s)[N]) : str{s, std::make_index_sequence<N>{}} | ||
| 114 | { | ||
| 115 | } | ||
| 116 | |||
| 117 | constexpr std::size_t size() const { return size_; } | ||
| 118 | 18 | constexpr std::string_view value() const { return std::string_view(&data_[0], size_); } | |
| 119 | |||
| 120 | 18 | friend std::ostream& operator<<(std::ostream& os, str const& s) { return os << '\'' << s.value() << '\''; } | |
| 121 | }; | ||
| 122 | } | ||
| 123 | |||
| 124 | //==================================================================================================================== | ||
| 125 | //! @ingroup utility | ||
| 126 | //! @brief Compile-time text based ID | ||
| 127 | //! @tparam ID Compile-time string for the ID | ||
| 128 | //==================================================================================================================== | ||
| 129 | template<literals::str ID> struct id_ | ||
| 130 | { | ||
| 131 | /// Inserts an rbr::id_ in an output stream | ||
| 132 | 31 | friend std::ostream& operator<<(std::ostream& os, id_ const&) { return os << ID; } | |
| 133 | }; | ||
| 134 | |||
| 135 | namespace literals | ||
| 136 | { | ||
| 137 | //================================================================================================================== | ||
| 138 | //! @ingroup udls | ||
| 139 | //! @brief Forms an ID constant literal | ||
| 140 | //! @return An instance of rbr::id_ for the specified string | ||
| 141 | //================================================================================================================== | ||
| 142 | template<str ID> constexpr auto operator""_id() noexcept | ||
| 143 | { | ||
| 144 | return id_<ID>{}; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 |