From 7608faa225ac8c4e98dc0aa0fbeab8de96893063 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 27 Mar 2024 23:42:04 +0000 Subject: [PATCH] Add meta info and examples --- examples/src/curry.cpp | 9 +++++++ examples/src/menu.cpp | 13 ++++++++++ examples/src/meta.cpp | 9 +++++++ examples/src/overloaded.cpp | 13 ++++++++++ include/utempl/loopholes/core.hpp | 20 +++++++++++++++ include/utempl/loopholes/counter.hpp | 35 +++++++++++++++++++++++++ include/utempl/meta_info.hpp | 38 ++++++++++++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 examples/src/curry.cpp create mode 100644 examples/src/menu.cpp create mode 100644 examples/src/meta.cpp create mode 100644 examples/src/overloaded.cpp create mode 100644 include/utempl/loopholes/core.hpp create mode 100644 include/utempl/loopholes/counter.hpp create mode 100644 include/utempl/meta_info.hpp diff --git a/examples/src/curry.cpp b/examples/src/curry.cpp new file mode 100644 index 0000000..1533cb9 --- /dev/null +++ b/examples/src/curry.cpp @@ -0,0 +1,9 @@ +#include +#include + +auto main() -> int { + constexpr auto f = utempl::Curry([](auto... args){ + return (0 + ... + args); + }); + std::cout << f(1)(2)(3) << std::endl; // Call on cast to return type +}; diff --git a/examples/src/menu.cpp b/examples/src/menu.cpp new file mode 100644 index 0000000..0fa6edb --- /dev/null +++ b/examples/src/menu.cpp @@ -0,0 +1,13 @@ +#include +#include + +auto main() -> int { + utempl::menu::Menu{} + .With<{"This is 0"}>([]{ + std::cout << "You entered 0" << std::endl; + }) + .With<{"Some Long", "S"}>([]{ + std::cout << "It aligns the output to the longest element" << std::endl; + }) + .Run<"[{0}]{2} - |{1}|\n">(); +}; diff --git a/examples/src/meta.cpp b/examples/src/meta.cpp new file mode 100644 index 0000000..3f73537 --- /dev/null +++ b/examples/src/meta.cpp @@ -0,0 +1,9 @@ +#include +#include +#include + +auto main() -> int { + constexpr std::array types = {utempl::kTypeId, utempl::kTypeId}; + static_assert(std::is_same_v::Type, int>); + static_assert(std::is_same_v::Type, void>); +}; diff --git a/examples/src/overloaded.cpp b/examples/src/overloaded.cpp new file mode 100644 index 0000000..28a1345 --- /dev/null +++ b/examples/src/overloaded.cpp @@ -0,0 +1,13 @@ +#include +#include + + +auto main() -> int { + utempl::Overloaded( + [](auto&& arg){ + }, + [](int arg) { + std::cout << arg << std::endl; + } + )(42); +}; diff --git a/include/utempl/loopholes/core.hpp b/include/utempl/loopholes/core.hpp new file mode 100644 index 0000000..953a87c --- /dev/null +++ b/include/utempl/loopholes/core.hpp @@ -0,0 +1,20 @@ +#pragma once + +namespace utempl::loopholes { + +template +struct Getter { + friend constexpr auto Magic(Getter); +}; +template +struct Injector { + friend constexpr auto Magic(Getter) {return Value;}; +}; + + +template +concept Injected = requires{Magic(Getter{});}; + + + +} // namespace utempl::loopholes diff --git a/include/utempl/loopholes/counter.hpp b/include/utempl/loopholes/counter.hpp new file mode 100644 index 0000000..f4d773d --- /dev/null +++ b/include/utempl/loopholes/counter.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include + +namespace utempl::loopholes { + +namespace impl { + +template +struct TagWithTalue {}; + +template {}>{}> +constexpr auto Counter(...) { + return I; +}; + +template +consteval auto Counter(std::size_t arg) requires Injected{}, Ts...> { + return Counter(arg); +}; +} // namespace impl; + +// For incerement counter need a unique Ts... +template < + typename Tag, + typename... Ts, + auto R = impl::Counter<0, Tag, Ts...>(std::size_t{}) +> +consteval auto Counter(auto...) { + return R; +}; + + + +} // namespace utempl::loopholes diff --git a/include/utempl/meta_info.hpp b/include/utempl/meta_info.hpp new file mode 100644 index 0000000..44ef97a --- /dev/null +++ b/include/utempl/meta_info.hpp @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include + +namespace utempl { + +namespace impl { + +struct Types {}; +} // namespace impl + + +template +struct MetaInfoKey { + +}; + + +template +struct MetaInfo { + static constexpr std::size_t kTypeId = loopholes::Counter(); + using Type = T; +private: + static constexpr auto _ = loopholes::Injector{}, TypeList{}>{}; + +}; + +template +inline constexpr std::size_t kTypeId = MetaInfo::kTypeId; + +template +using GetMetaInfo = MetaInfo{}>{}))::Type>; + +template +using GetType = GetMetaInfo::Type; + +} // namespace utempl