Extracted the class Latch

also fixed a buffer overrun and reduced the code size
This commit is contained in:
Benoit Blanchon
2020-02-13 14:53:54 +01:00
parent d2a67f362f
commit 6e52f242b2
3 changed files with 73 additions and 26 deletions

View 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