我有一個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