BHandler

BHandler

  • BHandler is a base class for objects that can receive and handle messages.
  • To handle messages, you subclass BHandler and override its MessageReceived() function.
  • The MessageReceived() function is called whenever a message is sent to the handler.
  • In the MessageReceived() function, you can inspect the message and take appropriate action based on its contents.

BHandler class is defined at:

  • Header file: headers/os/app/Handler.h
  • src file: src/kits/app/Handler.cpp

Example:

class MyHandler : public BHandler {
public:
    MyHandler() {}

    void MessageReceived(BMessage* message) {
        switch (message->what) {
            case 'HELW':
                printf("Hello World!\n");
                break;
            default:
                BHandler::MessageReceived(message);
                break;
        }
    }
};

Sending Messages to the Handler:

  • To send a message to a BHandler, you create a BMessage object and call the MessageReceived() function on the handler.
BMessage message('HELW');
myHandler->MessageReceived(&message);

Basic functions of the BHandler class

1 Constructor

BHandler::BHandler(const char* name = NULL);
  • Creates a new BHandler object with the given name.

2 MessageReceived()

virtual void BHandler::MessageReceived(BMessage* message);
  • This function is called whenever a message is received by the BHandler.
  • Override this function in your subclasses to handle specific types of messages.