FastLED 3.9.15
Loading...
Searching...
No Matches
strstream.h
Go to the documentation of this file.
1#pragma once
2
3#include "crgb.h"
4#include "str.h"
5
6#ifndef FASTLED_STRSTREAM_USES_SIZE_T
7#if defined(__AVR__) || defined(ESP8266) || defined(ESP32)
8#define FASTLED_STRSTREAM_USES_SIZE_T 0
9#else
10#define FASTLED_STRSTREAM_USES_SIZE_T 1
11#endif
12#endif
13
14namespace fl {
15
16class Tile2x2_u8;
17
18template <typename T> struct StrStreamHelper {
19 static void append(Str &str, const T &n) { str.append(n); }
20};
21
22template <> struct StrStreamHelper<int> {
23 static void append(Str &str, const int &n) { str.append(int32_t(n)); }
24};
25
26template <> struct StrStreamHelper<uint8_t> {
27 static void append(Str &str, const uint8_t &n) { str.append(uint16_t(n)); }
28};
29
30template <> struct StrStreamHelper<char> {
31 static void append(Str &str, const char &n) { str.append(uint16_t(n)); }
32};
33
34template <> struct StrStreamHelper<unsigned int> {
35 static void append(Str &str, const unsigned int &n) {
36 str.append(uint32_t(n));
37 }
38};
39
40class StrStream {
41 public:
42 StrStream() = default;
43 StrStream(const Str &str) : mStr(str) {}
44
45 void setTreatCharAsInt(bool treatCharAsInt) {
46 mTreatCharAsInt = treatCharAsInt;
47 }
48
49 const Str &str() const { return mStr; }
50 const char *c_str() const { return mStr.c_str(); }
51
52 StrStream &operator<<(const CRGB &rgb) {
53 mStr.append(rgb);
54 return *this;
55 }
56 StrStream &operator<<(const StrStream &strStream) {
57 mStr.append(strStream.str());
58 return *this;
59 }
60
61 StrStream &operator<<(const Tile2x2_u8 &subpixel);
62
63 StrStream &operator=(const uint16_t &n) {
64 mStr.clear();
65 (*this) << n;
66 return *this;
67 }
68
69 StrStream &operator=(const uint8_t &n) {
70 mStr.clear();
71 (*this) << n;
72 return *this;
73 }
74
76 mStr.clear();
77 (*this) << c;
78 return *this;
79 }
80
81 // << operator section
83 mStr.append(str);
84 return *this;
85 }
86
87 StrStream &operator<<(const char *str) {
88 mStr.append(str);
89 return *this;
90 }
91
92 StrStream &operator<<(const float &f) {
93 // multiply by 100 and round to get 2 decimal places
94 mStr.append(f);
95 return *this;
96 }
97
98 StrStream &operator<<(const double &f) {
99 // multiply by 100 and round to get 2 decimal places
100 mStr.append(f);
101 return *this;
102 }
103
104 StrStream &operator<<(const char &c) {
105 if (mTreatCharAsInt) {
107 } else {
109 }
110 return *this;
111 }
112
113#if FASTLED_STRSTREAM_USES_SIZE_T
114 StrStream &operator<<(size_t n) {
115 mStr.append(uint32_t(n));
116 return *this;
117 }
118#endif
119
120 template <typename T> StrStream &operator<<(T n) {
122 return *this;
123 }
124
125 StrStream &operator<<(const uint8_t &n) {
126 if (mTreatCharAsInt) {
127 mStr.append(uint16_t(n));
128 } else {
129 mStr.append(n);
130 }
131 return *this;
132 }
133
134 StrStream &operator<<(const uint16_t &n) {
135 mStr.append(n);
136 return *this;
137 }
138
139 StrStream &operator<<(const int16_t &n) {
140 mStr.append(n);
141 return *this;
142 }
143
144 StrStream &operator<<(const uint32_t &n) {
145 mStr.append(uint32_t(n));
146 return *this;
147 }
148
149 StrStream &operator<<(const int32_t &n) {
150 mStr.append(n);
151 return *this;
152 }
153
154 // assignment operator completely replaces the current string
156 mStr = str;
157 return *this;
158 }
159
160 StrStream &operator=(const char *str) {
161 mStr = str;
162 return *this;
163 }
164
165 // crgb
166 StrStream &operator=(const CRGB &rgb) {
167 mStr.clear();
168 (*this) << rgb;
169 return *this;
170 }
171
172 void clear() { mStr.clear(); }
173
174 private:
176 bool mTreatCharAsInt = true;
177};
178
180 public:
181 template <typename T> FakeStrStream &operator<<(const T &) { return *this; }
182
183 FakeStrStream &operator<<(const char *) { return *this; }
184
185 template <typename T> FakeStrStream &operator=(const T &) { return *this; }
186
187 FakeStrStream &operator<<(const CRGB &) { return *this; }
188 FakeStrStream &operator<<(const Str &) { return *this; }
189 FakeStrStream &operator<<(char) { return *this; }
190
191#if FASTLED_STRSTREAM_USES_SIZE_T
192 FakeStrStream &operator<<(size_t) { return *this; }
193#endif
194
195 FakeStrStream &operator<<(uint8_t) { return *this; }
196 FakeStrStream &operator<<(uint16_t) { return *this; }
197 FakeStrStream &operator<<(int16_t) { return *this; }
198 FakeStrStream &operator<<(uint32_t) { return *this; }
199 FakeStrStream &operator<<(int32_t) { return *this; }
200
201 FakeStrStream &operator=(const Str &) { return *this; }
202 FakeStrStream &operator=(const CRGB &) { return *this; }
203 FakeStrStream &operator=(uint16_t) { return *this; }
204 FakeStrStream &operator=(uint8_t) { return *this; }
205 FakeStrStream &operator=(char) { return *this; }
206};
207
208} // namespace fl
FakeStrStream & operator=(char)
Definition strstream.h:205
FakeStrStream & operator<<(const T &)
Definition strstream.h:181
FakeStrStream & operator<<(uint16_t)
Definition strstream.h:196
FakeStrStream & operator=(uint16_t)
Definition strstream.h:203
FakeStrStream & operator<<(const Str &)
Definition strstream.h:188
FakeStrStream & operator=(uint8_t)
Definition strstream.h:204
FakeStrStream & operator<<(char)
Definition strstream.h:189
FakeStrStream & operator<<(uint8_t)
Definition strstream.h:195
FakeStrStream & operator<<(const char *)
Definition strstream.h:183
FakeStrStream & operator<<(int32_t)
Definition strstream.h:199
FakeStrStream & operator=(const CRGB &)
Definition strstream.h:202
FakeStrStream & operator<<(int16_t)
Definition strstream.h:197
FakeStrStream & operator=(const T &)
Definition strstream.h:185
FakeStrStream & operator<<(const CRGB &)
Definition strstream.h:187
FakeStrStream & operator=(const Str &)
Definition strstream.h:201
FakeStrStream & operator<<(uint32_t)
Definition strstream.h:198
Str & append(const T &val)
Definition str.h:439
Definition str.h:388
bool mTreatCharAsInt
Definition strstream.h:176
StrStream & operator<<(const int32_t &n)
Definition strstream.h:149
StrStream & operator=(const CRGB &rgb)
Definition strstream.h:166
StrStream & operator<<(const int16_t &n)
Definition strstream.h:139
const char * c_str() const
Definition strstream.h:50
StrStream & operator<<(T n)
Definition strstream.h:120
StrStream & operator<<(const CRGB &rgb)
Definition strstream.h:52
StrStream & operator<<(const char &c)
Definition strstream.h:104
StrStream & operator<<(const uint16_t &n)
Definition strstream.h:134
StrStream & operator=(const uint8_t &n)
Definition strstream.h:69
StrStream(const Str &str)
Definition strstream.h:43
StrStream & operator<<(const Str &str)
Definition strstream.h:82
StrStream & operator<<(const double &f)
Definition strstream.h:98
StrStream & operator<<(const uint8_t &n)
Definition strstream.h:125
StrStream & operator<<(const char *str)
Definition strstream.h:87
StrStream & operator=(const Str &str)
Definition strstream.h:155
const Str & str() const
Definition strstream.h:49
StrStream & operator=(char c)
Definition strstream.h:75
StrStream & operator<<(const uint32_t &n)
Definition strstream.h:144
void setTreatCharAsInt(bool treatCharAsInt)
Definition strstream.h:45
StrStream & operator<<(const StrStream &strStream)
Definition strstream.h:56
StrStream & operator=(const char *str)
Definition strstream.h:160
StrStream()=default
StrStream & operator<<(const float &f)
Definition strstream.h:92
StrStream & operator=(const uint16_t &n)
Definition strstream.h:63
Defines the red, green, and blue (RGB) pixel struct.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55
static void append(Str &str, const char &n)
Definition strstream.h:31
static void append(Str &str, const int &n)
Definition strstream.h:23
static void append(Str &str, const uint8_t &n)
Definition strstream.h:27
static void append(Str &str, const unsigned int &n)
Definition strstream.h:35
static void append(Str &str, const T &n)
Definition strstream.h:19