BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
auto_list.h
Go to the documentation of this file.
1#ifndef BPMNOS_Execution_auto_list_H
2#define BPMNOS_Execution_auto_list_H
3
4#include <list>
5#include <tuple>
6#include <memory>
7#include "expired.h"
8
9namespace BPMNOS::Execution {
10
11/**
12 * @brief List of tuples with automatic removal of tuples containing an expired weak_ptr.
13 */
14template <typename... U>
15class auto_list {
16public:
17 struct iterator {
18 typename std::list< std::tuple< U... > >::iterator current;
20
21 iterator(typename std::list< std::tuple< U... > >::iterator iter, auto_list<U...>* cont)
22 : current(iter), container(cont) {
23 skipExpired();
24 }
25
27 ++current;
28 skipExpired();
29 return *this;
30 }
31
32 std::tuple< U... >& operator*() const {
33 return *current;
34 }
35
36 bool operator!=(const iterator& other) const {
37 return current != other.current;
38 }
39
40 private:
41 void skipExpired() {
42 while (current != container->tuples.end() && expired(*current)) {
43 current = container->tuples.erase(current);
44 }
45 }
46 };
47
49 return iterator(tuples.begin(), this);
50 }
51
53 return iterator(tuples.end(), this);
54 }
55
56 iterator cbegin() const {
57 return iterator(tuples.begin(), const_cast<auto_list<U...>*>(this));
58 }
59
60 iterator cend() const {
61 return iterator(tuples.end(), const_cast<auto_list<U...>*>(this));
62 }
63
64 iterator begin() const {
65 return iterator(tuples.begin(), const_cast<auto_list<U...>*>(this));
66 }
67
68 iterator end() const {
69 return iterator(tuples.end(), const_cast<auto_list<U...>*>(this));
70 }
71
72 void emplace_back(U... data) {
73 tuples.emplace_back(data...);
74 }
75
77 return iterator(tuples.erase(it.current), this);
78 }
79
80 template <typename T>
81 void remove(T* pointer) {
82 tuples.remove_if([pointer](const std::tuple< U... >& elements) {
83 auto& element = std::get< std::weak_ptr<T> >(elements);
84 if ( auto shared = element.lock() ) {
85 return shared.get() == pointer;
86 }
87 return false;
88 });
89 }
90
91 bool empty() const {
92 for ( [[maybe_unused]] auto& _ : *this ) {
93 return false;
94 }
95 return true;
96 }
97
98 void clear() {
99 tuples.clear();
100 }
101
102private:
103 mutable std::list< std::tuple< U... > > tuples;
104};
105
106} // namespace BPMNOS::Execution
107#endif // BPMNOS_Execution_auto_list_H
List of tuples with automatic removal of tuples containing an expired weak_ptr.
Definition auto_list.h:15
iterator cbegin() const
Definition auto_list.h:56
iterator cend() const
Definition auto_list.h:60
iterator begin() const
Definition auto_list.h:64
iterator erase(iterator it)
Definition auto_list.h:76
void remove(T *pointer)
Definition auto_list.h:81
void emplace_back(U... data)
Definition auto_list.h:72
bool expired(const std::tuple< U... > &t)
Determines whether any of the initial weak_ptr elements in a tuple is expired.
Definition expired.h:19
std::list< std::tuple< U... > >::iterator current
Definition auto_list.h:18
bool operator!=(const iterator &other) const
Definition auto_list.h:36
std::tuple< U... > & operator*() const
Definition auto_list.h:32
iterator(typename std::list< std::tuple< U... > >::iterator iter, auto_list< U... > *cont)
Definition auto_list.h:21