Instance.hxx 3.36 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 20 21 22 23
 * 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_INSTANCE_HXX
#define MPD_INSTANCE_HXX

#include "check.h"
24
#include "event/Loop.hxx"
25
#include "event/Thread.hxx"
26
#include "event/MaskMonitor.hxx"
27
#include "Compiler.h"
28

29 30 31 32 33
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Listener.hxx"
class NeighborGlue;
#endif

34 35
#ifdef ENABLE_DATABASE
#include "db/DatabaseListener.hxx"
36
class Database;
37
class Storage;
38
class UpdateService;
39 40
#endif

41 42
#include <list>

43 44
class ClientList;
struct Partition;
45
class StateFile;
46

47 48 49 50 51 52 53 54
/**
 * A utility class which, when used as the first base class, ensures
 * that the #EventLoop gets initialized before the other base classes.
 */
struct EventLoopHolder {
	EventLoop event_loop;
};

55
struct Instance final
56
	: EventLoopHolder
57
#if defined(ENABLE_DATABASE) || defined(ENABLE_NEIGHBOR_PLUGINS)
58
	,
59 60 61
#endif
#ifdef ENABLE_DATABASE
	public DatabaseListener
62
#ifdef ENABLE_NEIGHBOR_PLUGINS
63 64 65 66 67
	,
#endif
#endif
#ifdef ENABLE_NEIGHBOR_PLUGINS
	public NeighborListener
68 69
#endif
{
70 71
	EventThread io_thread;

72
	MaskMonitor idle_monitor;
73

74 75 76 77
#ifdef ENABLE_NEIGHBOR_PLUGINS
	NeighborGlue *neighbors;
#endif

78
#ifdef ENABLE_DATABASE
79 80
	Database *database;

81 82 83 84
	/**
	 * This is really a #CompositeStorage.  To avoid heavy include
	 * dependencies, we declare it as just #Storage.
	 */
85
	Storage *storage = nullptr;
86

87
	UpdateService *update = nullptr;
88
#endif
89

90 91
	ClientList *client_list;

92
	std::list<Partition> partitions;
93

94
	StateFile *state_file = nullptr;
95

96
	Instance();
97

98 99 100
	/**
	 * Initiate shutdown.  Wrapper for EventLoop::Break().
	 */
101 102 103
	void Shutdown() {
		event_loop.Break();
	}
104

105 106 107 108
	void EmitIdle(unsigned mask) {
		idle_monitor.OrMask(mask);
	}

109 110 111 112 113
	/**
	 * Find a #Partition with the given name.  Returns nullptr if
	 * no such partition was found.
	 */
	gcc_pure
114
	Partition *FindPartition(const char *name) noexcept;
115

116
#ifdef ENABLE_DATABASE
117 118 119 120 121
	/**
	 * Returns the global #Database instance.  May return nullptr
	 * if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
122 123 124
	Database *GetDatabase() {
		return database;
	}
125 126 127 128 129 130 131

	/**
	 * Returns the global #Database instance.  Throws
	 * DatabaseError if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
	const Database &GetDatabaseOrThrow() const;
132 133
#endif

134
private:
135
#ifdef ENABLE_DATABASE
136
	void OnDatabaseModified() override;
137
	void OnDatabaseSongRemoved(const char *uri) override;
138
#endif
139 140 141

#ifdef ENABLE_NEIGHBOR_PLUGINS
	/* virtual methods from class NeighborListener */
142 143
	void FoundNeighbor(const NeighborInfo &info) noexcept override;
	void LostNeighbor(const NeighborInfo &info) noexcept override;
144
#endif
145

146 147
	/* callback for #idle_monitor */
	void OnIdle(unsigned mask);
148 149 150
};

#endif