GlobalEvents: expose the internal class
Move the GlobalEvents::Monitor instance into class Instance. Eliminate all global variables.
This commit is contained in:
parent
b4d594eeff
commit
0e87ce4680
@ -19,35 +19,16 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "GlobalEvents.hxx"
|
#include "GlobalEvents.hxx"
|
||||||
#include "util/Manual.hxx"
|
|
||||||
#include "event/MaskMonitor.hxx"
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
namespace GlobalEvents {
|
inline void
|
||||||
class Monitor final : public MaskMonitor {
|
GlobalEvents::Monitor::Invoke(Event event)
|
||||||
public:
|
|
||||||
explicit Monitor(EventLoop &_loop):MaskMonitor(_loop) {}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void HandleMask(unsigned mask) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
static Manual<Monitor> monitor;
|
|
||||||
|
|
||||||
static Handler handlers[MAX];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invoke the callback for a certain event.
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
InvokeGlobalEvent(GlobalEvents::Event event)
|
|
||||||
{
|
{
|
||||||
assert((unsigned)event < GlobalEvents::MAX);
|
assert((unsigned)event < GlobalEvents::MAX);
|
||||||
assert(GlobalEvents::handlers[event] != nullptr);
|
assert(handlers[event] != nullptr);
|
||||||
|
|
||||||
GlobalEvents::handlers[event]();
|
handlers[event]();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -56,35 +37,22 @@ GlobalEvents::Monitor::HandleMask(unsigned f)
|
|||||||
for (unsigned i = 0; i < MAX; ++i)
|
for (unsigned i = 0; i < MAX; ++i)
|
||||||
if (f & (1u << i))
|
if (f & (1u << i))
|
||||||
/* invoke the event handler */
|
/* invoke the event handler */
|
||||||
InvokeGlobalEvent(Event(i));
|
Invoke(Event(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
GlobalEvents::Initialize(EventLoop &loop)
|
GlobalEvents::Monitor::Register(Event event, Handler callback)
|
||||||
{
|
|
||||||
monitor.Construct(loop);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
GlobalEvents::Deinitialize()
|
|
||||||
{
|
|
||||||
monitor.Destruct();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
GlobalEvents::Register(Event event, Handler callback)
|
|
||||||
{
|
{
|
||||||
assert((unsigned)event < MAX);
|
assert((unsigned)event < MAX);
|
||||||
assert(handlers[event] == nullptr);
|
|
||||||
|
|
||||||
handlers[event] = callback;
|
handlers[event] = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
GlobalEvents::Emit(Event event)
|
GlobalEvents::Monitor::Emit(Event event)
|
||||||
{
|
{
|
||||||
assert((unsigned)event < MAX);
|
assert((unsigned)event < MAX);
|
||||||
|
|
||||||
const unsigned mask = 1u << unsigned(event);
|
const unsigned mask = 1u << unsigned(event);
|
||||||
monitor->OrMask(mask);
|
OrMask(mask);
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#ifndef MPD_GLOBAL_EVENTS_HXX
|
#ifndef MPD_GLOBAL_EVENTS_HXX
|
||||||
#define MPD_GLOBAL_EVENTS_HXX
|
#define MPD_GLOBAL_EVENTS_HXX
|
||||||
|
|
||||||
class EventLoop;
|
#include "event/MaskMonitor.hxx"
|
||||||
|
|
||||||
namespace GlobalEvents {
|
namespace GlobalEvents {
|
||||||
enum Event {
|
enum Event {
|
||||||
@ -38,13 +38,25 @@ namespace GlobalEvents {
|
|||||||
|
|
||||||
typedef void (*Handler)();
|
typedef void (*Handler)();
|
||||||
|
|
||||||
void Initialize(EventLoop &loop);
|
class Monitor final : MaskMonitor {
|
||||||
|
Handler handlers[MAX];
|
||||||
|
|
||||||
void Deinitialize();
|
public:
|
||||||
|
explicit Monitor(EventLoop &_loop):MaskMonitor(_loop) {}
|
||||||
|
|
||||||
void Register(Event event, Handler handler);
|
void Register(Event event, Handler handler);
|
||||||
|
|
||||||
void Emit(Event event);
|
void Emit(Event event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Invoke the callback for a certain event.
|
||||||
|
*/
|
||||||
|
void Invoke(Event event);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void HandleMask(unsigned mask) override;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MAIN_NOTIFY_H */
|
#endif /* MAIN_NOTIFY_H */
|
||||||
|
@ -24,7 +24,8 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "Idle.hxx"
|
#include "Idle.hxx"
|
||||||
#include "GlobalEvents.hxx"
|
#include "Main.hxx"
|
||||||
|
#include "Instance.hxx"
|
||||||
#include "util/ASCII.hxx"
|
#include "util/ASCII.hxx"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
@ -58,7 +59,7 @@ idle_add(unsigned flags)
|
|||||||
unsigned old_flags = idle_flags.fetch_or(flags);
|
unsigned old_flags = idle_flags.fetch_or(flags);
|
||||||
|
|
||||||
if ((old_flags & flags) != flags)
|
if ((old_flags & flags) != flags)
|
||||||
GlobalEvents::Emit(GlobalEvents::IDLE);
|
instance->global_events.Emit(GlobalEvents::IDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "check.h"
|
#include "check.h"
|
||||||
#include "event/Loop.hxx"
|
#include "event/Loop.hxx"
|
||||||
|
#include "GlobalEvents.hxx"
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
||||||
@ -36,7 +37,6 @@ class Storage;
|
|||||||
class UpdateService;
|
class UpdateService;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class EventLoop;
|
|
||||||
class Error;
|
class Error;
|
||||||
class ClientList;
|
class ClientList;
|
||||||
struct Partition;
|
struct Partition;
|
||||||
@ -57,6 +57,8 @@ struct Instance final
|
|||||||
{
|
{
|
||||||
EventLoop event_loop;
|
EventLoop event_loop;
|
||||||
|
|
||||||
|
GlobalEvents::Monitor global_events;
|
||||||
|
|
||||||
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
||||||
NeighborGlue *neighbors;
|
NeighborGlue *neighbors;
|
||||||
#endif
|
#endif
|
||||||
@ -77,6 +79,8 @@ struct Instance final
|
|||||||
|
|
||||||
Partition *partition;
|
Partition *partition;
|
||||||
|
|
||||||
|
Instance():global_events(event_loop) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiate shutdown. Wrapper for EventLoop::Break().
|
* Initiate shutdown. Wrapper for EventLoop::Break().
|
||||||
*/
|
*/
|
||||||
|
@ -38,7 +38,6 @@
|
|||||||
#include "Idle.hxx"
|
#include "Idle.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
#include "LogInit.hxx"
|
#include "LogInit.hxx"
|
||||||
#include "GlobalEvents.hxx"
|
|
||||||
#include "input/Init.hxx"
|
#include "input/Init.hxx"
|
||||||
#include "event/Loop.hxx"
|
#include "event/Loop.hxx"
|
||||||
#include "IOThread.hxx"
|
#include "IOThread.hxx"
|
||||||
@ -520,8 +519,7 @@ static int mpd_main_after_fork(struct options options)
|
|||||||
try {
|
try {
|
||||||
Error error;
|
Error error;
|
||||||
|
|
||||||
GlobalEvents::Initialize(instance->event_loop);
|
instance->global_events.Register(GlobalEvents::IDLE, idle_event_emitted);
|
||||||
GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted);
|
|
||||||
|
|
||||||
if (!ConfigureFS(error)) {
|
if (!ConfigureFS(error)) {
|
||||||
LogError(error);
|
LogError(error);
|
||||||
@ -689,8 +687,6 @@ try {
|
|||||||
sticker_global_finish();
|
sticker_global_finish();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GlobalEvents::Deinitialize();
|
|
||||||
|
|
||||||
playlist_list_global_finish();
|
playlist_list_global_finish();
|
||||||
input_stream_global_finish();
|
input_stream_global_finish();
|
||||||
|
|
||||||
|
@ -67,13 +67,13 @@ Partition::SyncWithPlayer()
|
|||||||
void
|
void
|
||||||
Partition::OnPlayerSync()
|
Partition::OnPlayerSync()
|
||||||
{
|
{
|
||||||
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
|
instance.global_events.Emit(GlobalEvents::PLAYLIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Partition::OnPlayerTagModified()
|
Partition::OnPlayerTagModified()
|
||||||
{
|
{
|
||||||
GlobalEvents::Emit(GlobalEvents::TAG);
|
instance.global_events.Emit(GlobalEvents::TAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -26,7 +26,6 @@
|
|||||||
#include "PlaylistGlobal.hxx"
|
#include "PlaylistGlobal.hxx"
|
||||||
#include "Main.hxx"
|
#include "Main.hxx"
|
||||||
#include "Instance.hxx"
|
#include "Instance.hxx"
|
||||||
#include "GlobalEvents.hxx"
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
playlist_tag_event(void)
|
playlist_tag_event(void)
|
||||||
@ -43,6 +42,6 @@ playlist_event(void)
|
|||||||
void
|
void
|
||||||
playlist_global_init()
|
playlist_global_init()
|
||||||
{
|
{
|
||||||
GlobalEvents::Register(GlobalEvents::TAG, playlist_tag_event);
|
instance->global_events.Register(GlobalEvents::TAG, playlist_tag_event);
|
||||||
GlobalEvents::Register(GlobalEvents::PLAYLIST, playlist_event);
|
instance->global_events.Register(GlobalEvents::PLAYLIST, playlist_event);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user