FastLED 3.9.15
Loading...
Searching...
No Matches
strstream.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/int.h"
4#include "fl/str.h"
5#include "crgb.h"
6#include "str.h"
7
8
9namespace fl {
10
11class Tile2x2_u8;
12class Tile2x2_u8_wrap; // Forward declaration to support operator<< overload
13template <typename T> struct vec2; // Forward declaration from fl/geometry.h
14template <typename T, typename Alloc> class HeapVector; // Forward declaration from fl/vector.h
15struct FFTBins; // Forward declaration from fl/fft.h
16template <fl::u32 N> class BitsetFixed;
17class bitset_dynamic;
18template <fl::u32 N> class BitsetInlined;
19
20template <typename T> struct StrStreamHelper {
21 static void append(string &str, const T &n) { str.append(n); }
22};
23
24template <> struct StrStreamHelper<int> {
25 static void append(string &str, const int &n) { str.append(fl::i32(n)); }
26};
27
28template <> struct StrStreamHelper<fl::u8> {
29 static void append(string &str, const fl::u8 &n) { str.append(fl::u16(n)); }
30};
31
32template <> struct StrStreamHelper<char> {
33 static void append(string &str, const char &n) { str.append(n); } // Append as character, not number
34};
35
36template <> struct StrStreamHelper<unsigned int> {
37 static void append(string &str, const unsigned int &n) {
38 str.append(fl::u32(n));
39 }
40};
41
42class StrStream {
43 public:
44 StrStream() = default;
45 StrStream(const string &str) : mStr(str) {}
46
47 void setTreatCharAsInt(bool treatCharAsInt) {
48 mTreatCharAsInt = treatCharAsInt;
49 }
50
51 const string &str() const { return mStr; }
52 const char *c_str() const { return mStr.c_str(); }
53
54 StrStream &operator<<(const CRGB &rgb) {
55 mStr.append(rgb);
56 return *this;
57 }
58 StrStream &operator<<(const StrStream &strStream) {
59 mStr.append(strStream.str());
60 return *this;
61 }
62
63 StrStream &operator<<(const Tile2x2_u8 &subpixel);
64 StrStream &operator<<(const Tile2x2_u8_wrap &tile); // New overload for wrapped tiles
65
66 // FFTBins support - implemented in strstream.cpp.hpp
67 StrStream &operator<<(const FFTBins &bins);
68
69 // vec2<T> support - format as (x,y)
70 template<typename T>
72 mStr.append("(");
73 mStr.append(v.x);
74 mStr.append(",");
75 mStr.append(v.y);
76 mStr.append(")");
77 return *this;
78 }
79
80 // HeapVector<T, Alloc> support - format as [item1, item2, ...]
81 template<typename T, typename Alloc>
83 mStr.append("[");
84 for (fl::size i = 0; i < vec.size(); ++i) {
85 if (i > 0) {
86 mStr.append(", ");
87 }
88 mStr.append(vec[i]);
89 }
90 mStr.append("]");
91 return *this;
92 }
93
94 StrStream &operator=(const fl::u16 &n) {
95 mStr.clear();
96 (*this) << n;
97 return *this;
98 }
99
101 mStr.clear();
102 (*this) << n;
103 return *this;
104 }
105
107 mStr.clear();
108 (*this) << c;
109 return *this;
110 }
111
112 // << operator section
113 StrStream &operator<<(const string &str) {
114 mStr.append(str);
115 return *this;
116 }
117
118 StrStream &operator<<(const char *str) {
119 mStr.append(str);
120 return *this;
121 }
122
123 StrStream &operator<<(const float &f) {
124 // multiply by 100 and round to get 2 decimal places
125 mStr.append(f);
126 return *this;
127 }
128
129 StrStream &operator<<(const double &f) {
130 // multiply by 100 and round to get 2 decimal places
131 mStr.append(f);
132 return *this;
133 }
134
135 StrStream &operator<<(const char &c) {
136 if (mTreatCharAsInt) {
138 } else {
140 }
141 return *this;
142 }
143
144 template<fl::size N>
145 StrStream &operator<<(const char (&str)[N]) {
146 mStr.append(str);
147 return *this;
148 }
149
150 template<fl::u32 N>
152 // mStr.append(bs);
153 bs.to_string(&mStr);
154 return *this;
155 }
156
158 bs.to_string(&mStr);
159 return *this;
160 }
161
162 template<fl::u32 N>
164 bs.to_string(&mStr);
165 return *this;
166 }
167
168 // bool support - output as "true"/"false" for readability
170 mStr.append(b ? "true" : "false");
171 return *this;
172 }
173
175 if (mTreatCharAsInt) {
176 mStr.append(fl::u16(n));
177 } else {
178 mStr.append(n);
179 }
180 return *this;
181 }
182
183 StrStream &operator<<(const fl::i16 &n) {
184 mStr.append(n);
185 return *this;
186 }
187
188 StrStream &operator<<(const fl::i32 &n) {
189 mStr.append(n);
190 return *this;
191 }
192
193 StrStream &operator<<(const fl::u32 &n) {
194 mStr.append(n);
195 return *this;
196 }
197
198 StrStream &operator<<(const fl::u64 &n) {
199 mStr.append(n);
200 return *this;
201 }
202
203 StrStream &operator<<(const long long &n) {
204 mStr.append(static_cast<fl::i64>(n));
205 return *this;
206 }
207
208
209 // Unified handler for fl:: namespace size-like unsigned integer types and int
210 // This handles fl::size, fl::u16 from the fl:: namespace, and int type
211 template<typename T>
212 typename fl::enable_if<
216 StrStream&
217 >::type operator<<(T n) {
219 mStr.append(fl::i32(n));
220 } else {
221 mStr.append(fl::u32(n));
222 }
223 return *this;
224 }
225
226 // assignment operator completely replaces the current string
227 StrStream &operator=(const string &str) {
228 mStr = str;
229 return *this;
230 }
231
232 StrStream &operator=(const char *str) {
233 mStr.clear();
234 mStr.append(str);
235 return *this;
236 }
237
238
239 // crgb
240 StrStream &operator=(const CRGB &rgb) {
241 mStr.clear();
242 (*this) << rgb;
243 return *this;
244 }
245
246 void clear() { mStr.clear(); }
247
248 private:
249 string mStr;
250 bool mTreatCharAsInt = false; // Default to ASCII mode for readable text output
251};
252
254 public:
255 template <typename T> FakeStrStream &operator<<(const T &) { return *this; }
256
257 FakeStrStream &operator<<(const char *) { return *this; }
258
259 template<fl::size N>
260 FakeStrStream &operator<<(const char (&)[N]) { return *this; }
261
262 template <typename T> FakeStrStream &operator=(const T &) { return *this; }
263
264 FakeStrStream &operator<<(const CRGB &) { return *this; }
265 FakeStrStream &operator<<(const string &) { return *this; }
266 FakeStrStream &operator<<(char) { return *this; }
267
268 // bool support to match StrStream interface
269 FakeStrStream &operator<<(bool) { return *this; }
270
271 // Unified template for fl:: namespace types and int to avoid conflicts on AVR
272 template<typename T>
273 typename fl::enable_if<
278 >::type operator<<(T) { return *this; }
279
280 FakeStrStream &operator<<(fl::u8) { return *this; }
281 FakeStrStream &operator<<(fl::i16) { return *this; }
282 FakeStrStream &operator<<(fl::i32) { return *this; }
283 FakeStrStream &operator<<(fl::u32) { return *this; }
284
285 FakeStrStream &operator=(const string &) { return *this; }
286 FakeStrStream &operator=(const CRGB &) { return *this; }
287 FakeStrStream &operator=(fl::u16) { return *this; }
288 FakeStrStream &operator=(fl::u8) { return *this; }
289 FakeStrStream &operator=(char) { return *this; }
290};
291
292
293} // namespace fl
FakeStrStream & operator=(char)
Definition strstream.h:289
FakeStrStream & operator<<(fl::i32)
Definition strstream.h:282
FakeStrStream & operator<<(const T &)
Definition strstream.h:255
FakeStrStream & operator=(fl::u16)
Definition strstream.h:287
FakeStrStream & operator<<(const char(&)[N])
Definition strstream.h:260
FakeStrStream & operator<<(fl::u32)
Definition strstream.h:283
FakeStrStream & operator<<(bool)
Definition strstream.h:269
FakeStrStream & operator<<(fl::i16)
Definition strstream.h:281
FakeStrStream & operator<<(char)
Definition strstream.h:266
FakeStrStream & operator=(fl::u8)
Definition strstream.h:288
FakeStrStream & operator=(const string &)
Definition strstream.h:285
FakeStrStream & operator<<(const char *)
Definition strstream.h:257
fl::enable_if< fl::is_same< T, fl::size >::value||fl::is_same< T, fl::u16 >::value||fl::is_same< T, int >::value, FakeStrStream & >::type operator<<(T)
Definition strstream.h:278
FakeStrStream & operator=(const CRGB &)
Definition strstream.h:286
FakeStrStream & operator=(const T &)
Definition strstream.h:262
FakeStrStream & operator<<(const CRGB &)
Definition strstream.h:264
FakeStrStream & operator<<(fl::u8)
Definition strstream.h:280
FakeStrStream & operator<<(const string &)
Definition strstream.h:265
fl::size size() const
Definition vector.h:545
bool mTreatCharAsInt
Definition strstream.h:250
StrStream & operator<<(const BitsetInlined< N > &bs)
Definition strstream.h:163
StrStream & operator=(const CRGB &rgb)
Definition strstream.h:240
StrStream(const string &str)
Definition strstream.h:45
StrStream & operator<<(const HeapVector< T, Alloc > &vec)
Definition strstream.h:82
const char * c_str() const
Definition strstream.h:52
StrStream & operator<<(const CRGB &rgb)
Definition strstream.h:54
StrStream & operator<<(const char &c)
Definition strstream.h:135
StrStream & operator<<(const fl::u8 &n)
Definition strstream.h:174
StrStream & operator=(const fl::u16 &n)
Definition strstream.h:94
StrStream & operator<<(const char(&str)[N])
Definition strstream.h:145
StrStream & operator<<(const bitset_dynamic &bs)
Definition strstream.h:157
StrStream & operator<<(bool b)
Definition strstream.h:169
StrStream & operator<<(const long long &n)
Definition strstream.h:203
StrStream & operator<<(const double &f)
Definition strstream.h:129
const string & str() const
Definition strstream.h:51
StrStream & operator<<(const fl::u64 &n)
Definition strstream.h:198
StrStream & operator<<(const fl::i16 &n)
Definition strstream.h:183
StrStream & operator<<(const string &str)
Definition strstream.h:113
StrStream & operator<<(const fl::i32 &n)
Definition strstream.h:188
StrStream & operator<<(const char *str)
Definition strstream.h:118
StrStream & operator<<(const fl::u32 &n)
Definition strstream.h:193
StrStream & operator=(const fl::u8 &n)
Definition strstream.h:100
StrStream & operator=(char c)
Definition strstream.h:106
StrStream & operator=(const string &str)
Definition strstream.h:227
fl::enable_if< fl::is_same< T, fl::size >::value||fl::is_same< T, fl::u16 >::value||fl::is_same< T, int >::value, StrStream & >::type operator<<(T n)
Definition strstream.h:217
StrStream & operator<<(const vec2< T > &v)
Definition strstream.h:71
StrStream & operator<<(const BitsetFixed< N > &bs)
Definition strstream.h:151
void setTreatCharAsInt(bool treatCharAsInt)
Definition strstream.h:47
StrStream & operator<<(const StrStream &strStream)
Definition strstream.h:58
StrStream & operator=(const char *str)
Definition strstream.h:232
StrStream()=default
StrStream & operator<<(const float &f)
Definition strstream.h:123
FL_DISABLE_WARNING_POP void to_string(string *dst) const
Definition bitset.cpp:24
A dynamic bitset implementation that can be resized at runtime.
string & append(const BitsetFixed< N > &bs)
Definition str.h:675
Defines the red, green, and blue (RGB) pixel struct.
unsigned char u8
Definition int.h:17
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
static void append(string &str, const char &n)
Definition strstream.h:33
static void append(string &str, const fl::u8 &n)
Definition strstream.h:29
static void append(string &str, const int &n)
Definition strstream.h:25
static void append(string &str, const unsigned int &n)
Definition strstream.h:37
static void append(string &str, const T &n)
Definition strstream.h:21
static constexpr bool value
Definition type_traits.h:84
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190