Add meta info and examples

This commit is contained in:
sha512sum 2024-03-27 23:42:04 +00:00
parent 777424069c
commit 7608faa225
7 changed files with 137 additions and 0 deletions

9
examples/src/curry.cpp Normal file
View file

@ -0,0 +1,9 @@
#include <utempl/utils.hpp>
#include <iostream>
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
};

13
examples/src/menu.cpp Normal file
View file

@ -0,0 +1,13 @@
#include <utempl/menu.hpp>
#include <iostream>
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">();
};

9
examples/src/meta.cpp Normal file
View file

@ -0,0 +1,9 @@
#include <utempl/meta_info.hpp>
#include <array>
#include <type_traits>
auto main() -> int {
constexpr std::array types = {utempl::kTypeId<int>, utempl::kTypeId<void>};
static_assert(std::is_same_v<utempl::GetMetaInfo<types[0]>::Type, int>);
static_assert(std::is_same_v<utempl::GetMetaInfo<types[1]>::Type, void>);
};

View file

@ -0,0 +1,13 @@
#include <utempl/overloaded.hpp>
#include <iostream>
auto main() -> int {
utempl::Overloaded(
[](auto&& arg){
},
[](int arg) {
std::cout << arg << std::endl;
}
)(42);
};

View file

@ -0,0 +1,20 @@
#pragma once
namespace utempl::loopholes {
template <auto I>
struct Getter {
friend constexpr auto Magic(Getter<I>);
};
template <auto I, auto Value = 0>
struct Injector {
friend constexpr auto Magic(Getter<I>) {return Value;};
};
template <auto I, typename...>
concept Injected = requires{Magic(Getter<I>{});};
} // namespace utempl::loopholes

View file

@ -0,0 +1,35 @@
#pragma once
#include <utempl/loopholes/core.hpp>
#include <cstddef>
namespace utempl::loopholes {
namespace impl {
template <typename Tag, auto Value>
struct TagWithTalue {};
template <auto I = 0, typename Tag, typename... Ts, auto = Injector<TagWithTalue<Tag, I>{}>{}>
constexpr auto Counter(...) {
return I;
};
template <auto I = 0, typename Tag, typename... Ts>
consteval auto Counter(std::size_t arg) requires Injected<TagWithTalue<Tag, I>{}, Ts...> {
return Counter<I + 1, Tag, Ts...>(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

View file

@ -0,0 +1,38 @@
#pragma once
#include <utempl/loopholes/counter.hpp>
#include <array>
#include <utempl/type_list.hpp>
namespace utempl {
namespace impl {
struct Types {};
} // namespace impl
template <std::size_t Id>
struct MetaInfoKey {
};
template <typename T>
struct MetaInfo {
static constexpr std::size_t kTypeId = loopholes::Counter<impl::Types, T>();
using Type = T;
private:
static constexpr auto _ = loopholes::Injector<MetaInfoKey<kTypeId>{}, TypeList<T>{}>{};
};
template <typename T>
inline constexpr std::size_t kTypeId = MetaInfo<T>::kTypeId;
template <std::size_t Id>
using GetMetaInfo = MetaInfo<typename decltype(Magic(loopholes::Getter<MetaInfoKey<Id>{}>{}))::Type>;
template <std::size_t Id>
using GetType = GetMetaInfo<Id>::Type;
} // namespace utempl