mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Extracted the class Latch
also fixed a buffer overrun and reduced the code size
This commit is contained in:
		
							
								
								
									
										54
									
								
								src/ArduinoJson/Json/Latch.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/ArduinoJson/Json/Latch.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2020 | ||||
| // MIT License | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <ArduinoJson/Polyfills/assert.hpp> | ||||
|  | ||||
| namespace ARDUINOJSON_NAMESPACE { | ||||
|  | ||||
| template <typename TReader> | ||||
| class Latch { | ||||
|  public: | ||||
|   Latch(TReader reader) : _reader(reader), _loaded(false) { | ||||
| #ifdef ARDUINOJSON_DEBUG | ||||
|     _ended = false; | ||||
| #endif | ||||
|   } | ||||
|  | ||||
|   void clear() { | ||||
|     _loaded = false; | ||||
|   } | ||||
|  | ||||
|   int last() const { | ||||
|     return _current; | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE char current() { | ||||
|     if (!_loaded) { | ||||
|       load(); | ||||
|     } | ||||
|     return _current; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   void load() { | ||||
|     ARDUINOJSON_ASSERT(!_ended); | ||||
|     int c = _reader.read(); | ||||
| #ifdef ARDUINOJSON_DEBUG | ||||
|     if (c <= 0) _ended = true; | ||||
| #endif | ||||
|     _current = static_cast<char>(c > 0 ? c : 0); | ||||
|     _loaded = true; | ||||
|   } | ||||
|  | ||||
|   TReader _reader; | ||||
|   char _current; | ||||
|   bool _loaded; | ||||
| #ifdef ARDUINOJSON_DEBUG | ||||
|   bool _ended; | ||||
| #endif | ||||
| }; | ||||
|  | ||||
| }  // namespace ARDUINOJSON_NAMESPACE | ||||
		Reference in New Issue
	
	Block a user