diff --git a/CMakeLists.txt b/CMakeLists.txt index 70ade9a..870bed4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 5.4.0) +set(BLT_VERSION 5.4.1) set(BLT_TARGET BLT) diff --git a/include/blt/parse/argparse_v2.h b/include/blt/parse/argparse_v2.h index f44bf4e..948b218 100644 --- a/include/blt/parse/argparse_v2.h +++ b/include/blt/parse/argparse_v2.h @@ -37,6 +37,7 @@ #include #include #include +#include namespace blt::argparse { diff --git a/include/blt/std/variant.h b/include/blt/std/variant.h index 2ddd893..03b827a 100644 --- a/include/blt/std/variant.h +++ b/include/blt/std/variant.h @@ -19,6 +19,9 @@ #ifndef BLT_STD_VARIANT_H #define BLT_STD_VARIANT_H +#include +#include +#include #include #include #include @@ -178,6 +181,38 @@ namespace blt }); } + /** + * Call a set of member functions on the types stored in the variant. If a type has more than one of these functions declared on it, + * the implementation will use the first member function provided. By default, if the stored value doesn't have any of the member functions, + * nothing will happen, if should_throw boolean is true, then the implementation will throw a runtime error. + * @tparam should_throw Controls if the implementation should throw if the type stored in the variant doesn't have any matching member function + * @return Result of calling the member functions. All functions should return the same value, otherwise this won't compile. + */ + template + constexpr auto call_member(const MemberFuncs... funcs) + { + return std::visit([=](auto&& value) + { + using ValueType = std::decay_t; + + if constexpr (std::disjunction_v, ValueType>...> && should_throw) + { + throw std::runtime_error("No matching member function found"); + } + + return *(... || ([&](auto&& func) -> decltype(auto) + { + using FuncType = std::decay_t; + using ReturnType = std::invoke_result_t; + if constexpr (std::is_invocable_v) + { + return std::make_optional(std::invoke(func, value)); + } + return std::optional{}; + }(std::forward(funcs)))); + }, m_variant); + } + template [[nodiscard]] constexpr bool has_index() const noexcept diff --git a/src/blt/format/format.cpp b/src/blt/format/format.cpp index bf15857..50d6fbc 100644 --- a/src/blt/format/format.cpp +++ b/src/blt/format/format.cpp @@ -12,6 +12,7 @@ #include #include #include +#include inline constexpr char SEPARATOR = '-'; inline constexpr char CONNECTOR = '+'; diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index 19c91c6..815fa16 100644 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -9,6 +9,7 @@ #include #include #include "blt/std/utility.h" +#include namespace blt {