From 7b5c08464d45d273d6219f2b8e6a1c90ee85c2ac Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 28 Feb 2024 00:50:13 +0000 Subject: [PATCH] Add #pragma once to files, add utils --- include/utempl/menu.hpp | 4 +- include/utempl/tuple.hpp | 26 ++++--- include/utempl/type_list.hpp | 18 +++++ include/utempl/utils.hpp | 133 +++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 13 deletions(-) create mode 100644 include/utempl/utils.hpp diff --git a/include/utempl/menu.hpp b/include/utempl/menu.hpp index 1eac333..5d97183 100644 --- a/include/utempl/menu.hpp +++ b/include/utempl/menu.hpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -10,9 +11,6 @@ namespace utempl { -template -struct Wrapper {}; - constexpr std::size_t CountDigits(std::size_t num) { std::size_t count = 0; do { diff --git a/include/utempl/tuple.hpp b/include/utempl/tuple.hpp index c601396..5a2d468 100644 --- a/include/utempl/tuple.hpp +++ b/include/utempl/tuple.hpp @@ -1,7 +1,10 @@ +#pragma once #include namespace utempl { +template +struct Wrapper; namespace impl { template @@ -56,6 +59,11 @@ inline constexpr auto Get(Tuple&& tuple) -> auto&& requires (I < sizeof.. return std::move(static_cast&&>(tuple).value); }; +template +inline constexpr auto Get(T&& arg) -> decltype(get(std::forward(arg))) { + return get(std::forward(arg)); +}; + template struct Tuple : public impl::TupleHelper<0, Ts...> { template @@ -70,21 +78,19 @@ struct Tuple : public impl::TupleHelper<0, Ts...> { return {Get(*this)..., Get(other)...}; }(std::make_index_sequence(), std::make_index_sequence()); }; + template + inline constexpr auto operator[](Wrapper) const -> const auto& { + return Get(*this); + }; + template + inline constexpr auto operator[](Wrapper) -> auto& { + return Get(*this); + }; }; template Tuple(Ts&&...) -> Tuple...>; -template -struct TupleSize {}; - -template -struct TupleSize> { - static constexpr auto value = sizeof...(Ts); -}; -template -inline constexpr auto kTupleSize = TupleSize::value; - template consteval auto ListFromTuple(Tuple) -> TypeList { return {}; diff --git a/include/utempl/type_list.hpp b/include/utempl/type_list.hpp index aaee0fa..c66adc4 100644 --- a/include/utempl/type_list.hpp +++ b/include/utempl/type_list.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include @@ -16,6 +17,14 @@ struct Caster { template struct TypeList { }; + +template +inline constexpr auto kType = TypeList{}; + +template +inline constexpr auto kTypeList = TypeList{}; + + template consteval auto operator==(const TypeList& first, const TypeList& second) -> bool { return std::same_as; @@ -32,4 +41,13 @@ consteval auto Find(TypeList) -> std::size_t { return std::ranges::find(arr, true) - arr.begin(); }; +template +consteval auto Reverse(TypeList list) { + return [&](std::index_sequence) -> TypeList(list))...> { + return {}; + }(std::make_index_sequence()); +}; + + + } // namespace utempl diff --git a/include/utempl/utils.hpp b/include/utempl/utils.hpp new file mode 100644 index 0000000..1f47d13 --- /dev/null +++ b/include/utempl/utils.hpp @@ -0,0 +1,133 @@ +#pragma once +#include +#include +#include +#include +namespace utempl { + +template +struct Wrapper { + static constexpr auto kValue = Value; + inline constexpr auto operator==(auto&& arg) { + return arg == Value; + }; + consteval operator decltype(Value)() { + return Value; + }; +}; + +template +consteval auto ParseNumber() -> T { + T response{}; + for(const auto& c : string) { + if (c >= '0' && c <= '9') { + response = response * 10 + (c - '0'); + }; + }; + return response; +}; +namespace literals { + +template +consteval auto operator"" _c() { + return Wrapper({cs...})>()>{}; +}; + +} // namespace literals + +template +inline constexpr auto Arg(Ts&&... args) requires (I < sizeof...(Ts)) { + return [&](std::index_sequence){ + return [](decltype(Caster(Is))..., auto&& response, ...){ + return response; + }(std::forward(args)...); + }(std::make_index_sequence()); +}; + + +template +inline constexpr auto Times(auto&& f) { + [&](std::index_sequence){ + (Arg<0>(f, Is)(), ...); + }(std::make_index_sequence()); +}; + + +template +concept TupleLike = Overloaded( + []