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