我有一个C++程序发出各种事件,例如, StatusEvent
和DetectionEvent
与消息服务(当前活动MQ,通过activemq-cpp APU)不同的协议消息定义。我想写一个接收这些消息的消息监听器,解析它们并将它们写入cout,以用于调试目的。听众有status_event_pb.h
和detection_event_pb.h
链接。协议缓冲区多态性
我的问题是:如何解析接收的事件而不知道它的类型?我想这样做(在伪代码)
receive event
type = parseEventType(event);
if(type == events::StatusEventType) {
events::StatusEvent se = parseEvent(event);
// do stuff with se
}
else {
// handle the case when the event is a DetectionEvent
}
我看着this question但我不知道,如果扩展是去这里的正道。一个简短的代码片段指出的方式将不胜感激。 protobuf上的例子非常罕见!
谢谢!
看起来扩展确实是要走的路,但我有最后一点要清理。这里的原始定义,我到目前为止有:
// A general event, can be thought as base Event class for other event types.
message Event {
required int64 task_id = 1;
required string module_name = 2; // module that sent the event
extensions 100 to 199; // for different event types
}
// Extend the base Event with additional types of events.
extend Event {
optional StatusEvent statusEvent = 100;
optional DetectionEvent detectionEvent = 101;
}
// Contains one bounding box detected in a video frame,
// representing a region of interest.
message DetectionEvent {
optional int64 frame = 2;
optional int64 time = 4;
optional string label = 6;
}
// Indicate status change of current module to other modules in same service.
// In addition, parameter information that is to be used to other modules can
// be passed, e.g. the video frame dimensions.
message StatusEvent {
enum EventType {
MODULE_START = 1;
MODULE_END = 2;
MODULE_FATAL = 3;
}
required EventType type = 1;
required string module_name = 2; // module that sent the event
// Optional key-value pairs for data to be passed on.
message Data {
required string key = 1;
required string value = 2;
}
repeated Data data = 3;
}
我现在的问题是:(1)如何知道哪些特定的事件,该事件消息包含和(2)确保它仅包含一个这样的事件(根据定义,它可以包含StatusEvent
和DetectionEvent
)。
的protobuf的消息中找到这个有趣的螺纹:http://markmail.org/ message/dgmf5iuhhgoe7keb#query:protocol%20buffer%20polymorphism + page:1 + mid:73p5kddhvmokcpvo + state:results – recipriversexclusion 2010-08-11 17:06:48
[可以使用协议缓冲区做多态的正确方法是什么?](http://stackoverflow.com/q/3018743/1468366) – MvG 2013-02-25 13:11:41