FastLED
3.9.7
Loading...
Searching...
No Matches
template_magic.h
1
#pragma once
2
3
/*
4
Provides eanble_if and is_derived for compilers before C++14.
5
*/
6
7
#include <stdint.h>
8
9
#include "
fl/namespace.h
"
10
11
namespace
fl
{
// mandatory namespace to prevent name collision with std::enable_if.
12
13
// Define enable_if for SFINAE
14
template
<
bool
Condition,
typename
T =
void
>
15
struct
enable_if
{};
16
17
// Specialization for true condition
18
template
<
typename
T>
19
struct
enable_if
<true, T> {
20
using
type = T;
21
};
22
23
// if enable_if<Condition, T> is true, then there will be a member type
24
// called type. Otherwise it will not exist. This is (ab)used to enable
25
// constructors and other functions based on template parameters. If there
26
// is no member type, then the compiler will not fail to bind to the target
27
// function or operation.
28
template
<
bool
Condition,
typename
T =
void
>
29
using
enable_if_t =
typename
enable_if<Condition, T>::type
;
30
31
// Define is_base_of to check inheritance relationship
32
template
<
typename
Base,
typename
Derived>
33
struct
is_base_of
{
34
private
:
35
typedef
uint8_t yes;
36
typedef
uint16_t no;
37
static
yes test(Base*);
// Matches if Derived is convertible to Base*
38
static
no test(...);
// Fallback if not convertible
39
enum
{
40
kSizeDerived =
sizeof
(test(
static_cast<
Derived*
>
(
nullptr
))),
41
};
42
public
:
43
static
constexpr
bool
value = (kSizeDerived ==
sizeof
(yes));
44
};
45
46
// Define is_base_of_v for compatibility with pre-C++14
47
// Replaced variable template with a constant static member
48
template
<
typename
Base,
typename
Derived>
49
struct
is_base_of_v_helper
{
50
static
constexpr
bool
value =
is_base_of<Base, Derived>::value
;
51
};
52
53
// Define is_same trait
54
template
<
typename
T,
typename
U>
55
struct
is_same
{
56
static
constexpr
bool
value =
false
;
57
};
58
59
// Specialization for when T and U are the same type
60
template
<
typename
T>
61
struct
is_same
<T, T> {
62
static
constexpr
bool
value =
true
;
63
};
64
65
// Define is_same_v for compatibility with variable templates
66
template
<
typename
T,
typename
U>
67
struct
is_same_v_helper
{
68
static
constexpr
bool
value =
is_same<T, U>::value
;
69
};
70
71
// Define is_pod trait (basic implementation)
72
template
<
typename
T>
73
struct
is_pod
{
74
static
constexpr
bool
value =
false
;
// Default to false for safety
75
};
76
77
// Specializations for fundamental types
78
template
<>
struct
is_pod
<bool> {
static
constexpr
bool
value =
true
; };
79
template
<>
struct
is_pod
<char> {
static
constexpr
bool
value =
true
; };
80
template
<>
struct
is_pod
<signed char> {
static
constexpr
bool
value =
true
; };
81
template
<>
struct
is_pod
<unsigned char> {
static
constexpr
bool
value =
true
; };
82
template
<>
struct
is_pod
<short> {
static
constexpr
bool
value =
true
; };
83
template
<>
struct
is_pod
<unsigned short> {
static
constexpr
bool
value =
true
; };
84
template
<>
struct
is_pod
<int> {
static
constexpr
bool
value =
true
; };
85
template
<>
struct
is_pod
<unsigned int> {
static
constexpr
bool
value =
true
; };
86
template
<>
struct
is_pod
<long> {
static
constexpr
bool
value =
true
; };
87
template
<>
struct
is_pod
<unsigned long> {
static
constexpr
bool
value =
true
; };
88
template
<>
struct
is_pod
<long long> {
static
constexpr
bool
value =
true
; };
89
template
<>
struct
is_pod
<unsigned long long> {
static
constexpr
bool
value =
true
; };
90
template
<>
struct
is_pod
<float> {
static
constexpr
bool
value =
true
; };
91
template
<>
struct
is_pod
<double> {
static
constexpr
bool
value =
true
; };
92
template
<>
struct
is_pod
<long double> {
static
constexpr
bool
value =
true
; };
93
94
// Helper struct for is_pod_v (similar to other _v helpers)
95
template
<
typename
T>
96
struct
is_pod_v_helper
{
97
static
constexpr
bool
value =
is_pod<T>::value
;
98
};
99
100
// This uses template magic to maybe generate a type for the given condition. If that type
101
// doesn't exist then a type will fail to be generated, and the compiler will skip the
102
// consideration of a target function. This is useful for enabling template constructors
103
// that only become available if the class can be upcasted to the desired type.
104
//
105
// Example:
106
// This is an optional upcasting constructor for a Ref<T>. If the type U is not derived from T
107
// then the constructor will not be generated, and the compiler will skip it.
108
//
109
// template <typename U, typename = fl::is_derived<T, U>>
110
// Ref(const Ref<U>& refptr) : referent_(refptr.get());
111
template
<
typename
Base,
typename
Derived>
112
using
is_derived = enable_if_t<is_base_of<Base, Derived>::value>;
113
114
}
// namespace fl
115
116
117
118
119
120
// For comparison operators that return bool against pod data. The class obj will need
121
// to supply the comparison operator for the pod type.
122
// This example will show how to define a comparison operator for a class that can be
123
// compared against a pod type.
124
// Example:
125
// FASTLED_DEFINE_POD_COMPARISON_OPERATOR(Myclass, >=) will allow MyClass to be compared
126
// MyClass obj;
127
// return obj >= 0;
128
#define FASTLED_DEFINE_POD_COMPARISON_OPERATOR(CLASS, OP) \
129
template <typename T, typename U> \
130
typename fl::enable_if<fl::is_same<U, CLASS>::value && fl::is_pod<T>::value, bool>::type \
131
operator OP (const T& pod, const CLASS& obj) { return pod OP obj; } \
132
template <typename T> \
133
typename fl::enable_if<fl::is_pod<T>::value, bool>::type \
134
operator OP (const CLASS& obj, const T& pod) { return obj OP pod; }
namespace.h
Implements the FastLED namespace macros.
fl
Implements a simple red square effect for 2D LED grids.
Definition
crgb.h:16
fl::enable_if
Definition
template_magic.h:15
fl::is_base_of_v_helper
Definition
template_magic.h:49
fl::is_base_of
Definition
template_magic.h:33
fl::is_pod_v_helper
Definition
template_magic.h:96
fl::is_pod
Definition
template_magic.h:73
fl::is_same_v_helper
Definition
template_magic.h:67
fl::is_same
Definition
template_magic.h:55
src
fl
template_magic.h
Generated on Fri Dec 20 2024 20:54:48 for FastLED by
1.11.0