不用宏了

This commit is contained in:
Tian jianyong 2024-12-20 16:40:08 +08:00
parent 5e1b33814c
commit 248c45c0eb

View File

@ -8,14 +8,29 @@ using json = nlohmann::json;
class System; // 前向声明
// 1. 首先定义基础结构体
// 基础结构体
struct CoordinatePoint {
std::string point;
double latitude;
double longitude;
};
// 2. 定义 SystemConfig 类及其所有嵌套结构体
// JSON 序列化/反序列化函数
void to_json(json& j, const CoordinatePoint& p) {
j = json{
{"point", p.point},
{"latitude", p.latitude},
{"longitude", p.longitude}
};
}
void from_json(const json& j, CoordinatePoint& p) {
j.at("point").get_to(p.point);
j.at("latitude").get_to(p.latitude);
j.at("longitude").get_to(p.longitude);
}
// SystemConfig 类定义
class SystemConfig {
public:
struct Airport {
@ -99,16 +114,181 @@ private:
SystemConfig() = default;
};
// 3. 在类定义之后定义所有 NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE 宏
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CoordinatePoint, point, latitude, longitude)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Airport::ReferencePoint, latitude, longitude)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::WebSocket::PositionUpdate, aircraft_interval_ms, vehicle_interval_ms, traffic_light_interval_ms)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::CollisionDetection::Prediction, time_window, vehicle_size, aircraft_size, min_unmanned_speed)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Airport, name, iata, icao, reference_point, coordinate_points)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::WebSocket, port, max_connections, ping_interval_ms, position_update)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::CollisionDetection, update_interval_ms, prediction)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::DataSource, host, port, aircraft_path, vehicle_path, traffic_light_path, refresh_interval_ms, timeout_ms, read_timeout_ms)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Logging, level, file, max_size_mb, max_files, console_output)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Debug, enable_mock_data, save_raw_data, profile_performance)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Warning, warning_interval_ms, log_interval_ms)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig, airport, data_source, websocket, collision_detection, logging, debug, warning)
// 为每个嵌套结构体定义 JSON 序列化/反序列化函数
void to_json(json& j, const SystemConfig::Airport::ReferencePoint& p) {
j = json{
{"latitude", p.latitude},
{"longitude", p.longitude}
};
}
void from_json(const json& j, SystemConfig::Airport::ReferencePoint& p) {
j.at("latitude").get_to(p.latitude);
j.at("longitude").get_to(p.longitude);
}
void to_json(json& j, const SystemConfig::Airport& p) {
j = json{
{"name", p.name},
{"iata", p.iata},
{"icao", p.icao},
{"reference_point", p.reference_point},
{"coordinate_points", p.coordinate_points}
};
}
void from_json(const json& j, SystemConfig::Airport& p) {
j.at("name").get_to(p.name);
j.at("iata").get_to(p.iata);
j.at("icao").get_to(p.icao);
j.at("reference_point").get_to(p.reference_point);
j.at("coordinate_points").get_to(p.coordinate_points);
}
void to_json(json& j, const SystemConfig::WebSocket::PositionUpdate& p) {
j = json{
{"aircraft_interval_ms", p.aircraft_interval_ms},
{"vehicle_interval_ms", p.vehicle_interval_ms},
{"traffic_light_interval_ms", p.traffic_light_interval_ms}
};
}
void from_json(const json& j, SystemConfig::WebSocket::PositionUpdate& p) {
j.at("aircraft_interval_ms").get_to(p.aircraft_interval_ms);
j.at("vehicle_interval_ms").get_to(p.vehicle_interval_ms);
j.at("traffic_light_interval_ms").get_to(p.traffic_light_interval_ms);
}
void to_json(json& j, const SystemConfig::WebSocket& p) {
j = json{
{"port", p.port},
{"max_connections", p.max_connections},
{"ping_interval_ms", p.ping_interval_ms},
{"position_update", p.position_update}
};
}
void from_json(const json& j, SystemConfig::WebSocket& p) {
j.at("port").get_to(p.port);
j.at("max_connections").get_to(p.max_connections);
j.at("ping_interval_ms").get_to(p.ping_interval_ms);
j.at("position_update").get_to(p.position_update);
}
void to_json(json& j, const SystemConfig::CollisionDetection::Prediction& p) {
j = json{
{"time_window", p.time_window},
{"vehicle_size", p.vehicle_size},
{"aircraft_size", p.aircraft_size},
{"min_unmanned_speed", p.min_unmanned_speed}
};
}
void from_json(const json& j, SystemConfig::CollisionDetection::Prediction& p) {
j.at("time_window").get_to(p.time_window);
j.at("vehicle_size").get_to(p.vehicle_size);
j.at("aircraft_size").get_to(p.aircraft_size);
j.at("min_unmanned_speed").get_to(p.min_unmanned_speed);
}
void to_json(json& j, const SystemConfig::CollisionDetection& p) {
j = json{
{"update_interval_ms", p.update_interval_ms},
{"prediction", p.prediction}
};
}
void from_json(const json& j, SystemConfig::CollisionDetection& p) {
j.at("update_interval_ms").get_to(p.update_interval_ms);
j.at("prediction").get_to(p.prediction);
}
void to_json(json& j, const SystemConfig::DataSource& p) {
j = json{
{"host", p.host},
{"port", p.port},
{"aircraft_path", p.aircraft_path},
{"vehicle_path", p.vehicle_path},
{"traffic_light_path", p.traffic_light_path},
{"refresh_interval_ms", p.refresh_interval_ms},
{"timeout_ms", p.timeout_ms},
{"read_timeout_ms", p.read_timeout_ms}
};
}
void from_json(const json& j, SystemConfig::DataSource& p) {
j.at("host").get_to(p.host);
j.at("port").get_to(p.port);
j.at("aircraft_path").get_to(p.aircraft_path);
j.at("vehicle_path").get_to(p.vehicle_path);
j.at("traffic_light_path").get_to(p.traffic_light_path);
j.at("refresh_interval_ms").get_to(p.refresh_interval_ms);
j.at("timeout_ms").get_to(p.timeout_ms);
j.at("read_timeout_ms").get_to(p.read_timeout_ms);
}
void to_json(json& j, const SystemConfig::Logging& p) {
j = json{
{"level", p.level},
{"file", p.file},
{"max_size_mb", p.max_size_mb},
{"max_files", p.max_files},
{"console_output", p.console_output}
};
}
void from_json(const json& j, SystemConfig::Logging& p) {
j.at("level").get_to(p.level);
j.at("file").get_to(p.file);
j.at("max_size_mb").get_to(p.max_size_mb);
j.at("max_files").get_to(p.max_files);
j.at("console_output").get_to(p.console_output);
}
void to_json(json& j, const SystemConfig::Debug& p) {
j = json{
{"enable_mock_data", p.enable_mock_data},
{"save_raw_data", p.save_raw_data},
{"profile_performance", p.profile_performance}
};
}
void from_json(const json& j, SystemConfig::Debug& p) {
j.at("enable_mock_data").get_to(p.enable_mock_data);
j.at("save_raw_data").get_to(p.save_raw_data);
j.at("profile_performance").get_to(p.profile_performance);
}
void to_json(json& j, const SystemConfig::Warning& p) {
j = json{
{"warning_interval_ms", p.warning_interval_ms},
{"log_interval_ms", p.log_interval_ms}
};
}
void from_json(const json& j, SystemConfig::Warning& p) {
j.at("warning_interval_ms").get_to(p.warning_interval_ms);
j.at("log_interval_ms").get_to(p.log_interval_ms);
}
void to_json(json& j, const SystemConfig& config) {
j = json{
{"airport", config.airport},
{"data_source", config.data_source},
{"websocket", config.websocket},
{"collision_detection", config.collision_detection},
{"logging", config.logging},
{"debug", config.debug},
{"warning", config.warning}
};
}
void from_json(const json& j, SystemConfig& config) {
j.at("airport").get_to(config.airport);
j.at("data_source").get_to(config.data_source);
j.at("websocket").get_to(config.websocket);
j.at("collision_detection").get_to(config.collision_detection);
j.at("logging").get_to(config.logging);
j.at("debug").get_to(config.debug);
j.at("warning").get_to(config.warning);
}