English 中文(简体)
缓冲多变状态议定书
原标题:Protocol buffer polymorphism

我有一个C++方案,它分出各种活动,例如:StatusEventDetection Event,向信息服务处(目前是活跃的MQ,通过积极的mq-cpp APU)提供不同的信息定义。 我想写一个接收这些信息的听众,向他们说几句,并写上 co子,以图谋。 听众有<代码>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
}

参看,但我不敢确定延期是否是这里的正确途径。 简短的法典指明了前进的道路将受到高度赞赏。 pro夫的例子非常少!

感谢!


延期似乎确实是走的路,但我说,最后一点是清楚的。 下面是我迄今为止的推论:

// 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) 确保其只包含one这种事件(根据定义,它可以包含一个<条码>-StatusEvent和<条码>。

问题回答

我不会为此使用《Buffers议定书》,但这可能是很少使用和其他习惯的结合。

无论如何,我认为,我将在这里使用一个抽象的班子,以方便一般处理,并包含路线信息。 不使用制革法界定的等级,包括制革电。

class Message
{
public:
  Type const& GetType() const;

  Origin const& GetOrigin() const;
  Destination const& GetDestination() const;

  // ... other informations

  template <class T>
  void GetContent(T& proto) const
  {
    proto.ParseFromIstream(&mContent); // perhaps a try/catch ?
  }

private:
  // ...

  std::stringstream mContent;
};

有了这种结构,你在主人面前既有一般性的,也有具体的处理:

void receive(Message const& message)
{
  LOG("receive - " << message.GetType() << " from " << message.GetOrigin()
                   << " to " << message.GetDestination());

  if (message.GetType() == "StatusEvent")
  {
    StatusEvent statusEvent;
    message.Decode(statusEvent);
    // do something
  }
  else if (message.GetType() == "DetectionEvent")
  {
    DetectionEvent detectionEvent;
    message.Decode(detectionEvent);
    // do something
  }
  else
  {
    LOG("receive - Unhandled type");
  }
}

当然,如果你使用<代码>std:unordered_map<Type,Handler>,那将是先令。 如果+/否则链条,而不是硬编码的<代码>,但这一原则仍然相同:

  1. Encode the type of message sent in the header
  2. Decode only the header upon reception and dispatch based on this type
  3. Decode the protobuf message in a part of the code where the type is known statically




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签