1 | // Copyright Benoit Blanchon 2014-2016
|
---|
2 | // MIT License
|
---|
3 | //
|
---|
4 | // Arduino JSON library
|
---|
5 | // https://github.com/bblanchon/ArduinoJson
|
---|
6 | // If you like this project, please add a star!
|
---|
7 |
|
---|
8 | #pragma once
|
---|
9 |
|
---|
10 | #include "../JsonBuffer.hpp"
|
---|
11 | #include "ListConstIterator.hpp"
|
---|
12 | #include "ListIterator.hpp"
|
---|
13 |
|
---|
14 | namespace ArduinoJson {
|
---|
15 | namespace Internals {
|
---|
16 |
|
---|
17 | // A singly linked list of T.
|
---|
18 | // The linked list is composed of ListNode<T>.
|
---|
19 | // It is derived by JsonArray and JsonObject
|
---|
20 | template <typename T>
|
---|
21 | class List {
|
---|
22 | public:
|
---|
23 | typedef T value_type;
|
---|
24 | typedef ListNode<T> node_type;
|
---|
25 | typedef ListIterator<T> iterator;
|
---|
26 | typedef ListConstIterator<T> const_iterator;
|
---|
27 |
|
---|
28 | // Creates an empty List<T> attached to a JsonBuffer.
|
---|
29 | // The JsonBuffer allows to allocate new nodes.
|
---|
30 | // When buffer is NULL, the List is not able to grow and success() returns
|
---|
31 | // false. This is used to identify bad memory allocations and parsing
|
---|
32 | // failures.
|
---|
33 | explicit List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
---|
34 |
|
---|
35 | // Returns true if the object is valid
|
---|
36 | // Would return false in the following situation:
|
---|
37 | // - the memory allocation failed (StaticJsonBuffer was too small)
|
---|
38 | // - the JSON parsing failed
|
---|
39 | bool success() const { return _buffer != NULL; }
|
---|
40 |
|
---|
41 | // Returns the numbers of elements in the list.
|
---|
42 | // For a JsonObject, it would return the number of key-value pairs
|
---|
43 | size_t size() const;
|
---|
44 |
|
---|
45 | iterator begin() { return iterator(_firstNode); }
|
---|
46 | iterator end() { return iterator(NULL); }
|
---|
47 |
|
---|
48 | const_iterator begin() const { return const_iterator(_firstNode); }
|
---|
49 | const_iterator end() const { return const_iterator(NULL); }
|
---|
50 |
|
---|
51 | protected:
|
---|
52 | node_type *addNewNode();
|
---|
53 | void removeNode(node_type *nodeToRemove);
|
---|
54 |
|
---|
55 | JsonBuffer *_buffer;
|
---|
56 | node_type *_firstNode;
|
---|
57 | };
|
---|
58 | }
|
---|
59 | }
|
---|