130 lines
3.6 KiB
C++
130 lines
3.6 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <format>
|
|
#include <ranges>
|
|
|
|
namespace mcc::traits
|
|
{
|
|
|
|
template <typename R>
|
|
concept mcc_char_view = std::ranges::view<R> && std::same_as<std::ranges::range_value_t<R>, char>;
|
|
|
|
|
|
// input range of char/const char
|
|
template <typename R, typename CharT = char>
|
|
concept mcc_input_char_range =
|
|
std::ranges::input_range<R> && std::is_same_v<std::remove_cv_t<std::ranges::range_value_t<R>>, CharT>;
|
|
|
|
|
|
// output range of char/const char
|
|
template <typename R, typename CharT = char>
|
|
concept mcc_output_char_range =
|
|
std::ranges::output_range<R, CharT> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<R>>, CharT>;
|
|
|
|
|
|
template <typename R>
|
|
concept mcc_view_or_output_char_range = mcc_char_view<R> || mcc_output_char_range<R>;
|
|
|
|
|
|
template <typename R>
|
|
concept mcc_range_of_input_char_range =
|
|
std::ranges::range<R> && traits::mcc_input_char_range<std::ranges::range_value_t<R>>;
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/72430369/how-to-check-that-a-type-is-formattable-using-type-traits-concepts)
|
|
template <typename T>
|
|
concept mcc_formattable =
|
|
requires(T v, std::format_context ctx) { std::formatter<std::remove_cvref_t<T>>().format(v, ctx); };
|
|
|
|
|
|
// from https://stackoverflow.com/questions/74383254/concept-that-models-only-the-stdchrono-duration-types
|
|
template <typename T>
|
|
concept mcc_time_duration_c = requires {
|
|
[]<class Rep, class Period>(std::type_identity<std::chrono::duration<Rep, Period>>) {
|
|
|
|
}(std::type_identity<std::remove_cvref_t<T>>());
|
|
};
|
|
|
|
|
|
template <typename T>
|
|
concept mcc_systime_c = requires {
|
|
[]<class DT>(std::type_identity<std::chrono::sys_time<DT>>) {}(std::type_identity<std::remove_cvref_t<T>>());
|
|
};
|
|
|
|
|
|
/* a callable concept and its signature traits */
|
|
|
|
template <typename T>
|
|
concept mcc_is_callable = std::is_function_v<T> || (std::is_object_v<T> && requires(T) { &T::operator(); });
|
|
|
|
|
|
// helper classes for callable signature deducing
|
|
template <typename... Ts>
|
|
struct mcc_func_traits_helper_t;
|
|
|
|
template <typename R>
|
|
struct mcc_func_traits_helper_t<R> {
|
|
using ret_t = R;
|
|
using args_t = std::tuple<>;
|
|
using arg1_t = void;
|
|
static constexpr size_t arity = 0;
|
|
};
|
|
|
|
template <typename R, typename Arg, typename... Args>
|
|
struct mcc_func_traits_helper_t<R, Arg, Args...> {
|
|
using ret_t = R;
|
|
using args_t = std::tuple<Arg, Args...>;
|
|
using arg1_t = Arg;
|
|
static constexpr size_t arity = sizeof...(Args) + 1;
|
|
};
|
|
|
|
template <typename F>
|
|
struct mcc_func_traits {
|
|
// use of an empty struct here to match std::invoke_result behaivior (at least of GCC)
|
|
};
|
|
|
|
template <typename R, typename... Args>
|
|
struct mcc_func_traits<R (*)(Args...)> : mcc_func_traits_helper_t<R, Args...> {
|
|
};
|
|
|
|
template <typename R, typename... Args>
|
|
struct mcc_func_traits<R(Args...)> : mcc_func_traits_helper_t<R, Args...> {
|
|
};
|
|
|
|
template <typename C, typename R, typename... Args>
|
|
struct mcc_func_traits<R (C::*)(Args...)> : mcc_func_traits_helper_t<R, Args...> {
|
|
};
|
|
|
|
template <typename C, typename R, typename... Args>
|
|
struct mcc_func_traits<R (C::*)(Args...) const> : mcc_func_traits_helper_t<R, Args...> {
|
|
};
|
|
|
|
template <typename F>
|
|
requires mcc_is_callable<F>
|
|
struct mcc_func_traits<F> : mcc_func_traits<decltype(&F::operator())> {
|
|
};
|
|
|
|
// reference/const ref and rvalue helpers
|
|
template <typename F>
|
|
struct mcc_func_traits<F&> : mcc_func_traits<F> {
|
|
};
|
|
|
|
template <typename F>
|
|
struct mcc_func_traits<const F&> : mcc_func_traits<F> {
|
|
};
|
|
|
|
template <typename F>
|
|
struct mcc_func_traits<F&&> : mcc_func_traits<F> {
|
|
};
|
|
|
|
template <typename T>
|
|
using mcc_retval_t = typename mcc_func_traits<T>::ret_t;
|
|
|
|
template <typename T>
|
|
using mcc_func_arg1_t = typename mcc_func_traits<T>::arg1_t;
|
|
|
|
|
|
} // namespace mcc::traits
|