BLT/include/blt/meta/meta.h

155 lines
4.3 KiB
C
Raw Permalink Normal View History

2024-07-10 20:41:26 -04:00
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_META_H
#define BLT_META_H
#include <blt/std/types.h>
#include <utility>
2024-08-03 17:44:58 -04:00
#include <type_traits>
#include <ostream>
2024-07-10 20:41:26 -04:00
namespace blt::meta
{
2024-08-20 12:46:49 -04:00
template<typename IFirst = std::void_t<void>, typename... IArgs>
2024-07-10 20:41:26 -04:00
struct arg_helper
{
using First = IFirst;
using Next = arg_helper<IArgs...>;
};
2024-08-11 18:47:58 -04:00
template<typename IFirst>
struct arg_helper<IFirst>
{
using First = IFirst;
using Next = void;
};
2024-07-10 20:41:26 -04:00
template<>
2024-08-20 12:46:49 -04:00
struct arg_helper<>
2024-07-10 20:41:26 -04:00
{
using First = void;
using Next = void;
};
template<typename TheLambda, typename>
struct lambda_helper
{
using Lambda = TheLambda;
};
template<typename TheLambda, typename IReturn, typename IClass, typename... LArgs>
2024-08-06 03:44:20 -04:00
struct lambda_helper<TheLambda, IReturn (IClass::*)(LArgs...) const>
2024-07-10 20:41:26 -04:00
{
using Lambda = TheLambda;
using Return = IReturn;
using Class = IClass;
using Args = arg_helper<LArgs...>;
2024-08-06 03:44:20 -04:00
template<typename T>
explicit lambda_helper(T)
{}
lambda_helper() = default;
2024-07-10 20:41:26 -04:00
};
template<typename Lambda>
lambda_helper(Lambda) -> lambda_helper<Lambda, decltype(&Lambda::operator())>;
2024-08-03 17:44:58 -04:00
// https://stackoverflow.com/questions/66397071/is-it-possible-to-check-if-overloaded-operator-for-type-or-class-exists
2024-08-11 13:33:40 -04:00
template<typename T>
2024-08-03 17:44:58 -04:00
class is_streamable
{
private:
template<typename Subs>
static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<Subs>(), std::true_type())
{
return std::declval<std::true_type>();
}
template<typename>
static auto test(...) -> std::false_type
{
return std::declval<std::false_type>();
}
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
template<class T>
inline constexpr bool is_streamable_v = is_streamable<T>::value;
2024-08-13 00:33:44 -04:00
2024-09-23 16:24:22 -04:00
template<typename T>
struct arrow_return
{
2024-10-01 17:39:43 -04:00
using type = typename std::invoke_result_t<decltype(&T::operator->), T*>;
2024-09-23 16:24:22 -04:00
};
template<typename T>
struct arrow_return<T*>
{
using type = T*;
};
// gets the return type for arrow operator
template<typename T>
using arrow_return_t = typename arrow_return<T>::type;
template<typename T>
struct deref_return
{
2024-10-01 17:39:43 -04:00
using type = typename std::invoke_result_t<decltype(&T::operator*), T&>;
2024-09-23 16:24:22 -04:00
};
template<typename T>
struct deref_return<T*>
{
using type = T&;
};
// gets the return type for the reference operator
template<typename T>
using deref_return_t = typename deref_return<T>::type;
2024-08-11 13:33:40 -04:00
#define BLT_META_MAKE_FUNCTION_CHECK(FUNC, ...)\
template<typename T, typename = void> \
class has_func_##FUNC : public std::false_type \
{}; \
template<typename T> \
2024-08-12 22:02:54 -04:00
class has_func_##FUNC<T, std::void_t<decltype(std::declval<T>().FUNC(,##__VA_ARGS__))>> : public std::true_type \
2024-08-11 13:33:40 -04:00
{}; \
template<typename T> \
2024-08-12 22:02:54 -04:00
inline constexpr bool has_func_##FUNC##_v = has_func_##FUNC<T>::value;
2024-08-11 18:47:58 -04:00
2024-08-12 22:02:54 -04:00
#define BLT_META_MAKE_MEMBER_CHECK(MEMBER)\
template<typename T, typename = void> \
class has_member_##MEMBER : public std::false_type \
{}; \
template<typename T> \
class has_member_##MEMBER<T, std::void_t<decltype(T::MEMBER)>> : public std::true_type \
{}; \
template<typename T> \
inline constexpr bool has_member_##MEMBER##_v = has_member_##MEMBER<T>::value;
2024-07-10 20:41:26 -04:00
}
#endif //BLT_GP_META_H