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