From 5e1b33814c25b871394b87c45a59f74b8715e795 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Fri, 20 Dec 2024 16:17:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20json=20=E5=AE=8F=E7=9A=84?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/SystemConfig.h | 44 +++++++++++++++------------------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/config/SystemConfig.h b/src/config/SystemConfig.h index 3b313dc..ef64bab 100644 --- a/src/config/SystemConfig.h +++ b/src/config/SystemConfig.h @@ -4,6 +4,8 @@ #include #include +using json = nlohmann::json; + class System; // 前向声明 // 1. 首先定义基础结构体 @@ -11,28 +13,21 @@ struct CoordinatePoint { std::string point; double latitude; double longitude; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(CoordinatePoint, point, latitude, longitude) }; // 2. 定义 SystemConfig 类及其所有嵌套结构体 class SystemConfig { public: - // 嵌套结构体定义 struct Airport { struct ReferencePoint { double latitude; double longitude; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(ReferencePoint, latitude, longitude) } reference_point; std::string name; std::string iata; std::string icao; std::vector coordinate_points; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Airport, name, iata, icao, reference_point, coordinate_points) } airport; struct DataSource { @@ -44,8 +39,6 @@ public: int refresh_interval_ms; int timeout_ms; int read_timeout_ms; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(DataSource, host, port, aircraft_path, vehicle_path, traffic_light_path, refresh_interval_ms, timeout_ms, read_timeout_ms) } data_source; struct WebSocket { @@ -53,15 +46,11 @@ public: int aircraft_interval_ms; int vehicle_interval_ms; int traffic_light_interval_ms; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(PositionUpdate, aircraft_interval_ms, vehicle_interval_ms, traffic_light_interval_ms) } position_update; uint16_t port; int max_connections; int ping_interval_ms; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(WebSocket, port, max_connections, ping_interval_ms, position_update) } websocket; struct CollisionDetection { @@ -70,13 +59,9 @@ public: double vehicle_size; double aircraft_size; double min_unmanned_speed; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Prediction, time_window, vehicle_size, aircraft_size, min_unmanned_speed) } prediction; int update_interval_ms; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(CollisionDetection, update_interval_ms, prediction) } collision_detection; struct Logging { @@ -85,26 +70,19 @@ public: int max_size_mb; int max_files; bool console_output; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Logging, level, file, max_size_mb, max_files, console_output) } logging; struct Debug { bool enable_mock_data; bool save_raw_data; bool profile_performance; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Debug, enable_mock_data, save_raw_data, profile_performance) } debug; struct Warning { int warning_interval_ms; int log_interval_ms; - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Warning, warning_interval_ms, log_interval_ms) } warning; - // 公共方法 static SystemConfig& instance() { static SystemConfig instance; return instance; @@ -117,8 +95,20 @@ public: friend class System; - NLOHMANN_DEFINE_TYPE_INTRUSIVE(SystemConfig, airport, data_source, websocket, collision_detection, logging, debug, warning) - private: SystemConfig() = default; -}; \ No newline at end of file +}; + +// 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) \ No newline at end of file