Instance.hxx 3.16 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/MaskMonitor.hxx"
26
#include "Compiler.h"
27

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

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

40 41
class ClientList;
struct Partition;
42
class StateFile;
43

44 45 46 47 48 49 50 51
/**
 * 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;
};

52
struct Instance final
53
	: EventLoopHolder
54
#if defined(ENABLE_DATABASE) || defined(ENABLE_NEIGHBOR_PLUGINS)
55
	,
56 57 58
#endif
#ifdef ENABLE_DATABASE
	public DatabaseListener
59
#ifdef ENABLE_NEIGHBOR_PLUGINS
60 61 62 63 64
	,
#endif
#endif
#ifdef ENABLE_NEIGHBOR_PLUGINS
	public NeighborListener
65 66
#endif
{
67
	MaskMonitor idle_monitor;
68

69 70 71 72
#ifdef ENABLE_NEIGHBOR_PLUGINS
	NeighborGlue *neighbors;
#endif

73
#ifdef ENABLE_DATABASE
74 75
	Database *database;

76 77 78 79
	/**
	 * This is really a #CompositeStorage.  To avoid heavy include
	 * dependencies, we declare it as just #Storage.
	 */
80
	Storage *storage = nullptr;
81

82
	UpdateService *update = nullptr;
83
#endif
84

85 86 87 88
	ClientList *client_list;

	Partition *partition;

89 90
	StateFile *state_file;

91
	Instance()
92
		:idle_monitor(event_loop, BIND_THIS_METHOD(OnIdle)), state_file(nullptr) {}
93

94 95 96
	/**
	 * Initiate shutdown.  Wrapper for EventLoop::Break().
	 */
97 98 99
	void Shutdown() {
		event_loop.Break();
	}
100

101 102 103 104
	void EmitIdle(unsigned mask) {
		idle_monitor.OrMask(mask);
	}

105
#ifdef ENABLE_DATABASE
106 107 108 109 110
	/**
	 * Returns the global #Database instance.  May return nullptr
	 * if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
111 112 113
	Database *GetDatabase() {
		return database;
	}
114 115 116 117 118 119 120

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

123
private:
124
#ifdef ENABLE_DATABASE
125
	void OnDatabaseModified() override;
126
	void OnDatabaseSongRemoved(const char *uri) override;
127
#endif
128 129 130

#ifdef ENABLE_NEIGHBOR_PLUGINS
	/* virtual methods from class NeighborListener */
131 132
	void FoundNeighbor(const NeighborInfo &info) override;
	void LostNeighbor(const NeighborInfo &info) override;
133
#endif
134

135 136
	/* callback for #idle_monitor */
	void OnIdle(unsigned mask);
137 138 139
};

#endif