具有字符串化的可变参数宏的现代/通用方法
Modern / generic approach to variadic macro with stringizing
我有一个库(nlohmann/json的包装器(,允许我从JSON反序列化:
struct MyStruct {
int propertyA;
std::string propertyB;
std::vector<int> propertyC;
}
void from_json(const JSON::Node& json, MyStruct& s)
{
json_get(s.propertyA, "propertyA", json);
json_get(s.propertyB, "propertyB", json);
json_get(s.propertyC, "propertyC", json);
}
如您所见,这些定义中有很多样板。我正在使用一个 ECS 框架,其中包含数百个我想反序列化的组件。我希望用宏简化它,例如:
struct MyStruct {
int propertyA;
std::string propertyB;
std::vector<int> propertyC;
JSON(MyStruct, propertyA, propertyB, propertyC);
};
}
我知道手动重复宏 N 次的老派__VA_ARGS__
方法,但我希望用更通用/现代的方法避免这种情况。
使用可变参数模板可以做到这一点吗?有没有更好的方法来为此提供一种语法糖?我使用的是 C++17 编译器。
显然有一个库可以完全按照您的要求!下面是一个完整的示例:
#include <iostream>
#include <nlohmann/json.hpp>
#include <visit_struct/visit_struct.hpp>
struct MyStruct {
int propertyA;
std::string propertyB;
std::vector<int> propertyC;
};
VISITABLE_STRUCT(MyStruct, propertyA, propertyB, propertyC);
using nlohmann::json;
template <typename T>
std::enable_if_t<visit_struct::traits::is_visitable<std::decay_t<T>>::value>
from_json(const json &j, T &obj) {
visit_struct::for_each(obj, [&](const char *name, auto &value) {
// json_get(value, name, j);
j.at(name).get_to(value);
});
}
int main() {
json j = json::parse(R"(
{
"propertyA": 42,
"propertyB": "foo",
"propertyC": [7]
}
)");
MyStruct s = j.get<MyStruct>();
std::cout << "PropertyA: " << s.propertyA << 'n';
std::cout << "PropertyB: " << s.propertyB << 'n';
std::cout << "PropertyC: " << s.propertyC[0] << 'n';
}
相关文章:
- c++方法参数只能在linux的发布模式下自行更改
- 使用移动语义:右值引用作为方法参数
- 无法将动态创建的对象数组从 qml 发送到 c++ 作为方法参数
- 将方法参数类型更改为子类中的派生类
- ofstream作为C 中的方法参数
- 使用 gmock 匹配器将 std::function 设置为 EXPECT_CALL 中的方法参数
- C com 方法参数扣除
- 获取非静态方法参数计数
- 使用 gmock 返回模拟方法参数
- 运行 QML 时出现"未知方法参数类型"错误
- 基本C - 构造contaning对象引用,并将其作为方法参数传递
- 将C 方法参数转换为模板参数会因编译错误而失败
- 如何在方法参数中使用boost :: asio :: buffer
- qsharedPointer作为方法参数或方法的返回值的良好实践
- 如何将方法指针声明为Typedef方法参数
- 方法参数中的 OpenCV 垫"预期")""
- 方法参数中的 consst* 常量
- 带有“class”详细类型说明符的方法参数
- 具有原始方法参数派生类的 C++ 重载方法参数
- C++ std::string length() 或 size() 不适用于方法参数