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
#include <chrono>
#include <functional>
#include <memory>
#include <string_view>
#include <vector>
struct MapWindow;
......@@ -8,7 +10,6 @@ struct MapWindow;
class MapWindowWrapper {
public:
MapWindowWrapper(std::string_view windowTitle);
virtual ~MapWindowWrapper();
void addJunction(double x, double y);
void addRoad(std::string_view laneThereName, std::string_view laneBackName,
const std::vector<double> &coordinates);
......@@ -21,5 +22,5 @@ public:
void setTimeLabel(std::chrono::minutes time);
private:
MapWindow *mapWindow;
std::unique_ptr<MapWindow, std::function<void(MapWindow *)>> mapWindow;
};
#include "MapWindowWrapper.hpp"
#include <pi2-view.h>
#include <string_view>
#include <type_traits>
#include <vector>
MapWindowWrapper::MapWindowWrapper(std::string_view windowTitle)
: mapWindow(mapWindowCreate(windowTitle.data())) {}
MapWindowWrapper::~MapWindowWrapper() {
mapWindowDestroy(mapWindow);
mapWindow = nullptr;
}
: mapWindow(mapWindowCreate(windowTitle.data()),
[](MapWindow *mapWindow) { mapWindowDestroy(mapWindow); }) {}
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,
std::string_view laneBackName,
const std::vector<double> &coordinates) {
mapWindowAddRoad(mapWindow, laneThereName.data(), laneBackName.data(),
mapWindowAddRoad(mapWindow.get(), laneThereName.data(), laneBackName.data(),
coordinates.data(), coordinates.size());
}
......@@ -26,8 +23,8 @@ void MapWindowWrapper::addOrReplaceBicycle(std::string_view vehicleName,
std::string_view laneName,
const double positionOnLane,
const double speed) {
mapWindowAddOrReplaceBicycle(mapWindow, vehicleName.data(), laneName.data(),
positionOnLane, speed);
mapWindowAddOrReplaceBicycle(mapWindow.get(), vehicleName.data(),
laneName.data(), positionOnLane, speed);
}
void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName,
......@@ -35,10 +32,10 @@ void MapWindowWrapper::addOrReplaceCar(std::string_view vehicleName,
const double positionOnLane,
const double speed,
const double remainingFuel) {
mapWindowAddOrReplaceCar(mapWindow, vehicleName.data(), laneName.data(),
mapWindowAddOrReplaceCar(mapWindow.get(), vehicleName.data(), laneName.data(),
positionOnLane, speed, remainingFuel);
}
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