summaryrefslogtreecommitdiffstats
path: root/api/dead/src/TypeMagic.h
blob: fa9d12a9e6bd2b6a94ab920ea7ccdfee11fa9448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once

namespace TypeMagic
{
/** "Cleans" the given type T by stripping references (&) and cv-qualifiers (const, volatile) from it
 * const int => int
 * QString & => QString
 * const unsigned long long & => unsigned long long
 *
 * Usage:
 *   using Cleaned = Detail::CleanType<const int>;
 *   static_assert(std::is_same<Cleaned, int>, "Cleaned == int");
 */
// the order of remove_cv and remove_reference matters!
template <typename T>
using CleanType = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

/// For functors (structs with operator()), including lambdas, which in **most** cases are functors
/// "Calls" Function<Ret(*)(Arg)> or Function<Ret(C::*)(Arg)>
template <typename T> struct Function : public Function<decltype(&T::operator())> {};
/// For function pointers (&function), including static members (&Class::member)
template <typename Ret, typename Arg> struct Function<Ret(*)(Arg)> : public Function<Ret(Arg)> {};
/// Default specialization used by others.
template <typename Ret, typename Arg> struct Function<Ret(Arg)>
{
	using ReturnType = Ret;
	using Argument = Arg;
};
/// For member functions. Also used by the lambda overload if the lambda captures [this]
template <class C, typename Ret, typename Arg> struct Function<Ret(C::*)(Arg)> : public Function<Ret(Arg)> {};
template <class C, typename Ret, typename Arg> struct Function<Ret(C::*)(Arg) const> : public Function<Ret(Arg)> {};
/// Overload for references
template <typename F> struct Function<F&> : public Function<F> {};
/// Overload for rvalues
template <typename F> struct Function<F&&> : public Function<F> {};
// for more info: https://functionalcpp.wordpress.com/2013/08/05/function-traits/
}