Commit 48f8a3b4 authored by Martin Kröning's avatar Martin Kröning

Use std::unique_ptr to delete MapWindow in MapWindowWrapper

This also disallows copying the MapWindowWrapper
parent 0bf630e5
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <functional>
#include <memory>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
struct MapWindow; struct MapWindow;
...@@ -8,7 +10,6 @@ struct MapWindow; ...@@ -8,7 +10,6 @@ struct MapWindow;
class MapWindowWrapper { class MapWindowWrapper {
public: public:
MapWindowWrapper(std::string_view windowTitle); MapWindowWrapper(std::string_view windowTitle);
virtual ~MapWindowWrapper();
void addJunction(double x, double y); void addJunction(double x, double y);
void addRoad(std::string_view laneThereName, std::string_view laneBackName, void addRoad(std::string_view laneThereName, std::string_view laneBackName,
const std::vector<double> &coordinates); const std::vector<double> &coordinates);
...@@ -21,5 +22,5 @@ public: ...@@ -21,5 +22,5 @@ public:
void setTimeLabel(std::chrono::minutes time); void setTimeLabel(std::chrono::minutes time);
private: private:
MapWindow *mapWindow; std::unique_ptr<MapWindow, std::function<void(MapWindow *)>> mapWindow;
}; };
#include "MapWindowWrapper.hpp" #include "MapWindowWrapper.hpp"
#include <pi2-view.h> #include <pi2-view.h>
#include <string_view> #include <string_view>
#include <type_traits>
#include <vector> #include <vector>
MapWindowWrapper::MapWindowWrapper(std::string_view windowTitle) MapWindowWrapper::MapWindowWrapper(std::string_view windowTitle)
: mapWindow(mapWindowCreate(windowTitle.data())) {} : mapWindow(mapWindowCreate(windowTitle.data()),
[](MapWindow *mapWindow) { mapWindowDestroy(mapWindow); }) {}
MapWindowWrapper::~MapWindowWrapper() {
mapWindowDestroy(mapWindow);
mapWindow = nullptr;
}
void MapWindowWrapper::addJunction(const double x, const double y) { void MapWindowWrapper::addJunction(const double x, const double y) {
mapWindowAddJunction(mapWindow, x, y); mapWindowAddJunction(mapWindow.get(), x, y);
} }
void MapWindowWrapper::addRoad(std::string_view laneThereName, void MapWindowWrapper::addRoad(std::string_view laneThereName,
std::string_view laneBackName, std::string_view laneBackName,
const std::vector<double> &coordinates) { const std::vector<double> &coordinates) {
mapWindowAddRoad(mapWindow, laneThereName.data(), laneBackName.data(), mapWindowAddRoad(mapWindow.get(), laneThereName.data(), laneBackName.data(),
coordinates.data(), coordinates.size()); coordinates.data(), coordinates.size());
} }
...@@ -26,8 +23,8 @@ void MapWindowWrapper::addOrReplaceBicycle(std::string_view vehicleName, ...@@ -26,8 +23,8 @@ void MapWindowWrapper::addOrReplaceBicycle(std::string_view vehicleName,
std::string_view laneName, std::string_view laneName,
const double positionOnLane, const double positionOnLane,
const double speed) { const double speed) {
mapWindowAddOrReplaceBicycle(mapWindow, vehicleName.data(), laneName.data(), mapWindowAddOrReplaceBicycle(mapWindow.get(), vehicleName.data(),
positionOnLane, speed); laneName.data(), positionOnLane, speed);
} }
void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName, void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName,
...@@ -35,10 +32,10 @@ void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName, ...@@ -35,10 +32,10 @@ void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName,
const double positionOnLane, const double positionOnLane,
const double speed, const double speed,
const double remainingFuel) { const double remainingFuel) {
mapWindowAddOrReplaceCar(mapWindow, vehicleName.data(), laneName.data(), mapWindowAddOrReplaceCar(mapWindow.get(), vehicleName.data(), laneName.data(),
positionOnLane, speed, remainingFuel); positionOnLane, speed, remainingFuel);
} }
void MapWindowWrapper::setTimeLabel(const std::chrono::minutes time) { void MapWindowWrapper::setTimeLabel(const std::chrono::minutes time) {
mapWindowSetTimeLabel(mapWindow, time.count()); mapWindowSetTimeLabel(mapWindow.get(), time.count());
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment