From 5ee17627e9d350cb828c2f4b35f47aa8e4358e85 Mon Sep 17 00:00:00 2001 From: Huamonarch Date: Wed, 13 May 2026 15:46:06 +0800 Subject: [PATCH] fix: address code review issues - read_csv.hpp: replace std::exit(1) with std::runtime_error throw - read_csv.hpp: remove duplicate #pragma once - RNG.cc: wrap JSON load in try-catch to prevent crash on missing config - Generator.cc: fix comments to reflect actual 3-pass structure --- TestProject/RNG/Generator.cc | 5 +++-- TestProject/RNG/RNG.cc | 7 ++++++- TestProject/RNG/read_csv.hpp | 5 ++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/TestProject/RNG/Generator.cc b/TestProject/RNG/Generator.cc index b53c4ca..1432ccd 100644 --- a/TestProject/RNG/Generator.cc +++ b/TestProject/RNG/Generator.cc @@ -38,7 +38,7 @@ bool Generator::wtite_in_shm(int event_no) { ((chrono::system_clock::now() - stime).count() / (size_t)pow(10, 6)) / 20; - // First pass: create all models and link valve_pair models + // Pass 1: create model instances for (int i = 0; i < size; i++) { const char* spec = binary_tele[i].tables[1]; string specStr = (spec != nullptr && strlen(spec) > 0) ? string(spec) : ""; @@ -46,6 +46,7 @@ bool Generator::wtite_in_shm(int event_no) { string key = string(binary_tele[i].item); registry.getOrCreate(specStr, defaultVal, key); } + // Pass 2: link valve_pair models to action signals for (int i = 0; i < size; i++) { const char* spec = binary_tele[i].tables[1]; string specStr = (spec != nullptr && strlen(spec) > 0) ? string(spec) : ""; @@ -55,7 +56,7 @@ bool Generator::wtite_in_shm(int event_no) { model->linkPeers(registry); } - // Second pass: evaluate + // Pass 3: evaluate all models for (int i = 0; i < size; i++) { const char* spec = binary_tele[i].tables[1]; string specStr = (spec != nullptr && strlen(spec) > 0) ? string(spec) : ""; diff --git a/TestProject/RNG/RNG.cc b/TestProject/RNG/RNG.cc index c8213f6..cd96260 100644 --- a/TestProject/RNG/RNG.cc +++ b/TestProject/RNG/RNG.cc @@ -17,7 +17,12 @@ int RNG::start() { logger_->Info() << "-------RNG::start-------" << std::endl; con_mag_ = std::make_shared(); con_mag_->dbLogin(); - ModelRegistry::instance().loadModels("/users/dsc/code/TestProject/RNG/json/rng_models.json"); + try { + ModelRegistry::instance().loadModels("/users/dsc/code/TestProject/RNG/json/rng_models.json"); + } catch (const std::exception& e) { + logger_->Error() << "Failed to load rng_models.json: " << e.what() << endl; + return (-1); + } try { auto module_name = name(); RNG_server = new RNGICEI(); diff --git a/TestProject/RNG/read_csv.hpp b/TestProject/RNG/read_csv.hpp index 0df91c8..f49ab6f 100644 --- a/TestProject/RNG/read_csv.hpp +++ b/TestProject/RNG/read_csv.hpp @@ -1,8 +1,8 @@ #pragma once -#pragma once #include #include #include +#include #include #include #include @@ -20,8 +20,7 @@ struct ReadCSV { string word; using std::is_same_v; if (!infile.is_open()) { - std::cout << "Error: opening file fail" << std::endl; - std::exit(1); + throw std::runtime_error("Cannot open CSV file: " + file_dir); } while (std::getline(infile, line)) { std::istringstream sin;