|
| 1 | +// examples/meta.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_EXAMPLES_META |
| 5 | +#define INCLUDED_EXAMPLES_META |
| 6 | + |
| 7 | +#include <beman/execution/execution.hpp> |
| 8 | +#include <ostream> |
| 9 | +#include <string> |
| 10 | +#include <typeinfo> |
| 11 | + |
| 12 | +// ---------------------------------------------------------------------------- |
| 13 | + |
| 14 | +namespace meta { |
| 15 | +// The code in this namespace is a fairly basic way to print types. It can |
| 16 | +// almost certainly be done better, in particular using reflection. However, |
| 17 | +// that's beside the point of this example. The important part for the |
| 18 | +// example is that meta::type<T>::name() yields a string representing the |
| 19 | +// type T in some reasonable way |
| 20 | +template <template <typename...> class, typename...> |
| 21 | +struct list { |
| 22 | + static auto name() { return "unknown-list"; } |
| 23 | +}; |
| 24 | +template <> |
| 25 | +struct list<::beman::execution::completion_signatures> { |
| 26 | + static auto name() { return "ex::completion_signatures"; } |
| 27 | +}; |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +struct type { |
| 31 | + static auto name() { return typeid(T).name(); } |
| 32 | +}; |
| 33 | +template <typename T> |
| 34 | +struct type<T&> { |
| 35 | + static auto name() { return typeid(T).name() + std::string("&"); } |
| 36 | +}; |
| 37 | +template <typename T> |
| 38 | +struct type<T&&> { |
| 39 | + static auto name() { return typeid(T).name() + std::string("&&"); } |
| 40 | +}; |
| 41 | +template <typename T> |
| 42 | +struct type<T*> { |
| 43 | + static auto name() { return typeid(T).name() + std::string("*"); } |
| 44 | +}; |
| 45 | +template <typename T> |
| 46 | +struct type<const T> { |
| 47 | + static auto name() { return typeid(T).name() + std::string("const"); } |
| 48 | +}; |
| 49 | +template <typename T> |
| 50 | +struct type<volatile T> { |
| 51 | + static auto name() { return typeid(T).name() + std::string("volatile"); } |
| 52 | +}; |
| 53 | +template <typename T> |
| 54 | +struct type<const volatile T> { |
| 55 | + static auto name() { return typeid(T).name() + std::string("const volatile"); } |
| 56 | +}; |
| 57 | +// NOLINTBEGIN(hicpp-avoid-c-arrays) |
| 58 | +template <typename T, std::size_t N> |
| 59 | +struct type<T[N]> { |
| 60 | + static auto name() { return typeid(T).name() + std::string("[") + std::to_string(N) + "]"; } |
| 61 | +}; |
| 62 | +template <typename T, std::size_t N> |
| 63 | +struct type<T (&)[N]> { |
| 64 | + static auto name() { return typeid(T).name() + std::string("(&)[") + std::to_string(N) + "]"; } |
| 65 | +}; |
| 66 | +template <typename T, std::size_t N> |
| 67 | +struct type<T (*)[N]> { |
| 68 | + static auto name() { return typeid(T).name() + std::string("(*)[") + std::to_string(N) + "]"; } |
| 69 | +}; |
| 70 | +// NOLINTEND(hicpp-avoid-c-arrays) |
| 71 | + |
| 72 | +template <> |
| 73 | +struct type<::beman::execution::set_value_t> { |
| 74 | + static auto name() { return "ex::set_value_t"; } |
| 75 | +}; |
| 76 | +template <> |
| 77 | +struct type<::beman::execution::set_error_t> { |
| 78 | + static auto name() { return "ex::set_error_t"; } |
| 79 | +}; |
| 80 | +template <> |
| 81 | +struct type<::beman::execution::set_stopped_t> { |
| 82 | + static auto name() { return "ex::set_stopped_t"; } |
| 83 | +}; |
| 84 | + |
| 85 | +template <typename T> |
| 86 | +struct type<T()> { |
| 87 | + static auto name() { return type<T>::name() + std::string("()"); } |
| 88 | +}; |
| 89 | +template <typename T, typename A, typename... B> |
| 90 | +struct type<T(A, B...)> { |
| 91 | + static auto name() { |
| 92 | + return type<T>::name() + std::string("(") + (type<A>::name() + ... + (std::string(", ") + type<B>::name())) + |
| 93 | + ")"; |
| 94 | + } |
| 95 | +}; |
| 96 | +template <typename T> |
| 97 | +struct type<T (*)()> { |
| 98 | + static auto name() { return type<T>::name() + std::string("(*)()"); } |
| 99 | +}; |
| 100 | +template <typename T, typename A, typename... B> |
| 101 | +struct type<T (*)(A, B...)> { |
| 102 | + static auto name() { |
| 103 | + return type<T>::name() + std::string("(*)(") + |
| 104 | + (type<A>::name() + ... + (std::string(", ") + type<B>::name())) + ")"; |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +template <template <typename...> class L> |
| 109 | +struct type<L<>> { |
| 110 | + static auto name() { return list<L>::name() + std::string("<>"); } |
| 111 | +}; |
| 112 | + |
| 113 | +template <template <typename...> class L, typename T, typename... S> |
| 114 | +struct type<L<T, S...>> { |
| 115 | + static auto name() { |
| 116 | + return list<L>::name() + std::string("<") + (type<T>::name() + ... + (std::string(", ") + type<S>::name())) + |
| 117 | + ">"; |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +template <typename T> |
| 122 | +inline std::ostream& operator<<(std::ostream& out, const type<T>& t) { |
| 123 | + return out << t.name(); |
| 124 | +} |
| 125 | +} // namespace meta |
| 126 | + |
| 127 | +// ---------------------------------------------------------------------------- |
| 128 | + |
| 129 | +#endif |
0 commit comments