C++ Google 协议缓冲区比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3228107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google protocol buffers compare
提问by dimba
I want to compare two Messagesor (two sub parameters) of Google protocol buffers. I don't find an API to achieve it.
我想比较Google protocol buffers 的两个Messages或(两个子参数)。我没有找到实现它的 API。
Any ideas?
有任何想法吗?
回答by Amiga
You can use the class google::protobuf::util::MessageDifferencerfor this. I think it's only available since v3.0.2:
您可以为此使用类google::protobuf::util::MessageDifferencer。我认为它仅从v3.0.2 开始可用:
Introduced new utility functions/classes in the google/protobuf/util directory:
- MessageDifferencer: compare two proto messages and report their differences.
在 google/protobuf/util 目录中引入了新的实用程序函数/类:
- MessageDifferencer:比较两个原始消息并报告它们的差异。
#include <google/protobuf/util/message_differencer.h>
MessageDifferencer::Equals(msg1, msg2);
回答by Jake Askeland
You can rely on the fact that all of your protobuf messages inherit from the google::protobuf::MesageLite
type, which in turn has everything you need to compare any two protobuf messages, regardless of if they are even of the same derived type:
您可以依赖这样一个事实,即您的所有 protobuf 消息都继承自该google::protobuf::MesageLite
类型,而该类型又具有比较任何两个 protobuf 消息所需的一切,无论它们是否具有相同的派生类型:
bool operator==(const google::protobuf::MessageLite& msg_a,
const google::protobuf::MessageLite& msg_b) {
return (msg_a.GetTypeName() == msg_b.GetTypeName()) &&
(msg_a.SerializeAsString() == msg_b.SerializeAsString());
}
回答by Lorenz Winkler
Instead of using message.DebugString
you could also do
除了使用message.DebugString
你还可以做
std::string strMsg;
message.SerializeToString(&strMsg);
with both messages and then compare the two (binary) strings. I didn't test the performance but I assume that it is faster than comparing the human readable message strings returned by .DebugString(). +You can do that with the protobuf-lite library (while for message.DebugString you need the full version).
两个消息,然后比较两个(二进制)字符串。我没有测试性能,但我认为它比比较 .DebugString() 返回的人类可读消息字符串要快。+您可以使用 protobuf-lite 库来做到这一点(而对于 message.DebugString,您需要完整版本)。
回答by benjismith
Well, a protocol buffer is just a serialization format for some object type. Why not use the protocol buffer to reconstruct the original objects, and then allow those objects to compare themselves, using whatever comparison logic you've built into the class?
好吧,协议缓冲区只是某些对象类型的序列化格式。为什么不使用协议缓冲区来重建原始对象,然后使用您在类中内置的任何比较逻辑让这些对象进行自我比较?
回答by Gianni
This might not be the ideal solution, but I think it could be done by:
这可能不是理想的解决方案,但我认为可以通过以下方式完成:
messageA.DebugString() == messageB.DebugString();
Other than that, I think the only solution would be to create your own Message
child class and implement a bool operator==(const Message&)
.
除此之外,我认为唯一的解决方案是创建自己的Message
子类并实现bool operator==(const Message&)
.
回答by KukoBits
You can compare the descriptor's pointer (super fast):
您可以比较描述符的指针(超快):
if (mMessages[i]->body()->GetDescriptor() == T::descriptor())
mMessages it's a pool of network messages with header and crypto which creates a packet with the protobuf body(google::protobuf::Message*).
mMessages 它是一个带有标头和加密的网络消息池,它创建一个带有 protobuf 主体(google::protobuf::Message*)的数据包。
so, to get the right kind of message i compare the descriptors constant pointer which is the same for every single type of message (not %100 sure but i haven't got any problem so far).
因此,为了获得正确类型的消息,我比较了对于每种类型的消息都相同的描述符常量指针(不确定 %100,但到目前为止我没有遇到任何问题)。
That would be the fastest way to compare a protobuf Message wthout having to use string comparasion, which by the way you gan get the type name from the descriptor. :-)
这将是比较 protobuf 消息而不必使用字符串比较的最快方法,顺便说一下,您可以从描述符中获取类型名称。:-)