Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
messagehandler.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //======================================================================
21 // Message handler
22 //======================================================================
23 
24 #ifndef MESSAGEHANDLER_H
25 #define MESSAGEHANDLER_H
26 
27 #include <list>
28 #include <queue>
29 #include <memory>
30 #include <mutex>
31 #include "messagelistener.h"
32 
44 {
45 public:
47  template <class msgclass, class... Args>
48  static void send(Args&&... args) {
49  // this function has to be implemented in the header, since it is static template (linker errors elswise!)
50  getSingleton().addMessage(std::make_shared<msgclass>(args...), false);
51  }
53  static void send(Message::MessageTypes type) {send<Message>(type);}
54 
56  template <class msgclass, class... Args>
57  static void sendUnique(Args&&... args) {
58  // this function has to be implemented in the header, since it is static template (linker errors elswise!)
59  getSingleton().addMessage(std::make_shared<msgclass>(args...), true);
60  }
62  static void sendUnique(Message::MessageTypes type) {sendUnique<Message>(type);}
63 private:
66 
67 public:
68  ~MessageHandler();
69 
70  static MessageHandler &getSingleton();
72 
73  void process();
74 
75  void addListener(MessageListener *listener);
76  void removeListener(MessageListener *listener);
77  void addMessage(MessagePtr message, bool dropOlder = false);
78 
79 private:
80 
82  std::list<MessageListener*> mListeners;
83  std::list<MessageListener*> mListenersToAdd;
84  std::list<MessageListener*> mListenersToRemove;
85  mutable std::mutex mListenersChangesMutex;
86  std::list<MessagePtr> mMessages;
87  mutable std::mutex mMessageMutex;
88 };
89 
90 #endif // MESSAGEHANDLER_H
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
MessageTypes
Available message types:
Definition: message.h:47
static MessageHandler & getSingleton()
get a reference to the singleton class
MessageHandler()
private constructor since this class is a singleton
static void send(Message::MessageTypes type)
short function for creating and sending a simple message
static void send(Args &&...args)
short function for creating and sending a message
std::list< MessagePtr > mMessages
Queue of messages to be submitted.
std::mutex mMessageMutex
Mutex for accessing the queue.
void addMessage(MessagePtr message, bool dropOlder=false)
Submit a message.
void addListener(MessageListener *listener)
Connect a new message listener.
std::list< MessageListener * > mListenersToRemove
List of listeners to remove in the next frame.
static MessageHandler mSingleton
Singleton instance.
static void sendUnique(Message::MessageTypes type)
short function for creating and sending a simple message
std::list< MessageListener * > mListeners
List of all listeners.
void process()
Main task, processing the events in the queue.
std::list< MessageListener * > mListenersToAdd
List of listeners to add in the next frame.
Class for handling and sending messages.
std::mutex mListenersChangesMutex
Mutex for accessing the listeners list.
static void sendUnique(Args &&...args)
short function for creating and sending a message and drop older messages of the same type so that th...
static MessageHandler * getSingletonPtr()
get a pointer to the singleton class
~MessageHandler()
Empty desctructor.
void removeListener(MessageListener *listener)
Disconnect a message listener.