Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Clear11 message bus

Shulou Source: shulou.com Published: 2022-06-01 10:48:55 09月26日 Update

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!

Tags: Messages globals functions buses variables objects between parameters channels content more modules knowledge support practical rough learned and then advantages traditional Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Xiaomi macOS Huawei