Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Latch.hpp Source File

Latch.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Polyfills/assert.hpp>
00008 
00009 namespace ARDUINOJSON_NAMESPACE {
00010 
00011 template <typename TReader>
00012 class Latch {
00013  public:
00014   Latch(TReader reader) : _reader(reader), _loaded(false) {
00015 #if ARDUINOJSON_DEBUG
00016     _ended = false;
00017 #endif
00018   }
00019 
00020   void clear() {
00021     _loaded = false;
00022   }
00023 
00024   int last() const {
00025     return _current;
00026   }
00027 
00028   FORCE_INLINE char current() {
00029     if (!_loaded) {
00030       load();
00031     }
00032     return _current;
00033   }
00034 
00035  private:
00036   void load() {
00037     ARDUINOJSON_ASSERT(!_ended);
00038     int c = _reader.read();
00039 #if ARDUINOJSON_DEBUG
00040     if (c <= 0)
00041       _ended = true;
00042 #endif
00043     _current = static_cast<char>(c > 0 ? c : 0);
00044     _loaded = true;
00045   }
00046 
00047   TReader _reader;
00048   char _current;
00049   bool _loaded;
00050 #if ARDUINOJSON_DEBUG
00051   bool _ended;
00052 #endif
00053 };
00054 
00055 }  // namespace ARDUINOJSON_NAMESPACE