summaryrefslogtreecommitdiffstats
path: root/ComLightLib/utils/typeTraits.hpp
blob: c5ddb84d0a118529b4edaba14511bd5c035fab65 (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
38
39
40
41
42
43
#pragma once
#include <type_traits>

namespace ComLight
{
	namespace details
	{
		template<class TResult, class TValue>
		constexpr bool pointersAssignable()
		{
			// See this for why `&` is required: https://stackoverflow.com/a/52429468/126995
			return std::is_assignable<TResult*&, TValue*>::value;
		}
	}
}

// https://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
#define GENERATE_HAS_MEMBER(member)                                               \
                                                                                  \
template < class T >                                                              \
class HasMember_##member                                                          \
{                                                                                 \
private:                                                                          \
    using Yes = char[2];                                                          \
    using  No = char[1];                                                          \
                                                                                  \
    struct Fallback { int member; };                                              \
    struct Derived : T, Fallback { };                                             \
                                                                                  \
    template < class U >                                                          \
    static No& test ( decltype(U::member)* );                                     \
    template < typename U >                                                       \
    static Yes& test ( U* );                                                      \
                                                                                  \
public:                                                                           \
    static constexpr bool RESULT = sizeof(test<Derived>(nullptr)) == sizeof(Yes); \
};                                                                                \
                                                                                  \
template < class T >                                                              \
struct has_member_##member                                                        \
: public std::integral_constant<bool, HasMember_##member<T>::RESULT>              \
{                                                                                 \
};