FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
linked_list.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef LINKEDLIST_HH
22#define LINKEDLIST_HH
23
24#include "utilities.h"
25#include "types_traits.h"
26
34namespace containers
35{
37 class LinkImpl
38 {
39 public:
40 LinkImpl() INLINE = default;
41 LinkImpl(const LinkImpl&) = delete;
42 LinkImpl& operator=(const LinkImpl&) = delete;
43
44 protected:
45 LinkImpl* next_ = nullptr;
46 friend class LinkedListImpl;
47 template<class T, class B> friend struct types_traits::derives_from;
48 };
49
50 class LinkedListImpl
51 {
52 public:
53 LinkedListImpl() INLINE = default;
54 LinkedListImpl(const LinkedListImpl&) = delete;
55 LinkedListImpl& operator=(const LinkedListImpl&) = delete;
56 void insert(LinkImpl* item);
57 bool remove(LinkImpl* item);
58 template<typename F> void traverse(F f);
59
60 protected:
61 LinkImpl* head_ = nullptr;
62 };
64
80 template<typename T_> class LinkedList : private LinkedListImpl
81 {
82 public:
84 {
85 // Check that T_ is a LinkImpl subclass
86 using DERIVES_FROM_LINK = types_traits::derives_from<T_, LinkImpl>;
87 UNUSED DERIVES_FROM_LINK dummy = DERIVES_FROM_LINK();
88 }
89
93 using T = T_;
94
103 void insert(T& item) INLINE
104 {
105 LinkedListImpl::insert(&item);
106 }
107
118 bool remove(T& item) INLINE
119 {
120 return LinkedListImpl::remove(&item);
121 }
122
132 template<typename F> void traverse(F func)
133 {
134 T* current = head();
135 while (current != nullptr)
136 {
137 T* next = current->next();
138 if (func(*current)) remove(*current);
139 current = next;
140 }
141 }
142
143 private:
144 T* head() INLINE
145 {
146 return (T*) head_;
147 }
148 };
149
167 template<typename T_> class Link : private LinkImpl
168 {
169 public:
173 using T = T_;
174
175 protected:
176 Link() = default;
177
178 private:
179 T* next() INLINE
180 {
181 return (T*) next_;
182 }
183 friend class LinkedList<T>;
184 template<class T, class B> friend struct types_traits::derives_from;
185 };
186
217 template<typename T_, typename TREF_ = T_&, typename CTREF_ = const T_&>
218 class LinkWrapper : private LinkImpl
219 {
220 public:
221 LinkWrapper(const LinkWrapper&) = delete;
222 LinkWrapper& operator=(const LinkWrapper&) = delete;
223
225 using T = T_;
226
228 using TREF = TREF_;
229
231 using CTREF = CTREF_;
232
236 LinkWrapper(CTREF item) : item_{item} {}
237
242 {
243 return item_;
244 }
245
249 CTREF item() const
250 {
251 return item_;
252 }
253
254 private:
255 using TYPE = LinkWrapper<T, TREF, CTREF>;
256
257 TYPE* next() INLINE
258 {
259 return (TYPE*) next_;
260 }
261
262 T item_;
263
264 friend class LinkedList<TYPE>;
265 template<class T, class B> friend struct types_traits::derives_from;
266 };
267}
268
269#endif /* LINKEDLIST_HH */
Linked list of type T_ items.
Definition: linked_list.h:81
void traverse(F func)
Traverse all items of this list and execute f functor.
Definition: linked_list.h:132
void insert(T &item) INLINE
Insert item at the beginning of this list.
Definition: linked_list.h:103
bool remove(T &item) INLINE
Remove item from this list.
Definition: linked_list.h:118
T_ T
The type of items in this list.
Definition: linked_list.h:93
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
Definition: defines.h:45
#define F(ptr)
Force string constant to be stored as flash storage.
Definition: flash.h:150
Contains all FastArduino generic containers:
Definition: array.h:23
Defines traits and utility methods for standard types, like uint16_t.
Definition: types_traits.h:37
Utility class that checks, at compile-time, that type T is a subtype of type B.
Definition: types_traits.h:93
Useful traits for common types.
General utilities API that have broad application in programs.