-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLinkedMap.h
More file actions
136 lines (114 loc) · 3.28 KB
/
LinkedMap.h
File metadata and controls
136 lines (114 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef OTF_LINKEDMAP_H
#define OTF_LINKEDMAP_H
#if defined(ARDUINO)
#include <Arduino.h>
#else
#include <string.h>
#endif
namespace OTF {
template<class T>
class LinkedMapNode;
template<class T>
class LinkedMap {
friend class OpenThingsFramework;
friend class Response;
private:
LinkedMapNode<T> *head = nullptr;
LinkedMapNode<T> *tail = nullptr;
LinkedMapNode<T> *_findNode(const char *key, bool keyInFlash = false) const {
LinkedMapNode<T> *node = head;
while (node != nullptr) {
#if defined(ARDUINO)
if ((keyInFlash ? strcmp_P(node->key, key) : strcmp(node->key, key)) == 0) {
#else
if (strcmp(node->key, key) == 0) {
#endif
return node;
}
node = node->next;
}
// Indicate the key could not be found.
return nullptr;
}
void _add(LinkedMapNode<T> *node, bool keyInFlash = false) {
LinkedMapNode<T> *existingNode = _findNode(node->key, keyInFlash);
if (existingNode != nullptr) {
// Update the value of the existing node.
existingNode->value = node->value;
delete node;
} else {
// Add the new node to the end of the list.
if (head == nullptr) {
head = node;
tail = head;
} else {
tail->next = node;
tail = tail->next;
}
}
}
T _find(const char *key, bool keyInFlash = false) const {
LinkedMapNode<T> *node = _findNode(key, keyInFlash);
return node != nullptr ? node->value : nullptr;
}
public:
~LinkedMap() {
LinkedMapNode<T> *node = head;
while (node != nullptr) {
LinkedMapNode<T> *next = node->next;
delete node;
node = next;
}
}
// Borrowed: caller owns the key buffer; node does not free it.
void add(const char *key, T value) {
_add(new LinkedMapNode<T>(key, value));
}
// Owned: node takes ownership of a heap-allocated `new char[]` key
// and frees it on destruction. Non-const parameter signals ownership
// transfer and rejects string literals at the call site.
void addOwned(char *key, T value) {
_add(new LinkedMapNode<T>(key, value, true));
}
#if defined(ARDUINO)
void add(const __FlashStringHelper *key, T value) {
_add(new LinkedMapNode<T>(key, value), true);
}
#endif
T find(const char *key) const {
return _find(key, false);
}
#if defined(ARDUINO)
T find(const __FlashStringHelper *key) const {
return _find((char *) key, true);
}
#endif
};
template<class T>
class LinkedMapNode {
private:
/** True if the node owns its key buffer (allocated via `new char[]`)
* and must free it on destruction. False for borrowed pointers
* (e.g., keys pointing into a parsed request buffer). */
bool ownsKey = false;
public:
const char *key = nullptr;
T value;
LinkedMapNode<T> *next = nullptr;
LinkedMapNode(const char *key, T value) {
this->key = key;
this->value = value;
}
LinkedMapNode(const char *key, T value, bool ownsKey) {
this->key = key;
this->value = value;
this->ownsKey = ownsKey;
}
~LinkedMapNode() {
if (ownsKey) {
delete[] key;
}
}
};
}// namespace OTF
#endif