Instance.hxx 2.95 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
class Error;
41 42
class ClientList;
struct Partition;
43
class StateFile;
44

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

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

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

74
#ifdef ENABLE_DATABASE
75 76
	Database *database;

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

83
	UpdateService *update = nullptr;
84
#endif
85

86 87 88 89
	ClientList *client_list;

	Partition *partition;

90 91
	StateFile *state_file;

92
	Instance()
93
		:idle_monitor(event_loop, *this, &Instance::OnIdle) {}
94

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

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

106
#ifdef ENABLE_DATABASE
107 108 109 110 111 112
	/**
	 * Returns the global #Database instance.  May return nullptr
	 * if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
	Database *GetDatabase(Error &error);
113 114
#endif

115
private:
116
#ifdef ENABLE_DATABASE
117
	void OnDatabaseModified() override;
118
	void OnDatabaseSongRemoved(const char *uri) override;
119
#endif
120 121 122

#ifdef ENABLE_NEIGHBOR_PLUGINS
	/* virtual methods from class NeighborListener */
123 124
	void FoundNeighbor(const NeighborInfo &info) override;
	void LostNeighbor(const NeighborInfo &info) override;
125
#endif
126

127 128
	/* callback for #idle_monitor */
	void OnIdle(unsigned mask);
129 130 131
};

#endif