FastLED 3.10.10
Loading...
Searching...
No Matches
type_traits.h
Go to the documentation of this file.
1#pragma once
2
3/*
4Provides eanble_if and is_derived for compilers before C++14.
5*/
6
8#include "fl/stl/move.h"
9#include "fl/stl/int.h"
10#include "fl/stl/noexcept.h"
12
13namespace fl { // mandatory namespace to prevent name collision with
14 // std::enable_if.
15
16// Define integral_constant as base for true_type and false_type
17// Using enum instead of static constexpr to avoid ODR-use issues in C++11
18template <typename T, T v>
20 enum : T { value = v };
21 using value_type = T;
23 constexpr operator value_type() const FL_NO_EXCEPT { return value; }
24 constexpr value_type operator()() const FL_NO_EXCEPT { return value; }
25};
26
27// Define true_type and false_type
30
31// Define bool_constant for tag dispatch (C++17 compatibility)
32template <bool B>
34
35// Define identity trait - prevents template argument deduction
36template <typename T> struct identity {
37 using type = T;
38};
39
40// Define add_rvalue_reference trait (remove_reference is already defined in move.h)
41template <typename T> struct add_rvalue_reference {
42 using type = T&&;
43};
44
45template <typename T> struct add_rvalue_reference<T&> {
46 using type = T&;
47};
48
49// Define declval for use in SFINAE expressions
50template <typename T>
52
53// Define enable_if for SFINAE
54template <bool Condition, typename T = void> struct enable_if {};
55
56// Specialization for true condition
57template <typename T> struct enable_if<true, T> {
58 using type = T;
59};
60
61// if enable_if<Condition, T> is true, then there will be a member type
62// called type. Otherwise it will not exist. This is (ab)used to enable
63// constructors and other functions based on template parameters. If there
64// is no member type, then the compiler will not fail to bind to the target
65// function or operation.
66template <bool Condition, typename T = void>
68
69// Define is_base_of to check inheritance relationship
70template <typename Base, typename Derived> struct is_base_of {
71 private:
72 typedef u8 yes;
73 typedef u16 no;
74 static yes test(Base *) FL_NO_EXCEPT; // Matches if Derived is convertible to Base*
75 static no test(...) FL_NO_EXCEPT; // Fallback if not convertible
76 enum {
77 kSizeDerived = sizeof(test(static_cast<Derived *>(nullptr))),
78 };
79
80 public:
81 // Use enum instead of static constexpr to avoid ODR-use issues in C++11
82 enum : bool { value = (kSizeDerived == sizeof(yes)) };
83};
84
85// Define is_base_of_v for compatibility with pre-C++14
86// Replaced variable template with a constant static member
87template <typename Base, typename Derived> struct is_base_of_v_helper {
88 static constexpr bool value = is_base_of<Base, Derived>::value;
89};
90
91// Define is_same trait
92template <typename T, typename U> struct is_same {
93 enum : bool { value = false };
94};
95
96// Specialization for when T and U are the same type
97template <typename T> struct is_same<T, T> {
98 enum : bool { value = true };
99};
100
101// Define is_same_v for compatibility with variable templates
102template <typename T, typename U> struct is_same_v_helper {
103 static constexpr bool value = is_same<T, U>::value;
104};
105
106// Define conditional trait
107template <bool B, typename T, typename F> struct conditional {
108 using type = T;
109};
110
111template <typename T, typename F> struct conditional<false, T, F> {
112 using type = F;
113};
114
115template <bool B, typename T, typename F>
117
118// Define is_array trait
119template <typename T> struct is_array {
120 enum : bool { value = false };
121};
122
123template <typename T> struct is_array<T[]> {
124 enum : bool { value = true };
125};
126
127template <typename T, fl::size N> struct is_array<T[N]> {
128 enum : bool { value = true };
129};
130
131// Define remove_extent trait
132template <typename T> struct remove_extent {
133 using type = T;
134};
135
136template <typename T> struct remove_extent<T[]> {
137 using type = T;
138};
139
140template <typename T, fl::size N> struct remove_extent<T[N]> {
141 using type = T;
142};
143
144// Define is_function trait
145template <typename T> struct is_function {
146 enum : bool { value = false };
147};
148
149template <typename Ret, typename... Args> struct is_function<Ret(Args...)> {
150 enum : bool { value = true };
151};
152
153template <typename Ret, typename... Args>
154struct is_function<Ret(Args...) const> {
155 enum : bool { value = true };
156};
157
158template <typename Ret, typename... Args>
159struct is_function<Ret(Args...) volatile> {
160 enum : bool { value = true };
161};
162
163template <typename Ret, typename... Args>
164struct is_function<Ret(Args...) const volatile> {
165 enum : bool { value = true };
166};
167
168// Define add_pointer trait
169template <typename T> struct add_pointer {
170 using type = T *;
171};
172
173template <typename T> struct add_pointer<T &> {
174 using type = T *;
175};
176
177template <typename T> struct add_pointer<T &&> {
178 using type = T *;
179};
180
181template <typename T> using add_pointer_t = typename add_pointer<T>::type;
182
183// Define remove_const trait
184template <typename T> struct remove_const {
185 using type = T;
186};
187
188template <typename T> struct remove_const<const T> {
189 using type = T;
190};
191
192// Define is_const trait
193template <typename T> struct is_const {
194 enum : bool { value = false };
195};
196
197template <typename T> struct is_const<const T> {
198 enum : bool { value = true };
199};
200
201// Define is_lvalue_reference trait
202template <typename T> struct is_lvalue_reference {
203 enum : bool { value = false };
204};
205
206template <typename T> struct is_lvalue_reference<T &> {
207 enum : bool { value = true };
208};
209
210// Define is_rvalue_reference trait
211template <typename T> struct is_rvalue_reference {
212 enum : bool { value = false };
213};
214
215template <typename T> struct is_rvalue_reference<T &&> {
216 enum : bool { value = true };
217};
218
219// Define is_reference trait (true for both lvalue and rvalue references)
220template <typename T> struct is_reference {
222};
223
224// Define is_void trait
225template <typename T> struct is_void {
226 enum : bool { value = false };
227};
228
229template <> struct is_void<void> {
230 enum : bool { value = true };
231};
232
233namespace adl_barrier { // see fl/stl/adl_barrier.h
234// Implementation of forward
235template <typename T>
237 return static_cast<T &&>(t);
238}
239
240// Overload for rvalue references
241template <typename T>
244 "Cannot forward an rvalue as an lvalue");
245 return static_cast<T &&>(t);
246}
247} // namespace adl_barrier
248
249
250// Define remove_cv trait
251template <typename T> struct remove_cv {
252 using type = T;
253};
254
255template <typename T> struct remove_cv<const T> {
256 using type = T;
257};
258
259template <typename T> struct remove_cv<volatile T> {
260 using type = T;
261};
262
263template <typename T> struct remove_cv<const volatile T> {
264 using type = T;
265};
266
267template <typename T> using remove_cv_t = typename remove_cv<T>::type;
268
269// Define is_pointer trait
270template <typename T> struct is_pointer {
271 enum : bool { value = false };
272};
273
274template <typename T> struct is_pointer<T*> {
275 enum : bool { value = true };
276};
277
278template <typename T> struct is_pointer<T* const> {
279 enum : bool { value = true };
280};
281
282template <typename T> struct is_pointer<T* volatile> {
283 enum : bool { value = true };
284};
285
286template <typename T> struct is_pointer<T* const volatile> {
287 enum : bool { value = true };
288};
289
290// Define remove_pointer trait
291template <typename T> struct remove_pointer {
292 using type = T;
293};
294
295template <typename T> struct remove_pointer<T*> {
296 using type = T;
297};
298
299template <typename T> struct remove_pointer<T* const> {
300 using type = T;
301};
302
303template <typename T> struct remove_pointer<T* volatile> {
304 using type = T;
305};
306
307template <typename T> struct remove_pointer<T* const volatile> {
308 using type = T;
309};
310
311template <typename T> using remove_pointer_t = typename remove_pointer<T>::type;
312
313// Define decay trait
314template <typename T> struct decay {
315 private:
316 using U = typename remove_reference<T>::type;
317
318 public:
319 using type = typename conditional<
322 typename add_pointer<U>::type,
324};
325
326template <typename T> using decay_t = typename decay<T>::type;
327
328// Define is_pod trait (basic implementation)
329// Enums are POD types, so we use compiler intrinsics to detect them
330template <typename T> struct is_pod {
331#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
332 enum : bool { value = __is_enum(T) }; // Enums are POD
333#else
334 enum : bool { value = false }; // Default to false for safety
335#endif
336};
337
338// Specializations for fundamental types
339template <> struct is_pod<bool> {
340 enum : bool { value = true };
341};
342template <> struct is_pod<char> {
343 enum : bool { value = true };
344};
345template <> struct is_pod<signed char> {
346 enum : bool { value = true };
347};
348template <> struct is_pod<unsigned char> {
349 enum : bool { value = true };
350};
351template <> struct is_pod<short> {
352 enum : bool { value = true };
353};
354template <> struct is_pod<unsigned short> {
355 enum : bool { value = true };
356};
357template <> struct is_pod<int> {
358 enum : bool { value = true };
359};
360template <> struct is_pod<unsigned int> {
361 enum : bool { value = true };
362};
363template <> struct is_pod<long> {
364 enum : bool { value = true };
365};
366template <> struct is_pod<unsigned long> {
367 enum : bool { value = true };
368};
369template <> struct is_pod<long long> {
370 enum : bool { value = true };
371};
372template <> struct is_pod<unsigned long long> {
373 enum : bool { value = true };
374};
375template <> struct is_pod<float> {
376 enum : bool { value = true };
377};
378template <> struct is_pod<double> {
379 enum : bool { value = true };
380};
381template <> struct is_pod<long double> {
382 enum : bool { value = true };
383};
384
385// Helper struct for is_pod_v (similar to other _v helpers)
386template <typename T> struct is_pod_v_helper {
387 static constexpr bool value = is_pod<T>::value;
388};
389
390//----------------------------------------------------------------------------
391// trait to detect pointer‑to‑member‑function
392// must come before Function so SFINAE sees it
393//----------------------------------------------------------------------------
394template <typename T> struct is_member_function_pointer; // IWYU pragma: keep
395template <typename C, typename Ret, typename... A> // IWYU pragma: keep
396struct is_member_function_pointer<Ret (C::*)(A...)>; // IWYU pragma: keep
397template <typename C, typename Ret, typename... A> // IWYU pragma: keep
398struct is_member_function_pointer<Ret (C::*)(A...) const>; // IWYU pragma: keep
399
400template <typename T> struct is_member_function_pointer {
401 enum : bool { value = false };
402};
403
404template <typename C, typename Ret, typename... A>
405struct is_member_function_pointer<Ret (C::*)(A...)> {
406 enum : bool { value = true };
407};
408
409template <typename C, typename Ret, typename... A>
410struct is_member_function_pointer<Ret (C::*)(A...) const> {
411 enum : bool { value = true };
412};
413
414//-------------------------------------------------------------------------------
415// is_integral trait (built-in integer types only)
416//-------------------------------------------------------------------------------
417template <typename T> struct is_integral {
418 enum : bool { value = false };
419};
420template <> struct is_integral<bool> {
421 enum : bool { value = true };
422};
423template <> struct is_integral<char> {
424 enum : bool { value = true };
425};
426template <> struct is_integral<signed char> {
427 enum : bool { value = true };
428};
429template <> struct is_integral<unsigned char> {
430 enum : bool { value = true };
431};
432template <> struct is_integral<short> {
433 enum : bool { value = true };
434};
435template <> struct is_integral<unsigned short> {
436 enum : bool { value = true };
437};
438template <> struct is_integral<int> {
439 enum : bool { value = true };
440};
441template <> struct is_integral<unsigned int> {
442 enum : bool { value = true };
443};
444template <> struct is_integral<long> {
445 enum : bool { value = true };
446};
447template <> struct is_integral<unsigned long> {
448 enum : bool { value = true };
449};
450template <> struct is_integral<long long> {
451 enum : bool { value = true };
452};
453template <> struct is_integral<unsigned long long> {
454 enum : bool { value = true };
455};
456
457template <typename T> struct is_integral<const T> {
458 static constexpr bool value = is_integral<T>::value;
459};
460
461template <typename T> struct is_integral<volatile T> {
462 static constexpr bool value = is_integral<T>::value;
463};
464
465template <typename T> struct is_integral<T &> {
466 static constexpr bool value = is_integral<T>::value;
467};
468
469//-------------------------------------------------------------------------------
470// is_char_type trait - detects char, signed char, unsigned char
471//-------------------------------------------------------------------------------
472template <typename T> struct is_char_type {
473 enum : bool { value = false };
474};
475template <> struct is_char_type<char> {
476 enum : bool { value = true };
477};
478template <> struct is_char_type<signed char> {
479 enum : bool { value = true };
480};
481template <> struct is_char_type<unsigned char> {
482 enum : bool { value = true };
483};
484
485template <typename T> struct is_char_type<const T> {
486 static constexpr bool value = is_char_type<T>::value;
487};
488
489template <typename T> struct is_char_type<volatile T> {
490 static constexpr bool value = is_char_type<T>::value;
491};
492
493template <typename T> struct is_char_type<T &> {
494 static constexpr bool value = is_char_type<T>::value;
495};
496
497//-------------------------------------------------------------------------------
498// is_multi_byte_integer trait - integral types excluding char types
499//-------------------------------------------------------------------------------
500template <typename T> struct is_multi_byte_integer {
502};
503
504//-------------------------------------------------------------------------------
505// is_floating_point trait
506//-------------------------------------------------------------------------------
507template <typename T> struct is_floating_point {
508 enum : bool { value = false };
509};
510template <> struct is_floating_point<float> {
511 enum : bool { value = true };
512};
513template <> struct is_floating_point<double> {
514 enum : bool { value = true };
515};
516template <> struct is_floating_point<long double> {
517 enum : bool { value = true };
518};
519
520template <typename T> struct is_floating_point<const T> {
521 static constexpr bool value = is_floating_point<T>::value;
522};
523
524template <typename T> struct is_floating_point<volatile T> {
525 static constexpr bool value = is_floating_point<T>::value;
526};
527
528template <typename T> struct is_floating_point<T &> {
529 static constexpr bool value = is_floating_point<T>::value;
530};
531
532//-------------------------------------------------------------------------------
533// is_arithmetic trait - integral or floating-point types
534//-------------------------------------------------------------------------------
535template <typename T> struct is_arithmetic {
537};
538
539template <typename T> struct is_arithmetic<const T> {
540 static constexpr bool value = is_arithmetic<T>::value;
541};
542
543template <typename T> struct is_arithmetic<volatile T> {
544 static constexpr bool value = is_arithmetic<T>::value;
545};
546
547template <typename T> struct is_arithmetic<T &> {
548 static constexpr bool value = is_arithmetic<T>::value;
549};
550
551//-------------------------------------------------------------------------------
552// is_signed trait
553//-------------------------------------------------------------------------------
554template <typename T> struct is_signed {
555 enum : bool { value = false };
556};
557template <> struct is_signed<char> {
558 // char signedness is implementation-defined; detect at compile time.
559 enum : bool { value = (char(-1) < char(0)) };
560};
561template <> struct is_signed<signed char> {
562 enum : bool { value = true };
563};
564template <> struct is_signed<short> {
565 enum : bool { value = true };
566};
567template <> struct is_signed<int> {
568 enum : bool { value = true };
569};
570template <> struct is_signed<long> {
571 enum : bool { value = true };
572};
573template <> struct is_signed<long long> {
574 enum : bool { value = true };
575};
576template <> struct is_signed<float> {
577 enum : bool { value = true };
578};
579template <> struct is_signed<double> {
580 enum : bool { value = true };
581};
582template <> struct is_signed<long double> {
583 enum : bool { value = true };
584};
585// cv-qualified and reference forwarding
586template <typename T> struct is_signed<const T> {
587 enum : bool { value = is_signed<T>::value };
588};
589template <typename T> struct is_signed<volatile T> {
590 enum : bool { value = is_signed<T>::value };
591};
592template <typename T> struct is_signed<const volatile T> {
593 enum : bool { value = is_signed<T>::value };
594};
595// Note: sized integer types (i8, i16, i32, i64) are typedefs
596// for the basic types above, so they automatically inherit these specializations
597
598//-------------------------------------------------------------------------------
599// make_unsigned - converts signed integer types to their unsigned counterparts
600//-------------------------------------------------------------------------------
601// Uses size-based selection to work with any integer type (including platform-specific types)
602// Only valid for integral types excluding bool (matches std::make_unsigned behavior)
603
605
606// Size-based unsigned type selector
607template <fl::size Size> struct unsigned_by_size;
608template <> struct unsigned_by_size<1> { using type = unsigned char; };
609template <> struct unsigned_by_size<2> { using type = unsigned short; };
610// `unsigned int` is 16-bit on AVR, where the 4-byte unsigned type is
611// `unsigned long`.
612template <> struct unsigned_by_size<4> {
613 using type = typename conditional<sizeof(unsigned int) == 4, unsigned int,
614 unsigned long>::type;
615 FL_STATIC_ASSERT(sizeof(type) == 4, "no 4-byte unsigned type");
616};
617template <> struct unsigned_by_size<8> { using type = unsigned long long; };
618
619} // namespace make_unsigned_detail
620
621// Primary template - triggers static_assert for invalid types (float, double, bool, etc.)
622template <typename T, typename Enable = void>
624 // Dependent false to delay static_assert until instantiation
626 "make_unsigned requires a non-bool integral type");
627};
628
629// Specialization for valid integral types (excludes bool)
630template <typename T>
631struct make_unsigned<T, typename enable_if<
632 is_integral<T>::value && !is_same<typename remove_cv<T>::type, bool>::value
633>::type> {
635};
636
637// Checked on every target: a wrong width silently truncates (AVR, 16-bit int).
639 "make_unsigned<long> must keep the width of long");
641 "make_unsigned<short> must keep the width of short");
643 "make_unsigned<int> must keep the width of int");
645 "make_unsigned<long long> must keep the width of long long");
646
647//-------------------------------------------------------------------------------
648// Type size ranking for promotion rules
649//-------------------------------------------------------------------------------
650template <typename T> struct type_rank {
651 static constexpr int value = 0;
652};
653template <> struct type_rank<bool> {
654 static constexpr int value = 1;
655};
656template <> struct type_rank<signed char> {
657 static constexpr int value = 2;
658};
659template <> struct type_rank<unsigned char> {
660 static constexpr int value = 2;
661};
662template <> struct type_rank<char> {
663 static constexpr int value = 2;
664};
665template <> struct type_rank<short> {
666 static constexpr int value = 3;
667};
668template <> struct type_rank<unsigned short> {
669 static constexpr int value = 3;
670};
671template <> struct type_rank<int> {
672 static constexpr int value = 4;
673};
674template <> struct type_rank<unsigned int> {
675 static constexpr int value = 4;
676};
677template <> struct type_rank<long> {
678 static constexpr int value = 5;
679};
680template <> struct type_rank<unsigned long> {
681 static constexpr int value = 5;
682};
683template <> struct type_rank<long long> {
684 static constexpr int value = 6;
685};
686template <> struct type_rank<unsigned long long> {
687 static constexpr int value = 6;
688};
689template <> struct type_rank<float> {
690 static constexpr int value = 10;
691};
692template <> struct type_rank<double> {
693 static constexpr int value = 11;
694};
695template <> struct type_rank<long double> {
696 static constexpr int value = 12;
697};
698// Note: sized integer types (i8, i16, i32, i64) are typedefs
699// for the basic types above, so they automatically inherit these specializations
700
701//-------------------------------------------------------------------------------
702// Helper templates for integer type promotion logic
703//-------------------------------------------------------------------------------
704
705// Helper: Choose type based on size (larger wins)
706template <typename T, typename U>
708 using type = typename conditional<
709 (sizeof(T) > sizeof(U)), T,
710 typename conditional<
711 (sizeof(U) > sizeof(T)), U,
712 void // same size - handled elsewhere
713 >::type
714 >::type;
715};
716
717// Helper: Choose type based on type rank when same size (higher rank wins)
718template <typename T, typename U>
720 using type = typename conditional<
722 typename conditional<
724 void // same rank - handled elsewhere
725 >::type
726 >::type;
727};
728
729// Helper: Choose type based on signedness when same size and rank (signed wins)
730template <typename T, typename U>
732 static constexpr bool t_signed = is_signed<T>::value;
733 static constexpr bool u_signed = is_signed<U>::value;
734 static constexpr bool mixed_signedness = (t_signed != u_signed);
735
736 using type = typename conditional<
738 typename conditional<
740 T // same signedness - just pick first
741 >::type
742 >::type;
743};
744
745// Helper: Main integer promotion logic dispatcher
746template <typename T, typename U>
748 static constexpr bool same_size = (sizeof(T) == sizeof(U));
750
754
755 using type = typename conditional<
757 typename conditional<
759 by_signedness_result // same size and rank
760 >::type
761 >::type;
762};
763
764//-------------------------------------------------------------------------------
765// Common type trait for type promotion - now much cleaner!
766//-------------------------------------------------------------------------------
767
768// Primary template - fallback
769template <typename T, typename U, typename = void> struct common_type_impl {
770 using type = T;
771};
772
773// Same type specialization - handles all cases where T == U
774template <typename T> struct common_type_impl<T, T> {
775 using type = T;
776};
777
778// Float/double specializations - only exist when T is numeric but not the same type, otherwise compilation fails
779template <typename T>
780struct common_type_impl<T, float, typename enable_if<(is_integral<T>::value || is_floating_point<T>::value) && !is_same<T, float>::value>::type> {
781 using type = float;
782};
783
784template <typename T>
785struct common_type_impl<T, double, typename enable_if<(is_integral<T>::value || is_floating_point<T>::value) && !is_same<T, double>::value>::type> {
786 using type = double;
787};
788
789// Symmetric specializations - when first type is float/double and second is numeric but not the same type
790template <typename T>
791struct common_type_impl<float, T, typename enable_if<(is_integral<T>::value || is_floating_point<T>::value) && !is_same<T, float>::value>::type> {
792 using type = float;
793};
794
795template <typename T>
796struct common_type_impl<double, T, typename enable_if<(is_integral<T>::value || is_floating_point<T>::value) && !is_same<T, double>::value>::type> {
797 using type = double;
798};
799
800// Explicitly forbid i8 and u8 combinations
801// No type member = clear compilation error when accessed
802template <>
803struct common_type_impl<i8, u8, void> {
804 // Intentionally no 'type' member - will cause error:
805 // "no type named 'type' in 'struct fl::common_type_impl<signed char, unsigned char, void>'"
806};
807
808template <>
809struct common_type_impl<u8, i8, void> {
810 // Intentionally no 'type' member - will cause error:
811 // "no type named 'type' in 'struct fl::common_type_impl<unsigned char, signed char, void>'"
812};
813
814// Generic integer promotion logic - now much cleaner!
815template <typename T, typename U>
816struct common_type_impl<T, U, typename enable_if<
818 !is_same<T, U>::value &&
819 !((is_same<T, i8>::value && is_same<U, u8>::value) ||
820 (is_same<T, u8>::value && is_same<U, i8>::value))
821>::type> {
823};
824
825// Mixed floating point sizes - larger wins
826template <> struct common_type_impl<float, double> { using type = double; };
827template <> struct common_type_impl<double, float> { using type = double; };
828template <> struct common_type_impl<float, long double> { using type = long double; };
829template <> struct common_type_impl<long double, float> { using type = long double; };
830template <> struct common_type_impl<double, long double> { using type = long double; };
831template <> struct common_type_impl<long double, double> { using type = long double; };
832
833template <typename T, typename U> struct common_type {
835};
836
837template <typename T, typename U>
839
840// This uses template magic to maybe generate a type for the given condition. If
841// that type doesn't exist then a type will fail to be generated, and the
842// compiler will skip the consideration of a target function. This is useful for
843// enabling template constructors that only become available if the class can be
844// upcasted to the desired type.
845//
846// Example:
847// This is an optional upcasting constructor for a Ref<T>. If the type U is not
848// derived from T then the constructor will not be generated, and the compiler
849// will skip it.
850//
851// template <typename U, typename = fl::is_derived<T, U>>
852// Ref(const Ref<U>& refptr) : referent_(refptr.get());
853template <typename Base, typename Derived>
855
856//-----------------------------------------------------------------------------
857// detect whether T has a member void swap(T&)
858//-----------------------------------------------------------------------------
859template <typename T> struct has_member_swap {
860 private:
861 // must be 1 byte vs. >1 byte for sizeof test
862 typedef u8 yes;
863 typedef u16 no;
864
865 // helper<U, &U::swap> is only well-formed if U::swap(T&) exists with that
866 // signature
867 template <typename U, void (U::*M)(U &)> struct helper {};
868
869 // picks this overload if helper<U, &U::swap> is valid
870 template <typename U> static yes test(helper<U, &U::swap> *) FL_NO_EXCEPT;
871
872 // fallback otherwise
873 template <typename> static no test(...) FL_NO_EXCEPT;
874
875 public:
876 static constexpr bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
877};
878
879// primary template: dispatch on has_member_swap<T>::value
880template <typename T, bool = has_member_swap<T>::value> struct swap_impl;
881
882// POD case - now using move semantics for better performance
883template <typename T> struct swap_impl<T, false> {
884 static void apply(T &a, T &b) FL_NO_EXCEPT {
885 T tmp = fl::move(a);
886 a = fl::move(b);
887 b = fl::move(tmp);
888 }
889};
890
891// non‑POD case (requires T implements swap)
892template <typename T> struct swap_impl<T, true> {
893 static void apply(T &a, T &b) FL_NO_EXCEPT { a.swap(b); }
894};
895
896// single entry‑point
897//
898// Unlike the other generic templates (see fl/stl/adl_barrier.h), swap stays
899// directly in fl so unqualified `swap(a, b)` on fl types keeps finding it via
900// ADL. `b` is its own template parameter, constrained to the same type, which
901// makes this overload less specialized than std::swap(T&, T&). When std::swap
902// is visible (e.g. inside std::reverse / std::sort on CRGB, FastLED#4827) std
903// wins instead of the call being ambiguous; otherwise this one is used.
904template <typename T, typename U>
906 // if T is a POD, use use a simple data copy swap.
907 // if T is not a POD, use the T::Swap method.
909}
910
911
912template <typename T> void swap_by_copy(T &a, T &b) FL_NO_EXCEPT {
913 // Force copy semantics (for cases where move might not be safe)
914 T tmp = a;
915 a = b;
916 b = tmp;
917}
918
919// Container type checks.
920template <typename T, typename... Types> struct contains_type;
921
922template <typename T> struct contains_type<T> {
923 enum : bool { value = false };
924};
925
926template <typename T, typename U, typename... Rest>
927struct contains_type<T, U, Rest...> {
928 static constexpr bool value =
930};
931
932// Helper to get maximum size of types
933template <typename... Types> struct max_size;
934
935template <> struct max_size<> {
936 enum : fl::size { value = 0 };
937};
938
939template <typename T, typename... Rest> struct max_size<T, Rest...> {
940 enum : fl::size {
941 value = (sizeof(T) > max_size<Rest...>::value)
942 ? sizeof(T)
943 : max_size<Rest...>::value
944 };
945};
946
947// Helper to get maximum alignment of types
948template <typename... Types> struct max_align;
949
950template <> struct max_align<> {
951 enum : fl::size { value = 1 };
952};
953
954template <typename T, typename... Rest> struct max_align<T, Rest...> {
955 enum : fl::size {
956 value = (alignof(T) > max_align<Rest...>::value)
957 ? alignof(T)
958 : max_align<Rest...>::value
959 };
960};
961
962// alignment_of trait
963template <typename T>
965 enum : fl::size { value = alignof(T) };
966};
967
968//-------------------------------------------------------------------------------
969// is_enum trait - detects enum and enum class types
970//-------------------------------------------------------------------------------
971#if defined(__GNUC__) || defined(__clang__)
972// GCC and Clang support __is_enum intrinsic
973template <typename T> struct is_enum {
974 enum : bool { value = __is_enum(T) };
975};
976#elif defined(_MSC_VER)
977// MSVC also supports __is_enum
978template <typename T> struct is_enum {
979 enum : bool { value = __is_enum(T) };
980};
981#else
982// Fallback for other compilers - conservative approach
983// Assumes non-enum by default (safe but may need manual overloads)
984template <typename T> struct is_enum {
985 enum : bool { value = false };
986};
987#endif
988
989//-------------------------------------------------------------------------------
990// is_trivially_copyable trait - detects types safe for memcpy/realloc
991// Critical for determining if a type can be safely used with fl::realloc()
992//-------------------------------------------------------------------------------
993#if defined(__GNUC__) || defined(__clang__)
994// GCC and Clang support __is_trivially_copyable intrinsic
995template <typename T> struct is_trivially_copyable {
996 enum : bool { value = __is_trivially_copyable(T) };
997};
998#elif defined(_MSC_VER)
999// MSVC supports __is_trivially_copyable
1000template <typename T> struct is_trivially_copyable {
1001 enum : bool { value = __is_trivially_copyable(T) };
1002};
1003#else
1004// Fallback for other compilers - use is_pod as conservative approximation
1005// This is overly conservative but safe (may reject some valid types)
1006template <typename T> struct is_trivially_copyable {
1007 enum : bool { value = is_pod<T>::value };
1008};
1009#endif
1010
1011// Helper for variable template compatibility
1012template <typename T> struct is_trivially_copyable_v_helper {
1014};
1015
1016//-------------------------------------------------------------------------------
1017// underlying_type trait - gets underlying type of enum
1018//-------------------------------------------------------------------------------
1019#if defined(__GNUC__) || defined(__clang__)
1020// GCC and Clang support __underlying_type intrinsic
1021template <typename T> struct underlying_type {
1022 using type = __underlying_type(T);
1023};
1024#elif defined(_MSC_VER) && _MSC_VER >= 1900
1025// MSVC 2015+ supports __underlying_type
1026template <typename T> struct underlying_type {
1027 using type = __underlying_type(T);
1028};
1029#else
1030// Fallback - assume int for non-class enums (C++03 behavior)
1031template <typename T> struct underlying_type {
1032 using type = int;
1033};
1034#endif
1035
1036template <typename T>
1038
1039// C++11 requires out-of-class definitions for static constexpr members that are ODR-used
1040// These definitions are in src/fl/static_constexpr_defs.cpp to avoid duplicate symbols
1041
1042// For comparison operators that return bool against pod data. The class obj
1043// will need to supply the comparison operator for the pod type. This example
1044// will show how to define a comparison operator for a class that can be
1045// compared against a pod type.
1046// Example:
1047// FASTLED_DEFINE_POD_COMPARISON_OPERATOR(Myclass, >=) will allow MyClass to
1048// be compared MyClass obj; return obj >= 0;
1049#define FASTLED_DEFINE_POD_COMPARISON_OPERATOR(CLASS, OP) \
1050 template <typename T, typename U> \
1051 typename enable_if< \
1052 is_same<U, CLASS>::value && is_pod<T>::value, bool>::type \
1053 operator OP(const T &pod, const CLASS &obj) FL_NO_EXCEPT { \
1054 return pod OP obj; \
1055 } \
1056 template <typename T> \
1057 typename enable_if<is_pod<T>::value, bool>::type operator OP( \
1058 const CLASS &obj, const T &pod) FL_NO_EXCEPT { \
1059 return obj OP pod; \
1060 }
1061
1062//-------------------------------------------------------------------------------
1063// Integer type casting helper for string formatting
1064//-------------------------------------------------------------------------------
1065// Helper to determine target type for integer conversion based on size/signedness
1066// Used by both basic_string::append and sstream::operator<< for consistent formatting
1068 // Primary template: generic fallback using size/signedness
1069 // This handles any integer type by selecting the appropriate fl:: type based on its characteristics
1070 template<typename T, fl::size Size = sizeof(T), bool IsSigned = fl::is_signed<T>::value>
1072 // Generic implementation based on size and signedness
1073 // Selects appropriate fl:: integer type (i8/u8/i16/u16/i32/u32/i64/u64)
1074 using type = typename fl::conditional<
1075 IsSigned,
1076 // Signed integer path
1077 typename fl::conditional<Size == 1, fl::i8,
1078 typename fl::conditional<Size == 2, fl::i16,
1079 typename fl::conditional<Size == 4, fl::i32,
1080 typename fl::conditional<Size == 8, fl::i64,
1081 fl::i64 // Fallback to i64 for >8 byte types (rare)
1082 >::type
1083 >::type
1084 >::type
1085 >::type,
1086 // Unsigned integer path (note: 1-byte unsigned promotes to u16 for readability)
1087 typename fl::conditional<Size == 1, fl::u16, // u8 → u16 (avoid char display)
1088 typename fl::conditional<Size == 2, fl::u16,
1089 typename fl::conditional<Size == 4, fl::u32,
1090 typename fl::conditional<Size == 8, fl::u64,
1091 fl::u64 // Fallback to u64 for >8 byte types (rare)
1092 >::type
1093 >::type
1094 >::type
1095 >::type
1096 >::type;
1097 };
1098
1099 // Explicit specializations for common cases (optimization - avoids nested conditionals)
1100 // These provide the same mapping as the generic template but compile faster
1101
1102 // 1-byte unsigned → u16 (ergonomic: displays uint8_t as number, not char)
1103 template<typename T> struct cast_target<T, 1, false> { using type = fl::u16; };
1104
1105 // 1-byte signed → i8
1106 template<typename T> struct cast_target<T, 1, true> { using type = fl::i8; };
1107
1108 // 2-byte unsigned → u16
1109 template<typename T> struct cast_target<T, 2, false> { using type = fl::u16; };
1110
1111 // 2-byte signed → i16
1112 template<typename T> struct cast_target<T, 2, true> { using type = fl::i16; };
1113
1114 // 4-byte unsigned → u32
1115 template<typename T> struct cast_target<T, 4, false> { using type = fl::u32; };
1116
1117 // 4-byte signed → i32
1118 template<typename T> struct cast_target<T, 4, true> { using type = fl::i32; };
1119
1120 // 8-byte unsigned → u64
1121 template<typename T> struct cast_target<T, 8, false> { using type = fl::u64; };
1122
1123 // 8-byte signed → i64
1124 template<typename T> struct cast_target<T, 8, true> { using type = fl::i64; };
1125}
1126
1127//-------------------------------------------------------------------------------
1128// callable_traits - Extract signature from callable types (lambdas, functors, function pointers)
1129// Used for auto-deducing RPC method signatures from lambdas
1130// NOTE: args_tuple is NOT provided here due to header dependency issues (fl::tuple
1131// is defined in a header that includes this one). Use function_traits in typed_rpc.h
1132// if you need args_tuple.
1133//-------------------------------------------------------------------------------
1134
1135// Forward declaration
1136template <typename T, typename = void>
1138
1139// Member function pointer (non-const) - handles mutable lambdas and functors
1140template <typename C, typename R, typename... Args>
1141struct callable_traits<R(C::*)(Args...)> {
1142 using signature = R(Args...);
1143 using return_type = R;
1144 static constexpr fl::size arity = sizeof...(Args);
1145};
1146
1147// Member function pointer (const) - handles regular lambdas and const functors
1148template <typename C, typename R, typename... Args>
1149struct callable_traits<R(C::*)(Args...) const> {
1150 using signature = R(Args...);
1151 using return_type = R;
1152 static constexpr fl::size arity = sizeof...(Args);
1153};
1154
1155// Free function pointer
1156template <typename R, typename... Args>
1157struct callable_traits<R(*)(Args...)> {
1158 using signature = R(Args...);
1159 using return_type = R;
1160 static constexpr fl::size arity = sizeof...(Args);
1161};
1162
1163// Free function (not pointer)
1164template <typename R, typename... Args>
1165struct callable_traits<R(Args...)> {
1166 using signature = R(Args...);
1167 using return_type = R;
1168 static constexpr fl::size arity = sizeof...(Args);
1169};
1170
1171// Lambda/functor with operator() - delegates to member function pointer version
1172// Uses SFINAE to detect callable types with operator()
1173template <typename T>
1174struct callable_traits<T, typename enable_if<
1175 is_member_function_pointer<decltype(&T::operator())>::value
1176>::type> : callable_traits<decltype(&T::operator())> {};
1177
1178// =============================================================================
1179// Index sequence implementation for tuple unpacking (moved from typed_rpc.h)
1180// =============================================================================
1181
1182template <fl::size... Is>
1185 static constexpr fl::size size() FL_NO_EXCEPT { return sizeof...(Is); }
1186};
1187
1188// Log-depth construction: build the sequence for N/2 and concatenate it with
1189// itself shifted by N/2 (plus one extra index when N is odd). The naive
1190// N-1 recursion instantiated 256 nested templates for the gamma LUT, which
1191// cost ~50 ms of front-end time in every TU that included gamma_lut.h.
1192// No short names like S1/I1 here: board variants define pin names as
1193// macros (the SparkFun XRP's pins_arduino.h defines S1..S4), which would
1194// expand inside these declarations.
1195template <typename LhsSeq, typename RhsSeq>
1197
1198template <fl::size... Lhs, fl::size... Rhs>
1200 using type = index_sequence<Lhs..., (sizeof...(Lhs) + Rhs)...>;
1201};
1202
1203template <fl::size N>
1205 using type = typename index_sequence_concat<
1206 typename make_index_sequence_impl<N / 2>::type,
1207 typename make_index_sequence_impl<N - N / 2>::type>::type;
1208};
1209
1210template <>
1214
1215template <>
1219
1220template <fl::size N>
1222
1223//-------------------------------------------------------------------------------
1224// is_fixed_point trait — default false; specialized in fl/stl/fixed_point.h
1225//-------------------------------------------------------------------------------
1226template <typename T> struct is_fixed_point : false_type {};
1227
1228} // namespace fl
Keeps fl's generic templates out of argument-dependent lookup (ADL).
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
Definition cpp_compat.h:15
constexpr T && forward(typename remove_reference< T >::type &t) FL_NO_EXCEPT
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition s16x16x4.h:30
signed char i8
Definition s16x16x4.h:131
typename fl::conditional< IsSigned, typename fl::conditional< Size==1, fl::i8, typename fl::conditional< Size==2, fl::i16, typename fl::conditional< Size==4, fl::i32, typename fl::conditional< Size==8, fl::i64, fl::i64 >::type >::type >::type >::type, typename fl::conditional< Size==1, fl::u16, typename fl::conditional< Size==2, fl::u16, typename fl::conditional< Size==4, fl::u32, typename fl::conditional< Size==8, fl::u64, fl::u64 >::type >::type >::type >::type >::type type
unsigned char u8
Definition stdint.h:132
void swap(array< T, N > &lhs, array< T, N > &rhs) FL_NO_EXCEPT
Definition array.h:209
typename common_type< T, U >::type common_type_t
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
typename underlying_type< T >::type underlying_type_t
constexpr int type_rank< T >::value
typename conditional< B, T, F >::type conditional_t
void swap_by_copy(T &a, T &b) FL_NO_EXCEPT
typename remove_cv< T >::type remove_cv_t
add_rvalue_reference< T >::type declval() FL_NO_EXCEPT
integral_constant< bool, false > false_type
Definition type_traits.h:29
integral_constant< bool, B > bool_constant
Definition type_traits.h:33
typename remove_pointer< T >::type remove_pointer_t
typename decay< T >::type decay_t
fl::i64 i64
Definition s16x16x4.h:222
enable_if_t< is_base_of< Base, Derived >::value > is_derived
typename add_pointer< T >::type add_pointer_t
signed char i8
Definition stdint.h:131
typename make_index_sequence_impl< N >::type make_index_sequence
fl::u64 u64
Definition s16x16x4.h:221
typename enable_if< Condition, T >::type enable_if_t
Definition type_traits.h:67
integral_constant< bool, true > true_type
Definition type_traits.h:28
Base definition for an LED controller.
typename conditional<(sizeof(T) > sizeof(U)), T, typename conditional<(sizeof(U) > sizeof(T)), U, void >::type >::type type
typename common_type_impl< T, U >::type type
typename conditional<(type_rank< T >::value > type_rank< U >::value), T, typename conditional<(type_rank< U >::value > type_rank< T >::value), U, void >::type >::type type
typename index_sequence_concat< typename make_index_sequence_impl< N/2 >::type, typename make_index_sequence_impl< N - N/2 >::type >::type type
#define FL_STATIC_ASSERT(...)
Portable compile-time assertion wrapper.
static constexpr fl::size arity
static constexpr fl::size arity
static constexpr fl::size arity
static constexpr bool t_signed
static constexpr bool u_signed
typename conditional< mixed_signedness &&t_signed, T, typename conditional< mixed_signedness &&u_signed, U, T >::type >::type type
static constexpr bool mixed_signedness
typename remove_reference< T >::type U
typename conditional< is_array< U >::value, typename remove_extent< U >::type *, typename conditional< is_function< U >::value, typename add_pointer< U >::type, typename remove_cv< U >::type >::type >::type type
static no test(...) FL_NO_EXCEPT
static constexpr bool value
static yes test(helper< U, &U::swap > *) FL_NO_EXCEPT
index_sequence type
static constexpr fl::size size() FL_NO_EXCEPT
typename conditional< !same_size, by_size_result, typename conditional< same_size &&!same_rank, by_rank_result, by_signedness_result >::type >::type type
typename choose_by_signedness< T, U >::type by_signedness_result
typename choose_by_size< T, U >::type by_size_result
static constexpr bool same_rank
typename choose_by_rank< T, U >::type by_rank_result
static constexpr bool same_size
integral_constant type
Definition type_traits.h:22
constexpr value_type operator()() const FL_NO_EXCEPT
Definition type_traits.h:24
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
Definition type_traits.h:88
static no test(...) FL_NO_EXCEPT
static yes test(Base *) FL_NO_EXCEPT
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
FL_STATIC_ASSERT(sizeof(type)==4, "no 4-byte unsigned type")
typename conditional< sizeof(unsigned int)==4, unsigned int, unsigned long >::type type
FL_STATIC_ASSERT(is_integral< T >::value &&!is_same< typename remove_cv< T >::type, bool >::value, "make_unsigned requires a non-bool integral type")
static void apply(T &a, T &b) FL_NO_EXCEPT
static void apply(T &a, T &b) FL_NO_EXCEPT
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value
static constexpr int value