#ifndef MESSAGEMANAGER_H #define MESSAGEMANAGER_H #include #include #include #include #include #include #include #ifdef ANDROID #include #else #include #endif class MessageManager { #ifdef ANDROID using ParamEntry = boost::any; using BadEntryCast = boost::bad_any_cast; #else using ParamEntry = std::any; using BadEntryCast = std::bad_any_cast; #endif using AsyncParam = std::pair; using Executor = std::function; public: template class Message { public: explicit constexpr Message(const char *topic) : topic(topic) { } std::string_view topic; }; MessageManager(); ~MessageManager(); /** * @brief std::bind() is not recommended,recommended to use lambda. if you must use std::bind(),do not use * placeholders! * * @tparam Function * @param topic * @param f */ template void registerTopic(const std::string_view &topic, Function &&f); template void registerTopic(const Message &topic, Function &&f); template void removeTopic(const std::string_view &topic); template size_t topicCount(const std::string_view &topic); template void sendMessage(const std::string_view &topic, Args &&...args); template void sendMessage(const std::string_view &topic, Args &&...args); template void sendMessage(const Message &topic, Args &&...args); template void sendAsyncMessage(const std::string_view &topic, Args &&...args); template void sendAsyncMessage(const std::string_view &topic, Args &&...args); template void sendAsyncMessage(const Message &topic, Args &&...args); protected: MessageManager(const MessageManager &) = delete; template static Executor executorWrapper(Function &&f); void callExecutor(const std::string &signature, std::any &¶meter); void run(); private: std::unordered_multimap m_executors; std::queue m_params; std::thread m_thread; bool m_exit{false}; }; #include "MessageManager.inl" #endif // MESSAGEMANAGER_H