hydrogen 1.2.3
EventQueue.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#include <core/EventQueue.h>
24
25namespace H2Core
26{
27
28EventQueue* EventQueue::__instance = nullptr;
29
31{
32 if ( __instance == nullptr ) {
34 }
35}
36
37
39 : __read_index( 0 )
40 , __write_index( 0 )
41 , m_bSilent( false )
42{
43 __instance = this;
44
45 for ( int i = 0; i < MAX_EVENTS; ++i ) {
47 __events_buffer[ i ].value = 0;
48 }
49}
50
51
53{
54// infoLog( "DESTROY" );
55}
56
57
58void EventQueue::push_event( const EventType type, const int nValue )
59{
60 std::lock_guard< std::mutex > lock( m_mutex );
61 unsigned int nIndex = ++__write_index;
62 nIndex = nIndex % MAX_EVENTS;
63 Event ev;
64 ev.type = type;
65 ev.value = nValue;
66// INFOLOG( QString( "[pushEvent] %1 : %2 %3" ).arg( nIndex ).arg( ev.type ).arg( ev.value ) );
67
68 /* If the event queue is full, log an error. We could drop the old event, or the new event we're trying to
69 place. It's preferable to drop the oldest event in the queue, on the basis that many
70 change-of-state-events are probably no longer relevant or redundant based on newer events in the queue,
71 so we keep the new event. However, since the new event has overwritten the oldest event in the queue,
72 we also adjust the read pointer, otherwise pop_event would return this newest event on the next call,
73 then subsequent calls would get newer entries. */
74
75 if ( ! m_bSilent &&
77 ERRORLOG( QString( "Event queue full, lost event type %1 value %2" )
78 .arg( __events_buffer[nIndex].type )
79 .arg( __events_buffer[nIndex].value ));
81 }
82
83 __events_buffer[ nIndex ] = ev;
84
85}
86
87
89{
90 std::lock_guard< std::mutex > lock( m_mutex );
91 if ( __read_index == __write_index ) {
92 Event ev;
93 ev.type = EVENT_NONE;
94 ev.value = 0;
95 return ev;
96 }
97 unsigned int nIndex = ++__read_index;
98 nIndex = nIndex % MAX_EVENTS;
99// INFOLOG( QString( "[popEvent] %1 : %2 %3" ).arg( nIndex ).arg( __events_buffer[ nIndex ].type ).arg( __events_buffer[ nIndex ].value ) );
100 return __events_buffer[ nIndex ];
101}
102
103};
#define MAX_EVENTS
Maximum number of events to be stored in the H2Core::EventQueue::__events_buffer.
Definition EventQueue.h:33
#define ERRORLOG(x)
Definition Object.h:239
volatile unsigned int __write_index
Continuously growing number indexing the event, which has been written to the EventQueue most recentl...
Definition EventQueue.h:308
EventQueue()
Constructor of the EventQueue class.
void push_event(const EventType type, const int nValue)
Queues the next event into the EventQueue.
std::mutex m_mutex
Mutex to lock access to queue.
Definition EventQueue.h:320
static void create_instance()
If __instance equals 0, a new EventQueue singleton will be created and stored in it.
static EventQueue * __instance
Object holding the current EventQueue singleton.
Definition EventQueue.h:293
Event __events_buffer[MAX_EVENTS]
Array of all events contained in the EventQueue.
Definition EventQueue.h:315
bool m_bSilent
Whether or not to push log messages.
Definition EventQueue.h:323
Event pop_event()
Reads out the next event of the EventQueue.
volatile unsigned int __read_index
Continuously growing number indexing the event, which has been read from the EventQueue most recently...
Definition EventQueue.h:301
Basic building block for the communication between the core of Hydrogen and its GUI.
Definition EventQueue.h:186
EventType type
Specifies the context the event is create in and which function should be triggered to handle it.
Definition EventQueue.h:190
int value
Additional information to describe the actual context of the engine.
Definition EventQueue.h:193
EventType
Basic types of communication between the core part of Hydrogen and its GUI.
Definition EventQueue.h:40
@ EVENT_NONE
Fallback event.
Definition EventQueue.h:42