/src/arduinojson/src/ArduinoJson/Json/Latch.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2023, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <ArduinoJson/Polyfills/assert.hpp> |
8 | | |
9 | | ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE |
10 | | |
11 | | template <typename TReader> |
12 | | class Latch { |
13 | | public: |
14 | 2.16k | Latch(TReader reader) : reader_(reader), loaded_(false) { |
15 | 2.16k | #if ARDUINOJSON_DEBUG |
16 | 2.16k | ended_ = false; |
17 | 2.16k | #endif |
18 | 2.16k | } |
19 | | |
20 | 57.9k | void clear() { |
21 | 57.9k | loaded_ = false; |
22 | 57.9k | } |
23 | | |
24 | 836 | int last() const { |
25 | 836 | return current_; |
26 | 836 | } |
27 | | |
28 | 121k | FORCE_INLINE char current() { |
29 | 121k | if (!loaded_) { |
30 | 59.6k | load(); |
31 | 59.6k | } |
32 | 121k | return current_; |
33 | 121k | } |
34 | | |
35 | | private: |
36 | 59.6k | void load() { |
37 | 59.6k | ARDUINOJSON_ASSERT(!ended_); |
38 | 0 | int c = reader_.read(); |
39 | 59.6k | #if ARDUINOJSON_DEBUG |
40 | 59.6k | if (c <= 0) |
41 | 1.63k | ended_ = true; |
42 | 59.6k | #endif |
43 | 59.6k | current_ = static_cast<char>(c > 0 ? c : 0); |
44 | 59.6k | loaded_ = true; |
45 | 59.6k | } |
46 | | |
47 | | TReader reader_; |
48 | | char current_; // NOLINT(clang-analyzer-optin.cplusplus.UninitializedObject) |
49 | | // Not initialized in constructor (+10 bytes on AVR) |
50 | | bool loaded_; |
51 | | #if ARDUINOJSON_DEBUG |
52 | | bool ended_; |
53 | | #endif |
54 | | }; |
55 | | |
56 | | ARDUINOJSON_END_PRIVATE_NAMESPACE |