utils-lists 4.0.2
The µOS++ Intrusive Lists
Loading...
Searching...
No Matches
lists.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.com/).
3 * Copyright (c) 2016 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
49
50#ifndef MICRO_OS_PLUS_UTILS_LISTS_H_
51#define MICRO_OS_PLUS_UTILS_LISTS_H_
52
53// ----------------------------------------------------------------------------
54
55#ifdef __cplusplus
56
57// ----------------------------------------------------------------------------
58
59#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
60#include <micro-os-plus/config.h>
61#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
62
63#include <cstdint>
64#include <cstddef>
65#include <cassert>
66#include <iterator>
67
68// ----------------------------------------------------------------------------
69
70#if defined(__GNUC__)
71#pragma GCC diagnostic push
72
73#pragma GCC diagnostic ignored "-Waggregate-return"
74#if defined(__clang__)
75#pragma clang diagnostic ignored "-Wc++98-compat"
76#endif
77#endif
78
94{
95 // ==========================================================================
96
112 {
113 public:
117 constexpr double_list_links_base ();
118
122
123 // The rule of five.
127 operator= (const double_list_links_base&)
128 = delete;
130 operator= (double_list_links_base&&)
131 = delete;
132
136
140 constexpr ~double_list_links_base ();
141
149 bool
150 uninitialized (void) const;
151
159 constexpr void
160 initialize (void);
161
169 void
170 initialize_once (void);
171
179 void
181
189 void
191
197 void
198 unlink (void);
199
205 bool
206 linked (void) const;
207
212 constexpr double_list_links_base*
213 next (void) const;
214
219 constexpr double_list_links_base*
220 previous (void) const;
221
222 protected:
227
232 };
233
234 // ==========================================================================
235
251 {
252 public:
257 using is_statically_allocated = std::false_type;
258
262 constexpr double_list_links ();
263
267
268 // The rule of five.
269 double_list_links (const double_list_links&) = delete;
272 operator= (const double_list_links&)
273 = delete;
275 operator= (double_list_links&&)
276 = delete;
277
281
285 constexpr ~double_list_links ();
286 };
287
288 // ==========================================================================
289
324 {
325 public:
329 using is_statically_allocated = std::true_type;
330
335 constexpr static_double_list_links ();
336
340
341 // The rule of five.
345 operator= (const static_double_list_links&)
346 = delete;
348 operator= (static_double_list_links&&)
349 = delete;
350
354
358 constexpr ~static_double_list_links ();
359
367 void
368 nullify (void);
369 };
370
371 // ==========================================================================
372
387 template <class T, class N = T, class U = T>
389 {
390 public:
394 using value_type = U;
395
400
405
410
414 using difference_type = ptrdiff_t;
415
419 using iterator_category = std::forward_iterator_tag;
420
421 // ------------------------------------------------------------------------
423
424 constexpr explicit double_list_iterator (iterator_pointer const node);
425
426 constexpr explicit double_list_iterator (reference element);
427
428 // DO NOT delete the copy constructors, since the default ones are
429 // used.
430
431 constexpr pointer
432 operator->() const;
433
434 constexpr reference
435 operator* () const;
436
437 constexpr double_list_iterator&
439
440 constexpr double_list_iterator
442
443 constexpr double_list_iterator&
445
446 constexpr double_list_iterator
448
449 constexpr bool
451
452 constexpr bool
454
455 constexpr pointer
456 get_pointer () const;
457
458 constexpr iterator_pointer
460
461 protected:
466 };
467
468 // ==========================================================================
469
500 template <class T, class L = double_list_links>
502 {
503 public:
504 static_assert (std::is_base_of<double_list_links_base, L>::value == true,
505 "L must be derived from double_list_links_base!");
506 static_assert (std::is_base_of<double_list_links_base, T>::value == true,
507 "T must be derived from double_list_links_base!");
508
513 using links_type = L;
514
518 using value_type = T;
519
524
529
534
539
544 typename links_type::is_statically_allocated;
545
549 double_list ();
550
554
555 // The rule of five.
556 double_list (const double_list&) = delete;
557 double_list (double_list&&) = delete;
559 operator= (const double_list&)
560 = delete;
562 operator= (double_list&&)
563 = delete;
564
568
572 constexpr ~double_list ();
573
574 public:
583 bool
584 uninitialized (void) const;
585
593 void
594 initialize_once (void);
595
603 bool
604 empty (void) const;
605
613 void
614 clear (void);
615
622 constexpr pointer
623 head (void) const;
624
631 constexpr pointer
632 tail (void) const;
633
637 void
638 link_tail (reference node);
639
643 void
644 link_head (reference node);
645
646 // ------------------------------------------------------------------------
647
653 begin () const;
654
660 end () const;
661
662 // Required in derived class iterator end(), where direct
663 // access to member fails.
668 constexpr const links_type*
670 {
671 return &links_;
672 }
673
674 // ------------------------------------------------------------------------
675
676 protected:
689 };
690
691 // ==========================================================================
692
708 template <class T, class N, N T::*MP, class U = T>
710 {
711 public:
715 using value_type = U;
716
721
726
731
735 using difference_type = ptrdiff_t;
736
740 using iterator_category = std::forward_iterator_tag;
741
742 // ------------------------------------------------------------------------
744
745 constexpr explicit intrusive_list_iterator (iterator_pointer const node);
746
747 constexpr explicit intrusive_list_iterator (reference element);
748
749 // DO NOT delete the copy constructors, since the default ones are
750 // used.
751
752 pointer
753 operator->() const;
754
756 operator* () const;
757
760
763
766
769
770 bool
772
773 bool
775
780 pointer
781 get_pointer (void) const;
782
785
786 protected:
791 };
792
793 // ==========================================================================
794
795#if defined(__clang__)
796#pragma clang diagnostic push
797#pragma clang diagnostic ignored "-Wdocumentation"
798#endif
832#if defined(__clang__)
833#pragma clang diagnostic pop
834#endif
835
836 template <class T, class N, N T::*MP, class L = double_list_links,
837 class U = T>
838 class intrusive_list : public double_list<N, L>
839 {
840 public:
841 static_assert (std::is_base_of<double_list_links_base, L>::value == true,
842 "L must be derived from double_list_links_base!");
843 static_assert (std::is_base_of<double_list_links_base, N>::value == true,
844 "N must be derived from double_list_links_base!");
845
850 using links_type = L;
851
855 using value_type = U;
856
861
866
871
876 typename links_type::is_statically_allocated;
877
882
886 using difference_type = ptrdiff_t;
887
891 constexpr intrusive_list ();
892
896
897 // The rule of five.
898 intrusive_list (const intrusive_list&) = delete;
899 intrusive_list (intrusive_list&&) = delete;
901 operator= (const intrusive_list&)
902 = delete;
904 operator= (intrusive_list&&)
905 = delete;
906
910
914 constexpr ~intrusive_list ();
915
916 public:
924 void
925 initialize_once (void);
926
934 constexpr bool
935 empty (void) const;
936
943 void
944 link_tail (reference node);
945
952 void
953 link_head (reference node);
954
959 pointer
960 unlink_tail (void);
961
966 pointer
967 unlink_head (void);
968
969 // ------------------------------------------------------------------------
970
976 begin () const;
977
983 end () const;
984
985 // ------------------------------------------------------------------------
986 protected:
991 pointer
992 get_pointer (iterator_pointer node) const;
993 };
994
995 // --------------------------------------------------------------------------
996} // namespace micro_os_plus::utils
997
998#if defined(__GNUC__)
999#pragma GCC diagnostic pop
1000#endif
1001
1002// ----------------------------------------------------------------------------
1003
1004#endif // __cplusplus
1005
1006// ===== Inline & template implementations ====================================
1007
1008// All other inlines.
1009#include "inlines.h"
1010
1011// ----------------------------------------------------------------------------
1012
1013#endif // MICRO_OS_PLUS_UTILS_LISTS_H_
1014
1015// ----------------------------------------------------------------------------
A class template for a doubly linked list forward iterator.
Definition lists.h:389
constexpr double_list_iterator(reference element)
ptrdiff_t difference_type
Type of pointer difference.
Definition lists.h:414
constexpr bool operator==(const double_list_iterator &other) const
Definition inlines.h:252
constexpr pointer get_pointer() const
constexpr pointer operator->() const
Definition inlines.h:204
value_type * pointer
Type of pointer to object pointed to by the iterator.
Definition lists.h:399
constexpr double_list_iterator & operator--()
Definition inlines.h:235
constexpr bool operator!=(const double_list_iterator &other) const
Definition inlines.h:260
std::forward_iterator_tag iterator_category
Category of iterator.
Definition lists.h:419
constexpr iterator_pointer get_iterator_pointer() const
Definition inlines.h:268
constexpr double_list_iterator & operator++()
Definition inlines.h:218
constexpr reference operator*() const
Definition inlines.h:211
constexpr double_list_iterator(iterator_pointer const node)
Definition inlines.h:185
value_type & reference
Type of reference to object pointed to by the iterator.
Definition lists.h:404
U value_type
Type of value pointed to by the iterator.
Definition lists.h:394
N * iterator_pointer
Type of reference to the iterator internal pointer.
Definition lists.h:409
void link_head(reference node)
Add a node to the head of the list.
Definition inlines.h:423
links_type links_
The list top node used to point to head and tail nodes.
Definition lists.h:688
L links_type
Type of the links node object where the pointers to the list head and tail are stored.
Definition lists.h:513
value_type * iterator_pointer
Type of reference to the iterator internal pointer.
Definition lists.h:538
constexpr pointer tail(void) const
Get the list tail.
Definition inlines.h:403
void initialize_once(void)
Initialize the list only at first run.
Definition inlines.h:364
value_type * pointer
Type of pointer to object pointed to by the iterator.
Definition lists.h:523
constexpr const links_type * links_pointer() const
Get the address of the node storing the list links.
Definition lists.h:669
constexpr ~double_list()
Destruct the list.
Definition inlines.h:311
value_type & reference
Type of reference to object pointed to by the iterator.
Definition lists.h:528
double_list()
Construct a doubly linked list.
Definition inlines.h:283
void clear(void)
Clear the list.
Definition inlines.h:386
constexpr pointer head(void) const
Get the list head.
Definition inlines.h:396
iterator end() const
Iterator end.
Definition inlines.h:448
T value_type
Type of value pointed to by the iterator.
Definition lists.h:518
iterator begin() const
Iterator begin.
Definition inlines.h:436
double_list_iterator< value_type > iterator
Type of iterator over the values.
Definition lists.h:533
bool empty(void) const
Check if the list is empty.
Definition inlines.h:374
void link_tail(reference node)
Add a node to the tail of the list.
Definition inlines.h:410
bool uninitialized(void) const
Check if the list is uninitialised (only statically allocated lists can be uninitialised).
Definition inlines.h:337
typename links_type::is_statically_allocated is_statically_allocated
Type indicating that the links node is statically allocated.
Definition lists.h:543
A class template for the intrusive list iterator.
Definition lists.h:710
value_type * pointer
Type of pointer to object pointed to by the iterator.
Definition lists.h:720
std::forward_iterator_tag iterator_category
Category of iterator.
Definition lists.h:740
pointer get_pointer(void) const
Get the object node from the intrusive node.
Definition inlines.h:547
intrusive_list_iterator & operator++()
Definition inlines.h:497
N * iterator_pointer
Type of reference to the iterator internal pointer.
Definition lists.h:730
bool operator==(const intrusive_list_iterator &other) const
Definition inlines.h:531
iterator_pointer get_iterator_pointer() const
Definition inlines.h:565
value_type & reference
Type of reference to object pointed to by the iterator.
Definition lists.h:725
bool operator!=(const intrusive_list_iterator &other) const
Definition inlines.h:539
U value_type
Type of value pointed to by the iterator.
Definition lists.h:715
constexpr intrusive_list_iterator(iterator_pointer const node)
Definition inlines.h:466
constexpr intrusive_list_iterator(reference element)
Definition inlines.h:473
intrusive_list_iterator & operator--()
Definition inlines.h:514
ptrdiff_t difference_type
Type of pointer difference.
Definition lists.h:735
constexpr intrusive_list()
Construct an intrusive doubly linked list.
Definition inlines.h:573
L links_type
Type of the list links node object where the pointers to the list head and tail are stored.
Definition lists.h:850
constexpr bool empty(void) const
Check if the list is empty.
Definition inlines.h:604
void link_tail(reference node)
Add a node to the tail of the list.
Definition inlines.h:611
N * iterator_pointer
Type of reference to the iterator internal pointer.
Definition lists.h:881
iterator begin() const
Iterator begin.
Definition inlines.h:650
typename links_type::is_statically_allocated is_statically_allocated
Type of reference to the iterator internal pointer.
Definition lists.h:875
pointer unlink_head(void)
Unlink the first element from the list.
Definition inlines.h:694
void link_head(reference node)
Add a node to the head of the list.
Definition inlines.h:628
constexpr ~intrusive_list()
Destruct the list.
Definition inlines.h:578
intrusive_list_iterator< T, N, MP, U > iterator
Type of iterator over the values.
Definition lists.h:870
void initialize_once(void)
Initialize the list only at first run.
Definition inlines.h:597
U value_type
Type of value pointed to by the iterator.
Definition lists.h:855
iterator end() const
Iterator begin.
Definition inlines.h:660
value_type & reference
Type of reference to object pointed to by the iterator.
Definition lists.h:865
ptrdiff_t difference_type
Type of pointer difference.
Definition lists.h:886
value_type * pointer
Type of pointer to object pointed to by the iterator.
Definition lists.h:860
pointer unlink_tail(void)
Unlink the last element from the list.
Definition inlines.h:708
pointer get_pointer(iterator_pointer node) const
Get the address of the node.
Definition inlines.h:676
The file with the implementations of the µOS++ lists inlined methods.
The µOS++ utilities definitions.
Definition inlines.h:46