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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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