FastLED 3.10.8
Loading...
Searching...
No Matches
pair.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/move.h"
7#include "fl/stl/noexcept.h"
9
10namespace fl {
11
12template <typename T1, typename T2> struct pair {
13 // Member typedefs for std::pair compatibility
14 using first_type = T1;
15 using second_type = T2;
16
17 T1 first = T1();
18 T2 second = T2();
19
20 // Default constructor
21 pair() = default;
22
23 // Constructor from values
26 pair(const T1 &k, const T2 &v) FL_NO_EXCEPT : first(k), second(v) {}
28
29 // Perfect forwarding constructor
30 template <typename U1, typename U2>
31 pair(U1&& u1, U2&& u2) FL_NO_EXCEPT : first(fl::forward<U1>(u1)), second(fl::forward<U2>(u2)) {}
32
33 // Copy constructor from different pair types
34 template <typename U1, typename U2>
35 pair(const pair<U1, U2> &other) FL_NO_EXCEPT : first(other.first), second(other.second) {}
36
37 // Move constructor from different pair types
38 template <typename U1, typename U2>
39 pair(pair<U1, U2> &&other) FL_NO_EXCEPT : first(fl::move(other.first)), second(fl::move(other.second)) {}
40
41 // Rule of 5: copy constructor, copy assignment, move constructor, move assignment, destructor
42 pair(const pair &other) FL_NO_EXCEPT = default;
43 pair &operator=(const pair &other) = default;
44 pair(pair &&other) FL_NO_EXCEPT : first(fl::move(other.first)), second(fl::move(other.second)) {}
45 pair &operator=(pair &&other) FL_NO_EXCEPT = default;
46
47 // Note: Template assignment operators removed to avoid issues with const members
48 // The default copy and move assignment operators will handle same-type assignments
49
50 // Swap member function
51 void swap(pair &other) FL_NO_EXCEPT {
52 fl::swap(first, other.first);
53 fl::swap(second, other.second);
54 }
55};
56
57// Comparison operators
58template <typename T1, typename T2, typename U1, typename U2>
59bool operator==(const pair<T1, T2> &lhs, const pair<U1, U2> &rhs) FL_NO_EXCEPT {
60 return lhs.first == rhs.first && lhs.second == rhs.second;
61}
62
63template <typename T1, typename T2, typename U1, typename U2>
64bool operator!=(const pair<T1, T2> &lhs, const pair<U1, U2> &rhs) FL_NO_EXCEPT {
65 return !(lhs == rhs);
66}
67
68template <typename T1, typename T2, typename U1, typename U2>
69bool operator<(const pair<T1, T2> &lhs, const pair<U1, U2> &rhs) FL_NO_EXCEPT {
70 return lhs.first < rhs.first || (!(rhs.first < lhs.first) && lhs.second < rhs.second);
71}
72
73template <typename T1, typename T2, typename U1, typename U2>
74bool operator<=(const pair<T1, T2> &lhs, const pair<U1, U2> &rhs) FL_NO_EXCEPT {
75 return !(rhs < lhs);
76}
77
78template <typename T1, typename T2, typename U1, typename U2>
79bool operator>(const pair<T1, T2> &lhs, const pair<U1, U2> &rhs) FL_NO_EXCEPT {
80 return rhs < lhs;
81}
82
83template <typename T1, typename T2, typename U1, typename U2>
84bool operator>=(const pair<T1, T2> &lhs, const pair<U1, U2> &rhs) FL_NO_EXCEPT {
85 return !(lhs < rhs);
86}
87
88// Non-member swap function
89template <typename T1, typename T2>
91 lhs.swap(rhs);
92}
93
94namespace adl_barrier { // see fl/stl/adl_barrier.h
95// make_pair function
96template <typename T1, typename T2>
100} // namespace adl_barrier
101
102
103// Helper for get function
104template <fl::size I, typename T1, typename T2>
106
107template <typename T1, typename T2>
108struct pair_element<0, T1, T2> {
109 using type = T1;
110};
111
112template <typename T1, typename T2>
113struct pair_element<1, T1, T2> {
114 using type = T2;
115};
116
117// get function overloads for tuple-like access by index
118template <fl::size I, typename T1, typename T2>
120 FL_STATIC_ASSERT(I < 2, "Index out of bounds for pair");
121 if (I == 0) {
122 return p.first;
123 } else {
124 return p.second;
125 }
126}
127
128template <fl::size I, typename T1, typename T2>
130 FL_STATIC_ASSERT(I < 2, "Index out of bounds for pair");
131 if (I == 0) {
132 return p.first;
133 } else {
134 return p.second;
135 }
136}
137
138template <fl::size I, typename T1, typename T2>
140 FL_STATIC_ASSERT(I < 2, "Index out of bounds for pair");
141 if (I == 0) {
142 return fl::move(p.first);
143 } else {
144 return fl::move(p.second);
145 }
146}
147
148// get by type overloads (when T1 and T2 are different types)
149template <typename T, typename T1, typename T2>
152 "Type T must be one of the pair's element types");
154 "Type T must be unique in the pair");
156 return p.first;
157 } else {
158 return p.second;
159 }
160}
161
162template <typename T, typename T1, typename T2>
163const T& get(const pair<T1, T2> &p) FL_NO_EXCEPT {
165 "Type T must be one of the pair's element types");
167 "Type T must be unique in the pair");
169 return p.first;
170 } else {
171 return p.second;
172 }
173}
174
175template <typename T, typename T1, typename T2>
178 "Type T must be one of the pair's element types");
180 "Type T must be unique in the pair");
182 return fl::move(p.first);
183 } else {
184 return fl::move(p.second);
185 }
186}
187
188// Tuple-like helper classes
189template <typename T>
191
192template <typename T1, typename T2>
193struct tuple_size<pair<T1, T2>> {
194 static constexpr fl::size value = 2;
195};
196
197template <fl::size I, typename T>
199
200template <typename T1, typename T2>
201struct tuple_element<0, pair<T1, T2>> {
202 using type = T1;
203};
204
205template <typename T1, typename T2>
206struct tuple_element<1, pair<T1, T2>> {
207 using type = T2;
208};
209
210template <typename T1, typename T2>
211using Pair = pair<T1, T2>; // Backwards compatibility for 3.10.1
212
213} // namespace fl
Keeps fl's generic templates out of argument-dependent lookup (ADL).
pair< typename fl::decay< T1 >::type, typename fl::decay< T2 >::type > make_pair(T1 &&t, T2 &&u) FL_NO_EXCEPT
Definition pair.h:97
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition s16x16x4.h:30
constexpr T && forward(typename remove_reference< T >::type &t) FL_NO_EXCEPT
Definition s16x16x4.h:236
enable_if_t< is_same< T, U >::value > swap(T &a, U &b) FL_NO_EXCEPT
Definition s16x16x4.h:889
void swap(array< T, N > &lhs, array< T, N > &rhs) FL_NO_EXCEPT
Definition array.h:209
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
pair< T1, T2 > Pair
Definition pair.h:211
FASTLED_FORCE_INLINE bool operator==(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if two CRGB objects have the same color data.
Definition crgb.h:740
FASTLED_FORCE_INLINE bool operator>(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is greater than another.
Definition crgb.h:761
FASTLED_FORCE_INLINE bool operator<=(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is less than or equal to another.
Definition crgb.h:779
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NO_EXCEPT
Definition pair.h:119
FASTLED_FORCE_INLINE bool operator!=(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if two CRGB objects do not have the same color data.
Definition crgb.h:746
FASTLED_FORCE_INLINE bool operator>=(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is greater than or equal to another.
Definition crgb.h:770
FASTLED_FORCE_INLINE bool operator<(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is less than another.
Definition crgb.h:752
Base definition for an LED controller.
#define FL_STATIC_ASSERT(...)
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_NULL_DEREFERENCE
#define FL_DISABLE_WARNING_POP
Portable compile-time assertion wrapper.
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
Definition s16x16x4.h:319
pair(const pair &other) FL_NO_EXCEPT=default
pair & operator=(pair &&other) FL_NO_EXCEPT=default
pair(pair< U1, U2 > &&other) FL_NO_EXCEPT
Definition pair.h:39
pair(pair &&other) FL_NO_EXCEPT
Definition pair.h:44
void swap(pair &other) FL_NO_EXCEPT
Definition pair.h:51
vec2< u16 > first_type
Definition pair.h:14
pair()=default
pair(const pair< U1, U2 > &other) FL_NO_EXCEPT
Definition pair.h:35
pair & operator=(const pair &other)=default
FL_DISABLE_WARNING_POP pair(U1 &&u1, U2 &&u2) FL_NO_EXCEPT
Definition pair.h:31
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_NULL_DEREFERENCE pair(const T1 &k, const T2 &v) FL_NO_EXCEPT
Definition pair.h:26
static constexpr fl::size value
Definition pair.h:194