summaryrefslogtreecommitdiff
path: root/server/safe-list.hpp
blob: 77e2807ce8f2eede79705ea33586f90ced5a0f7a (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
   Copyright (C) 2020 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

/* Implementation of a list with more "safe" iterators.
 * Specifically the item under an iterator can be removed while scanning
 * the list. This to allow objects in the list to delete themselves from
 * the list.
 */
#pragma once

#include <memory>
#include <forward_list>
#include <algorithm>
#include <glib.h>

#include "push-visibility.h"

namespace red {

template <class T>
struct Mallocator
{
    typedef T value_type;
    Mallocator() = default;
    template <class U>
    constexpr Mallocator(const Mallocator<U>&) noexcept
    {
    }
    T* allocate(size_t n)
    {
        return static_cast<T*>(g_malloc_n(n, sizeof(T)));
    }
    void deallocate(T* p, size_t) noexcept
    {
        g_free(p);
    }
};

template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&)
{
    return true;
}

template <class T, class U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&)
{
    return false;
}


template <typename T>
class safe_list
{
    typedef typename std::forward_list<T> wrapped;
    typename std::forward_list<T,Mallocator<T>> list;
    class iterator;
public:
    void push_front(const T& v)
    {
        list.push_front(v);
    }
    void remove(const T& v)
    {
        list.remove(v);
    }
    void clear()
    {
        list.clear();
    }
    void pop_front()
    {
        list.pop_front();
    }
    iterator begin() noexcept
    {
        return iterator(list.begin());
    }
    iterator end() noexcept
    {
        return iterator(list.end());
    }
    size_t size()
    {
        return std::distance(begin(), end());
    }
    bool empty() const
    {
        return list.empty();
    }
};

template <typename T>
class safe_list<T>::iterator
{
    typedef typename std::forward_list<T,Mallocator<T>>::iterator wrapped;
    wrapped curr, next;
public:
    using iterator_category = std::forward_iterator_tag;
    using value_type = T;
    using difference_type = ptrdiff_t;
    using pointer = const value_type*;
    using reference = const value_type&;

    iterator(wrapped init_curr) :
        curr(init_curr),
        next(init_curr != wrapped() ? ++init_curr : wrapped())
    {
    }
    iterator& operator++()
    {
        curr = next;
        if (next != wrapped()) {
            ++next;
        }
        return *this;
    }
    iterator operator++(int)
    {
        iterator tmp(*this);
        operator++();
        return tmp;
    }
    bool operator==(const iterator& rhs) const
    {
        return curr == rhs.curr;
    }
    bool operator!=(const iterator& rhs) const
    {
        return curr != rhs.curr;
    }
    T& operator*()
    {
        return *curr;
    }
};

} // namespace red

#include "pop-visibility.h"