FastLED 3.9.15
Loading...
Searching...
No Matches
istream.h
Go to the documentation of this file.
1// allow-include-after-namespace
2
3#pragma once
4
5// Forward declarations to avoid pulling in fl/io.h and causing fl/io.cpp to be compiled
6#ifndef FL_IO_H_INCLUDED
7namespace fl {
8 int available();
9 int read();
10#ifdef FASTLED_TESTING
11 template<typename T> class function; // Forward declare
12 void clear_io_handlers();
13 void inject_available_handler(const function<int()>& handler);
14 void inject_read_handler(const function<int()>& handler);
15#endif
16}
17#endif
18
19#include "fl/str.h"
20#include "fl/type_traits.h"
21#include "fl/sketch_macros.h"
22
23#include "fl/int.h"
24
25namespace fl {
26
28private:
29 static const fl::size BUFFER_SIZE = 256;
31 fl::size buffer_len_ = 0;
32 fl::size pos_ = 0;
33 bool failed_ = false;
34
35 // Helper to read a line from input
36 bool readLine();
37
38 // Helper to skip whitespace
39 void skipWhitespace();
40
41 // Helper to read until whitespace or end
42 bool readToken(string& token);
43
44public:
45 istream_real() = default;
46
47 // Check if stream is in good state
48 bool good() const { return !failed_; }
49 bool fail() const { return failed_; }
50 bool eof() const { return pos_ >= buffer_len_ && fl::available() == 0; }
51
52 // Clear error state
53 void clear() { failed_ = false; }
54
55 // Stream input operators
56 istream_real& operator>>(string& str);
57 istream_real& operator>>(char& c);
60 istream_real& operator>>(fl::i16& n);
61 istream_real& operator>>(fl::i32& n);
62 istream_real& operator>>(fl::u32& n);
63 istream_real& operator>>(float& f);
64 istream_real& operator>>(double& d);
65
66 // Unified handler for fl:: namespace size-like unsigned integer types to avoid conflicts
67 // This only handles fl::size and fl::u16 from the fl:: namespace
68 template<typename T>
69 typename fl::enable_if<
73 >::type operator>>(T& n);
74
75 // Get a line from input
76 istream_real& getline(string& str);
77
78 // Get next character
79 int get();
80
81 // Put back a character
82 istream_real& putback(char c);
83
84 // Peek at next character without consuming it
85 int peek();
86};
87
88// Function to get singleton instance of istream_real (for better linker elimination)
89istream_real& cin_real();
90
91// Stub istream class that conditionally delegates to istream_real
92class istream {
93private:
94#if SKETCH_HAS_LOTS_OF_MEMORY
95 istream_real real_stream_;
96#endif
97
98public:
99 istream() = default;
100
101 // Check if stream is in good state
102 bool good() const {
103#if SKETCH_HAS_LOTS_OF_MEMORY
104 return real_stream_.good();
105#else
106 return true; // Always good on memory-constrained platforms
107#endif
108 }
109
110 bool fail() const {
111#if SKETCH_HAS_LOTS_OF_MEMORY
112 return real_stream_.fail();
113#else
114 return false; // Never fail on memory-constrained platforms
115#endif
116 }
117
118 bool eof() const {
119#if SKETCH_HAS_LOTS_OF_MEMORY
120 return real_stream_.eof();
121#else
122 return true; // Always EOF on memory-constrained platforms
123#endif
124 }
125
126 // Clear error state
127 void clear() {
128#if SKETCH_HAS_LOTS_OF_MEMORY
129 real_stream_.clear();
130#endif
131 }
132
133 // Stream input operators
134 istream& operator>>(string& str) {
135#if SKETCH_HAS_LOTS_OF_MEMORY
136 real_stream_ >> str;
137#else
138 // No-op on memory-constrained platforms
139 str.clear();
140#endif
141 return *this;
142 }
143
144 istream& operator>>(char& c) {
145#if SKETCH_HAS_LOTS_OF_MEMORY
146 real_stream_ >> c;
147#else
148 // No-op on memory-constrained platforms
149 c = '\0';
150#endif
151 return *this;
152 }
153
155#if SKETCH_HAS_LOTS_OF_MEMORY
156 real_stream_ >> n;
157#else
158 // No-op on memory-constrained platforms
159 n = 0;
160#endif
161 return *this;
162 }
163
165#if SKETCH_HAS_LOTS_OF_MEMORY
166 real_stream_ >> n;
167#else
168 // No-op on memory-constrained platforms
169 n = 0;
170#endif
171 return *this;
172 }
173
174 istream& operator>>(fl::i16& n) {
175#if SKETCH_HAS_LOTS_OF_MEMORY
176 real_stream_ >> n;
177#else
178 // No-op on memory-constrained platforms
179 n = 0;
180#endif
181 return *this;
182 }
183
184 istream& operator>>(fl::i32& n) {
185#if SKETCH_HAS_LOTS_OF_MEMORY
186 real_stream_ >> n;
187#else
188 // No-op on memory-constrained platforms
189 n = 0;
190#endif
191 return *this;
192 }
193
194 istream& operator>>(fl::u32& n) {
195#if SKETCH_HAS_LOTS_OF_MEMORY
196 real_stream_ >> n;
197#else
198 // No-op on memory-constrained platforms
199 n = 0;
200#endif
201 return *this;
202 }
203
204 istream& operator>>(float& f) {
205#if SKETCH_HAS_LOTS_OF_MEMORY
206 real_stream_ >> f;
207#else
208 // No-op on memory-constrained platforms
209 f = 0.0f;
210#endif
211 return *this;
212 }
213
214 istream& operator>>(double& d) {
215#if SKETCH_HAS_LOTS_OF_MEMORY
216 real_stream_ >> d;
217#else
218 // No-op on memory-constrained platforms
219 d = 0.0;
220#endif
221 return *this;
222 }
223
224 // Unified handler for fl:: namespace size-like unsigned integer types to avoid conflicts
225 template<typename T>
226 typename fl::enable_if<
229 istream&
230 >::type operator>>(T& n) {
231#if SKETCH_HAS_LOTS_OF_MEMORY
232 real_stream_ >> n;
233#else
234 // No-op on memory-constrained platforms
235 n = 0;
236#endif
237 return *this;
238 }
239
240 // Get a line from input
241 istream& getline(string& str) {
242#if SKETCH_HAS_LOTS_OF_MEMORY
243 real_stream_.getline(str);
244#else
245 // No-op on memory-constrained platforms
246 str.clear();
247#endif
248 return *this;
249 }
250
251 // Get next character
252 int get() {
253#if SKETCH_HAS_LOTS_OF_MEMORY
254 return real_stream_.get();
255#else
256 // No-op on memory-constrained platforms
257 return -1;
258#endif
259 }
260
261 // Put back a character
262 istream& putback(char c) {
263#if SKETCH_HAS_LOTS_OF_MEMORY
264 real_stream_.putback(c);
265#endif
266 return *this;
267 }
268
269 // Peek at next character without consuming it
270 int peek() {
271#if SKETCH_HAS_LOTS_OF_MEMORY
272 return real_stream_.peek();
273#else
274 // No-op on memory-constrained platforms
275 return -1;
276#endif
277 }
278};
279
280// Global cin instance for input (now uses the stub)
281extern istream cin;
282
283// Template implementation for istream_real
284template<typename T>
285typename fl::enable_if<
288 istream_real&
290 // Use existing fl::u32 parsing logic for both fl::size and fl::u16
291 // since they're both unsigned integer types that fit in fl::u32
292 fl::u32 temp;
293 (*this) >> temp;
294 n = static_cast<T>(temp);
295 return *this;
296}
297
298} // namespace fl
void clear(bool freeMemory=false)
Definition str.h:398
bool fail() const
Definition istream.h:49
void skipWhitespace()
Definition istream.cpp:158
bool good() const
Definition istream.h:48
fl::size buffer_len_
Definition istream.h:31
istream_real & putback(char c)
Definition istream.cpp:384
char buffer_[BUFFER_SIZE]
Definition istream.h:30
fl::size pos_
Definition istream.h:32
static const fl::size BUFFER_SIZE
Definition istream.h:29
istream_real & operator>>(string &str)
Definition istream.cpp:204
istream_real()=default
bool eof() const
Definition istream.h:50
bool readToken(string &token)
Definition istream.cpp:173
istream_real & getline(string &str)
Definition istream.cpp:337
int peek()
Definition istream.h:270
istream & operator>>(fl::u8 &n)
Definition istream.h:164
bool good() const
Definition istream.h:102
fl::enable_if< fl::is_same< T, fl::size >::value||fl::is_same< T, fl::u16 >::value, istream & >::type operator>>(T &n)
Definition istream.h:230
istream()=default
istream & operator>>(double &d)
Definition istream.h:214
istream & operator>>(char &c)
Definition istream.h:144
istream & operator>>(fl::i32 &n)
Definition istream.h:184
bool eof() const
Definition istream.h:118
istream & getline(string &str)
Definition istream.h:241
bool fail() const
Definition istream.h:110
istream & operator>>(fl::i16 &n)
Definition istream.h:174
void clear()
Definition istream.h:127
int get()
Definition istream.h:252
istream & operator>>(fl::u32 &n)
Definition istream.h:194
istream & operator>>(float &f)
Definition istream.h:204
istream & operator>>(fl::i8 &n)
Definition istream.h:154
istream & putback(char c)
Definition istream.h:262
istream & operator>>(string &str)
Definition istream.h:134
int available()
Definition io.cpp:117
unsigned char u8
Definition int.h:17
int read()
Definition io.cpp:148
FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS istream cin
Definition istream.cpp:126
istream_real & cin_real()
Definition istream.cpp:129
signed char i8
Definition int.h:16
IMPORTANT!
Definition crgb.h:20
static constexpr bool value
Definition type_traits.h:84