从 Any 包中提取和匹配 protobuf 消息类型名的首选方法
Preferred way of extracting and matching protobuf message typename from an Any package
我一直在使用Any
来打包protobuf的动态消息。 在接收端,我使用Any.type_url()
来匹配包含的消息类型。
如果我错了,请纠正我,知道.GetDescriptor()
不可用Any
,我仍然想让匹配不那么混乱。我尝试像这样用蛮力提取消息类型:
MyAny pouch;
// unpacking pouch
// Assume the message is "type.googleapis.com/MyTypeName"
....
const char* msgPrefix = "type.googleapis.com/";
auto lenPrefix = strlen(msgPrefix);
const std::string& msgURL = pouch.msg().type_url();
MamStr msgName = msgURL.substr(lenPrefix, msgURL.size());
if (msgName == "MyTypeName") {
// do stuff ...
}
但我仍然想知道是否有更干净的方法来跳过前缀以获取类型 URL 的"基本名称"。
谢谢!
你可以试试
std::string getBaseName(std::string const & url) {
return url.substr(url.find_last_of("/\") + 1);
}
如果它适合你。
虽然在某些情况下,但它可能无法正确爆炸它。
假设您有两个参数作为基名称:http://url.com/example/2
这将获得最新的,即 2...
如果您不是在寻找跨平台支持,您可以随时选择 https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/splitpath-wsplitpath?view=vs-2019
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 是否有内置方法可以强制转换为不同的基础类型,但保留常量限定符?
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 调用具有未标识类型的类的方法
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- 我的模板类方法返回错误类型?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 构造智能点数据类型以及普通数据类型的通用方法
- 如何在没有实例的情况下获取非静态方法的类型?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 使用类型id运算符的最佳替代方法
- Java 调用 dll 字符串返回类型方法
- C++ 如何绑定和调用模板化类型方法
- 将返回类型推断为模板参数类型方法
- 返回true的模板化类型方法是在条件中进行优化的良好候选者