//! Copyright (C) 2013 Roland Winklmeier //! This Source Code Form is subject to the terms of the Mozilla Public //! License, v. 2.0. If a copy of the MPL was not distributed with this //! file, You can obtain one at http://mozilla.org/MPL/2.0/ #ifndef MESSAGE_HANDLER_H #define MESSAGE_HANDLER_H #include "blackmisc/message_dispatcher.h" #include "blackmisc/message.h" #include "blackmisc/debug.h" #include "blackmisc/type_info.h" #include namespace BlackMisc { class IFunctionHandler { public: virtual ~IFunctionHandler() {} void exec (const IMessage* message) { call (message); } private: virtual void call (const IMessage*) = 0; }; template class MemberFunctionHandler : public IFunctionHandler { public: typedef void (T::*MemberFunction)(MessageT*); MemberFunctionHandler(T* instance, MemberFunction memfunc) : m_instance(instance), m_function(memfunc) {} void call(const IMessage* message) { (m_instance->*m_function)(static_cast(message)); } private: T* m_instance; MemberFunction m_function; }; class CMessageHandler { public: ~CMessageHandler(); void handleMessage(const IMessage* message); template void registerMessageFunction(T*, void (T::*memfunc)(MessageT*)); private: typedef QMap TFunctionHandlerMap; TFunctionHandlerMap m_messagehandler; }; template void CMessageHandler::registerMessageFunction(T* obj, void (T::*memfunc)(MessageT*)) { CTypeInfo typeinfo = CTypeInfo(typeid(MessageT)); m_messagehandler[typeinfo]= new MemberFunctionHandler(obj, memfunc); CMessageDispatcher::getInstance().registerClass(obj, typeinfo); } } // namespace BlackMisc #endif // MESSAGE_HANDLER_H