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