├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── examples ├── examples.pro └── main.cpp ├── libgitlevtbus.pro ├── src ├── gitldef.h ├── gitlevent.cpp ├── gitlevent.h ├── gitleventbus.cpp ├── gitleventbus.h ├── gitleventparam.cpp ├── gitleventparam.h ├── gitlmodule.cpp ├── gitlmodule.h ├── gitlmoduledelegate.cpp ├── gitlmoduledelegate.h └── src.pro └── test ├── TestGitlEvtBus ├── TestGitlEvtBus.pro └── testcase.cpp ├── TestMacro ├── TestMacro.pro └── testmacro.cpp └── test.pro /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | 15 | *.user -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: required 3 | 4 | before_install: 5 | - yes | sudo add-apt-repository ppa:ubuntu-sdk-team/ppa 6 | - sudo apt-get update -qq 7 | - sudo apt-get install qtdeclarative5-dev 8 | 9 | script: 10 | - qmake -qt=qt5 libgitlevtbus.pro -r "CONFIG+=release" 11 | - make 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | libgitlevtbus is Licensed using the revised BSD license. 2 | If you have any questions, contact Huang Li at 3 | 4 | 5 | Copyright (c) 2013, Huang Li , IIPL 6 | All rights reserved. 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | * Redistributions of source code must retain the above copyright notice, this list 10 | of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright notice, this list 12 | of conditions and the following disclaimer in the documentation and/or other 13 | materials provided with the distribution. 14 | * Neither the name of the IIPL nor the names of its contributors may be used 15 | to endorse or promote products derived from this software without specific prior 16 | written permission. 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============= 3 | [![Build Status](https://travis-ci.org/lheric/libgitlevtbus.png?branch=master)](https://travis-ci.org/lheric/libgitlevtbus) 4 | 5 | libgitlevtbus is an open-source event bus (or message bus) based on Qt under BSD lisence. 6 | 7 | Features 8 | ======== 9 | 1. Easy to use (c++11 feature supported: lambda expression, member function callback, ...) 10 | 2. Custom event support (carry custom parameters) 11 | 3. Events can be deliverd across threads 12 | 13 | Requirements 14 | ============ 15 | Qt 5.1.0 or later with MSVC, gcc or intel complier 16 | 17 | **c++11 support required** 18 | 19 | Quick start 20 | =========== 21 | See examples/examples.pro 22 | ```c++ 23 | #include "gitlmodule.h" 24 | #include 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | GitlModule cModule; 29 | 30 | /// subscribe to an event 31 | cModule.subscribeToEvtByName("I am a test event", 32 | [](GitlEvent& rcEvt)->bool 33 | { 34 | qDebug() << "Hello GitlEvtBus!"; 35 | return true; 36 | } 37 | ); 38 | 39 | GitlEvent cEvent("I am a test event"); ///< create an event 40 | cEvent.dispatch(); ///< dispatch 41 | /// output: "Hello GitlEvtBus!"*/ 42 | return 0; 43 | } 44 | ``` 45 | 46 | 47 | For more, you may refer to the code in test/testcase.cpp 48 | -------------------------------------------------------------------------------- /examples/examples.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2013-08-01T14:53:53 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core 8 | 9 | QT -= gui 10 | 11 | TARGET = examples 12 | CONFIG += console 13 | CONFIG += c++11 14 | CONFIG -= app_bundle 15 | 16 | TEMPLATE = app 17 | 18 | 19 | SOURCES += main.cpp 20 | 21 | INCLUDEPATH += ../../libgitlevtbus/src 22 | 23 | LIBS += -L$${OUT_PWD}/.. 24 | 25 | CONFIG(debug, debug|release){ 26 | LIBS += -lGitlEvtBusd 27 | } 28 | CONFIG(release, debug|release){ 29 | LIBS += -lGitlEvtBus 30 | } 31 | -------------------------------------------------------------------------------- /examples/main.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #include "gitlmodule.h" 27 | #include 28 | 29 | int main(int argc, char *argv[]) 30 | { 31 | Q_UNUSED(argc) 32 | Q_UNUSED(argv) 33 | 34 | GitlModule cModule; 35 | 36 | /// subscribe to an event 37 | cModule.subscribeToEvtByName("I am a test event", 38 | [](GitlEvent& rcEvt)->bool 39 | { 40 | Q_UNUSED(rcEvt) 41 | qDebug() << "Hello GitlEvtBus!"; 42 | return true; 43 | } 44 | ); 45 | 46 | GitlEvent cEvent("I am a test event"); ///< create an event 47 | cEvent.dispatch(); ///< dispatch 48 | /// output: "Hello GitlEvtBus!"*/ 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /libgitlevtbus.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = subdirs 2 | CONFIG += ordered 3 | SUBDIRS = src \ 4 | test \ 5 | examples 6 | -------------------------------------------------------------------------------- /src/gitldef.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #ifndef GITLDEF_H 27 | #define GITLDEF_H 28 | #include 29 | #include 30 | #include 31 | 32 | /*! concatenating multiple args into one*/ 33 | #define CONCATE(...) __VA_ARGS__ 34 | 35 | 36 | /*! Q_PROPERTY getter and setter generator*/ 37 | #define ADD_QPROP_RW_INIT(type, name, getter, setter, init) \ 38 | public: \ 39 | Q_PROPERTY(type name MEMBER m_##name READ getter WRITE setter NOTIFY name##Changed) \ 40 | type& getter() { return m_##name; } \ 41 | type const & getter() const{ return m_##name; } \ 42 | void setter(type name) { m_##name = name; emit name##Changed(name);} \ 43 | Q_SIGNAL void name##Changed(type& name); \ 44 | private: \ 45 | type m_##name = init; 46 | 47 | #define ADD_QPROP_RW(type, name, getter, setter) \ 48 | public: \ 49 | Q_PROPERTY(type name MEMBER m_##name READ getter WRITE setter NOTIFY name##Changed) \ 50 | type& getter() { return m_##name; } \ 51 | type const & getter() const{ return m_##name; } \ 52 | void setter(type name) { m_##name = name; emit name##Changed(name);} \ 53 | Q_SIGNAL void name##Changed(type& name); \ 54 | private: \ 55 | type m_##name; 56 | 57 | 58 | #define ADD_QPROP_RO_INIT(type, name, getter, init) \ 59 | public: \ 60 | Q_PROPERTY(type name MEMBER m_##name READ getter) \ 61 | type& getter() { return m_##name; } \ 62 | type const & getter() const{ return m_##name; } \ 63 | private: \ 64 | type m_##name = init; 65 | 66 | #define ADD_QPROP_RO(type, name, getter) \ 67 | public: \ 68 | Q_PROPERTY(type name MEMBER m_##name READ getter) \ 69 | type& getter() { return m_##name; } \ 70 | type const & getter() const{ return m_##name; } \ 71 | private: \ 72 | type m_##name; 73 | 74 | #define ADD_QPROP_PR_INIT(type, name, init) \ 75 | public: \ 76 | Q_PROPERTY(type name MEMBER m_##name) \ 77 | private: \ 78 | type m_##name = init; 79 | 80 | #define ADD_QPROP_PR(type, name) \ 81 | public: \ 82 | Q_PROPERTY(type name MEMBER m_##name) \ 83 | private: \ 84 | type m_##name; 85 | 86 | 87 | 88 | /*! getter and setter generator for class memeber */ 89 | #define ADD_FIELD_INIT(type, name, getter, setter, init) \ 90 | public: \ 91 | type& getter() { return m_##name; } \ 92 | type const & getter() const{ return m_##name; } \ 93 | void setter(type name) { m_##name = name; } \ 94 | private: \ 95 | type m_##name = init; 96 | 97 | #define ADD_FIELD(type, name, getter, setter) ADD_CLASS_FIELD(type, name, getter, setter) 98 | #define ADD_CLASS_FIELD(type, name, getter, setter) \ 99 | public: \ 100 | type& getter() { return m_##name; } \ 101 | type const & getter() const{ return m_##name; } \ 102 | void setter(type name) { m_##name = name; } \ 103 | private: \ 104 | type m_##name; 105 | 106 | #define ADD_FIELD_NOSETTER_INIT(type, name, getter, init) \ 107 | public: \ 108 | type& getter() { return m_##name; } \ 109 | type const & getter() const{ return m_##name; } \ 110 | private: \ 111 | type m_##name = init; 112 | 113 | #define ADD_FIELD_NOSETTER(type, name, getter) ADD_CLASS_FIELD_NOSETTER(type, name, getter) 114 | #define ADD_CLASS_FIELD_NOSETTER(type, name, getter) \ 115 | public: \ 116 | type& getter() { return m_##name; } \ 117 | type const & getter() const{ return m_##name; } \ 118 | private: \ 119 | type m_##name; 120 | 121 | #define ADD_FIELD_PRIVATE_INIT(type, name, init ) \ 122 | private: \ 123 | type m_##name = init; 124 | 125 | #define ADD_FIELD_PRIVATE(type, name ) ADD_CLASS_FIELD_PRIVATE(type, name ) 126 | #define ADD_CLASS_FIELD_PRIVATE(type, name ) \ 127 | private: \ 128 | type m_##name; 129 | 130 | 131 | /*! SINGLETON DESIGN PATTERN (Thread Safe) */ 132 | #define SINGLETON_PATTERN_DECLARE(classname)\ 133 | public: \ 134 | static classname* getInstance() { QMutexLocker cLocker(&m_cGetInstanceMutex); if(m_instance==NULL) m_instance=new classname(); return m_instance; } \ 135 | private: \ 136 | static classname* m_instance; \ 137 | static QMutex m_cGetInstanceMutex; 138 | 139 | #define SINGLETON_PATTERN_IMPLIMENT(classname)\ 140 | classname* classname::m_instance = NULL; \ 141 | QMutex classname::m_cGetInstanceMutex; 142 | 143 | /*! PROTOTYPE PATTERN */ 144 | #define CLONABLE(classname)\ 145 | public:\ 146 | virtual classname* clone() const { return new classname(*this); } 147 | 148 | 149 | /* CLIP c BETWEEN a AND b */ 150 | #define VALUE_CLIP(min,max,value) ( ((value)>(max))?(max):((value)<(min))?(min):(value) ) 151 | 152 | /* SCOPE GUARD C++11 Required*/ 153 | template 154 | struct ScopeExit { 155 | ScopeExit(F f) : f(f) {} 156 | ~ScopeExit() { f(); } 157 | F f; 158 | }; 159 | template 160 | ScopeExit MakeScopeExit(F f) { 161 | return ScopeExit(f); 162 | } 163 | #define SCOPE_EXIT(code) \ 164 | auto scope_exit_##__LINE__ = MakeScopeExit([=](){code;}) 165 | 166 | 167 | /*make callback functor*/ 168 | #define MAKE_CALLBACK(memberFunc) \ 169 | std::bind( &memberFunc, this, std::placeholders::_1 ) 170 | 171 | #define MAKE_CALLBACK_OBJ(object, memberFunc) \ 172 | std::bind( &memberFunc, &(object), std::placeholders::_1 ) 173 | 174 | 175 | 176 | #endif 177 | -------------------------------------------------------------------------------- /src/gitlevent.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #include "gitlevent.h" 27 | #include "gitlmodule.h" 28 | #include 29 | #include "gitleventbus.h" 30 | #include 31 | GitlEvent::GitlEvent( const QString& strEvtName ) 32 | { 33 | this->m_strEvtName = strEvtName; 34 | } 35 | 36 | 37 | GitlEvent::GitlEvent() 38 | { 39 | this->m_strEvtName = "UNKNOWN"; 40 | } 41 | 42 | bool GitlEvent::hasParameter(QString strParam) const 43 | { 44 | return m_cParameters.hasParameter(strParam); 45 | } 46 | 47 | QVariant GitlEvent::getParameter(const QString& strParam ) const 48 | { 49 | return m_cParameters.getParameter(strParam); 50 | } 51 | 52 | bool GitlEvent::setParameter(const QString& strParam, const QVariant& rvValue) 53 | { 54 | m_cParameters.setParameter(strParam, rvValue); 55 | return true; 56 | } 57 | 58 | void GitlEvent::dispatch(GitlEventBus* pcEventBus) const 59 | { 60 | if(pcEventBus == nullptr) 61 | GitlEventBus::getInstance()->post(*this); 62 | else 63 | pcEventBus->post(*this); 64 | } 65 | -------------------------------------------------------------------------------- /src/gitlevent.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #ifndef GITLEVENT_H 27 | #define GITLEVENT_H 28 | #include 29 | #include 30 | #include 31 | #include "gitldef.h" 32 | #include "gitleventparam.h" 33 | 34 | class GitlModule; 35 | class GitlEventBus; 36 | 37 | /*! 38 | * \brief The GitlEvent class represents an event. 39 | * If you want to create an custom event by inherit GitlEvent, you ***MUST*** 40 | * reimplement the 'clone' method in this class. This can be done by adding 41 | * VIRTUAL_COPY_PATTERN(subclassname) in the subclass. Otherwise the application 42 | * may crash 43 | */ 44 | class GitlEvent 45 | { 46 | /// virtual copy pattern, please add this macro to all the subclass 47 | CLONABLE(GitlEvent) 48 | 49 | public: 50 | GitlEvent( const QString& strEvtName ); 51 | GitlEvent(); 52 | virtual ~GitlEvent() {} 53 | 54 | /*! 55 | * \brief hasParameter if this event carries a specific parameter 56 | * \param strParam parameter name 57 | * \return 58 | */ 59 | bool hasParameter(QString strParam) const; 60 | 61 | /*! 62 | * \brief getParameter get the value of a specific parameter 63 | * \param strParam parameter name 64 | * \return parameter value, if it does not exist, return a default-constructed QVariant 65 | */ 66 | QVariant getParameter(const QString& strParam ) const; 67 | 68 | /*! 69 | * \brief setParameter set the value of a specific parameter 70 | * \param strParam parameter name 71 | * \param rvValue parameter value 72 | * \return 73 | */ 74 | bool setParameter(const QString& strParam, const QVariant& rvValue); 75 | 76 | 77 | /*! 78 | * \brief dispatch dispatch this event to event bus, all module subscribed to this event name will be notified. 79 | * \param pcEventBus If pcEventBus is NULL, it will find a global (default) event bus and post the event onto the bus. 80 | * Or you can specify another event bus. 81 | */ 82 | void dispatch(GitlEventBus *pcEventBus = NULL) const; 83 | 84 | protected: 85 | 86 | ADD_CLASS_FIELD(QString, strEvtName, getEvtName, setEvtName) ///< event name 87 | 88 | ADD_CLASS_FIELD_NOSETTER(GitlEventParam, cParameters, getParameters) ///< event parameters-value pair 89 | 90 | }; 91 | 92 | 93 | #endif // GITLEVENT_H 94 | -------------------------------------------------------------------------------- /src/gitleventbus.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #include "gitleventbus.h" 27 | #include 28 | 29 | SINGLETON_PATTERN_IMPLIMENT(GitlEventBus) 30 | 31 | GitlEventBus::GitlEventBus() 32 | { 33 | } 34 | 35 | GitlEventBus *GitlEventBus::create() 36 | { 37 | return new GitlEventBus(); 38 | } 39 | 40 | /*! connect a module to the event bus 41 | */ 42 | Q_DECLARE_METATYPE( QSharedPointer ) 43 | bool GitlEventBus::registerModule(GitlModuleDelegate* pcModule) 44 | { 45 | qRegisterMetaType< QSharedPointer >("QSharedPointer"); 46 | connect(this, SIGNAL(eventTriggered(QSharedPointer) ), 47 | pcModule, SLOT (detonate (QSharedPointer) ), 48 | Qt::AutoConnection ); 49 | 50 | return true; 51 | } 52 | 53 | bool GitlEventBus::unregisterModule(GitlModuleDelegate *pcModule) 54 | { 55 | return disconnect(this, nullptr, pcModule, nullptr); 56 | } 57 | 58 | 59 | /*! send event to event bus 60 | */ 61 | void GitlEventBus::post(const GitlEvent& rcEvt) const 62 | { 63 | QSharedPointer pcEvtCopy( rcEvt.clone() ); 64 | 65 | 66 | /// notify modules 67 | emit eventTriggered(pcEvtCopy); 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /src/gitleventbus.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #ifndef GITLEVENTBUS_H 27 | #define GITLEVENTBUS_H 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "gitldef.h" 35 | #include "gitlevent.h" 36 | #include "gitlmodule.h" 37 | 38 | 39 | class GitlModuleDelegate; 40 | /*! 41 | * \brief The GitlEventBus class represents the event bus 42 | */ 43 | class GitlEventBus : public QObject 44 | { 45 | Q_OBJECT 46 | private: 47 | GitlEventBus(); 48 | 49 | public: 50 | /*! 51 | * \brief create The safe way to explictly create a new event bus 52 | * \return 53 | */ 54 | static GitlEventBus *create(); 55 | 56 | /*! 57 | * \brief registerModule connect a module to the event bus 58 | * \param pcModule 59 | * \return 60 | */ 61 | bool registerModule(GitlModuleDelegate *pcModule); 62 | 63 | 64 | /*! 65 | * \brief unregisterModule disconncet a module from the event bus 66 | * \param pcModule 67 | * \return 68 | */ 69 | bool unregisterModule(GitlModuleDelegate *pcModule); 70 | 71 | public slots: 72 | /*! send event to event bus 73 | */ 74 | void post(const GitlEvent &rcEvt) const; 75 | 76 | signals: 77 | /*! message to send 78 | */ 79 | void eventTriggered( QSharedPointer pcEvt ) const; 80 | 81 | 82 | ///SINGLETON design pattern 83 | SINGLETON_PATTERN_DECLARE(GitlEventBus) 84 | 85 | }; 86 | 87 | #endif // GITLEVTBUS_H 88 | -------------------------------------------------------------------------------- /src/gitleventparam.cpp: -------------------------------------------------------------------------------- 1 | #include "gitleventparam.h" 2 | #include 3 | GitlEventParam::GitlEventParam() 4 | { 5 | } 6 | 7 | bool GitlEventParam::hasParameter(QString strParam) const 8 | { 9 | return m_cParameters.contains(strParam); 10 | } 11 | 12 | QVariant GitlEventParam::getParameter(const QString& strParam ) const 13 | { 14 | QVariant rvValue; 15 | if( m_cParameters.contains(strParam) ) 16 | { 17 | rvValue = m_cParameters[strParam]; 18 | } 19 | else 20 | { 21 | qWarning() << QString("Parameter %1 NOT found.").arg(strParam); 22 | } 23 | return rvValue; 24 | } 25 | 26 | bool GitlEventParam::setParameter(const QString& strParam, const QVariant& rvValue) 27 | { 28 | m_cParameters[strParam] = rvValue; 29 | return true; 30 | } 31 | -------------------------------------------------------------------------------- /src/gitleventparam.h: -------------------------------------------------------------------------------- 1 | #ifndef GITLEVENTPARAM_H 2 | #define GITLEVENTPARAM_H 3 | #include 4 | #include 5 | #include 6 | #include "gitldef.h" 7 | 8 | class GitlEventParam 9 | { 10 | public: 11 | GitlEventParam(); 12 | 13 | /*! 14 | * \brief hasParameter if this event carries a specific parameter 15 | * \param strParam parameter name 16 | * \return 17 | */ 18 | bool hasParameter(QString strParam) const; 19 | 20 | /*! 21 | * \brief getParameter get the value of a specific parameter 22 | * \param strParam parameter name 23 | * \return parameter value, if it does not exist, return a default-constructed QVariant 24 | */ 25 | QVariant getParameter(const QString& strParam ) const; 26 | 27 | /*! 28 | * \brief setParameter set the value of a specific parameter 29 | * \param strParam parameter name 30 | * \param rvValue parameter value 31 | * \return 32 | */ 33 | bool setParameter(const QString& strParam, const QVariant& rvValue); 34 | 35 | 36 | ADD_CLASS_FIELD_PRIVATE( CONCATE(QMap), cParameters) ///< parameters name-value pair 37 | 38 | }; 39 | 40 | #endif // GITLEVENTPARAM_H 41 | -------------------------------------------------------------------------------- /src/gitlmodule.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #include "gitlmodule.h" 27 | #include "gitleventbus.h" 28 | #include 29 | 30 | GitlModule::GitlModule(GitlEventBus *pcEventBus) : 31 | m_cDelegate(this, pcEventBus) 32 | { 33 | } 34 | 35 | void GitlModule::subscribeToEvtByName( const QString& strEvtName, 36 | const GitlCallBack& pfListener ) 37 | { 38 | return m_cDelegate.subscribeToEvtByName(strEvtName, pfListener); 39 | } 40 | 41 | void GitlModule::unsubscribeToEvtByName( const QString& strEvtName ) 42 | { 43 | return m_cDelegate.unsubscribeToEvtByName(strEvtName); 44 | } 45 | 46 | void GitlModule::dispatchEvt( GitlEvent& rcEvt ) 47 | { 48 | m_cDelegate.dispatchEvt(rcEvt); 49 | } 50 | 51 | void GitlModule::setModuleName( QString strModuleName ) 52 | { 53 | m_cDelegate.setModuleName(strModuleName); 54 | } 55 | 56 | GitlEventBus *GitlModule::getEventBus() 57 | { 58 | return m_cDelegate.getGitlEvtBus(); 59 | } 60 | 61 | void GitlModule::detach() 62 | { 63 | m_cDelegate.detach(); 64 | } 65 | 66 | void GitlModule::attach(GitlEventBus *pcEventBus) 67 | { 68 | m_cDelegate.attach(pcEventBus); 69 | } 70 | -------------------------------------------------------------------------------- /src/gitlmodule.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #ifndef GITLMODULE_H 27 | #define GITLMODULE_H 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include "gitldef.h" 34 | #include "gitlevent.h" 35 | 36 | #include "gitlmoduledelegate.h" 37 | 38 | class GitlEventBus; 39 | 40 | /*! 41 | * \brief The GitlModule class represents a module 42 | */ 43 | 44 | class GitlModule 45 | { 46 | public: 47 | /** 48 | * @brief GitlModule Represents a module in the event bus. It will keep listening to events 49 | * in the event bus and catch those it is interested in. 50 | * @param pcEventBus If pcEventBus it will find a gloabl event bus using singleton pattern. 51 | * Or you can specify an exsiting event bus. 52 | */ 53 | GitlModule(GitlEventBus* pcEventBus = NULL); 54 | 55 | /*! 56 | * \brief subscribeToEvtByName Subscribe to an event 57 | * \param strEvtName event name 58 | * \param pfListener listener callback function 59 | */ 60 | void subscribeToEvtByName(const QString& strEvtName, 61 | const GitlCallBack& pfListener ); 62 | 63 | /*! 64 | * \brief unsubscribeToEvtByName Unsubscribe to an event 65 | * \param strEvtName event name 66 | */ 67 | void unsubscribeToEvtByName( const QString& strEvtName ); 68 | 69 | /*! 70 | * \brief dispatchEvt Dispatch an event 71 | * \param rcEvt event 72 | */ 73 | void dispatchEvt(GitlEvent &rcEvt ); 74 | 75 | /*! 76 | * \brief setModuleName Set the name of this module. That's ok if you do not 77 | * give a name to this module. But for better debugging, we recommend you name it. 78 | * \param strModuleName name for this module 79 | */ 80 | void setModuleName(QString strModuleName ); 81 | 82 | /** 83 | * @brief getEventBus Get the event bus that this module is attached to 84 | * @return 85 | */ 86 | GitlEventBus* getEventBus(); 87 | 88 | /*! 89 | * \brief detach Detach the module 90 | */ 91 | void detach(); 92 | 93 | /*! 94 | * \brief attach Attach the module to a new event bus 95 | * \param pcEventBus 96 | */ 97 | void attach(GitlEventBus *pcEventBus); 98 | 99 | 100 | /// Delegate pattern 101 | /// Avoiding this class becoming a subclass of QObject 102 | /// (GUI class is based on QOBject, but QObject doesn't support virtual inheritance). 103 | ADD_CLASS_FIELD_PRIVATE( GitlModuleDelegate, cDelegate ) 104 | }; 105 | 106 | #endif // GITLMODULE_H 107 | -------------------------------------------------------------------------------- /src/gitlmoduledelegate.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #include "gitlmoduledelegate.h" 27 | #include "gitleventbus.h" 28 | #include 29 | #include 30 | using namespace std; 31 | GitlModuleDelegate::GitlModuleDelegate(GitlModule *pcDelegator, GitlEventBus* pcEventBus) 32 | { 33 | m_pcDelegator = pcDelegator; 34 | if(pcEventBus == nullptr) 35 | m_pcGitlEvtBus = GitlEventBus::getInstance(); 36 | else 37 | m_pcGitlEvtBus = pcEventBus; 38 | m_pcGitlEvtBus->registerModule(this); 39 | m_strModuleName = "undefined_module_name"; 40 | 41 | } 42 | 43 | 44 | 45 | void GitlModuleDelegate::subscribeToEvtByName(const QString& strEvtName, GitlCallBack pfListener ) 46 | { 47 | m_cListeningEvts.insert(strEvtName, pfListener); 48 | return; 49 | } 50 | 51 | 52 | void GitlModuleDelegate::unsubscribeToEvtByName( const QString& strEvtName ) 53 | { 54 | m_cListeningEvts.remove(strEvtName); 55 | } 56 | 57 | bool GitlModuleDelegate::detonate(QSharedPointer pcEvt ) 58 | { 59 | QMap>::iterator p = 60 | m_cListeningEvts.find(pcEvt->getEvtName()); 61 | 62 | if( p != m_cListeningEvts.end() ) 63 | (p.value())(*pcEvt.data()); 64 | return true; 65 | } 66 | 67 | bool GitlModuleDelegate::xIsListenToEvt( const QString& strEvtName ) 68 | { 69 | return m_cListeningEvts.contains(strEvtName); 70 | } 71 | 72 | void GitlModuleDelegate::dispatchEvt( const GitlEvent& rcEvt ) const 73 | { 74 | if(m_pcGitlEvtBus != nullptr) 75 | m_pcGitlEvtBus->post(rcEvt); 76 | } 77 | 78 | void GitlModuleDelegate::detach() 79 | { 80 | if(m_pcGitlEvtBus != nullptr) 81 | m_pcGitlEvtBus->unregisterModule(this); 82 | m_pcGitlEvtBus = nullptr; 83 | } 84 | 85 | void GitlModuleDelegate::attach(GitlEventBus *pcEventBus) 86 | { 87 | if(pcEventBus == nullptr) 88 | return; 89 | detach(); 90 | m_pcGitlEvtBus = pcEventBus; 91 | m_pcGitlEvtBus->registerModule(this); 92 | } 93 | -------------------------------------------------------------------------------- /src/gitlmoduledelegate.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #ifndef GITLMODULEDELEGATE_H 27 | #define GITLMODULEDELEGATE_H 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include "gitldef.h" 36 | #include "gitlevent.h" 37 | 38 | class GitlModule; 39 | class GitlEventBus; 40 | 41 | /// 42 | /// \brief GitlCallBack gitl event callback function 43 | /// 44 | typedef std::function GitlCallBack; 45 | 46 | class GitlModuleDelegate : public QObject 47 | { 48 | Q_OBJECT 49 | friend class GitlModule; 50 | private: 51 | explicit GitlModuleDelegate(GitlModule *pcDelegator, GitlEventBus *pcEventBus = NULL); 52 | 53 | public: 54 | /*! 55 | * \brief subscribeToEvtByName listening to an event by name 56 | * \param strEvtName event name 57 | */ 58 | void subscribeToEvtByName( const QString& strEvtName, 59 | GitlCallBack pfListener ); 60 | 61 | /*! 62 | * \brief subscribeToEvtByName not listening to an event by name 63 | * \param strEvtName event name 64 | */ 65 | void unsubscribeToEvtByName( const QString& strEvtName ); 66 | 67 | /*! 68 | * \brief dispatchEvt dispatch an event to subscribers 69 | * \param pcEvt event 70 | */ 71 | void dispatchEvt(const GitlEvent &rcEvt ) const; 72 | 73 | 74 | /*! 75 | * \brief detach Detach the module 76 | */ 77 | void detach(); 78 | 79 | 80 | /*! 81 | * \brief attach Attach the module to a new event bus 82 | * \param pcEventBus 83 | */ 84 | void attach(GitlEventBus *pcEventBus); 85 | 86 | public slots: 87 | /*! 88 | * \brief detonate notifyed by event bus 89 | * \param cEvt 90 | * \return 91 | */ 92 | bool detonate( QSharedPointer pcEvt ); 93 | 94 | protected: 95 | bool xIsListenToEvt(const QString& strEvtName); 96 | 97 | ADD_CLASS_FIELD( QString, strModuleName, getModuleName, setModuleName ) 98 | ADD_CLASS_FIELD_PRIVATE( CONCATE(QMap), cListeningEvts ) 99 | ADD_CLASS_FIELD_NOSETTER( GitlEventBus*, pcGitlEvtBus, getGitlEvtBus ) 100 | ADD_CLASS_FIELD_PRIVATE(GitlModule*, pcDelegator) 101 | 102 | }; 103 | 104 | #endif // GITLMODULEDELEGATE_H 105 | -------------------------------------------------------------------------------- /src/src.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2012-11-02T17:27:25 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core 8 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 9 | 10 | CONFIG += c++11 11 | CONFIG(debug, debug|release){ 12 | TARGET = GitlEvtBusd 13 | } 14 | CONFIG(release, debug|release){ 15 | TARGET = GitlEvtBus 16 | } 17 | 18 | DESTDIR = $${OUT_PWD}/.. 19 | 20 | # c++11 enalbed 21 | CONFIG += c++11 22 | CONFIG += console 23 | CONFIG += staticlib 24 | CONFIG -= app_bundle 25 | 26 | TEMPLATE = lib 27 | 28 | SOURCES += \ 29 | gitlevent.cpp \ 30 | gitleventbus.cpp \ 31 | gitlmoduledelegate.cpp \ 32 | gitlmodule.cpp \ 33 | gitleventparam.cpp 34 | 35 | HEADERS += \ 36 | gitldef.h \ 37 | gitlevent.h \ 38 | gitleventbus.h \ 39 | gitlmodule.h \ 40 | gitlmoduledelegate.h \ 41 | gitleventparam.h 42 | -------------------------------------------------------------------------------- /test/TestGitlEvtBus/TestGitlEvtBus.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2013-05-28T02:26:05 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core testlib 8 | 9 | QT -= gui 10 | 11 | TARGET = TestGitlEvtBus 12 | CONFIG += console 13 | CONFIG += c++11 14 | CONFIG -= app_bundle 15 | 16 | TEMPLATE = app 17 | 18 | INCLUDEPATH += ../../../libgitlevtbus/src 19 | 20 | SOURCES += \ 21 | testcase.cpp 22 | 23 | LIBS += -L$${OUT_PWD}/../.. 24 | 25 | CONFIG(debug, debug|release){ 26 | LIBS += -lGitlEvtBusd 27 | } 28 | CONFIG(release, debug|release){ 29 | LIBS += -lGitlEvtBus 30 | } 31 | 32 | HEADERS += 33 | -------------------------------------------------------------------------------- /test/TestGitlEvtBus/testcase.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * Copyright (c) 2013, Huang Li , IIPL 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright notice, this list 8 | * of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright notice, this list 10 | * of conditions and the following disclaimer in the documentation and/or other 11 | * materials provided with the distribution. 12 | * * Neither the name of the IIPL nor the names of its contributors may be used 13 | * to endorse or promote products derived from this software without specific prior 14 | * written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | *******************************************************************************************/ 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "gitldef.h" 34 | #include "gitlmodule.h" 35 | #include "gitleventbus.h" 36 | using namespace std; 37 | 38 | /// test event bus 39 | class TestModule : public GitlModule 40 | { 41 | public: 42 | TestModule(GitlEventBus* pcEventBus = NULL): 43 | GitlModule(pcEventBus) 44 | { 45 | this->m_bNotified = false; 46 | 47 | } 48 | 49 | void subscribeInsideClass() 50 | { 51 | subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK(TestModule::callback)); 52 | } 53 | 54 | bool callback( GitlEvent& rcEvt) 55 | { 56 | Q_UNUSED(rcEvt) 57 | this->m_bNotified = true; 58 | return true; 59 | } 60 | 61 | 62 | 63 | 64 | ADD_CLASS_FIELD(bool, bNotified, getNotified, setNotified) 65 | }; 66 | 67 | 68 | /// custom event 69 | class CustomEvent : public GitlEvent 70 | { 71 | CLONABLE(CustomEvent) 72 | public: 73 | CustomEvent( const QString& strEvtName ) : GitlEvent(strEvtName) { m_strCustomVar = "Custom String"; } 74 | ADD_CLASS_FIELD(QString, strCustomVar, getCustomVar, setCustomVar) 75 | }; 76 | 77 | class CustomEventListener : public GitlModule 78 | { 79 | public: 80 | CustomEventListener() 81 | { 82 | this->m_bNotified = false; 83 | } 84 | 85 | bool callback( GitlEvent& rcEvt) 86 | { 87 | CustomEvent& pcCusEvt = static_cast(rcEvt); 88 | this->m_bNotified = true; 89 | this->m_strCustomVar = pcCusEvt.getCustomVar(); 90 | return true; 91 | } 92 | 93 | 94 | 95 | ADD_CLASS_FIELD(bool, bNotified, getNotified, setNotified) 96 | ADD_CLASS_FIELD(QString, strCustomVar, getCustomVar, setCustomVar) 97 | }; 98 | 99 | 100 | 101 | /// test case 102 | class TestCase : public QObject 103 | { 104 | Q_OBJECT 105 | 106 | private slots: 107 | void lamdaListening() 108 | { 109 | TestModule cModule; 110 | cModule.subscribeToEvtByName("TEST_EVENT_1", 111 | [&](GitlEvent& e)->bool 112 | { 113 | Q_UNUSED(e) 114 | cModule.setNotified(true); 115 | return true; 116 | }); 117 | QVERIFY(!cModule.getNotified()); 118 | GitlEvent cEvt("TEST_EVENT_1"); 119 | cModule.dispatchEvt(cEvt); 120 | QVERIFY(cModule.getNotified()); 121 | } 122 | 123 | 124 | void listenInsideClass() 125 | { 126 | TestModule cModule; 127 | cModule.subscribeInsideClass(); 128 | QVERIFY(!cModule.getNotified()); 129 | GitlEvent cEvt("TEST_EVENT_1"); 130 | cEvt.dispatch(); 131 | QVERIFY(cModule.getNotified()); 132 | } 133 | 134 | void listenOutsideClass() 135 | { 136 | TestModule cModule; 137 | cModule.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule, TestModule::callback)); 138 | QVERIFY(!cModule.getNotified()); 139 | GitlEvent cEvt("TEST_EVENT_1"); 140 | cModule.dispatchEvt(cEvt); 141 | QVERIFY(cModule.getNotified()); 142 | } 143 | 144 | void unsubscribe() 145 | { 146 | TestModule cModule; 147 | cModule.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule, TestModule::callback)); 148 | cModule.unsubscribeToEvtByName("TEST_EVENT_1"); 149 | QVERIFY(!cModule.getNotified()); 150 | GitlEvent cEvt("TEST_EVENT_1"); 151 | cModule.dispatchEvt(cEvt); 152 | QVERIFY(!cModule.getNotified()); 153 | } 154 | 155 | 156 | 157 | void oneToMany() 158 | { 159 | TestModule cSender; 160 | TestModule cModule1; 161 | TestModule cModule2; 162 | TestModule cModule3; 163 | cModule1.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule1, TestModule::callback)); 164 | cModule2.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule2, TestModule::callback)); 165 | cModule3.subscribeToEvtByName("TEST_EVENT_2", MAKE_CALLBACK_OBJ(cModule3, TestModule::callback)); 166 | 167 | GitlEvent cEvt1("TEST_EVENT_1"); 168 | cSender.dispatchEvt(cEvt1); 169 | QVERIFY(cModule1.getNotified()); 170 | QVERIFY(cModule2.getNotified()); 171 | QVERIFY(!cModule3.getNotified()); 172 | 173 | GitlEvent cEvt2("TEST_EVENT_2"); 174 | cSender.dispatchEvt(cEvt2); 175 | QVERIFY(cModule3.getNotified()); 176 | 177 | } 178 | 179 | void customEventTest() 180 | { 181 | CustomEventListener cModule; 182 | cModule.subscribeToEvtByName("TEST_EVENT_1",MAKE_CALLBACK_OBJ(cModule, CustomEventListener::callback)); 183 | CustomEvent cEvt("TEST_EVENT_1"); 184 | cEvt.dispatch(); 185 | QVERIFY(cModule.getNotified()); 186 | QVERIFY(cModule.getCustomVar() == QString("Custom String")); 187 | } 188 | 189 | void multiplyEventBus() 190 | { 191 | GitlEventBus* pcBus1 = GitlEventBus::create(); TestModule cModule1(pcBus1); TestModule cModule2(pcBus1); 192 | GitlEventBus* pcBus2 = GitlEventBus::create(); TestModule cModule3(pcBus2); TestModule cModule4(pcBus2); 193 | 194 | /// all module are listening to the same events, but on different event buses. 195 | cModule1.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule1, TestModule::callback)); 196 | cModule2.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule2, TestModule::callback)); 197 | cModule3.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule3, TestModule::callback)); 198 | cModule4.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule4, TestModule::callback)); 199 | 200 | /// event 201 | CustomEvent cEvt("TEST_EVENT_1"); 202 | 203 | /// no one get notified because no module is attached to the default event bus 204 | cEvt.dispatch(); 205 | QVERIFY(!cModule1.getNotified()); 206 | QVERIFY(!cModule2.getNotified()); 207 | QVERIFY(!cModule3.getNotified()); 208 | QVERIFY(!cModule4.getNotified()); 209 | 210 | /// this will only notify module 1 & 2 211 | cEvt.dispatch(pcBus1); 212 | QVERIFY(cModule1.getNotified()); 213 | QVERIFY(cModule2.getNotified()); 214 | QVERIFY(!cModule3.getNotified()); 215 | QVERIFY(!cModule4.getNotified()); 216 | 217 | /// this will notify module 3 & 4 218 | cEvt.dispatch(cModule3.getEventBus()); 219 | QVERIFY(cModule3.getNotified()); 220 | QVERIFY(cModule4.getNotified()); 221 | 222 | /// make sure everyone is attached to the correct event bus 223 | QVERIFY(cModule1.getEventBus() == pcBus1); 224 | QVERIFY(cModule2.getEventBus() == pcBus1); 225 | QVERIFY(cModule3.getEventBus() == pcBus2); 226 | QVERIFY(cModule4.getEventBus() == pcBus2); 227 | 228 | /// 229 | TestModule cModule5(pcBus1); 230 | cModule5.subscribeToEvtByName("TEST_EVENT_1", MAKE_CALLBACK_OBJ(cModule5, TestModule::callback)); 231 | 232 | cEvt.dispatch(pcBus2); 233 | QVERIFY(!cModule5.getNotified()); 234 | 235 | cModule5.attach(pcBus2); 236 | cEvt.dispatch(pcBus2); 237 | QVERIFY(cModule5.getNotified()); 238 | 239 | } 240 | }; 241 | 242 | 243 | /// test main 244 | QTEST_MAIN(TestCase) 245 | #include "testcase.moc" 246 | 247 | -------------------------------------------------------------------------------- /test/TestMacro/TestMacro.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2013-05-28T02:26:05 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core testlib 8 | 9 | QT -= gui 10 | 11 | TARGET = TestMacro 12 | CONFIG += console 13 | CONFIG += c++11 14 | CONFIG -= app_bundle 15 | 16 | TEMPLATE = app 17 | 18 | INCLUDEPATH += ../../../libgitlevtbus/src 19 | 20 | SOURCES += \ 21 | testmacro.cpp 22 | 23 | LIBS += -L$${OUT_PWD}/.. 24 | 25 | -------------------------------------------------------------------------------- /test/TestMacro/testmacro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "gitldef.h" 6 | 7 | /// test case 8 | class ExampleClass : public QObject 9 | { 10 | Q_OBJECT 11 | 12 | 13 | 14 | public: 15 | ExampleClass() 16 | { 17 | } 18 | 19 | signals: 20 | 21 | ADD_QPROP_PR(int, iIntPR ) 22 | ADD_QPROP_PR_INIT(int, iIntPRInit, 123) 23 | 24 | ADD_QPROP_RO(QString, strRO, getRO) 25 | ADD_QPROP_RO_INIT(QString, strROInit, getROInit, "ROInit") 26 | 27 | ADD_QPROP_RW(QString, strRW, getRW, setRW) 28 | ADD_QPROP_RW_INIT(QString, strRWInit, getRWInit, setRWInit, "RWInit") 29 | 30 | ADD_FIELD(int, iIntField, getIntField, setIntField) 31 | ADD_FIELD_INIT(int, iIntFieldInit, getIntFieldInit, setIntFieldInit, 987) 32 | 33 | }; 34 | 35 | 36 | 37 | /// test macro 38 | class TestMacro: public QObject 39 | { 40 | Q_OBJECT 41 | public: 42 | TestMacro(){} 43 | 44 | private slots: 45 | 46 | void test_ADD_QPROP_PR() 47 | { 48 | ExampleClass exampleClass; 49 | QCOMPARE(exampleClass.property("iIntPR").isValid(), true); 50 | QCOMPARE(exampleClass.property("iIntPRInit").isValid(), true); 51 | QCOMPARE(exampleClass.property("iIntPRInit").toInt(), 123); 52 | 53 | QCOMPARE(exampleClass.property("strRO").isValid(), true); 54 | QCOMPARE(exampleClass.property("strROInit").isValid(), true); 55 | QCOMPARE(exampleClass.getRO(), QString("")); 56 | QCOMPARE(exampleClass.getROInit(), QString("ROInit")); 57 | 58 | QCOMPARE(exampleClass.property("strRW").isValid(), true); 59 | QCOMPARE(exampleClass.property("strRWInit").isValid(), true); 60 | QCOMPARE(exampleClass.getRW(), QString("")); 61 | QCOMPARE(exampleClass.getRWInit(), QString("RWInit")); 62 | exampleClass.getRWInit() = "Modified"; 63 | QCOMPARE(exampleClass.getRWInit(), QString("Modified")); 64 | 65 | exampleClass.getIntField(); 66 | QCOMPARE(exampleClass.getIntFieldInit(), 987); 67 | } 68 | 69 | void test_signals() 70 | { 71 | ExampleClass exampleClass; 72 | 73 | bool bTriggered = false; 74 | connect(&exampleClass, &ExampleClass::strRWInitChanged, [&](QString &strRWInitChanged){Q_UNUSED(strRWInitChanged); bTriggered = true;}); 75 | exampleClass.setProperty("strRWInit","changed"); 76 | 77 | QCOMPARE(exampleClass.getRWInit(), QString("changed")); 78 | 79 | } 80 | 81 | 82 | 83 | }; 84 | 85 | 86 | 87 | /// test main 88 | QTEST_MAIN(TestMacro) 89 | #include "testmacro.moc" 90 | -------------------------------------------------------------------------------- /test/test.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = subdirs 2 | CONFIG += ordered 3 | SUBDIRS = TestMacro\ 4 | TestGitlEvtBus 5 | --------------------------------------------------------------------------------