libcamera: message: Add user message types
Reserve identifiers for user-defined message types and add an operation to the Message class to register the type identifiers. Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
parent
3e29a2d520
commit
fae053307d
3 changed files with 54 additions and 0 deletions
|
@ -7,6 +7,8 @@
|
||||||
#ifndef __LIBCAMERA_MESSAGE_H__
|
#ifndef __LIBCAMERA_MESSAGE_H__
|
||||||
#define __LIBCAMERA_MESSAGE_H__
|
#define __LIBCAMERA_MESSAGE_H__
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace libcamera {
|
namespace libcamera {
|
||||||
|
|
||||||
class Object;
|
class Object;
|
||||||
|
@ -19,6 +21,7 @@ public:
|
||||||
enum Type {
|
enum Type {
|
||||||
None = 0,
|
None = 0,
|
||||||
SignalMessage = 1,
|
SignalMessage = 1,
|
||||||
|
UserMessage = 1000,
|
||||||
};
|
};
|
||||||
|
|
||||||
Message(Type type);
|
Message(Type type);
|
||||||
|
@ -27,11 +30,15 @@ public:
|
||||||
Type type() const { return type_; }
|
Type type() const { return type_; }
|
||||||
Object *receiver() const { return receiver_; }
|
Object *receiver() const { return receiver_; }
|
||||||
|
|
||||||
|
static Type registerMessageType();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Thread;
|
friend class Thread;
|
||||||
|
|
||||||
Type type_;
|
Type type_;
|
||||||
Object *receiver_;
|
Object *receiver_;
|
||||||
|
|
||||||
|
static std::atomic_uint nextUserType_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SignalMessage : public Message
|
class SignalMessage : public Message
|
||||||
|
|
|
@ -31,6 +31,8 @@ namespace libcamera {
|
||||||
|
|
||||||
LOG_DEFINE_CATEGORY(Message)
|
LOG_DEFINE_CATEGORY(Message)
|
||||||
|
|
||||||
|
std::atomic_uint Message::nextUserType_{ Message::UserMessage };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \class Message
|
* \class Message
|
||||||
* \brief A message that can be posted to a Thread
|
* \brief A message that can be posted to a Thread
|
||||||
|
@ -43,6 +45,8 @@ LOG_DEFINE_CATEGORY(Message)
|
||||||
* \brief Invalid message type
|
* \brief Invalid message type
|
||||||
* \var Message::SignalMessage
|
* \var Message::SignalMessage
|
||||||
* \brief Asynchronous signal delivery across threads
|
* \brief Asynchronous signal delivery across threads
|
||||||
|
* \var Message::UserMessage
|
||||||
|
* \brief First value available for user-defined messages
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,6 +74,38 @@ Message::~Message()
|
||||||
* \return The message receiver
|
* \return The message receiver
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Reserve and register a custom user-defined message type
|
||||||
|
*
|
||||||
|
* Custom message types use values starting at Message::UserMessage. Assigning
|
||||||
|
* custom types manually may lead to accidental duplicated types. To avoid this
|
||||||
|
* problem, this method reserves and returns the next available user-defined
|
||||||
|
* message type.
|
||||||
|
*
|
||||||
|
* The recommended way to use this method is to subclass Message and provide a
|
||||||
|
* static accessor for the custom message type.
|
||||||
|
*
|
||||||
|
* \code{.cpp}
|
||||||
|
* class MyCustomMessage : public Message
|
||||||
|
* {
|
||||||
|
* public:
|
||||||
|
* MyCustomMessage() : Message(type()) { }
|
||||||
|
*
|
||||||
|
* static Message::Type type()
|
||||||
|
* {
|
||||||
|
* static MessageType type = registerMessageType();
|
||||||
|
* return type;
|
||||||
|
* }
|
||||||
|
* };
|
||||||
|
* \endcode
|
||||||
|
*
|
||||||
|
* \return A new unique message type
|
||||||
|
*/
|
||||||
|
Message::Type Message::registerMessageType()
|
||||||
|
{
|
||||||
|
return static_cast<Message::Type>(nextUserType_++);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \class SignalMessage
|
* \class SignalMessage
|
||||||
* \brief A message carrying a Signal across threads
|
* \brief A message carrying a Signal across threads
|
||||||
|
|
|
@ -52,6 +52,17 @@ class MessageTest : public Test
|
||||||
protected:
|
protected:
|
||||||
int run()
|
int run()
|
||||||
{
|
{
|
||||||
|
Message::Type msgType[2] = {
|
||||||
|
Message::registerMessageType(),
|
||||||
|
Message::registerMessageType(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (msgType[0] != Message::UserMessage ||
|
||||||
|
msgType[1] != Message::UserMessage + 1) {
|
||||||
|
cout << "Failed to register message types" << endl;
|
||||||
|
return TestFail;
|
||||||
|
}
|
||||||
|
|
||||||
MessageReceiver receiver;
|
MessageReceiver receiver;
|
||||||
receiver.moveToThread(&thread_);
|
receiver.moveToThread(&thread_);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue