replaced pointer by smart pointers

This commit is contained in:
2018-12-28 14:17:47 +01:00
parent 07205459d8
commit d4b4a37847
7 changed files with 77 additions and 68 deletions

View File

@@ -319,14 +319,15 @@ void HardwareSetup::run() {
sleep(1); sleep(1);
cout << "Port can't be opened" << endl; cout << "Port can't be opened" << endl;
fd = open(PORT_PATH, O_RDONLY); fd = open(PORT_PATH, O_RDONLY);
if (this->stop) {
break;
}
} }
cout << "Port opened" << endl;
// prepare buffer // prepare buffer
struct TransCheck * received; std::shared_ptr<struct TransCheck> received =
std::make_shared<struct TransCheck>();
uint8_t uintBuf[sizeof(struct TransCheck)]; uint8_t uintBuf[sizeof(struct TransCheck)];
void * voidBuf; // void * voidBuf;
// configure among others non-canonical mode (important) // configure among others non-canonical mode (important)
struct termios config; struct termios config;
tcgetattr(fd, &config); tcgetattr(fd, &config);
@@ -336,19 +337,25 @@ void HardwareSetup::run() {
// read raw data to buffer // read raw data to buffer
ssize_t length = read(fd, uintBuf, sizeof(struct TransCheck)); ssize_t length = read(fd, uintBuf, sizeof(struct TransCheck));
// flush existing data if (this->stop == 0) {
if (static_cast<unsigned long>(length) < sizeof(struct TransCheck)) { cout << "Port opened" << endl;
tcflush(fd, TCIFLUSH); // flush existing data
if (static_cast<unsigned long>(length) <
sizeof(struct TransCheck)) {
tcflush(fd, TCIFLUSH);
}
} }
while (!this->stop) { while (!this->stop) {
// read data // read data
cout << "try to read data" << endl; cout << "try to read data" << endl;
read(this->fd, uintBuf, sizeof(struct TransCheck)); read(this->fd, uintBuf, sizeof(struct TransCheck));
cout << "data read" << endl; cout << "data read" << endl;
voidBuf = static_cast<void *>(uintBuf); // voidBuf = static_cast<void *>(uintBuf);
received = static_cast<struct TransCheck *>(voidBuf); memcpy(received.get(), uintBuf, sizeof(struct TransCheck));
// voidBuf = static_cast<void *>(uintBuf);
// received = static_cast<struct TransCheck *>(voidBuf);
usleep(50000); // 150ms usleep(50000); // 150ms

View File

@@ -43,7 +43,7 @@ MainWindow::MainWindow(QWidget * parent)
QObject::connect(startRennen, SIGNAL(activated()), this, QObject::connect(startRennen, SIGNAL(activated()), this,
SLOT(WindowRennen())); SLOT(WindowRennen()));
this->db = new DataBase; this->db = std::make_shared<DataBase>();
// vector<vector<QString>> daten = db->getData("select * from Fahrer", 2); // vector<vector<QString>> daten = db->getData("select * from Fahrer", 2);
} }
@@ -52,17 +52,18 @@ void MainWindow::closeEvent(QCloseEvent * event) {
} }
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
delete this->db;
delete ui; delete ui;
} }
void MainWindow::WindowTraining() { void MainWindow::WindowTraining() {
this->interfaceTraining = new Training(this, this->db); // this->interfaceTraining = new Training(this, this->db);
this->interfaceTraining = std::make_shared<Training>(this, this->db);
this->interfaceTraining->show(); this->interfaceTraining->show();
} }
void MainWindow::NewWindowSettings() { void MainWindow::NewWindowSettings() {
this->interfaceSettings = new WindowsSettings(this->db, this); this->interfaceSettings =
std::make_shared<WindowsSettings>(this->db.get(), this);
this->interfaceSettings->show(); this->interfaceSettings->show();
} }
@@ -107,15 +108,16 @@ void MainWindow::WindowRennen() {
Q_UNUSED(e); Q_UNUSED(e);
} }
this->interfaceRace = new WindowRace(this->db, this); this->interfaceRace = std::make_shared<WindowRace>(this->db, this);
this->interfaceRace->show(); this->interfaceRace->show();
this->interfaceRennliste = new WindowRennliste(this->db, this); this->interfaceRennliste =
std::make_shared<WindowRennliste>(this->db.get(), this);
this->interfaceRennliste->show(); this->interfaceRennliste->show();
this->interfaceRace->setWindowRennliste(this->interfaceRennliste); this->interfaceRace->setWindowRennliste(this->interfaceRennliste);
this->interfaceRennliste->setWindowRace(this->interfaceRace); this->interfaceRennliste->setWindowRace(this->interfaceRace.get());
} }
void MainWindow::WindowEvaluation() { void MainWindow::WindowEvaluation() {
this->interfaceEvaluation = new Evaluation(this->db); this->interfaceEvaluation = std::make_shared<Evaluation>(this->db.get());
this->interfaceEvaluation->show(); this->interfaceEvaluation->show();
} }

View File

@@ -11,6 +11,7 @@
#include "windowrennliste.h" #include "windowrennliste.h"
#include "windowrennliste2.h" #include "windowrennliste2.h"
#include "windowssettings.h" #include "windowssettings.h"
#include <memory>
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@@ -20,7 +21,7 @@ class MainWindow : public QMainWindow {
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget * parent = 0); explicit MainWindow(QWidget * parent = nullptr);
~MainWindow(); ~MainWindow();
public slots: public slots:
@@ -30,14 +31,14 @@ class MainWindow : public QMainWindow {
void WindowEvaluation(); void WindowEvaluation();
private: private:
Training * interfaceTraining; std::shared_ptr<Training> interfaceTraining;
void closeEvent(QCloseEvent * event); void closeEvent(QCloseEvent * event);
DataBase * db; std::shared_ptr<DataBase> db;
Ui::MainWindow * ui; Ui::MainWindow * ui;
WindowRace * interfaceRace; std::shared_ptr<WindowRace> interfaceRace;
WindowsSettings * interfaceSettings; std::shared_ptr<WindowsSettings> interfaceSettings;
WindowRennliste * interfaceRennliste; std::shared_ptr<WindowRennliste> interfaceRennliste;
Evaluation * interfaceEvaluation; std::shared_ptr<Evaluation> interfaceEvaluation;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@@ -15,7 +15,7 @@ using std::endl;
using std::string; using std::string;
using std::vector; using std::vector;
Training::Training(QWidget * parent, DataBase * db) Training::Training(QWidget * parent, std::shared_ptr<DataBase> db)
: QMainWindow(parent), ui(new Ui::Training) { : QMainWindow(parent), ui(new Ui::Training) {
ui->setupUi(this); ui->setupUi(this);
this->db = db; this->db = db;
@@ -38,12 +38,12 @@ Training::Training(QWidget * parent, DataBase * db)
started = true; started = true;
paused = false; paused = false;
this->Hardware = new HardwareSetup; this->Hardware = std::make_shared<HardwareSetup>();
Hardware->start(); Hardware->start();
QObject::connect(Hardware, SIGNAL(Dea(int, int)), this, QObject::connect(Hardware.get(), SIGNAL(Dea(int, int)), this,
SLOT(deaSlot(int, int))); SLOT(deaSlot(int, int)));
QObject::connect(Hardware, SIGNAL(Shell(int, int)), this, QObject::connect(Hardware.get(), SIGNAL(Shell(int, int)), this,
SLOT(shellSlot(int, int))); SLOT(shellSlot(int, int)));
// QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this, // QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this,
// SLOT(prepareNextRace())); QObject::connect(this->ui->pBStop, // SLOT(prepareNextRace())); QObject::connect(this->ui->pBStop,
@@ -255,6 +255,7 @@ void Training::shellSlot(int time, int sector) {
} }
void Training::closeEvent(QCloseEvent * event) { void Training::closeEvent(QCloseEvent * event) {
Hardware->setStop(); Hardware->setStop();
this->close();
Q_UNUSED(event); Q_UNUSED(event);
} }
@@ -362,6 +363,5 @@ Training::~Training() {
cout << "destr Training" << endl; cout << "destr Training" << endl;
this->Hardware->setStop(); this->Hardware->setStop();
usleep(1000000); usleep(1000000);
delete this->Hardware;
delete ui; delete ui;
} }

View File

@@ -5,6 +5,7 @@
#include "hardwaresetup.h" #include "hardwaresetup.h"
#include "timemodel.h" #include "timemodel.h"
#include <QMainWindow> #include <QMainWindow>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -16,7 +17,7 @@ class Training : public QMainWindow {
Q_OBJECT Q_OBJECT
public: public:
explicit Training(QWidget * parent, DataBase * db); explicit Training(QWidget * parent, std::shared_ptr<DataBase> db);
~Training(); ~Training();
private: private:
@@ -26,7 +27,7 @@ class Training : public QMainWindow {
void closeEvent(QCloseEvent * event); void closeEvent(QCloseEvent * event);
bool started; bool started;
HardwareSetup * Hardware; std::shared_ptr<HardwareSetup> Hardware;
bool firstTimeShell; bool firstTimeShell;
bool firstTimeDea; bool firstTimeDea;
QVector<QVector<int>> VecShell; QVector<QVector<int>> VecShell;
@@ -37,7 +38,7 @@ class Training : public QMainWindow {
bool finished; bool finished;
Counter counterShell; Counter counterShell;
Counter counterDea; Counter counterDea;
DataBase * db; std::shared_ptr<DataBase> db;
long minTimeOneRound; long minTimeOneRound;
int minimumTime; int minimumTime;

View File

@@ -15,7 +15,7 @@ using std::endl;
using std::string; using std::string;
using std::vector; using std::vector;
WindowRace::WindowRace(DataBase * db, QWidget * parent) WindowRace::WindowRace(std::shared_ptr<DataBase> db, QWidget * parent)
: QMainWindow(parent), ui(new Ui::WindowRace) { : QMainWindow(parent), ui(new Ui::WindowRace) {
ui->setupUi(this); ui->setupUi(this);
this->ui->pBNextRace->setEnabled(false); this->ui->pBNextRace->setEnabled(false);
@@ -27,12 +27,12 @@ WindowRace::WindowRace(DataBase * db, QWidget * parent)
vector<vector<QString>> res; vector<vector<QString>> res;
res = db->getData("select dauer from renndauer", 1); res = db->getData("select dauer from renndauer", 1);
this->fahrzeit = res[0][0].toInt(); this->fahrzeit = res.at(0).at(0).toInt();
string statement = string statement =
"select minimumroundtime from rennen order by id DESC limit 1"; "select minimumroundtime from rennen order by id DESC limit 1";
res = db->getData(statement, 1); res = db->getData(statement, 1);
this->minimumTime = res[0][0].toInt(); this->minimumTime = res.at(0).at(0).toInt();
// fill minSecTimes vector // fill minSecTimes vector
statement = "select minsec1, minsec2, minsec3, mindestRundenDauer from " statement = "select minsec1, minsec2, minsec3, mindestRundenDauer from "
@@ -40,10 +40,10 @@ WindowRace::WindowRace(DataBase * db, QWidget * parent)
"DESC limit 1"; "DESC limit 1";
// cout << statement << endl; // cout << statement << endl;
res = db->getData(statement, 4); res = db->getData(statement, 4);
this->minSecTime.append(res[0][0].toInt()); this->minSecTime.append(res.at(0).at(0).toInt());
this->minSecTime.append(res[0][1].toInt()); this->minSecTime.append(res.at(0).at(1).toInt());
this->minSecTime.append(res[0][2].toInt()); this->minSecTime.append(res.at(0).at(2).toInt());
this->minSecTime.append(res[0][3].toInt()); this->minSecTime.append(res.at(0).at(3).toInt());
QVectorHelper::minSec1 = this->minSecTime.at(0); QVectorHelper::minSec1 = this->minSecTime.at(0);
QVectorHelper::minSec2 = this->minSecTime.at(1); QVectorHelper::minSec2 = this->minSecTime.at(1);
@@ -55,28 +55,28 @@ WindowRace::WindowRace(DataBase * db, QWidget * parent)
firstTimeShell = true; firstTimeShell = true;
started = false; started = false;
countdownValue = this->fahrzeit; countdownValue = this->fahrzeit;
countdown = new Countdown; countdown = std::make_shared<Countdown>();
startAmpelThread = new Ampel; startAmpelThread = std::make_shared<Ampel>();
ampelCounter = 0; ampelCounter = 0;
ui->pBStart->setFocus(); ui->pBStart->setFocus();
paused = false; paused = false;
Hardware = new HardwareSetup; Hardware = std::make_shared<HardwareSetup>();
Hardware->start(); Hardware->start();
QObject::connect(Hardware, SIGNAL(Dea(int, int)), this, QObject::connect(Hardware.get(), SIGNAL(Dea(int, int)), this,
SLOT(deaSlot(int, int))); SLOT(deaSlot(int, int)));
QObject::connect(Hardware, SIGNAL(Shell(int, int)), this, QObject::connect(Hardware.get(), SIGNAL(Shell(int, int)), this,
SLOT(shellSlot(int, int))); SLOT(shellSlot(int, int)));
QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go())); QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go()));
QObject::connect(countdown, SIGNAL(CountdownUpdate()), this, QObject::connect(countdown.get(), SIGNAL(CountdownUpdate()), this,
SLOT(countdownUpdate())); SLOT(countdownUpdate()));
QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, QObject::connect(startAmpelThread.get(), SIGNAL(ampelUpdate()), this,
SLOT(ampelSlot())); SLOT(ampelSlot()));
QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, QObject::connect(startAmpelThread.get(), SIGNAL(ampelUpdate()), this,
SLOT(laufcheck())); SLOT(laufcheck()));
QObject::connect(ui->pBBreak, SIGNAL(clicked()), this, QObject::connect(ui->pBBreak, SIGNAL(clicked()), this,
SLOT(breakCounter())); SLOT(breakCounter()));
@@ -264,18 +264,19 @@ void WindowRace::stopClicked() {
this->prepareNextRace(); this->prepareNextRace();
} }
void WindowRace::setWindowRennliste(WindowRennliste * ptrInstance) { void WindowRace::setWindowRennliste(
std::shared_ptr<WindowRennliste> ptrInstance) {
this->wRennliste = ptrInstance; this->wRennliste = ptrInstance;
this->wRennlisteSeted = true; this->wRennlisteSeted = true;
QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this->wRennliste, QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()),
SLOT(changeSelection())); this->wRennliste.get(), SLOT(changeSelection()));
} }
void WindowRace::setDriverAndCarId(vector<QString> vec) { void WindowRace::setDriverAndCarId(vector<QString> vec) {
this->shellDriverId = vec[0].toInt(); this->shellDriverId = vec.at(0).toInt();
this->shellCarId = vec[1].toInt(); this->shellCarId = vec.at(1).toInt();
this->deaDriverId = vec[2].toInt(); this->deaDriverId = vec.at(2).toInt();
this->deaCarId = vec[3].toInt(); this->deaCarId = vec.at(3).toInt();
} }
void WindowRace::closeEvent(QCloseEvent * event) { void WindowRace::closeEvent(QCloseEvent * event) {
countdown->setStop(); countdown->setStop();
@@ -359,11 +360,7 @@ WindowRace::~WindowRace() {
this->startAmpelThread->setStop(); this->startAmpelThread->setStop();
this->countdown->setStop(); this->countdown->setStop();
this->Hardware->setStop(); this->Hardware->setStop();
usleep(1000010); // eine Sekunde usleep(1000000); // 1 second
delete this->countdown;
delete this->Hardware;
delete this->startAmpelThread;
// delete this->db; // delete this->db;

View File

@@ -23,9 +23,10 @@ class WindowRace : public QMainWindow {
Q_OBJECT Q_OBJECT
public: public:
explicit WindowRace(DataBase * db, QWidget * parent = 0); explicit WindowRace(std::shared_ptr<DataBase> db,
QWidget * parent = nullptr);
~WindowRace(); ~WindowRace();
void setWindowRennliste(WindowRennliste * ptrInstance); void setWindowRennliste(std::shared_ptr<WindowRennliste> ptrInstance);
void setDriverAndCar(vector<QString> vec); void setDriverAndCar(vector<QString> vec);
void setDriverAndCarId(vector<QString> vec); void setDriverAndCarId(vector<QString> vec);
@@ -37,8 +38,8 @@ class WindowRace : public QMainWindow {
void closeEvent(QCloseEvent * event); void closeEvent(QCloseEvent * event);
bool started; bool started;
Countdown * countdown; std::shared_ptr<Countdown> countdown;
HardwareSetup * Hardware; std::shared_ptr<HardwareSetup> Hardware;
Ui::WindowRace * ui; Ui::WindowRace * ui;
Counter counterShell; Counter counterShell;
Counter counterDea; Counter counterDea;
@@ -51,12 +52,12 @@ class WindowRace : public QMainWindow {
long getMinimum(std::vector<long> a); long getMinimum(std::vector<long> a);
QString timeWrapper(long zahl); QString timeWrapper(long zahl);
long countdownValue; // in sec long countdownValue; // in sec
Ampel * startAmpelThread; std::shared_ptr<Ampel> startAmpelThread;
int ampelCounter; int ampelCounter;
bool paused; bool paused;
DataBase * db; std::shared_ptr<DataBase> db;
bool finished; bool finished;
WindowRennliste * wRennliste; std::shared_ptr<WindowRennliste> wRennliste;
bool wRennlisteSeted; bool wRennlisteSeted;
int fahrzeit; int fahrzeit;
int renn_id; int renn_id;