具有字符串化的可变参数宏的现代/通用方法

Modern / generic approach to variadic macro with stringizing

本文关键字:方法 参数 字符串 变参      更新时间:2023-10-16

我有一个库(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';
}