Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Clear11 message bus

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use Clear11 message bus". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is the Clear11 message bus?

Recently, in the C++ project, we need to deal with a large number of messages between objects, and if we use traditional SendMessage and PostMessage, the coupling between objects will be too high, so we finally adopted a message bus library based on Cellular PostMessage 11, which has the following advantages:

Easy to use, send and receive only one statement, and use lambda to simplify the callback of the function

Can be highly decoupled between modules, which depends on a global MessageBus object

Support for sending custom messages or packets

Support sending messages with and without parameters

Basic principles

You can think of MessageBus as a global message channel. You can put your own identified messages on this channel, and other modules can get your messages through this identifier. All these depend on a global object MessageBus g_bus, which acts as a global message channel.

Basic usage to send and receive no-parameter messages

Send:

/ / g_bus is a global variable

Extern MessageBus g_bus

/ / send a no-parameter message with the identification word `InitAll`

G_bus.SendReq ("InitAll")

Receive:

/ / g_bus is a global variable

Extern MessageBus g_bus

/ / binding messages with the `InitAll` identification word is usually done in the constructor.

/ / as soon as the message is received, the InitAll function is called, using a simple and rough lambda expression

G_bus.Attach ([this] () {InitAll ();}, "InitAll")

/ / unbind a message with `InitAll`, usually in the destructor

G_bus.Remove ("InitAll"); send and receive parameter messages

Send:

/ / g_bus is a global variable

Extern MessageBus gatebuster YourType m_yourType

/ / send a parameter message with the parameter of `YourType` type

G_bus.SendReq (m_yourType, "YourType")

Receive:

/ / g_bus is a global variable

Extern MessageBus g_bus

/ / binding a parameter message with the identification word `YourType` is usually done in the constructor.

/ / as soon as the message is received, you_function will be called

/ / and the parameter `m_ yourtype` passed when sending is passed to the `your_ type` here

/ / is it very simple to finally pass it to `you_ function`?

G_bus.Attach ([this] (const YourType& your_type) {you_function (your_type);}, "YourType")

/ / unbind the message identified by `YourType`, which is usually done in the destructor

This is the end of the introduction of g_bus.Remove ("YourType"); "how to use the message bus 11". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report