Migrate to C++20 modules

This commit is contained in:
sha512sum 2024-07-31 02:19:28 +00:00
parent 16889ef3b0
commit 6367dccf19
35 changed files with 623 additions and 604 deletions

24
.gitignore vendored Normal file
View file

@ -0,0 +1,24 @@
.cache/
.ninja_deps
.ninja_log
CMakeCache.txt
CMakeFiles/
CPM_modules/
CTestTestfile.cmake
_deps/
build.ninja
cmake/CPM.cmake
cmake_install.cmake
compile_commands.json
compile_commands.json.tmp9cdd8
cpm-package-lock.cmake
examples/output/
install_manifest.txt
lib__cmake_cxx23.a
utemplConfig.cmake
utemplConfigVersion.cmake
utempl_tests
utempl_tests\[1\]_include.cmake
utempl_tests\[1\]_tests.cmake
libutempl_shared.so
libutempl_static.so

View file

@ -1,12 +1,26 @@
cmake_minimum_required(VERSION 3.27)
cmake_minimum_required(VERSION 3.30)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD
"0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
project(utempl
VERSION 0.1)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDART 23)
VERSION 0.1
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CXX_EXTENSIONS NO)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_MODULE_STD 1)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(FMT_MODULE ON)
set(BOOST_INCLUDE_LIBRARIES "pfr")
option(CPM_USE_LOCAL_PACKAGES "Use local packages" ON)
option(BUILD_SHARED_LIBS "Build shared libraries (.so)" ON)
option(BUILD_STATIC_LIBS "Build static libraries (.a)" OFF)
file(GLOB_RECURSE SOURCES "src/*.cpp")
set_target_properties(__cmake_cxx23 PROPERTIES CXX_EXTENSIONS OFF)
file(
@ -26,38 +40,71 @@ CPMAddPackage(
OPTIONS "BOOST_SKIP_INSTALL_RULES OFF"
)
CPMAddPackage("gh:fmtlib/fmt#10.2.1")
set(TMP ${CPM_USE_LOCAL_PACKAGES})
set(CPM_USE_LOCAL_PACKAGES OFF)
CPMAddPackage(
NAME fmt
URL
"https://github.com/linuxnyasha/fmt_import/archive/refs/heads/linuxnyasha-patch-1.zip"
# Fixes for fmt module and constexpr fmt::formatted_size
OPTIONS "CMAKE_CXX_FLAGS -DFMT_ATTACH_TO_GLOBAL_MODULE"
)
set(CPM_USE_LOCAL_PACKAGES ${TMP})
function(configure target)
target_compile_features(${target} INTERFACE cxx_std_23)
target_include_directories(${target} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_sources(${target} PUBLIC FILE_SET ${target} TYPE CXX_MODULES
FILES ${SOURCES})
install(TARGETS ${target}
EXPORT utemplTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
FILE_SET ${target} DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
if(TARGET Boost::pfr)
target_link_libraries(${target} PUBLIC fmt::fmt
Boost::pfr)
else()
find_package(Boost 1.85.0 REQUIRED)
target_link_libraries(${target} PUBLIC fmt::fmt
${Boost_LIBRARIES})
endif()
endfunction(configure)
cmake_policy(SET CMP0079 NEW)
include(GNUInstallDirs)
add_library(utempl INTERFACE)
add_library(utempl::utempl ALIAS utempl)
target_include_directories(
utempl
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(TARGET Boost::pfr)
target_link_libraries(utempl INTERFACE fmt::fmt-header-only Boost::pfr)
else()
find_package(Boost 1.85.0 REQUIRED)
target_link_libraries(utempl INTERFACE fmt::fmt-header-only ${Boost_LIBRARIES})
if(BUILD_SHARED_LIBS)
add_library(utempl_shared)
add_library(utempl::utempl ALIAS utempl_shared)
configure(utempl_shared)
endif()
target_compile_features(utempl INTERFACE cxx_std_23)
install(TARGETS utempl
EXPORT utemplTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(BUILD_STATIC_LIBS)
add_library(utempl_static)
if(NOT BUILD_SHARED_LIBS)
add_library(utempl ALIAS utempl_static)
add_library(utempl::utempl ALIAS utempl_static)
endif()
configure(utempl_static)
endif()
include(CMakePackageConfigHelpers)
write_basic_package_version_file("utemplConfigVersion.cmake"
@ -87,8 +134,10 @@ if(ENABLE_TESTS)
find_package(GTest REQUIRED)
enable_testing()
file(GLOB SOURCES tests/* tests/*/* tests/*/*/*)
add_executable(utempl_tests ${SOURCES})
target_link_libraries(utempl_tests GTest::gtest_main utempl)
add_executable(utempl_tests)
target_sources(utempl_tests PUBLIC FILE_SET utempl_tests TYPE CXX_MODULES
FILES ${SOURCES})
target_link_libraries(utempl_tests GTest::gtest_main utempl::utempl)
set_property(TARGET utempl_tests PROPERTY CXX_STANDARD 23)
include(GoogleTest)
gtest_discover_tests(utempl_tests)
@ -98,7 +147,7 @@ if(ENABLE_EXAMPLES)
foreach(EXAMPLE_SRC ${EXAMPLES_SRC})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE)
add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC})
target_link_libraries(${EXAMPLE_NAME} utempl)
target_link_libraries(${EXAMPLE_NAME} utempl::utempl)
set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23)
set_target_properties(${EXAMPLE_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/output")

View file

@ -1,5 +1,7 @@
#include <utempl/attributes.hpp>
#include <string>
import std;
import utempl.attributes;
import utempl.type_list;
#include <utempl/macro.hpp>
template <typename T>
struct AttributeData {
@ -7,25 +9,21 @@ struct AttributeData {
constexpr auto operator==(const AttributeData<T>&) const -> bool = default;
};
#define MY_ATTRIBUTE(type, ...) GENERIC_ATTRIBUTE(AttributeData<type>{__VA_ARGS__}) // NOLINT
#define MY_ATTRIBUTE(type, ...) GENERIC_ATTRIBUTE(AttributeData<type>{__VA_ARGS__})
// clang-format off
ATTRIBUTE_STRUCT(SomeStruct,
MY_ATTRIBUTE(int, .value = 2);
int field;
SKIP_ATTRIBUTE();
int field2;
MY_ATTRIBUTE(std::string, .value = "HEY!")
MY_ATTRIBUTE(std::string, .value = "HEY!");
std::string field3;
);
static_assert(utempl::GetAttributes<SomeStruct>()
== utempl::Tuple{
AttributeData<int>{.value = 2},
utempl::NoInfo{},
AttributeData<std::string>{.value = "HEY!"}});
// clang-format on
static_assert(utempl::GetAttributes<SomeStruct>() ==
utempl::Tuple{AttributeData<int>{.value = 2}, utempl::NoInfo{}, AttributeData<std::string>{.value = "HEY!"}});
struct SomeOtherStruct {
static_assert(utempl::OpenStruct<SomeOtherStruct>());
@ -35,13 +33,6 @@ struct SomeOtherStruct {
static_assert(utempl::CloseStruct());
};
static_assert(utempl::GetAttributes<SomeOtherStruct>()
== utempl::Tuple{
utempl::kTypeList<int>,
utempl::NoInfo{},
utempl::kTypeList<void>});
static_assert(utempl::GetAttributes<SomeOtherStruct>() == utempl::Tuple{utempl::kTypeList<int>, utempl::NoInfo{}, utempl::kTypeList<void>});
auto main() -> int {};

View file

@ -1,8 +1,8 @@
#include <utempl/utils.hpp>
#include <iostream>
import utempl.utils;
import std;
auto main() -> int {
constexpr auto f = utempl::Curry([](auto... args){
constexpr auto f = utempl::Curry([](auto... args) {
return (0 + ... + args);
});
std::cout << f(1)(2)(3) << std::endl; // Call on cast to return type

View file

@ -1,5 +1,6 @@
#include <utempl/go_interface.hpp>
#include <fmt/core.h>
import fmt;
import utempl.go_interface;
import std;
struct SomeInterfaceImpl {
int a;
@ -7,15 +8,14 @@ struct SomeInterfaceImpl {
};
using SomeInterface = utempl::GoInterface<SomeInterfaceImpl>;
struct SomeStruct {
short a;
short b;
std::int16_t a;
std::int16_t b;
};
inline auto Func(SomeInterface arg) {
fmt::print("{} {}\n", arg.a, arg.b);
};
auto main() -> int {
Func(SomeStruct{42, 300});
Func(SomeStruct{42, 300}); // NOLINT
};

View file

@ -1,4 +1,6 @@
#include <utempl/utils.hpp>
import utempl.utils;
import utempl.tuple;
import std;
auto main() -> int {
constexpr auto value = utempl::LeftFold(

View file

@ -1,4 +1,5 @@
#include <utempl/utils.hpp>
import utempl.utils;
import std;
auto main() -> int {
constexpr auto arr = utempl::TupleMaker<std::array<void*, 0>>::Make();

View file

@ -1,12 +1,12 @@
#include <utempl/menu.hpp>
#include <iostream>
import utempl.menu;
import std;
auto main() -> int {
utempl::menu::Menu{}
.With<{"This is 0"}>([]{
.With<{"This is 0"}>([] {
std::cout << "You entered 0" << std::endl;
})
.With<{"Some Long", "S"}>([]{
.With<{"Some Long", "S"}>([] {
std::cout << "It aligns the output to the longest element" << std::endl;
})
.Run<"[{0}]{2} - |{1}|\n">();

View file

@ -1,6 +1,5 @@
#include <utempl/meta_info.hpp>
#include <array>
#include <type_traits>
import utempl.meta_info;
import std;
auto main() -> int {
constexpr std::array types = {utempl::kTypeId<int>, utempl::kTypeId<void>};

View file

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

View file

@ -1,4 +1,4 @@
#include <utempl/utils.hpp>
import utempl;
struct Container {
float data{};
@ -6,14 +6,16 @@ struct Container {
auto main() -> int {
using namespace utempl;
constexpr auto value = Tuple{1, 2, 3, 4, 5, 6}
| Take<5>()
| Map([](int arg) {return arg + 1;})
| Map([](int arg) -> float {return arg;})
| Map([](float arg) -> Container {return {.data = arg};})
| Reverse()
| Take<3>()
| Reduce(0.f, [](auto accumulator, Container arg) -> float {
constexpr auto value = Tuple{1, 2, 3, 4, 5, 6} | Take<5>() | Map([](int arg) {
return arg + 1;
}) |
Map([](int arg) -> float {
return arg;
}) |
Map([](float arg) -> Container {
return {.data = arg};
}) |
Reverse() | Take<3>() | Reduce(0.f, [](auto accumulator, Container arg) -> float {
return accumulator + arg.data;
}); // Lazy evavalue
static_assert(value == 15.0f);

View file

@ -1,9 +1,10 @@
#include <utempl/utils.hpp>
#include <iostream>
import utempl.utils;
import utempl.tuple;
import std;
auto main() -> int {
auto tuple = Reverse(utempl::Tuple{4, 3, 2, 1});
ForEach(tuple, [](auto arg){
ForEach(tuple, [](auto arg) {
std::cout << arg << std::endl;
});
};

View file

@ -1,4 +1,6 @@
#include <utempl/utils.hpp>
import utempl.utils;
import utempl.tuple;
import std;
auto main() -> int {
static_assert(utempl::Switch<int>(utempl::Tuple{2, 1, 0}, utempl::Tuple{0, 1, 2}, 0, [](int value) {

View file

@ -1,9 +1,9 @@
#include <utempl/utils.hpp>
#include <iostream>
import utempl.utils;
import std;
auto main() -> int {
std::size_t i = 0;
utempl::Times<10>([&](){
utempl::Times<10>([&]() {
++i;
std::cout << i << std::endl;
});

View file

@ -1,7 +1,8 @@
#include <utempl/tuple.hpp>
#include <utempl/utils.hpp>
#include <iostream>
#include <cassert>
import utempl.utils;
import utempl.tuple;
import utempl.overloaded;
import std;
auto main() -> int {
using utempl::literals::operator""_c;
@ -10,14 +11,14 @@ auto main() -> int {
std::ignore = tuple2;
std::cout << utempl::kTupleSize<decltype(tuple)> << std::endl; // Get tuple size
std::cout << tuple[0_c] << std::endl; // Get element using [] with literal
auto newTuple = Transform(tuple, utempl::Overloaded(
[](auto arg){
auto newTuple = Transform(tuple,
utempl::Overloaded(
[](auto arg) {
return std::to_string(arg);
},
[](const char* arg) {
return std::string(arg);
}
));
}));
auto flag = newTuple == utempl::Tuple<std::string, std::string, std::string>{"42", "3.141500", "Hello World"};
assert(flag);
};

View file

@ -1,103 +0,0 @@
#include <utempl/loopholes/counter.hpp>
#include <utempl/meta_info.hpp>
#include <utempl/utils.hpp>
namespace utempl {
namespace impl {
struct AttributesTag {};
template <typename T>
struct AttributesCounterTag {};
} // namespace impl
template <typename T, typename..., auto f = [] {}, auto = AddTypeToTag<impl::AttributesTag, T, decltype(f)>()>
consteval auto OpenStruct() -> bool {
return true;
};
template <typename...,
auto f = [] {},
auto I = loopholes::CountValue<impl::AttributesTag, decltype(f)>(),
auto II = (I >= 2) ? I - 2 : I - 1,
typename T = decltype(Magic(loopholes::Getter<MetaInfoKey<II, impl::AttributesTag>{}>{}))::Type,
auto = AddTypeToTag<impl::AttributesTag, T, decltype(f)>()>
consteval auto CloseStruct() -> bool {
return true;
};
struct NoInfo {
consteval auto operator==(const NoInfo&) const -> bool = default;
};
namespace impl {
template <typename... Ts>
struct FieldAttributeData {
using Type = TypeList<Ts...>;
};
template <>
struct FieldAttributeData<> {
using Type = NoInfo;
};
template <typename T,
typename... Ts,
auto f = [] {},
typename Current = decltype(GetCurrentTagType<impl::AttributesTag, decltype(f)>())::Type,
auto = AddTypeToTag<impl::AttributesCounterTag<Current>, typename FieldAttributeData<Ts...>::Type, decltype(f)>()>
consteval auto FieldAttribute() -> T;
} // namespace impl
template <typename... Ts>
using FieldAttribute = decltype(impl::FieldAttribute<Ts...>());
#define ATTRIBUTE_STRUCT(name, ...) /* NOLINT */ \
\
struct name { \
static_assert(::utempl::OpenStruct<name>()); \
template <std::size_t N> \
static consteval auto GetAttribute(); \
__VA_ARGS__ \
static_assert(::utempl::CloseStruct()); \
}
#define GENERIC_ATTRIBUTE(value) /* NOLINT */ \
template <> \
consteval auto GetAttribute<::utempl::loopholes::Counter< \
::utempl::impl::AttributesCounterTag<decltype(::utempl::GetCurrentTagType<::utempl::impl::AttributesTag, decltype([] {})>())::Type>, \
decltype([] {})>()>() { \
return value; \
}
#define SKIP_ATTRIBUTE() /* NOLINT */ GENERIC_ATTRIBUTE(::utempl::NoInfo{})
template <typename T, auto f = [] {}, bool R = (loopholes::CountValue<impl::AttributesCounterTag<T>, decltype(f)>() > 0)>
concept HasAttributes = R;
template <typename T>
concept HasMacroAttributes = requires { T::template GetAttribute<0>(); };
template <HasAttributes T>
consteval auto GetAttributes()
requires HasMacroAttributes<T>
{
constexpr auto I = loopholes::CountValue<impl::AttributesCounterTag<T>>();
return [](auto... is) {
return Tuple{T::template GetAttribute<is>()...};
} | kSeq<I>;
};
template <typename T>
consteval auto GetAttributes() {
constexpr auto I = loopholes::CountValue<impl::AttributesCounterTag<T>>();
return [](auto... is) {
return utempl::Tuple{typename decltype(Magic(loopholes::Getter<MetaInfoKey<is, impl::AttributesCounterTag<T>>{}>{}))::Type{}...};
} | kSeq<I>;
};
} // namespace utempl

20
include/utempl/macro.hpp Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#define ATTRIBUTE_STRUCT(name, ...) /* NOLINT */ \
\
struct name { \
static_assert(::utempl::OpenStruct<name>()); \
template <std::size_t N> \
static consteval auto GetAttribute(); \
__VA_ARGS__ \
static_assert(::utempl::CloseStruct()); \
}
#define GENERIC_ATTRIBUTE(value) /* NOLINT */ \
template <> \
consteval auto GetAttribute<::utempl::loopholes::Counter< \
::utempl::impl::AttributesCounterTag<decltype(::utempl::GetCurrentTagType<::utempl::impl::AttributesTag, decltype([] {})>())::Type>, \
decltype([] {})>()>() { \
return value; \
}
#define SKIP_ATTRIBUTE() /* NOLINT */ GENERIC_ATTRIBUTE(::utempl::NoInfo{})

View file

@ -1,40 +0,0 @@
#pragma once
namespace utempl {
template <typename T>
struct ReferenceWrapper;
template <typename T>
struct ReferenceWrapper<T&&> {
constexpr auto operator*() -> T& {
return this->value;
};
constexpr auto operator->() -> T* {
return &this->value;
};
T&& value;
};
template <typename T>
struct ReferenceWrapper<const T&> {
constexpr auto operator*() -> const T& {
return this->value;
};
constexpr auto operator->() -> const T* {
return &this->value;
};
const T& value;
};
template <typename T>
struct ReferenceWrapper<T&> {
constexpr auto operator*() -> T& {
return this->value;
};
constexpr auto operator->() -> T* {
return &this->value;
};
T& value;
};
} // namespace utempl

82
src/attributes.cpp Normal file
View file

@ -0,0 +1,82 @@
export module utempl.attributes;
export import utempl.meta_info;
export import utempl.tuple;
export import utempl.loopholes.counter;
import utempl.utils;
import utempl.type_list;
namespace utempl {
namespace impl {
export struct AttributesTag {};
export template <typename T>
struct AttributesCounterTag {};
} // namespace impl
export template <typename T, typename..., auto f = [] {}, auto = AddTypeToTag<impl::AttributesTag, T, decltype(f)>()>
consteval auto OpenStruct() -> bool {
return true;
};
export template <typename...,
auto f = [] {},
auto I = loopholes::CountValue<impl::AttributesTag, decltype(f)>(),
auto II = (I >= 2) ? I - 2 : I - 1,
typename T = decltype(Magic(loopholes::Getter<MetaInfoKey<II, impl::AttributesTag>{}>{}))::Type,
auto = AddTypeToTag<impl::AttributesTag, T, decltype(f)>()>
consteval auto CloseStruct() -> bool {
return true;
};
export struct NoInfo {
consteval auto operator==(const NoInfo&) const -> bool = default;
};
template <typename... Ts>
struct FieldAttributeData {
using Type = TypeList<Ts...>;
};
template <>
struct FieldAttributeData<> {
using Type = NoInfo;
};
template <typename T,
typename... Ts,
auto f = [] {},
typename Current = decltype(GetCurrentTagType<impl::AttributesTag, decltype(f)>())::Type,
auto = AddTypeToTag<impl::AttributesCounterTag<Current>, typename FieldAttributeData<Ts...>::Type, decltype(f)>()>
consteval auto FieldAttributeHelper() -> T;
export template <typename... Ts>
using FieldAttribute = decltype(FieldAttributeHelper<Ts...>());
export template <typename T, auto f = [] {}, bool R = (loopholes::CountValue<impl::AttributesCounterTag<T>, decltype(f)>() > 0)>
concept HasAttributes = R;
export template <typename T>
concept HasMacroAttributes = requires { T::template GetAttribute<0>(); };
export template <HasAttributes T>
consteval auto GetAttributes()
requires HasMacroAttributes<T>
{
constexpr auto I = loopholes::CountValue<impl::AttributesCounterTag<T>>();
return [](auto... is) {
return Tuple{T::template GetAttribute<is>()...};
} | kSeq<I>;
};
export template <typename T>
consteval auto GetAttributes() {
constexpr auto I = loopholes::CountValue<impl::AttributesCounterTag<T>>();
return [](auto... is) {
return utempl::Tuple{typename decltype(Magic(loopholes::Getter<MetaInfoKey<is, impl::AttributesCounterTag<T>>{}>{}))::Type{}...};
} | kSeq<I>;
};
} // namespace utempl

View file

@ -1,28 +1,31 @@
#pragma once
#include <fmt/format.h>
#include <algorithm>
module;
#include <fmt/compile.h>
export module utempl.string;
import std;
// import fmt;
namespace utempl {
template <std::size_t>
export template <std::size_t>
struct ConstexprString;
} // namespace utempl
template <std::size_t Size>
struct fmt::formatter<utempl::ConstexprString<Size>> : public fmt::formatter<std::string_view> {
export {
template <std::size_t Size>
struct fmt::formatter<utempl::ConstexprString<Size>> : public fmt::formatter<std::string_view> {
constexpr auto parse(format_parse_context& ctx) const {
return ctx.begin();
};
constexpr auto format(const utempl::ConstexprString<Size>& str, auto& ctx) const {
return fmt::formatter<std::string_view>::format({str.begin()}, ctx);
};
};
};
namespace utempl {
export namespace utempl {
template <std::size_t Size>
struct ConstexprString {
std::array<char, Size> data;
using char_type = char;
constexpr auto begin() -> char* {
return this->data.begin();
};
@ -74,6 +77,11 @@ struct ConstexprString {
constexpr ConstexprString(ConstexprString&&) = default;
};
template <ConstexprString Str>
consteval auto GetFmtCompiledString() {
return FMT_COMPILE(Str.data.begin());
};
template <std::size_t N>
constexpr auto operator<<(std::ostream& stream, const ConstexprString<N>& str) -> std::ostream& {
stream << static_cast<std::string_view>(str);

View file

@ -1,7 +1,8 @@
#pragma once
module;
#include <boost/pfr.hpp>
#include <type_traits>
#include <utempl/constexpr_string.hpp>
export module utempl.go_interface;
import std;
import utempl.string;
namespace utempl {
@ -19,8 +20,6 @@ constexpr auto Get(T&& arg) {
template <auto...>
struct EmptyField {};
namespace impl {
template <ConstexprString name, typename T>
constexpr auto TryGet(T&& arg) -> decltype(boost::pfr::get<FindField<name, T>()>(std::forward<T>(arg)))
requires(FindField<name, T>() < boost::pfr::tuple_size_v<std::remove_cvref_t<T>>)
@ -41,9 +40,7 @@ constexpr auto Transform(Transformer&& transformer, From&& from) {
}(std::make_index_sequence<boost::pfr::tuple_size_v<To>>());
};
} // namespace impl
struct DefaultFieldTransformer {
export struct DefaultFieldTransformer {
constexpr auto operator()(auto&& arg) -> auto&& {
return arg;
};
@ -56,12 +53,12 @@ struct DefaultFieldTransformer {
};
};
template <typename Value, typename Transformer = DefaultFieldTransformer>
export template <typename Value, typename Transformer = DefaultFieldTransformer>
struct GoInterface : Value {
constexpr GoInterface(Value&& value) : Value(std::move(value)) {}; // NOLINT
constexpr GoInterface(const Value& value) : Value(value) {}; // NOLINT
template <typename T>
constexpr GoInterface(T&& value) : Value(impl::Transform<Value>(Transformer{}, std::forward<T>(value))){}; // NOLINT
constexpr GoInterface(T&& value) : Value(Transform<Value>(Transformer{}, std::forward<T>(value))){}; // NOLINT
constexpr auto operator=(const GoInterface&) -> GoInterface& = default;
constexpr auto operator=(GoInterface&&) -> GoInterface& = default;
constexpr auto operator==(const Value& other) const -> bool
@ -78,9 +75,8 @@ struct GoInterface : Value {
using Type = std::remove_cvref_t<T>;
Transformer transformer;
// + 1 - NULL Terminator
return (
(boost::pfr::get<Is>(static_cast<const Value&>(*this)) ==
transformer(impl::TryGet<ConstexprString<boost::pfr::get_name<Is, Type>().size() + 1>{boost::pfr::get_name<Is, Type>().begin()}>(
return ((boost::pfr::get<Is>(static_cast<const Value&>(*this)) ==
transformer(TryGet<ConstexprString<boost::pfr::get_name<Is, Type>().size() + 1>{boost::pfr::get_name<Is, Type>().begin()}>(
other))) &&
...);
}(std::make_index_sequence<boost::pfr::tuple_size_v<Value>>());

View file

@ -1,19 +1,20 @@
#include <type_traits>
#pragma once
export module utempl.loopholes.core;
import std;
namespace utempl::loopholes {
template <auto I>
struct Getter {
export {
template <auto I>
struct Getter {
friend constexpr auto Magic(Getter<I>);
};
template <auto I, auto Value = 0>
struct Injector {
};
template <auto I, auto Value = 0>
struct Injector {
friend constexpr auto Magic(Getter<I>) {
return Value;
};
};
};
}
template <auto, typename = void, typename... Ts>
struct InjectedImpl {
static constexpr bool value = false;
@ -23,7 +24,7 @@ struct InjectedImpl<V, std::void_t<decltype(Magic(Getter<V>{}))>, Ts...> {
static constexpr bool value = true;
};
template <auto I, typename... Ts>
export template <auto I, typename... Ts>
concept Injected = InjectedImpl<I, void, Ts...>::value;
} // namespace utempl::loopholes

View file

@ -1,19 +1,16 @@
#pragma once
#include <cstddef>
#include <tuple>
#include <utempl/loopholes/core.hpp>
export module utempl.loopholes.counter;
export import utempl.loopholes.core;
import std;
namespace utempl::loopholes {
namespace impl {
template <typename Tag, std::size_t Value>
struct TagWithValue {};
template <bool Add, typename Tag, std::size_t I, typename... Ts>
consteval auto Counter() -> std::size_t {
consteval auto CounterHelper() -> std::size_t {
if constexpr(requires { Magic(Getter<TagWithValue<Tag, I>{}>{}); }) {
return Counter<Add, Tag, I + 1, Ts...>();
return CounterHelper<Add, Tag, I + 1, Ts...>();
};
if constexpr(Add) {
return (std::ignore = Injector<TagWithValue<Tag, I>{}, 0>{}, I);
@ -22,16 +19,14 @@ consteval auto Counter() -> std::size_t {
};
};
} // namespace impl
// For incerement counter need a unique Ts...
template <typename Tag, typename... Ts, std::size_t R = impl::Counter<true, Tag, 0, Ts...>()>
export template <typename Tag, typename... Ts, std::size_t R = CounterHelper<true, Tag, 0, Ts...>()>
consteval auto Counter(auto...) -> std::size_t {
return R;
};
// Without increment
template <typename Tag, typename... Ts, std::size_t R = impl::Counter<false, Tag, 0, Ts...>()>
export template <typename Tag, typename... Ts, std::size_t R = CounterHelper<false, Tag, 0, Ts...>()>
consteval auto CountValue(auto...) -> std::size_t {
return R;
};

View file

@ -0,0 +1,3 @@
export module utempl.loopholes;
export import utempl.loopholes.core;
export import utempl.loopholes.counter;

View file

@ -1,13 +1,14 @@
#pragma once
#include <fmt/compile.h>
#include <fmt/format.h>
module;
#include <cstdio>
export module utempl.menu;
#include <array>
#include <iostream>
#include <utempl/constexpr_string.hpp>
#include <utempl/optional.hpp>
#include <utempl/tuple.hpp>
#include <utempl/utils.hpp>
import fmt;
import std;
import utempl.string;
import utempl.optional;
import utempl.tuple;
import utempl.utils;
import utempl.type_list;
namespace utempl {
@ -46,8 +47,6 @@ constexpr auto GetMax(Range&& range) {
namespace menu {
namespace impl {
template <std::size_t N1, std::size_t N2>
struct CallbackMessage {
ConstexprString<N1> message;
@ -62,17 +61,24 @@ CallbackMessage(const char (&)[N1], const char (&)[N2]) -> CallbackMessage<N2, N
template <std::size_t N1>
CallbackMessage(const char (&)[N1]) -> CallbackMessage<N1, 0>; // NOLINT
} // namespace impl
template <Tuple storage = Tuple{}, typename... Fs>
export template <Tuple storage = Tuple{}, typename... Fs>
struct Menu {
private:
template <ConstexprString fmt, ConstexprString message, ConstexprString alignment, ConstexprString neededInput>
static consteval auto FormatMessage() {
constexpr auto fmtlib = GetFmtCompiledString<fmt>();
// + 1 - NULL Terminator
constexpr auto size = fmt::formatted_size(FMT_COMPILE(fmt.data.begin()), neededInput, message, alignment) + 1;
constexpr auto size = fmt::formatted_size(fmtlib,
static_cast<std::string_view>(neededInput),
static_cast<std::string_view>(message),
static_cast<std::string_view>(alignment)) +
1;
std::array<char, size> data{};
fmt::format_to(data.begin(), FMT_COMPILE(fmt.data.begin()), neededInput, message, alignment);
fmt::format_to(data.begin(),
fmtlib,
static_cast<std::string_view>(neededInput),
static_cast<std::string_view>(message),
static_cast<std::string_view>(alignment));
return ConstexprString<size>(data);
};
template <ConstexprString fmt, std::size_t I>
@ -101,7 +107,7 @@ struct Menu {
: CountDigits(Is))...});
}(std::index_sequence_for<Fs...>());
};
template <impl::CallbackMessage message, std::invocable F>
template <CallbackMessage message, std::invocable F>
constexpr auto With(F&& f) const {
return Menu<storage + Tuple{message}, Fs..., std::remove_cvref_t<F>>{.functionStorage =
this->functionStorage + Tuple(std::forward<F>(f))};
@ -120,7 +126,7 @@ struct Menu {
std::string input;
std::getline(in, input);
(
[&]<auto I, impl::CallbackMessage message = Get<I>(storage)>(Wrapper<I>) {
[&]<auto I, CallbackMessage message = Get<I>(storage)>(Wrapper<I>) {
if constexpr(message.need) {
if(*message.need == input) {
Get<I>(this->functionStorage)();

View file

@ -1,18 +1,16 @@
#pragma once
#include <utempl/loopholes/counter.hpp>
#include <utempl/type_list.hpp>
export module utempl.meta_info;
import utempl.loopholes;
import utempl.type_list;
import std;
namespace utempl {
namespace impl {
export struct Types {};
struct Types {};
} // namespace impl
template <std::size_t Id, typename Tag>
export template <std::size_t Id, typename Tag>
struct MetaInfoKey {};
template <typename T, typename Tag = impl::Types>
export template <typename T, typename Tag = Types>
struct MetaInfo {
static constexpr std::size_t kTypeId = loopholes::Counter<Tag, T>();
using Type = T;
@ -21,10 +19,10 @@ struct MetaInfo {
static constexpr auto _ = loopholes::Injector<MetaInfoKey<kTypeId, Tag>{}, TypeList<T>{}>{};
};
template <typename T, typename Tag = impl::Types>
export template <typename T, typename Tag = Types>
inline constexpr std::size_t kTypeId = MetaInfo<T, Tag>::kTypeId;
template <typename Tag,
export template <typename Tag,
typename T,
typename... Ts,
typename... TTs,
@ -34,14 +32,12 @@ consteval auto AddTypeToTag(TTs&&...) -> std::size_t {
return Id;
};
template <std::size_t Id, typename Tag = impl::Types>
export template <std::size_t Id, typename Tag = Types>
using GetMetaInfo = MetaInfo<typename decltype(Magic(loopholes::Getter<MetaInfoKey<Id, Tag>{}>{}))::Type>;
template <std::size_t Id, typename Tag = impl::Types>
export template <std::size_t Id, typename Tag = Types>
using GetType = GetMetaInfo<Id, Tag>::Type;
namespace impl {
template <typename Tag, std::size_t I = 0, typename... Ts, typename G>
static consteval auto GetTypeListForTag(G g)
requires(I == 0 || requires { Magic(loopholes::Getter<MetaInfoKey<I, Tag>{}>{}); })
@ -59,14 +55,12 @@ static consteval auto GetTypeListForTag(G g)
};
};
} // namespace impl
template <typename Tag = impl::Types, typename... Ts>
export template <typename Tag = Types, typename... Ts>
consteval auto GetTypeListForTag() {
return impl::GetTypeListForTag<Tag>(TypeList<Ts...>{});
return GetTypeListForTag<Tag>(TypeList<Ts...>{});
};
template <typename Tag, typename... Ts, auto I = utempl::loopholes::CountValue<Tag, Ts...>() - 1>
export template <typename Tag, typename... Ts, auto I = utempl::loopholes::CountValue<Tag, Ts...>() - 1>
consteval auto GetCurrentTagType() {
return Magic(utempl::loopholes::Getter<MetaInfoKey<I, Tag>{}>());
};
@ -74,7 +68,7 @@ consteval auto GetCurrentTagType() {
/*
static_assert(kTypeId<int> == 0);
static_assert(kTypeId<void> == 1);
static_assert(AddTypeToTag<impl::Types, void, int>() == 2);
static_assert(AddTypeToTag<Types, void, int>() == 2);
static_assert(std::is_same_v<decltype(GetTypeListForTag()), TypeList<int, void, void>>);
*/

View file

@ -1,9 +1,9 @@
#pragma once
#include <optional>
export module utempl.optional;
import std;
namespace utempl {
template <typename T>
export template <typename T>
struct Optional { // NOLINT
bool flag = false;
union {

View file

@ -1,8 +1,8 @@
#pragma once
#include <utility>
export module utempl.overloaded;
import std;
namespace utempl {
template <typename... Fs>
export template <typename... Fs>
constexpr auto Overloaded(Fs&&... fs) {
struct Overloaded : public std::remove_cvref_t<Fs>... {
using Fs::operator()...;

View file

@ -1,11 +1,25 @@
#pragma once
#include <utempl/overloaded.hpp>
#include <utempl/type_list.hpp>
export module utempl.tuple;
import utempl.type_list;
import utempl.overloaded;
import std;
namespace utempl {
template <auto>
struct Wrapper;
namespace impl {
export template <auto Value>
struct Wrapper {
static constexpr auto kValue = Value;
static constexpr auto value = Value;
inline constexpr auto operator==(auto&& arg) const {
return arg == Value;
};
consteval operator decltype(Value)() const { // NOLINT
return Value;
};
consteval auto operator*() const {
return Value;
};
};
template <auto, typename T>
struct TupleLeaf {
@ -17,60 +31,58 @@ template <typename, typename...>
struct TupleHelper;
template <std::size_t... Is, typename... Ts>
struct TupleHelper<std::index_sequence<Is...>, Ts...> : public impl::TupleLeaf<Is, Ts>... {
struct TupleHelper<std::index_sequence<Is...>, Ts...> : public TupleLeaf<Is, Ts>... {
constexpr auto operator==(const TupleHelper&) const -> bool = default;
};
} // namespace impl
template <typename... Ts>
export template <typename... Ts>
struct Tuple;
template <std::size_t I, typename... Ts>
export template <std::size_t I, typename... Ts>
constexpr auto Get(Tuple<Ts...>& tuple) -> auto&
requires(I < sizeof...(Ts))
{
using Type = decltype(Get<I>(TypeList<Ts...>{}));
return static_cast<impl::TupleLeaf<I, Type>&>(tuple).value;
return static_cast<TupleLeaf<I, Type>&>(tuple).value;
};
template <std::size_t I, typename... Ts>
export template <std::size_t I, typename... Ts>
constexpr auto Get(const Tuple<Ts...>& tuple) -> const auto&
requires(I < sizeof...(Ts))
{
using Type = decltype(Get<I>(TypeList<Ts...>{}));
return static_cast<const impl::TupleLeaf<I, Type>&>(tuple).value;
return static_cast<const TupleLeaf<I, Type>&>(tuple).value;
};
template <std::size_t I, typename... Ts>
export template <std::size_t I, typename... Ts>
constexpr auto Get(Tuple<Ts...>&& tuple) -> decltype(auto)
requires(I < sizeof...(Ts))
{
using Type = decltype(Get<I>(TypeList<Ts...>{}));
return std::move(static_cast<impl::TupleLeaf<I, Type>&&>(tuple).value);
return std::move(static_cast<TupleLeaf<I, Type>&&>(tuple).value);
};
template <std::size_t I, typename T>
export template <std::size_t I, typename T>
constexpr auto Get(T&& arg) -> decltype(get<I>(std::forward<T>(arg))) {
return get<I>(std::forward<T>(arg));
};
template <typename... Ts>
struct Tuple : impl::TupleHelper<std::index_sequence_for<Ts...>, Ts...> {
export template <typename... Ts>
struct Tuple : TupleHelper<std::index_sequence_for<Ts...>, Ts...> {
template <typename... TTs>
constexpr Tuple(TTs&&... args) /* NOLINT */ : impl::TupleHelper<std::index_sequence_for<Ts...>, Ts...>{{std::forward<TTs>(args)}...} {};
constexpr Tuple(TTs&&... args) /* NOLINT */ : TupleHelper<std::index_sequence_for<Ts...>, Ts...>{{std::forward<TTs>(args)}...} {};
template <std::invocable... Fs>
constexpr Tuple(Fs&&... fs) // NOLINT
requires(!std::invocable<Ts> || ...)
: impl::TupleHelper<std::index_sequence_for<Ts...>, Ts...>{{fs()}...} {};
constexpr Tuple(Ts&&... args) : impl::TupleHelper<std::index_sequence_for<Ts...>, Ts...>{{args}...} {}; // NOLINT
: TupleHelper<std::index_sequence_for<Ts...>, Ts...>{{fs()}...} {};
constexpr Tuple(Ts&&... args) : TupleHelper<std::index_sequence_for<Ts...>, Ts...>{{args}...} {}; // NOLINT
constexpr Tuple(const Tuple&) = default;
constexpr Tuple(Tuple&&) = default;
constexpr Tuple(Tuple&) = default;
constexpr auto operator=(const Tuple&) -> Tuple& = default;
constexpr auto operator=(Tuple&&) -> Tuple& = default;
constexpr auto operator=(Tuple&) -> Tuple& = default;
constexpr Tuple() : impl::TupleHelper<std::index_sequence_for<Ts...>, Ts...>() {};
constexpr Tuple() : TupleHelper<std::index_sequence_for<Ts...>, Ts...>() {};
constexpr auto operator==(const Tuple<Ts...>& other) const -> bool {
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return ((Get<Is>(*this) == Get<Is>(other)) && ...);
@ -94,7 +106,7 @@ struct Tuple : impl::TupleHelper<std::index_sequence_for<Ts...>, Ts...> {
};
};
template <>
export template <>
struct Tuple<> {
template <typename... Ts>
constexpr auto operator+(const Tuple<Ts...>& other) const -> Tuple<Ts...> {
@ -104,10 +116,10 @@ struct Tuple<> {
return true;
};
};
template <typename... Ts>
export template <typename... Ts>
Tuple(Ts&&...) -> Tuple<std::decay_t<Ts>...>;
template <typename... Ts>
export template <typename... Ts>
consteval auto ListFromTuple(Tuple<Ts...>) -> TypeList<Ts...> {
return {};
};

View file

@ -1,27 +1,25 @@
#pragma once
#include <algorithm>
#include <array>
#include <concepts>
#include <utempl/overloaded.hpp>
#include <utility>
export module utempl.type_list;
import std;
import utempl.overloaded;
namespace utempl {
template <typename... Ts>
export template <typename... Ts>
struct TypeList {};
template <typename T>
export template <typename T>
struct TypeList<T> {
using Type = T;
};
template <typename T>
export template <typename T>
inline constexpr auto kType = TypeList<T>{};
template <typename... Ts>
export template <typename... Ts>
inline constexpr auto kTypeList = TypeList<Ts...>{};
template <typename T>
export template <typename T>
concept IsTypeList = Overloaded(
[]<typename... Ts>(TypeList<TypeList<Ts...>>) {
return true;
@ -30,8 +28,6 @@ concept IsTypeList = Overloaded(
return false;
})(kType<std::remove_cvref_t<T>>);
namespace impl {
template <std::size_t I, typename T>
struct IndexedType {};
@ -41,45 +37,45 @@ struct Caster {};
template <std::size_t... Is, typename... Ts>
struct Caster<std::index_sequence<Is...>, Ts...> : IndexedType<Is, Ts>... {};
} // namespace impl
template <typename... Ts, typename... TTs>
export template <typename... Ts, typename... TTs>
consteval auto operator==(const TypeList<Ts...>& first, const TypeList<TTs...>& second) -> bool {
return std::same_as<decltype(first), decltype(second)>;
};
template <typename... Ts, typename... TTs>
export template <typename... Ts, typename... TTs>
consteval auto operator+(const TypeList<Ts...>&, const TypeList<TTs...>&) -> TypeList<Ts..., TTs...> {
return {};
};
template <std::size_t I, typename... Ts>
consteval auto Get(TypeList<Ts...>) -> decltype([]<typename T>(impl::IndexedType<I, T>&&) -> T {
}(impl::Caster<std::index_sequence_for<Ts...>, Ts...>{}));
export template <std::size_t I, typename... Ts>
consteval auto Get(TypeList<Ts...>) -> decltype([]<typename T>(IndexedType<I, T>&&) -> T {
}(Caster<std::index_sequence_for<Ts...>, Ts...>{}));
template <typename T, typename... Ts>
export template <typename T, typename... Ts>
consteval auto Find(TypeList<Ts...>) -> std::size_t {
std::array<bool, sizeof...(Ts)> arr{std::same_as<T, Ts>...};
return std::ranges::find(arr, true) - arr.begin();
};
template <template <typename...> typename F, typename... Ts>
export template <template <typename...> typename F, typename... Ts>
consteval auto Find(TypeList<Ts...>) -> std::size_t {
std::array<bool, sizeof...(Ts)> arr{F<Ts>::value...};
return std::ranges::find(arr, true) - arr.begin();
};
template <typename... Ts>
export template <typename... Ts>
consteval auto Reverse(TypeList<Ts...> list) {
return [&]<auto... Is>(std::index_sequence<Is...>) -> TypeList<decltype(Get<sizeof...(Ts) - Is - 1>(list))...> {
return {};
}(std::index_sequence_for<Ts...>());
};
template <typename... Ts>
export template <typename... Ts>
consteval auto Transform(TypeList<Ts...>, auto&& f) -> TypeList<decltype(f(TypeList<Ts>{}))...> {
return {};
};
template <typename... Ts>
export template <typename... Ts>
consteval auto FilterTypeList(TypeList<Ts...>, auto&& f) {
return ((kTypeList<> +
[](auto&& list) {
@ -92,12 +88,12 @@ consteval auto FilterTypeList(TypeList<Ts...>, auto&& f) {
...);
};
template <typename... Ts>
export template <typename... Ts>
consteval auto Size(TypeList<Ts...>) -> std::size_t {
return sizeof...(Ts);
};
template <std::size_t N, std::size_t From = 0, typename... Ts>
export template <std::size_t N, std::size_t From = 0, typename... Ts>
consteval auto TakeFrom(TypeList<Ts...> list)
requires(N + From <= sizeof...(Ts))
{
@ -106,12 +102,12 @@ consteval auto TakeFrom(TypeList<Ts...> list)
}(std::make_index_sequence<N>());
};
template <typename T, std::size_t N, typename... Ts>
export template <typename T, std::size_t N, typename... Ts>
consteval auto PushTo(TypeList<Ts...> list) {
return TakeFrom<N>(list) + kType<T> + TakeFrom<sizeof...(Ts) - N, N>(list);
};
template <std::size_t N, typename... Ts>
export template <std::size_t N, typename... Ts>
consteval auto Erase(TypeList<Ts...> list) {
return TakeFrom<N>(list) + TakeFrom<sizeof...(Ts) - N - 1, N + 1>(list);
};

12
src/utempl.cpp Normal file
View file

@ -0,0 +1,12 @@
export module utempl;
export import utempl.type_list;
export import utempl.tuple;
export import utempl.string;
export import utempl.menu;
export import utempl.loopholes;
export import utempl.attributes;
export import utempl.meta_info;
export import utempl.overloaded;
export import utempl.utils;
export import utempl.go_interface;
export import utempl.optional;

View file

@ -1,57 +1,39 @@
#pragma once
#include <fmt/format.h>
export module utempl.utils;
#include <optional>
#include <ranges>
#include <utempl/constexpr_string.hpp>
#include <utempl/overloaded.hpp>
#include <utempl/tuple.hpp>
import std;
import fmt;
import utempl.string;
import utempl.tuple;
import utempl.type_list;
import utempl.overloaded;
namespace utempl {
template <typename T>
using ForwardType = decltype(std::forward<T>(std::declval<T>()));
template <auto Value>
struct Wrapper {
static constexpr auto kValue = Value;
inline constexpr auto operator==(auto&& arg) const {
return arg == Value;
};
consteval operator decltype(Value)() const { // NOLINT
return Value;
};
consteval auto operator*() const {
return Value;
};
};
export template <auto Value>
constexpr Wrapper<Value> kWrapper;
template <auto Value>
inline constexpr Wrapper<Value> kWrapper;
template <typename T>
export template <typename T>
requires std::same_as<T, void> || requires { T{}; }
constexpr auto kDefaultCreator = [] {
return T{};
};
template <>
export template <>
constexpr auto kDefaultCreator<void> = [] {};
namespace impl {
template <std::size_t N>
struct kSeq {
struct kSeqType {
template <typename F>
friend constexpr auto operator|(F&& f, const kSeq<N>&) -> decltype(auto) {
friend constexpr auto operator|(F&& f, const kSeqType<N>&) -> decltype(auto) {
return [&]<std::size_t... Is>(std::index_sequence<Is...>) -> decltype(auto) {
return std::forward<F>(f)(kWrapper<Is>...);
}(std::make_index_sequence<N>());
};
};
} // namespace impl
template <typename From, typename To>
concept ImplicitConvertibleTo = std::same_as<From, To> || requires(From from) { [](To) {}(from); };
@ -65,8 +47,8 @@ concept Function = []<typename R, typename... Ts>(TypeList<R(Ts...)>) {
static_assert(Function<decltype([]() {}), void()>);
template <std::size_t N>
inline constexpr impl::kSeq<N> kSeq;
export template <std::size_t N>
constexpr kSeqType<N> kSeq;
template <ConstexprString string, typename T = std::size_t>
consteval auto ParseNumber() -> T {
@ -80,15 +62,15 @@ consteval auto ParseNumber() -> T {
};
namespace literals {
template <char... cs>
export template <char... cs>
consteval auto operator"" _c() {
return Wrapper<ParseNumber<ConstexprString<sizeof...(cs)>({cs...})>()>{};
};
} // namespace literals
template <std::size_t I, typename... Ts>
inline constexpr auto Arg(Ts&&... args) -> decltype(auto)
export template <std::size_t I, typename... Ts>
constexpr auto Arg(Ts&&... args) -> decltype(auto)
requires(I < sizeof...(Ts))
{
return [&]<auto... Is>(std::index_sequence<Is...>) -> decltype(auto) {
@ -98,55 +80,55 @@ inline constexpr auto Arg(Ts&&... args) -> decltype(auto)
}(std::make_index_sequence<I>());
};
template <std::size_t Count>
inline constexpr auto Times(auto&& f) {
export template <std::size_t Count>
constexpr auto Times(auto&& f) {
[&]<auto... Is>(std::index_sequence<Is...>) {
(Arg<0>(f, Is)(), ...);
}(std::make_index_sequence<Count>());
};
template <typename T>
inline constexpr std::size_t kTupleSize = []() -> std::size_t {
export template <typename T>
constexpr std::size_t kTupleSize = []() -> std::size_t {
static_assert(!sizeof(T), "Not Found");
return 0;
}();
template <typename T>
inline constexpr std::size_t kTupleSize<T&&> = kTupleSize<std::remove_reference_t<T>>;
export template <typename T>
constexpr std::size_t kTupleSize<T&&> = kTupleSize<std::remove_reference_t<T>>;
template <typename T>
inline constexpr std::size_t kTupleSize<T&> = kTupleSize<std::remove_reference_t<T>>;
export template <typename T>
constexpr std::size_t kTupleSize<T&> = kTupleSize<std::remove_reference_t<T>>;
template <typename T>
inline constexpr std::size_t kTupleSize<const T> = kTupleSize<std::remove_cv_t<T>>;
export template <typename T>
constexpr std::size_t kTupleSize<const T> = kTupleSize<std::remove_cv_t<T>>;
template <template <typename...> typename M, typename... Ts>
inline constexpr std::size_t kTupleSize<M<Ts...>> = sizeof...(Ts);
export template <template <typename...> typename M, typename... Ts>
constexpr std::size_t kTupleSize<M<Ts...>> = sizeof...(Ts);
template <template <typename, std::size_t> typename Array, typename T, std::size_t N>
inline constexpr std::size_t kTupleSize<Array<T, N>> = N;
export template <template <typename, std::size_t> typename Array, typename T, std::size_t N>
constexpr std::size_t kTupleSize<Array<T, N>> = N;
template <typename T>
export template <typename T>
struct TupleMaker {};
template <typename... Ts>
export template <typename... Ts>
struct TupleMaker<std::tuple<Ts...>> {
template <typename... Args>
static inline constexpr auto Make(Args&&... args) {
static constexpr auto Make(Args&&... args) {
return std::tuple{std::forward<Args>(args)...};
};
};
template <typename... Ts>
export template <typename... Ts>
struct TupleMaker<Tuple<Ts...>> {
template <typename... Args>
static inline constexpr auto Make(Args&&... args) {
static constexpr auto Make(Args&&... args) {
return Tuple{std::forward<Args>(args)...};
};
};
template <typename T, std::size_t N>
export template <typename T, std::size_t N>
struct TupleMaker<std::array<T, N>> {
template <typename Arg, typename... Args>
static inline constexpr auto Make(Arg&& arg, Args&&... args)
static constexpr auto Make(Arg&& arg, Args&&... args)
requires(std::same_as<std::remove_cvref_t<Arg>, std::remove_cvref_t<Args>> && ...)
{
return std::array{std::forward<Arg>(arg), std::forward<Args>(args)...};
@ -156,40 +138,38 @@ struct TupleMaker<std::array<T, N>> {
};
};
template <typename T>
export template <typename T>
struct TupleTieMaker {};
template <typename... Ts>
export template <typename... Ts>
struct TupleTieMaker<std::tuple<Ts...>> {
template <typename... Args>
static inline constexpr auto Make(Args&... args) -> std::tuple<Args...> {
static constexpr auto Make(Args&... args) -> std::tuple<Args...> {
return {args...};
};
};
template <typename... Ts>
export template <typename... Ts>
struct TupleTieMaker<Tuple<Ts...>> {
template <typename... Args>
static inline constexpr auto Make(Args&... args) -> Tuple<Args...> {
static constexpr auto Make(Args&... args) -> Tuple<Args...> {
return {args...};
};
};
template <typename T = Tuple<>, typename... Args>
inline constexpr auto MakeTuple(Args&&... args) -> decltype(TupleMaker<std::remove_cvref_t<T>>::Make(std::forward<Args>(args)...)) {
export template <typename T = Tuple<>, typename... Args>
constexpr auto MakeTuple(Args&&... args) -> decltype(TupleMaker<std::remove_cvref_t<T>>::Make(std::forward<Args>(args)...)) {
return TupleMaker<std::remove_cvref_t<T>>::Make(std::forward<Args>(args)...);
};
template <typename T = Tuple<>, typename... Args>
inline constexpr auto MakeTie(Args&... args) -> decltype(TupleTieMaker<std::remove_cvref_t<T>>::Make(args...)) {
export template <typename T = Tuple<>, typename... Args>
constexpr auto MakeTie(Args&... args) -> decltype(TupleTieMaker<std::remove_cvref_t<T>>::Make(args...)) {
return TupleTieMaker<std::remove_cvref_t<T>>::Make(args...);
};
template <typename T>
concept HasMakeTie = requires(int& arg) { MakeTie<T>(arg); };
namespace impl {
template <typename T>
struct Getter {
friend consteval auto Magic(Getter<T>);
@ -215,7 +195,7 @@ consteval auto TrueF(auto&&...) {
};
template <typename T>
inline constexpr const SafeTupleChecker<T> kSafeTupleChecker;
constexpr const SafeTupleChecker<T> kSafeTupleChecker;
template <typename T, bool = false>
struct IsSafeTuple {
@ -230,19 +210,17 @@ struct IsSafeTuple<
static constexpr bool value = false;
};
} // namespace impl
export template <typename T>
constexpr bool kForceEnableTuple = false;
template <typename T>
inline constexpr bool kForceEnableTuple = false;
export template <typename T, std::size_t N>
constexpr bool kForceEnableTuple<std::array<T, N>> = true;
template <typename T, std::size_t N>
inline constexpr bool kForceEnableTuple<std::array<T, N>> = true;
template <typename T>
export template <typename T>
concept TupleLike = kForceEnableTuple<std::remove_cvref_t<T>> ||
(requires { Get<0>(MakeTuple<T>(42)); } && impl::IsSafeTuple<std::remove_cvref_t<T>>::value); // NOLINT
(requires { Get<0>(MakeTuple<T>(42)); } && IsSafeTuple<std::remove_cvref_t<T>>::value); // NOLINT
template <typename F, typename Tuple>
export template <typename F, typename Tuple>
concept TupleTransformer = requires(F f, Tuple&& tuple) {
{ f(std::move(tuple)) };
};
@ -252,58 +230,58 @@ struct LazyTuple {
F f;
using ResultType = std::invoke_result_t<F>;
std::optional<ResultType> result{std::nullopt};
inline constexpr auto Evaluate() {
constexpr auto Evaluate() {
if(!this->result) {
this->result.emplace(this->f());
};
};
inline constexpr auto operator()() -> decltype(auto) {
constexpr auto operator()() -> decltype(auto) {
this->Evaluate();
return *this->result;
};
inline constexpr auto operator()() const -> decltype(auto) {
constexpr auto operator()() const -> decltype(auto) {
return this->f();
};
template <typename T>
inline constexpr auto operator==(T&& other)
constexpr auto operator==(T&& other)
requires requires(ResultType result) { result == std::forward<T>(other); }
{
return (*this)() == other;
};
template <typename T>
inline constexpr auto operator==(T&& other) const
constexpr auto operator==(T&& other) const
requires requires(ResultType result) { result == std::forward<T>(other); }
{
return (*this)() == other;
};
inline constexpr operator std::invoke_result_t<F>() { // NOLINT
constexpr operator std::invoke_result_t<F>() { // NOLINT
return (*this)();
};
inline constexpr operator std::invoke_result_t<F>() const { // NOLINT
constexpr operator std::invoke_result_t<F>() const { // NOLINT
return (*this)();
};
template <std::size_t I>
friend inline constexpr auto Get(LazyTuple&& tuple) -> decltype(auto)
friend constexpr auto Get(LazyTuple&& tuple) -> decltype(auto)
requires TupleLike<ResultType>
{
return Get<I>(std::move(tuple)());
};
template <std::size_t I>
friend inline constexpr auto Get(const LazyTuple&& tuple) -> decltype(auto)
friend constexpr auto Get(const LazyTuple&& tuple) -> decltype(auto)
requires TupleLike<ResultType>
{
return Get<I>(std::move(tuple)());
};
template <std::size_t I>
friend inline constexpr auto Get(LazyTuple& tuple) -> decltype(auto)
friend constexpr auto Get(LazyTuple& tuple) -> decltype(auto)
requires TupleLike<ResultType>
{
return Get<I>(tuple());
};
template <std::size_t I>
friend inline constexpr auto Get(const LazyTuple& tuple) -> decltype(auto)
friend constexpr auto Get(const LazyTuple& tuple) -> decltype(auto)
requires TupleLike<ResultType>
{
return Get<I>(tuple());
@ -319,22 +297,20 @@ struct LazyTuple {
template <std::invocable F>
struct TupleMaker<LazyTuple<F>> {
template <typename... Ts>
static inline constexpr auto Make(Ts&&... args)
static constexpr auto Make(Ts&&... args)
requires requires { TupleMaker<typename LazyTuple<F>::ResultType>::Make(std::forward<Ts>(args)...); }
{
return TupleMaker<typename LazyTuple<F>::ResultType>::Make(std::forward<Ts>(args)...);
};
};
template <TupleLike Tuple, TupleTransformer<Tuple> FF>
inline constexpr auto operator|(Tuple&& tuple, FF&& f) {
export template <TupleLike Tuple, TupleTransformer<Tuple> FF>
constexpr auto operator|(Tuple&& tuple, FF&& f) {
return LazyTuple{[tuple = std::forward<Tuple>(tuple), f = std::forward<FF>(f)]() -> decltype(auto) {
return f(std::move(tuple));
}};
};
namespace impl {
template <typename F, typename Tuple>
concept UnpackConcept = []<std::size_t... Is>(std::index_sequence<Is...>) {
return std::invocable<F, decltype(Get<Is>(std::declval<Tuple>()))...>;
@ -355,32 +331,30 @@ concept ForEachConcept = []<std::size_t... Is>(std::index_sequence<Is...>) {
return (std::invocable<F, decltype(Get<Is>(std::declval<Tuple>()))> && ...);
}(std::make_index_sequence<kTupleSize<Tuple>>());
} // namespace impl
template <TupleLike Tuple, impl::UnpackConcept<Tuple> F>
inline constexpr auto Unpack(Tuple&& tuple, F&& f) -> decltype(auto) {
export template <TupleLike Tuple, UnpackConcept<Tuple> F>
constexpr auto Unpack(Tuple&& tuple, F&& f) -> decltype(auto) {
return [&](auto... is) -> decltype(auto) {
return f(Get<is>(std::forward<Tuple>(tuple))...);
} | kSeq<kTupleSize<Tuple>>;
};
template <typename F>
export template <typename F>
constexpr auto Unpack(F&& f) {
return [f = std::forward<F>(f)]<TupleLike Tuple>(Tuple&& tuple) -> decltype(auto)
requires impl::UnpackConcept<F, Tuple>
requires UnpackConcept<F, Tuple>
{
return Unpack(std::forward<Tuple>(tuple), std::move(f));
};
};
template <TupleLike Tuple, TupleLike R = Tuple, impl::TransformConcept<Tuple> F>
export template <TupleLike Tuple, TupleLike R = Tuple, TransformConcept<Tuple> F>
constexpr auto Transform(Tuple&& container, F&& f, TypeList<R> = {}) {
return Unpack(std::forward<Tuple>(container), [&]<typename... Ts>(Ts&&... args) {
return MakeTuple<R>(f(std::forward<Ts>(args))...);
});
};
template <typename F, typename R = void>
export template <typename F, typename R = void>
constexpr auto Transform(F&& f, TypeList<R> result = {}) {
return [f = std::forward<F>(f), result]<TupleLike TTuple, typename RR = decltype([] {
if constexpr(std::same_as<R, void>) {
@ -389,23 +363,23 @@ constexpr auto Transform(F&& f, TypeList<R> result = {}) {
return kType<R>;
};
}())::Type>(TTuple&& tuple)
requires impl::TransformConcept<F, TTuple>
requires TransformConcept<F, TTuple>
{
return Transform(std::forward<TTuple>(tuple), std::move(f), kType<RR>);
};
};
template <TupleLike Tuple, TupleLike R = Tuple, impl::TransformConcept<Tuple> F>
export template <TupleLike Tuple, TupleLike R = Tuple, TransformConcept<Tuple> F>
constexpr auto Map(Tuple&& tuple, F&& f, TypeList<R> result = {}) {
return Transform(std::forward<Tuple>(tuple), std::forward<F>(f), result);
};
template <typename F, typename R = void>
export template <typename F, typename R = void>
constexpr auto Map(F&& f, TypeList<R> result = {}) -> decltype(Transform(std::forward<F>(f), result)) {
return Transform(std::forward<F>(f), result);
};
template <auto Tuple, TupleLike To = decltype(Tuple)>
export template <auto Tuple, TupleLike To = decltype(Tuple)>
consteval auto PackConstexprWrapper()
requires TupleLike<decltype(Tuple)>
{
@ -414,35 +388,33 @@ consteval auto PackConstexprWrapper()
}(std::make_index_sequence<kTupleSize<decltype(Tuple)>>());
};
template <TupleLike Tuple>
export template <TupleLike Tuple>
constexpr auto Reverse(Tuple&& tuple) {
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return MakeTuple<Tuple>(Get<kTupleSize<Tuple> - Is - 1>(std::forward<Tuple>(tuple))...);
}(std::make_index_sequence<kTupleSize<Tuple>>());
};
consteval auto Reverse() {
export consteval auto Reverse() {
return []<TupleLike Tuple>(Tuple&& tuple) {
return Reverse(std::forward<Tuple>(tuple));
};
};
namespace impl {
template <typename...>
struct LeftFold;
struct LeftFoldHelper;
template <typename T>
struct LeftFold<T> {
struct LeftFoldHelper<T> {
T data;
};
template <typename T, typename F>
struct LeftFold<T, F> {
struct LeftFoldHelper<T, F> {
T data;
F f;
template <typename TT>
constexpr auto operator|(LeftFold<TT>&& other) -> LeftFold<std::invoke_result_t<F, T, TT>, F> {
constexpr auto operator|(LeftFoldHelper<TT>&& other) -> LeftFoldHelper<std::invoke_result_t<F, T, TT>, F> {
return {.data = std::forward<F>(this->f)(std::forward<T>(this->data), std::forward<TT>(other.data)), .f = std::forward<F>(this->f)};
};
};
@ -471,40 +443,38 @@ concept LeftFoldConcept = decltype(Unpack(std::declval<Tuple>(), []<typename...
return kWrapper<decltype((LeftFoldIsOk<F, T>{} | ... | LeftFoldIsOk<F, Ts>{}))::value>;
}))::kValue;
} // namespace impl
template <TupleLike Tuple, std::move_constructible T, impl::LeftFoldConcept<T, Tuple> F = decltype(kDefaultCreator<void>)&>
export template <TupleLike Tuple, std::move_constructible T, LeftFoldConcept<T, Tuple> F = decltype(kDefaultCreator<void>)&>
constexpr auto LeftFold(Tuple&& tuple, T&& init, F&& f = kDefaultCreator<void>) -> decltype(auto) {
return Unpack(std::forward<Tuple>(tuple), [&]<typename... Ts>(Ts&&... args) -> decltype(auto) {
return (impl::LeftFold<ForwardType<T>, ForwardType<F>>{.data = std::forward<T>(init), .f = std::forward<F>(f)} | ... |
impl::LeftFold<ForwardType<Ts>>{.data = std::forward<Ts>(args)})
return (LeftFoldHelper<ForwardType<T>, ForwardType<F>>{.data = std::forward<T>(init), .f = std::forward<F>(f)} | ... |
LeftFoldHelper<ForwardType<Ts>>{.data = std::forward<Ts>(args)})
.data;
});
};
template <TupleLike Tuple, std::move_constructible T, impl::LeftFoldConcept<T, Tuple> F>
export template <TupleLike Tuple, std::move_constructible T, LeftFoldConcept<T, Tuple> F>
constexpr auto Reduce(Tuple&& tuple, T&& init, F&& f) -> decltype(auto) {
return LeftFold(std::forward<Tuple>(tuple), std::forward<T>(init), std::forward<F>(f));
};
template <typename T, typename F>
export template <typename T, typename F>
constexpr auto Reduce(T&& init, F&& f) -> decltype(auto) {
return [init = std::forward<T>(init), f = std::forward<F>(f)]<TupleLike Tuple>(Tuple&& tuple) -> decltype(auto)
requires impl::LeftFoldConcept<F, T, Tuple>
requires LeftFoldConcept<F, T, Tuple>
{
return Reduce(std::forward<Tuple>(tuple), std::move(init), std::move(f));
};
};
template <TupleLike Tuple, TupleLike Tuple2>
inline constexpr auto TupleCat(Tuple&& tuple, Tuple2&& tuple2) {
export template <TupleLike Tuple, TupleLike Tuple2>
constexpr auto TupleCat(Tuple&& tuple, Tuple2&& tuple2) {
return [&]<auto... Is, auto... IIs>(std::index_sequence<Is...>, std::index_sequence<IIs...>) {
return MakeTuple<Tuple>(Get<Is>(std::forward<Tuple>(tuple))..., Get<IIs>(std::forward<Tuple2>(tuple2))...);
}(std::make_index_sequence<kTupleSize<Tuple>>(), std::make_index_sequence<kTupleSize<Tuple2>>());
};
template <TupleLike... Tuples>
inline constexpr auto TupleCat(Tuples&&... tuples)
export template <TupleLike... Tuples>
constexpr auto TupleCat(Tuples&&... tuples)
requires(sizeof...(tuples) >= 1)
{
return LeftFold(Tuple{std::forward<Tuples>(tuples)...},
@ -514,18 +484,18 @@ inline constexpr auto TupleCat(Tuples&&... tuples)
});
};
template <TupleLike... Tuples, typename F>
inline constexpr auto Unpack(Tuples&&... tuples, F&& f) -> decltype(Unpack(TupleCat(std::forward<Tuples>(tuples)...), std::forward<F>(f))) {
export template <TupleLike... Tuples, typename F>
constexpr auto Unpack(Tuples&&... tuples, F&& f) -> decltype(Unpack(TupleCat(std::forward<Tuples>(tuples)...), std::forward<F>(f))) {
return Unpack(TupleCat(std::forward<Tuples>(tuples)...), std::forward<F>(f));
};
template <typename... Ts>
inline constexpr auto Tie(Ts&... args) -> Tuple<Ts&...> {
export template <typename... Ts>
constexpr auto Tie(Ts&... args) -> Tuple<Ts&...> {
return {args...};
};
template <template <typename...> typename F, TupleLike Tuple>
inline constexpr bool kEveryElement = [](auto... is) {
constexpr bool kEveryElement = [](auto... is) {
return (F<std::decay_t<decltype(Get<is>(std::declval<Tuple>()))>>::value && ...);
} | kSeq<kTupleSize<Tuple>>;
@ -541,8 +511,8 @@ consteval auto PartialCallerF(TypeList<Ts...>) {
};
};
template <TupleLike Tuple, std::move_constructible T>
inline constexpr auto FirstOf(Tuple&& tuple, T&& init)
export template <TupleLike Tuple, std::move_constructible T>
constexpr auto FirstOf(Tuple&& tuple, T&& init)
requires kEveryElement<std::is_invocable, Tuple>
{
return LeftFold(std::forward<Tuple>(tuple), std::forward<T>(init), []<typename TT, typename F>(TT&& value, F&& f) -> TT {
@ -553,23 +523,19 @@ inline constexpr auto FirstOf(Tuple&& tuple, T&& init)
});
};
template <typename T>
inline constexpr auto FirstOf(T&& init) {
export template <typename T>
constexpr auto FirstOf(T&& init) {
return [init = std::forward<T>(init)]<TupleLike Tuple>(Tuple&& tuple) {
return FirstOf(std::forward<Tuple>(tuple), std::move(init));
};
};
namespace impl {
template <typename F, typename Tuple>
concept FilterConcept = decltype(Unpack(std::declval<Tuple>(), []<typename... Ts>(Ts&&...) {
return kWrapper<(Function<F, bool(Ts)> && ...)>;
}))::kValue;
} // namespace impl
template <TupleLike Tuple, impl::FilterConcept<Tuple> F>
export template <TupleLike Tuple, FilterConcept<Tuple> F>
constexpr auto Filter(Tuple&& tuple, F&& f) {
return LeftFold(
std::forward<Tuple>(tuple), MakeTuple<Tuple>(), [&]<TupleLike Accumulator, typename T>(Accumulator&& accumulator, T&& add) {
@ -581,16 +547,16 @@ constexpr auto Filter(Tuple&& tuple, F&& f) {
});
};
template <typename F>
export template <typename F>
constexpr auto Filter(F&& f) {
return [f = std::forward<F>(f)]<TupleLike Tuple>(Tuple&& tuple)
requires impl::FilterConcept<F, Tuple>
requires FilterConcept<F, Tuple>
{
return Filter(std::forward<Tuple>(tuple), std::move(f));
};
};
template <TupleLike Tuple, impl::ForEachConcept<Tuple> F>
export template <TupleLike Tuple, ForEachConcept<Tuple> F>
constexpr auto ForEach(Tuple&& tuple, F&& f) {
Unpack(std::forward<Tuple>(tuple), [&]<typename... Ts>(Ts&&... args) {
(f(std::forward<Ts>(args)), ...);
@ -601,23 +567,23 @@ template <typename F, typename... Ts>
struct Curryer {
F f;
Tuple<Ts...> data;
inline constexpr operator std::invoke_result_t<F, Ts...>() const { // NOLINT
constexpr operator std::invoke_result_t<F, Ts...>() const { // NOLINT
return [&]<auto... Is>(std::index_sequence<Is...>) {
return this->f(Get<Is>(this->data)...);
}(std::index_sequence_for<Ts...>());
};
template <typename T>
inline constexpr auto operator()(T&& arg) const -> Curryer<F, Ts..., std::remove_cvref_t<T>> {
constexpr auto operator()(T&& arg) const -> Curryer<F, Ts..., std::remove_cvref_t<T>> {
return {.f = this->f, .data = this->data + Tuple{std::forward<T>(arg)}};
};
};
template <typename F>
inline constexpr auto Curry(F&& f) -> Curryer<std::remove_cvref_t<F>> {
export template <typename F>
constexpr auto Curry(F&& f) -> Curryer<std::remove_cvref_t<F>> {
return {.f = std::forward<F>(f), .data = Tuple{}};
};
template <TupleLike Tuple, typename T>
inline constexpr auto Find(Tuple&& tuple, T&& find) -> std::size_t {
export template <TupleLike Tuple, typename T>
constexpr auto Find(Tuple&& tuple, T&& find) -> std::size_t {
return Unpack(std::forward<Tuple>(tuple), [&]<typename... Ts>(Ts&&... args) {
using Type = std::remove_cvref_t<T>;
std::array<bool, kTupleSize<Tuple>> bs{Overloaded(
@ -640,8 +606,8 @@ inline constexpr auto Find(Tuple&& tuple, T&& find) -> std::size_t {
});
};
template <std::size_t N, TupleLike Tuple>
inline constexpr auto Take(Tuple&& tuple) {
export template <std::size_t N, TupleLike Tuple>
constexpr auto Take(Tuple&& tuple) {
if constexpr(std::is_lvalue_reference_v<Tuple> && HasMakeTie<Tuple>) {
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return MakeTie<Tuple>(Get<Is>(std::forward<Tuple>(tuple))...);
@ -653,22 +619,22 @@ inline constexpr auto Take(Tuple&& tuple) {
};
};
template <std::size_t N>
export template <std::size_t N>
consteval auto Take() {
return [&]<TupleLike Tuple>(Tuple&& tuple) {
return Take<N>(std::forward<Tuple>(tuple));
};
};
template <TupleLike Tuple, std::move_constructible T>
inline constexpr auto operator<<(Tuple&& tuple, T&& t) {
export template <TupleLike Tuple, std::move_constructible T>
constexpr auto operator<<(Tuple&& tuple, T&& t) {
return Unpack(std::forward<Tuple>(tuple), [&]<typename... Ts>(Ts&&... args) {
return MakeTuple<Tuple>(std::forward<Ts>(args)..., std::forward<T>(t));
});
};
template <std::size_t N, TupleLike Tuple = Tuple<>, typename T>
inline constexpr auto Generate(T&& value)
export template <std::size_t N, TupleLike Tuple = Tuple<>, typename T>
constexpr auto Generate(T&& value)
requires(std::copyable<T> || N == 1 && std::move_constructible<T>)
{
if constexpr(N == 1) {
@ -680,7 +646,7 @@ inline constexpr auto Generate(T&& value)
};
};
template <TupleLike Tuple>
export template <TupleLike Tuple>
constexpr auto Enumerate(Tuple&& tuple) {
return Unpack(std::forward<Tuple>(tuple), [](auto&&... vs) {
return [&](auto... is) {
@ -689,8 +655,6 @@ constexpr auto Enumerate(Tuple&& tuple) {
});
};
namespace impl {
template <typename Key, typename KeysTuple>
concept ComparableSwitchConcept = decltype(Unpack(std::declval<KeysTuple>(), []<typename... Ts>(Ts&&... args) {
return kWrapper<(requires { args == std::declval<Key>(); } && ...)>;
@ -701,15 +665,13 @@ concept CallableSwitchConcept = std::same_as<R, void> || decltype(Unpack(std::de
return kWrapper<(Function<F, std::optional<R>(Ts)> && ...)>;
}))::kValue;
} // namespace impl
template <typename R = void,
export template <typename R = void,
TupleLike KeysTuple,
TupleLike ValuesTuple,
impl::ComparableSwitchConcept<KeysTuple> Key,
impl::CallableSwitchConcept<ValuesTuple, R> F,
Function<R()> Default = decltype(kDefaultCreator<R>)>
constexpr auto Switch(KeysTuple&& keysTuple, ValuesTuple&& valuesTuple, Key&& key, F&& f, Default&& def = {}) -> R
ComparableSwitchConcept<KeysTuple> Key,
CallableSwitchConcept<ValuesTuple, R> F,
Function<R()> Default = std::add_lvalue_reference_t<decltype(kDefaultCreator<R>)>>
constexpr auto Switch(KeysTuple&& keysTuple, ValuesTuple&& valuesTuple, Key&& key, F&& f, Default&& def = kDefaultCreator<R>) -> R
requires(std::move_constructible<R> || std::same_as<R, void>) && (kTupleSize<KeysTuple> == kTupleSize<ValuesTuple>)
{
return Unpack(std::forward<KeysTuple>(keysTuple), [&]<typename... Keys>(Keys&&... keys) {
@ -740,14 +702,14 @@ constexpr auto Switch(KeysTuple&& keysTuple, ValuesTuple&& valuesTuple, Key&& ke
});
};
template <std::size_t N, typename R = Tuple<>>
export template <std::size_t N, typename R = Tuple<>>
consteval auto GetIndexesTuple() {
return [](auto... is) {
return MakeTuple<R>(*is...);
} | kSeq<N>;
};
template <typename R = Tuple<>, typename... Ts>
export template <typename R = Tuple<>, typename... Ts>
consteval auto GetTuple(TypeList<Ts...>, TypeList<R> = {}) {
return MakeTuple<R>(kType<Ts>...);
};

View file

@ -1,5 +1,7 @@
module;
#include <gtest/gtest.h>
#include <utempl/go_interface.hpp>
export module tests.go_interface;
import utempl.go_interface;
namespace utempl {
@ -31,4 +33,3 @@ TEST(GoInterface, Equal) {
EXPECT_EQ(obj, SomeStruct2{1});
};
} // namespace utempl

View file

@ -1,5 +1,7 @@
module;
#include <gtest/gtest.h>
#include <utempl/menu.hpp>
export module tests.menu;
import utempl.menu;
namespace utempl {
@ -8,7 +10,7 @@ TEST(Menu, Basic) {
std::istringstream stream("t\n");
int value = 0;
menu::Menu{}
.With<{"t", "This is t"}>([&]{
.With<{"t", "This is t"}>([&] {
std::cout << "Success!" << std::endl;
value = 1;
})

View file

@ -1,12 +1,16 @@
module;
#include <gtest/gtest.h>
#include <utempl/overloaded.hpp>
export module tests.overloaded;
import utempl.overloaded;
namespace utempl {
TEST(Overloaded, Basic) {
constexpr auto f = Overloaded([](int){
constexpr auto f = Overloaded(
[](int) {
return 1;
}, [](auto&&){
},
[](auto&&) {
return 2;
});
EXPECT_EQ(f(1), 1);