Commit b677c262 authored by Martin Kröning's avatar Martin Kröning

Use `std::string_view` instead of `const std::string &`

parent 2222f3b0
#pragma once #pragma once
#include "Vehicle.hpp" #include "Vehicle.hpp"
#include <string> #include <string_view>
class MapAppAPIWrapper; class MapAppAPIWrapper;
class Bicycle : public Vehicle { class Bicycle : public Vehicle {
public: public:
Bicycle(std::string name, double topSpeed, double timeOfStart, double time); Bicycle(std::string name, double topSpeed, double timeOfStart, double time);
void draw(MapAppAPIWrapper &mapApp, const std::string &laneName, void draw(MapAppAPIWrapper &mapApp, std::string_view laneName,
double laneLength, double speedLimit) const override; double laneLength, double speedLimit) const override;
double getSpeed(double speedLimit) const override; double getSpeed(double speedLimit) const override;
......
...@@ -2,15 +2,16 @@ ...@@ -2,15 +2,16 @@
#include "Vehicle.hpp" #include "Vehicle.hpp"
#include <string> #include <string>
#include <string_view>
class MapAppAPIWrapper; class MapAppAPIWrapper;
class Car : public Vehicle { class Car : public Vehicle {
public: public:
/// The fuel tank is half full at construction /// The fuel tank is half full at construction
Car(std::string name, double topSpeed, double timeOfStart, double fuelConsumption, Car(std::string name, double topSpeed, double timeOfStart,
double fuelCapacity, double time); double fuelConsumption, double fuelCapacity, double time);
void draw(MapAppAPIWrapper &mapApp, const std::string &laneName, void draw(MapAppAPIWrapper &mapApp, std::string_view laneName,
double laneLength, double speedLimit) const override; double laneLength, double speedLimit) const override;
double getSpeed(double speedLimit) const override; double getSpeed(double speedLimit) const override;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include <random> #include <random>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
class Vehicle; class Vehicle;
class MapAppAPIWrapper; class MapAppAPIWrapper;
...@@ -14,9 +15,8 @@ public: ...@@ -14,9 +15,8 @@ public:
/// Creates two lanes between \p junctionA and \p junctionB /// Creates two lanes between \p junctionA and \p junctionB
static void connect(const std::weak_ptr<Junction> &junctionA, static void connect(const std::weak_ptr<Junction> &junctionA,
const std::weak_ptr<Junction> &junctionB, const std::weak_ptr<Junction> &junctionB,
const std::string &laneAToBName, std::string laneAToBName, std::string laneBToAName,
const std::string &laneBToAName, double length, double length, double speedLimit, bool noPassing);
double speedLimit, bool noPassing);
Junction(Junction &&other) noexcept; Junction(Junction &&other) noexcept;
Junction &operator=(Junction &&other) noexcept; Junction &operator=(Junction &&other) noexcept;
...@@ -34,7 +34,7 @@ public: ...@@ -34,7 +34,7 @@ public:
double time); double time);
void acceptVehicle(std::unique_ptr<Vehicle> vehicle, void acceptVehicle(std::unique_ptr<Vehicle> vehicle,
const std::string &prohibitedLaneName); std::string_view prohibitedLaneName);
private: private:
/// Mersenne Twister random number generator /// Mersenne Twister random number generator
...@@ -51,5 +51,5 @@ private: ...@@ -51,5 +51,5 @@ private:
Lane &randomOutboundLane(); Lane &randomOutboundLane();
Lane &randomOutboundLaneExcept(const std::string &laneName); Lane &randomOutboundLaneExcept(std::string_view laneName);
}; };
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
#include <chrono> #include <chrono>
#include <iosfwd> #include <iosfwd>
#include <memory> #include <memory>
#include <string> #include <string_view>
#include <vector> #include <vector>
class Junction; class Junction;
class Map { class Map {
public: public:
Map(std::istream &is, const std::string &windowTitle); Map(std::istream &is, std::string_view windowTitle);
void simulate(std::chrono::seconds duration, double speedFactor, void simulate(std::chrono::seconds duration, double speedFactor,
double frequency); double frequency);
...@@ -20,7 +20,7 @@ private: ...@@ -20,7 +20,7 @@ private:
std::vector<std::shared_ptr<Junction>> junctions; std::vector<std::shared_ptr<Junction>> junctions;
MapAppAPIWrapper mapApp; MapAppAPIWrapper mapApp;
std::weak_ptr<Junction> getJunction(const std::string &name); std::weak_ptr<Junction> getJunction(std::string_view name);
/// Required format: /// Required format:
/// `name` `fuelAmount` `x` `y` /// `name` `fuelAmount` `x` `y`
......
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <string> #include <string_view>
#include <vector> #include <vector>
struct MapApp; struct MapApp;
class MapAppAPIWrapper { class MapAppAPIWrapper {
public: public:
MapAppAPIWrapper(const std::string &windowTitle); MapAppAPIWrapper(std::string_view windowTitle);
virtual ~MapAppAPIWrapper(); virtual ~MapAppAPIWrapper();
void addJunction(double x, double y); void addJunction(double x, double y);
void addRoad(const std::string &laneThereName, void addRoad(std::string_view laneThereName, std::string_view laneBackName,
const std::string &laneBackName,
const std::vector<double> &coordinates); const std::vector<double> &coordinates);
void addOrReplaceBicycle(const std::string &vehicleName, void addOrReplaceBicycle(std::string_view vehicleName,
const std::string &laneName, double positionOnLane, std::string_view laneName, double positionOnLane,
double speed); double speed);
void addOrReplaceCar(const std::string &vehicleName, void addOrReplaceCar(std::string_view vehicleName, std::string_view laneName,
const std::string &laneName, double positionOnLane, double positionOnLane, double speed,
double speed, double remainingFuel); double remainingFuel);
void setDurationLabel(std::chrono::minutes minutes); void setDurationLabel(std::chrono::minutes minutes);
private: private:
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
#include "Named.hpp" #include "Named.hpp"
#include <string> #include <string>
#include <string_view>
class MapAppAPIWrapper; class MapAppAPIWrapper;
class Vehicle : public Named { class Vehicle : public Named {
public: public:
// Draws the vehicle on the provided \p mapApp on the specified \p laneName // Draws the vehicle on the provided \p mapApp on the specified \p laneName
// which has a \p laneLength and a \p speedLimit // which has a \p laneLength and a \p speedLimit
virtual void draw(MapAppAPIWrapper &mapApp, const std::string &laneName, virtual void draw(MapAppAPIWrapper &mapApp, std::string_view laneName,
double laneLength, double speedLimit) const = 0; double laneLength, double speedLimit) const = 0;
/// \returns The current speed in km/h /// \returns The current speed in km/h
......
...@@ -8,7 +8,7 @@ Bicycle::Bicycle(std::string name, const double topSpeed, ...@@ -8,7 +8,7 @@ Bicycle::Bicycle(std::string name, const double topSpeed,
const double timeOfStart, const double time) const double timeOfStart, const double time)
: Vehicle(std::move(name), topSpeed, timeOfStart, time) {} : Vehicle(std::move(name), topSpeed, timeOfStart, time) {}
void Bicycle::draw(MapAppAPIWrapper &mapApp, const std::string &laneName, void Bicycle::draw(MapAppAPIWrapper &mapApp, std::string_view laneName,
const double laneLength, const double speedLimit) const { const double laneLength, const double speedLimit) const {
const auto positionOnLane = getDistanceOnLane() / laneLength; const auto positionOnLane = getDistanceOnLane() / laneLength;
mapApp.addOrReplaceBicycle(getName(), laneName, positionOnLane, mapApp.addOrReplaceBicycle(getName(), laneName, positionOnLane,
......
...@@ -10,7 +10,7 @@ Car::Car(std::string name, const double topSpeed, const double timeOfStart, ...@@ -10,7 +10,7 @@ Car::Car(std::string name, const double topSpeed, const double timeOfStart,
fuelConsumption(fuelConsumption), fuelCapacity(fuelCapacity), fuelConsumption(fuelConsumption), fuelCapacity(fuelCapacity),
fuel(fuelCapacity / 2) {} fuel(fuelCapacity / 2) {}
void Car::draw(MapAppAPIWrapper &mapApp, const std::string &laneName, void Car::draw(MapAppAPIWrapper &mapApp, std::string_view laneName,
const double laneLength, const double speedLimit) const { const double laneLength, const double speedLimit) const {
const auto positionOnLane = getDistanceOnLane() / laneLength; const auto positionOnLane = getDistanceOnLane() / laneLength;
mapApp.addOrReplaceCar(getName(), laneName, positionOnLane, mapApp.addOrReplaceCar(getName(), laneName, positionOnLane,
......
...@@ -9,12 +9,12 @@ class MapAppAPIWrapper; ...@@ -9,12 +9,12 @@ class MapAppAPIWrapper;
void Junction::connect(const std::weak_ptr<Junction> &junctionA, void Junction::connect(const std::weak_ptr<Junction> &junctionA,
const std::weak_ptr<Junction> &junctionB, const std::weak_ptr<Junction> &junctionB,
const std::string &laneAToBName, std::string laneAToBName, std::string laneBToAName,
const std::string &laneBToAName, const double length, const double length, const double speedLimit,
const double speedLimit, const bool noPassing) { const bool noPassing) {
Lane laneAToB(laneAToBName, laneBToAName, length, speedLimit, noPassing, Lane laneAToB(laneAToBName, laneBToAName, length, speedLimit, noPassing,
junctionB); junctionB);
Lane laneBToA(laneBToAName, laneAToBName, length, speedLimit, noPassing, Lane laneBToA(std::move(laneBToAName), std::move(laneAToBName), length, speedLimit, noPassing,
junctionA); junctionA);
junctionA.lock()->outboundLanes.push_back(std::move(laneAToB)); junctionA.lock()->outboundLanes.push_back(std::move(laneAToB));
junctionB.lock()->outboundLanes.push_back(std::move(laneBToA)); junctionB.lock()->outboundLanes.push_back(std::move(laneBToA));
...@@ -54,7 +54,7 @@ void Junction::createBicycle(std::string name, const double topSpeed, ...@@ -54,7 +54,7 @@ void Junction::createBicycle(std::string name, const double topSpeed,
} }
void Junction::acceptVehicle(std::unique_ptr<Vehicle> vehicle, void Junction::acceptVehicle(std::unique_ptr<Vehicle> vehicle,
const std::string &prohibitedLaneName) { std::string_view prohibitedLaneName) {
fillUp(*vehicle); fillUp(*vehicle);
vehicle->resetDistanceOnLane(); vehicle->resetDistanceOnLane();
randomOutboundLaneExcept(prohibitedLaneName) randomOutboundLaneExcept(prohibitedLaneName)
...@@ -79,7 +79,7 @@ Lane &Junction::randomOutboundLane() { ...@@ -79,7 +79,7 @@ Lane &Junction::randomOutboundLane() {
return outboundLanes.at(dis(gen)); return outboundLanes.at(dis(gen));
} }
Lane &Junction::randomOutboundLaneExcept(const std::string &laneName) { Lane &Junction::randomOutboundLaneExcept(std::string_view laneName) {
auto &randomLane = randomOutboundLane(); auto &randomLane = randomOutboundLane();
return randomLane.getName() != laneName || outboundLanes.size() <= 1 return randomLane.getName() != laneName || outboundLanes.size() <= 1
? randomLane ? randomLane
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
Map::Map(std::istream &is, const std::string &windowTitle) Map::Map(std::istream &is, std::string_view windowTitle)
: time(0), mapApp(windowTitle) { : time(0), mapApp(windowTitle) {
std::string type; std::string type;
while (is >> type) { while (is >> type) {
...@@ -46,7 +46,7 @@ void Map::simulate(const std::chrono::seconds duration, ...@@ -46,7 +46,7 @@ void Map::simulate(const std::chrono::seconds duration,
} }
} }
std::weak_ptr<Junction> Map::getJunction(const std::string &name) { std::weak_ptr<Junction> Map::getJunction(std::string_view name) {
return *std::find_if( return *std::find_if(
junctions.begin(), junctions.end(), junctions.begin(), junctions.end(),
[&name](const auto &junction) { return junction->getName() == name; }); [&name](const auto &junction) { return junction->getName() == name; });
......
#include "MapAppAPIWrapper.hpp" #include "MapAppAPIWrapper.hpp"
#include "pi2-view.h" #include "pi2-view.h"
#include <string> #include <string_view>
#include <vector> #include <vector>
MapAppAPIWrapper::MapAppAPIWrapper(const std::string &windowTitle) MapAppAPIWrapper::MapAppAPIWrapper(std::string_view windowTitle)
: mapApp(mapAppCreate(windowTitle.c_str())) {} : mapApp(mapAppCreate(windowTitle.data())) {}
MapAppAPIWrapper::~MapAppAPIWrapper() { MapAppAPIWrapper::~MapAppAPIWrapper() {
mapAppDestroy(mapApp); mapAppDestroy(mapApp);
...@@ -15,27 +15,27 @@ void MapAppAPIWrapper::addJunction(const double x, const double y) { ...@@ -15,27 +15,27 @@ void MapAppAPIWrapper::addJunction(const double x, const double y) {
mapAppAddJunction(mapApp, x, y); mapAppAddJunction(mapApp, x, y);
} }
void MapAppAPIWrapper::addRoad(const std::string &laneThereName, void MapAppAPIWrapper::addRoad(std::string_view laneThereName,
const std::string &laneBackName, std::string_view laneBackName,
const std::vector<double> &coordinates) { const std::vector<double> &coordinates) {
mapAppAddRoad(mapApp, laneThereName.c_str(), laneBackName.c_str(), mapAppAddRoad(mapApp, laneThereName.data(), laneBackName.data(),
coordinates.data(), coordinates.size()); coordinates.data(), coordinates.size());
} }
void MapAppAPIWrapper::addOrReplaceBicycle(const std::string &vehicleName, void MapAppAPIWrapper::addOrReplaceBicycle(std::string_view vehicleName,
const std::string &laneName, std::string_view laneName,
const double positionOnLane, const double positionOnLane,
const double speed) { const double speed) {
mapAppAddOrReplaceBicycle(mapApp, vehicleName.c_str(), laneName.c_str(), mapAppAddOrReplaceBicycle(mapApp, vehicleName.data(), laneName.data(),
positionOnLane, speed); positionOnLane, speed);
} }
void MapAppAPIWrapper::addOrReplaceCar(const std::string &vehicleName, void MapAppAPIWrapper::addOrReplaceCar(std::string_view vehicleName,
const std::string &laneName, std::string_view laneName,
const double positionOnLane, const double positionOnLane,
const double speed, const double speed,
const double remainingFuel) { const double remainingFuel) {
mapAppAddOrReplaceCar(mapApp, vehicleName.c_str(), laneName.c_str(), mapAppAddOrReplaceCar(mapApp, vehicleName.data(), laneName.data(),
positionOnLane, speed, remainingFuel); positionOnLane, speed, remainingFuel);
} }
......
#pragma once #pragma once
#include <Drawable.hpp> #include <Drawable.hpp>
#include <string> #include <string_view>
#include <valarray> #include <valarray>
#include <vector> #include <vector>
namespace Cairo { namespace Cairo {
...@@ -17,10 +17,10 @@ public: ...@@ -17,10 +17,10 @@ public:
void draw(const Cairo::RefPtr<Cairo::Context> &cr) const override; void draw(const Cairo::RefPtr<Cairo::Context> &cr) const override;
void getRegion(int &x, int &y, int &width, int &height) const override; void getRegion(int &x, int &y, int &width, int &height) const override;
void getCoordinatesOf(const std::string &laneName, double positionOnLane, void getCoordinatesOf(std::string_view laneName, double positionOnLane,
double &x, double &y) const; double &x, double &y) const;
bool contains(const std::string &laneName) const; bool contains(std::string_view laneName) const;
private: private:
const static double width; const static double width;
......
...@@ -62,7 +62,7 @@ void DrawableRoad::getRegion(int &x, int &y, int &width, int &height) const { ...@@ -62,7 +62,7 @@ void DrawableRoad::getRegion(int &x, int &y, int &width, int &height) const {
} }
} }
void DrawableRoad::getCoordinatesOf(const std::string &laneName, void DrawableRoad::getCoordinatesOf(std::string_view laneName,
const double positionOnLane, double &x, const double positionOnLane, double &x,
double &y) const { double &y) const {
if (positionOnLane < 0 || positionOnLane > 1) { if (positionOnLane < 0 || positionOnLane > 1) {
...@@ -107,6 +107,6 @@ void DrawableRoad::getCoordinatesOf(const std::string &laneName, ...@@ -107,6 +107,6 @@ void DrawableRoad::getCoordinatesOf(const std::string &laneName,
y = currentCoordinatePair[1]; y = currentCoordinatePair[1];
} }
bool DrawableRoad::contains(const std::string &laneName) const { bool DrawableRoad::contains(std::string_view laneName) const {
return laneThereName == laneName || laneBackName == laneName; return laneThereName == laneName || laneBackName == laneName;
} }
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