From 73b096aa3d4e4b8c9fda32a3da263e66c1604ba8 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 1 Jul 2022 20:00:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=88=B0=E7=AB=99?= =?UTF-8?q?=E4=B8=8D=E5=81=9C=E8=BD=A6=E7=9A=84=E9=97=AE=E9=A2=98=20?= =?UTF-8?q?=E7=94=A8nullptr=E6=9B=BF=E4=BB=A3=E4=BA=86NULL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/BusStrategyBase.h | 15 ++------------ src/BusFCFSStrategy.cpp | 3 +-- src/BusSCANStrategy.cpp | 20 +++++++++---------- src/BusSSTFStrategy.cpp | 16 +++++++-------- src/BusStrategyBase.cpp | 42 +++++++++++++++++++-------------------- src/StrategyFactory.cpp | 4 ++-- src/mainwindow.cpp | 7 +++++++ 7 files changed, 51 insertions(+), 56 deletions(-) diff --git a/include/BusStrategyBase.h b/include/BusStrategyBase.h index dbf8f1d..faf5eb1 100644 --- a/include/BusStrategyBase.h +++ b/include/BusStrategyBase.h @@ -46,7 +46,6 @@ public: virtual ~BusStrategyBase(); - /** * 获得当前公交车应该前进的方向 * @return 公交车前进的方向 @@ -78,11 +77,6 @@ public: */ void PrintStateSignal(QString string); - /** - * 获得公交车前进方向的信号 - */ - void GetBusDirectionSignal(); - /** * 运行公交车的信号 * @param direction 公交车前进的方向 @@ -113,11 +107,6 @@ public slots: */ void OneTickSlot(int remaining_time); - /** - * 获得公交车前进方向的槽函数 - */ - void GetBusDirectionSlot(); - /** * 处理到站事件的槽函数 */ @@ -136,9 +125,9 @@ private: QString PrintState(int remaining_time) const; /** - * 设置各种连接 + * 决定公交车状态的函数 */ - void SetConnection() const; + void DetermineBusStatus(); /** * 处理请求 diff --git a/src/BusFCFSStrategy.cpp b/src/BusFCFSStrategy.cpp index 3d8493a..845eaa6 100644 --- a/src/BusFCFSStrategy.cpp +++ b/src/BusFCFSStrategy.cpp @@ -8,11 +8,10 @@ int BusFCFSStrategy::GetBusDirection() { bus_query_t *p = bus_model->target_query; - if(p == NULL) + if(p == nullptr) { return BUS_STOP; } //如果没有请求,公交车停止 - else { int clockwise = 0; diff --git a/src/BusSCANStrategy.cpp b/src/BusSCANStrategy.cpp index 036e25c..022465b 100644 --- a/src/BusSCANStrategy.cpp +++ b/src/BusSCANStrategy.cpp @@ -9,7 +9,7 @@ int BusSCANStrategy::GetBusDirection() int orientation = bus_model->direction; bus_query_t *query = bus_model->target_query; - if(query == NULL) + if(query == nullptr) { return BUS_STOP; } @@ -53,22 +53,22 @@ bus_query_t *BusSCANStrategy::GetTargetQuery() int direction = bus_model->direction; // 当前没有请求 - if(queries == NULL) + if(queries == nullptr) { - return NULL; + return nullptr; } if(direction == BUS_STOP) { // 在停止的状态下第一次开始选择方向 int distance = 9999; - bus_query_t *query = NULL; + bus_query_t *query = nullptr; bus_query_t *p = queries; // 遍历顺时针方向 // 在两个方向路程相同时选择顺时针方向 // 所以先遍历顺时针方向 - while (p != NULL) + while (p != nullptr) { int temp = bus_model->GetQueryDistance(p, BUS_CLOCK_WISE); if(temp < distance) @@ -81,7 +81,7 @@ bus_query_t *BusSCANStrategy::GetTargetQuery() // 遍历逆时针方向 p = queries; - while (p != NULL) + while (p != nullptr) { int temp = bus_model->GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); if(temp < distance) @@ -98,10 +98,10 @@ bus_query_t *BusSCANStrategy::GetTargetQuery() { // 在已经有方向的情况下处理方向 int distance = 9999; - bus_query_t *query = NULL; + bus_query_t *query = nullptr; bus_query_t *p = queries; - while (p != NULL) + while (p != nullptr) { int temp = bus_model->GetQueryDistance(p, direction); if(temp < distance) @@ -123,7 +123,7 @@ bus_query_t *BusSCANStrategy::HandleBTWQuery() bus_query_t *p = query_model->queries; - while(p != NULL) + while(p != nullptr) { if(p->node == now_position) { @@ -135,5 +135,5 @@ bus_query_t *BusSCANStrategy::HandleBTWQuery() p = p->next_node; }//遍历请求链表,判断是否有可以顺便处理的请求 - return NULL; + return nullptr; } diff --git a/src/BusSSTFStrategy.cpp b/src/BusSSTFStrategy.cpp index 0d56a80..2cf9617 100644 --- a/src/BusSSTFStrategy.cpp +++ b/src/BusSSTFStrategy.cpp @@ -8,7 +8,7 @@ int BusSSTFStrategy::GetBusDirection() { bus_query_t *query = bus_model->target_query; - if (query == NULL) + if (query == nullptr) { return BUS_STOP; } @@ -33,10 +33,10 @@ bus_query_t *BusSSTFStrategy::HandleBTWQuery() // 这里只是处理顺便服务的代码 bus_query_t *query = query_model->queries; - bus_query_t *allow_query = NULL; + bus_query_t *allow_query = nullptr; rail_node_t *now_node = bus_model->rail_pos; - while (query != NULL) + while (query != nullptr) { if(query->node == now_node) { @@ -62,19 +62,19 @@ bus_query_t *BusSSTFStrategy::GetTargetQuery() bus_query_t *queries = query_model->queries; // 当前没有请求 - if(queries == NULL) + if(queries == nullptr) { - return NULL; + return nullptr; } int distance = 9999; - bus_query_t *query = NULL; + bus_query_t *query = nullptr; bus_query_t *p = queries; // 遍历顺时针方向 // 在两个方向路程相同时选择顺时针方向 // 所以先遍历顺时针方向 - while (p != NULL) + while (p != nullptr) { int temp = bus_model->GetQueryDistance(p, BUS_CLOCK_WISE); if(temp < distance) @@ -87,7 +87,7 @@ bus_query_t *BusSSTFStrategy::GetTargetQuery() // 遍历逆时针方向 p = queries; - while (p != NULL) + while (p != nullptr) { int temp = bus_model->GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); if(temp < distance) diff --git a/src/BusStrategyBase.cpp b/src/BusStrategyBase.cpp index 6e32c67..46bca67 100644 --- a/src/BusStrategyBase.cpp +++ b/src/BusStrategyBase.cpp @@ -12,8 +12,6 @@ BusStrategyBase::BusStrategyBase() bus_tick = 0; strategy = -1; - - SetConnection(); } BusStrategyBase::~BusStrategyBase() @@ -23,12 +21,6 @@ BusStrategyBase::~BusStrategyBase() delete bus_model; } -void BusStrategyBase::SetConnection() const -{ - QObject::connect(this, &BusStrategyBase::GetBusDirectionSignal, - this, &BusStrategyBase::GetBusDirectionSlot); -} - void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) { rail_node_t *node = rails_model->FindNode(node_id); @@ -37,7 +29,7 @@ void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) // 如果公交车停车且在系统在运行 if(bus_model->direction == BUS_STOP && status == BUS_RUNNING) { - emit GetBusDirectionSignal(); + DetermineBusStatus(); } } @@ -59,7 +51,7 @@ void BusStrategyBase::BusBeginSlot() QString str = PrintState(0); emit PrintStateSignal(str); - emit GetBusDirectionSignal(); + DetermineBusStatus(); } void BusStrategyBase::BusEndSlot() @@ -70,7 +62,7 @@ void BusStrategyBase::BusEndSlot() bus_model->ResetBus(rails_model->rails); } -void BusStrategyBase::GetBusDirectionSlot() +void BusStrategyBase::DetermineBusStatus() { bus_model->target_query = GetTargetQuery(); bus_model->direction = GetBusDirection(); @@ -108,6 +100,8 @@ void BusStrategyBase::HandleQuery() { if(bus_model->target_query->node == bus_model->rail_pos) { + bus_model->direction = BUS_STOP; + // 如果已经到站 while (bus_model->target_query != nullptr and bus_model->target_query->node == bus_model->rail_pos) @@ -118,24 +112,31 @@ void BusStrategyBase::HandleQuery() } // 需要停一tick处理请求 - emit BusRunningSignal(BUS_STOP, 1000); + emit BusRunningSignal(BUS_STOP, Settings::tick); } else { // 没有到站就进行顺便处理 bus_query_t *query = HandleBTWQuery(); - - if(query != nullptr) - { - // 需要停一tick处理请求 - emit BusRunningSignal(BUS_STOP, 1000); - } + bool is_handled = false; while(query != nullptr) { emit DeleteQuerySignal(query->type, query->node->id); query_model->DeleteQuery(query); query = HandleBTWQuery(); + is_handled = true; + } + + if(is_handled) + { + // 需要停一tick处理请求 + bus_model->direction = BUS_STOP; + emit BusRunningSignal(BUS_STOP, Settings::tick); + } + else + { + DetermineBusStatus(); } } } @@ -151,9 +152,8 @@ void BusStrategyBase::OnStopSlot() { bus_model->rail_pos = bus_model->rail_pos->last_node; } - HandleQuery(); - emit GetBusDirectionSignal(); + HandleQuery(); } QString BusStrategyBase::PrintState(int remaining_time) const @@ -164,7 +164,7 @@ QString BusStrategyBase::PrintState(int remaining_time) const if(node == nullptr) { - return QString("No rails"); + return {"No rails"}; } do diff --git a/src/StrategyFactory.cpp b/src/StrategyFactory.cpp index dee32f8..831e259 100644 --- a/src/StrategyFactory.cpp +++ b/src/StrategyFactory.cpp @@ -8,7 +8,7 @@ BusStrategyBase *StrategyFactory::GetStrategy(const QString& file_name) { QByteArray bytes = file_name.toLatin1(); - FILE *config_file = NULL; + FILE *config_file = nullptr; char buffer[30]; int total_station = 0; int distance = 0; @@ -17,7 +17,7 @@ BusStrategyBase *StrategyFactory::GetStrategy(const QString& file_name) fopen_s(&config_file, bytes.data(), "r"); // 循环读取文件的每一行 - while (fgets(buffer, sizeof buffer, config_file) != NULL) + while (fgets(buffer, sizeof buffer, config_file) != nullptr) { char first_char = buffer[0]; char *p; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4903b63..5118226 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -139,6 +139,13 @@ void MainWindow::RunBusClicked() void MainWindow::StopBusClicked() { + // 处理在未加载配置文件的情况下 + // 点击停止按钮自动闪退的问题 + if(controller == nullptr) + { + return; + } + central_widget->ResetOutput(); emit StopBusSignal(); }