Hydra 0.20
|
00001 00002 /* 00003 * Copyright (c) 2010 Aleksander B. Demko 00004 * This source code is distributed under the MIT license. 00005 * See the accompanying file LICENSE.MIT.txt for details. 00006 */ 00007 00008 #ifndef __INCLUDED_HYDRA_DESKTOP_RUNNABLEEVENT_H__ 00009 #define __INCLUDED_HYDRA_DESKTOP_RUNNABLEEVENT_H__ 00010 00011 #include <QObject> 00012 #include <QEvent> 00013 #include <QRunnable> 00014 00015 #include <hydra/TR1.h> 00016 00017 namespace desktop 00018 { 00019 class RunnableEventProcessor; 00020 class RunnableEvent; 00021 class RunnableEventFunction; 00022 } 00023 00024 // in the future, make this also a decendant of QRunnable 00025 // (or has a QRunnable member that it calls?) 00026 00027 class desktop::RunnableEventProcessor : public QObject 00028 { 00029 public: 00030 RunnableEventProcessor(void); 00031 virtual ~RunnableEventProcessor(); 00032 00033 static RunnableEventProcessor * instance(void) { return dm_instance; } 00034 00035 protected: 00036 virtual void customEvent(QEvent * event); 00037 00038 private: 00039 static RunnableEventProcessor *dm_instance; 00040 }; 00041 00042 /** 00043 * This is the base class of events that have run methods. 00044 * The run method will be run when the event is processed. 00045 * 00046 * The receiver (a global instance RunnableEventProcessor) 00047 * of these events has a customEvent() implementation that calls 00048 * RunnableEvent::customEvent. 00049 * 00050 * @author Aleksander Demko 00051 */ 00052 class desktop::RunnableEvent : public QEvent 00053 { 00054 public: 00055 RunnableEvent(void); 00056 00057 /// the run method that will be called by the customEvent 00058 /// handler 00059 virtual void run(void) = 0; 00060 00061 /** 00062 * Gets the next priority value in an always incrementing numeric sequence. 00063 * Not mutex protected, call only in the main thread. 00064 * 00065 * @author Aleksander Demko 00066 */ 00067 static int nextPriority(void); 00068 }; 00069 00070 /** 00071 * An object who's run() method will call the given function. 00072 * 00073 * This class can be used where QRunnable's are needed 00074 * or QEvent's (via the RunnableEvent base). 00075 * 00076 * @author Aleksander Demko 00077 */ 00078 class desktop::RunnableEventFunction : public RunnableEvent, public QRunnable 00079 { 00080 public: 00081 RunnableEventFunction(std::function<void()> f); 00082 00083 virtual void run(void); 00084 00085 /** 00086 * Queues the function, via a new RunnableEventFunction to the main 00087 * gui thread via QCoreApplication::postEvent 00088 * 00089 * @author Aleksander Demko 00090 */ 00091 static void enqueueMain(std::function<void()> f); 00092 00093 /** 00094 * Queues a function to the background worker thread pool. 00095 * 00096 * @author Aleksander Demko 00097 */ 00098 static void enqueueWorker(std::function<void()> f, int priority = 0); 00099 00100 private: 00101 std::function<void()> dm_f; 00102 }; 00103 00104 #endif 00105