Remove.hxx 1.77 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#ifndef MPD_UPDATE_REMOVE_HXX
#define MPD_UPDATE_REMOVE_HXX
22 23

#include "check.h"
24
#include "event/DeferEvent.hxx"
25
#include "thread/Mutex.hxx"
26
#include "util/Compiler.h"
27

28 29 30
#include <forward_list>
#include <string>

31
struct Song;
32
class DatabaseListener;
33 34

/**
35 36
 * This class handles #Song removal.  It defers the action to the main
 * thread to ensure that all references to the #Song are gone.
37
 */
38
class UpdateRemoveService final {
39 40
	DatabaseListener &listener;

41
	Mutex mutex;
42

43
	std::forward_list<std::string> uris;
44

45 46
	DeferEvent defer;

47
public:
48
	UpdateRemoveService(EventLoop &_loop, DatabaseListener &_listener)
49 50
		:listener(_listener),
		 defer(_loop, BIND_THIS_METHOD(RunDeferred)) {}
51 52 53 54 55 56 57

	/**
	 * Sends a signal to the main thread which will in turn remove
	 * the song: from the sticker database and from the playlist.
	 * This serialized access is implemented to avoid excessive
	 * locking.
	 */
58
	void Remove(std::string &&uri);
59 60

private:
61 62
	/* DeferEvent callback */
	void RunDeferred() noexcept;
63
};
64 65

#endif