Cancellable.hxx 3.43 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_NFS_CANCELLABLE_HXX
#define MPD_NFS_CANCELLABLE_HXX

23
#include "util/Compiler.h"
24

25 26
#include <boost/intrusive/list.hpp>

27
#include <algorithm>
28
#include <cassert>
29 30

template<typename T>
31 32
class CancellablePointer
	: public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
33
public:
34
	typedef T *pointer;
35 36
	typedef T &reference;
	typedef const T &const_reference;
37 38

private:
39
	pointer p;
40 41

public:
42
	explicit CancellablePointer(reference _p):p(&_p) {}
43 44 45 46 47 48 49 50 51 52 53 54 55

	CancellablePointer(const CancellablePointer &) = delete;

	constexpr bool IsCancelled() const {
		return p == nullptr;
	}

	void Cancel() {
		assert(!IsCancelled());

		p = nullptr;
	}

56
	reference Get() {
57 58 59 60 61
		assert(p != nullptr);

		return *p;
	}

62
	constexpr bool Is(const_reference other) const {
63 64 65 66 67 68 69
		return p == &other;
	}
};

template<typename T, typename CT=CancellablePointer<T>>
class CancellableList {
public:
70 71
	typedef typename CT::reference reference;
	typedef typename CT::const_reference const_reference;
72 73

private:
74 75
	typedef boost::intrusive::list<CT,
				       boost::intrusive::constant_time_size<false>> List;
76 77 78 79 80
	typedef typename List::iterator iterator;
	typedef typename List::const_iterator const_iterator;
	List list;

	class MatchPointer {
81
		const_reference p;
82 83

	public:
84
		explicit constexpr MatchPointer(const_reference _p)
85 86 87 88 89 90 91 92
			:p(_p) {}

		constexpr bool operator()(const CT &a) const {
			return a.Is(p);
		}
	};

	gcc_pure
93
	iterator Find(reference p) noexcept {
94 95 96 97
		return std::find_if(list.begin(), list.end(), MatchPointer(p));
	}

	gcc_pure
98
	const_iterator Find(const_reference p) const noexcept {
99 100 101 102
		return std::find_if(list.begin(), list.end(), MatchPointer(p));
	}

	gcc_pure
103
	iterator Find(CT &c) noexcept {
104
		return list.iterator_to(c);
105 106 107
	}

	gcc_pure
108
	const_iterator Find(const CT &c) const noexcept {
109
		return list.iterator_to(c);
110 111 112 113 114
	}

public:
#ifndef NDEBUG
	gcc_pure
115
	bool IsEmpty() const noexcept {
116
		return std::all_of(list.begin(), list.end(), [](const auto &c) { return c.IsCancelled(); });
117 118 119 120
	}
#endif

	gcc_pure
121
	bool Contains(const_reference p) const noexcept {
122 123 124 125
		return Find(p) != list.end();
	}

	template<typename... Args>
126
	CT &Add(reference p, Args&&... args) {
127 128
		assert(Find(p) == list.end());

129 130 131
		CT *c = new CT(p, std::forward<Args>(args)...);
		list.push_back(*c);
		return *c;
132 133 134 135 136 137 138
	}

	void Remove(CT &ct) {
		auto i = Find(ct);
		assert(i != list.end());

		list.erase(i);
139
		delete &ct;
140 141
	}

142
	void Cancel(reference p) {
143 144 145 146 147
		auto i = Find(p);
		assert(i != list.end());

		i->Cancel();
	}
148

149
	CT &Get(reference p) noexcept {
150 151 152 153 154
		auto i = Find(p);
		assert(i != list.end());

		return *i;
	}
155 156 157 158 159 160

	template<typename F>
	void ForEach(F &&f) {
		for (CT &i : list)
			f(i);
	}
161 162 163
};

#endif