Skip to content

Commit

Permalink
fix: memleaks in race/iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-anne committed Nov 5, 2020
1 parent ebc67db commit d12d896
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/log/Colleague.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void Colleague::notify(std::vector<eng::Car *>cars, Race* r) {
void Colleague::notify(eng::Car *broken, Race *r) {
std::vector<eng::Car*> brokenCar;
brokenCar.push_back(broken);
logisticsDept->sendCarToFactory(brokenCar, r, true)
logisticsDept->sendCarToFactory(brokenCar, r, true);
}


Expand Down
34 changes: 25 additions & 9 deletions src/log/Logistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using namespace lg;

// TODO : @jo Implement and check accuracy of pop and push for cars
// IDEA : approach budget people in the middle to ask for raise?
// IDEA : use up the budget
// TODO : @both fix memleaks

/**
* @author Jo
Expand All @@ -31,15 +34,23 @@ Logistics::Logistics(int numDriverCarPairs) {
carsInSeasonIDs.reserve(numDriverCarPairs);
seasonPointTally.reserve(numDriverCarPairs);
transportManager = nullptr;
raceIterator = nullptr;
racingCalendar = nullptr;
europeanContainer = nullptr;
currentTeamStrategy = nullptr;
tyreSpecs = nullptr;
budget = -1;
}

Logistics::~Logistics() = default;
// TODO : @jo finish destructor
Logistics::~Logistics(){
if (transportManager) delete transportManager;
if (racingCalendar) {
//delete accordingly
}
if (europeanContainer) delete europeanContainer;
if (currentTeamStrategy) delete currentTeamStrategy;
if (tyreSpecs) delete tyreSpecs;
};

/**
* @status completed
Expand Down Expand Up @@ -67,6 +78,7 @@ void Logistics::doYearPlanning() {

//2. Hire emplpoyees: each department
for (auto const&[key, val] : departments) {
//TODO : check by departemente - kan julle dit hanteer dat ons re-hire gegewe 'n tweede seisoen?
val->hireEmployees(budget);
}

Expand Down Expand Up @@ -178,14 +190,11 @@ void Logistics::simulateEvent(Race *r) {
}

//2. Transport Drivers
//IDEA : add fly functionality for drivers
pr::Doc::detail("The two drivers are transported in a luxury mode of transport to ");
pr::Doc::detail(r->getLocation());

//Transport drivers
//IDEA : add fly functionality for drivers

//3. get correct container, transport and fly and fly

transportManager->transport(r->prevRace(), r);
if (r->isRaceEuropean()) {
callRacingDept()->preRaceArrival(carClipboard, drivers, r, getEuropeanContainer(), tyreSpecs);
Expand Down Expand Up @@ -218,7 +227,7 @@ void Logistics::simulateEvent(Race *r) {

/**
* @author Jo
* @status nearly there
* @status I wash my hands off this one
*/
void Logistics::putRacesIntoCalender() {
racingCalendar = new RacesList;
Expand Down Expand Up @@ -257,8 +266,13 @@ void Logistics::putRacesIntoCalender() {
void Logistics::raceSeason() {
//And the season starts
pr::Doc::summary("And the season begins!");
int developTracker = 0;
for (RaceIterator t = racingCalendar->begin(); !(t == racingCalendar->end()); ++t) {
simulateEvent(t.currentItem());
developTracker++;
if (developTracker==7 || developTracker==14) { //third of the way through
carsInDevIDs.push_back(callEngDept()->buildCar(budget));
}
}
}

Expand Down Expand Up @@ -371,13 +385,14 @@ void Logistics::driverBootCamp() {
}
}

void Logistics::sponsoredBudget(int sumPositions) {
void Logistics::sponsoredBudget(int sumPositions) { //default is 0

pr::Doc::summary("Approaching sponsors to negotiate a new budget\n");

if (budget == 0) { //default argument
if (budget == -1) { //default argument in constructor
budget = abs(rand() % 100 + 1);
} else {
//todolist : check accuracy at runtime
if (sumPositions >= 3) {
pr::Doc::detail("Rolex is the team's next sponsor! Budget increases wildly\n");
budget = max((int) ((double) budget * 1.5), (100 - (int) ((double) budget * 0.5)));
Expand All @@ -403,6 +418,7 @@ void Logistics::sponsoredBudget(int sumPositions) {
void Logistics::sendCarToFactory(std::vector<eng::Car *> cars, Race *r, bool isBroken) {
if (isBroken) {
for (int i = 0; i < cars.size(); ++i) {
// TODO: @marike descriptionality of the rush to get the car fixed
transportManager->transport(r, nullptr, cars[0]);
callEngDept()->carArrivesAtFactory(cars[0]);
callEngDept()->fixCar(cars[0]->getId());
Expand Down
2 changes: 1 addition & 1 deletion src/log/Logistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ namespace lg {
std::vector<ppl::Driver *> drivers;
TransportHandler *transportManager;
//Won't be holding a handle to car as will always be passing directly from one place to another
RaceIterator *raceIterator;
RacesList *racingCalendar;
std::vector<int> carsInSeasonIDs;
std::vector<int> carsInDevIDs;
std::vector<Container *> nonEuropeanContainers; //lots of containers for non-European
Container *europeanContainer; //1 container for European
rce::CreateStrategy *currentTeamStrategy;
Expand Down
2 changes: 2 additions & 0 deletions src/log/races/Race.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ int Race::getTrackID() {
return id;
}

Race::~Race() = default;

/*std::ostream &lg::operator<<(std::ostream &os, const Race &rc) {
os << rc.location << '/nEurope: ' << rc.isInEurope << '/nNumLaps: ' << rc.numLaps << std::endl;
return os;
Expand Down
2 changes: 2 additions & 0 deletions src/log/races/Race.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace lg {

Race(std::string, int complexity, bool inEU, int laps, Race *next = nullptr, Race *prev = nullptr);

~Race();

void setNextRace(Race *);

void setPrevRace(Race *);
Expand Down
1 change: 0 additions & 1 deletion src/log/races/RaceIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void RaceIterator::first() {
current = head;
}

//RaceIterator RaceIterator::operator++() {
void RaceIterator::operator++() {
if (this != nullptr) {
this->current = this->current->nextRace();
Expand Down
10 changes: 10 additions & 0 deletions src/log/races/RacesList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Doc.h"

namespace lg {
// TODO : find suitable spot for this function
std::string convertComplexityToString(TrackComplexity t) {
switch (t) {
case Easy:
Expand All @@ -28,6 +29,15 @@ RacesList::RacesList() {
numRaces = 0;
}

RacesList::~RacesList() {
for (RaceIterator t = begin(); !(t == end()); ) {
Race* temp = t.currentItem();
++t;
delete temp;
}

}

//IS DONE?
void RacesList::addRace(Race *race) {
if (isEmpty()) {
Expand Down
6 changes: 2 additions & 4 deletions src/log/races/RacesList.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
#include "RaceIterator.h"

namespace lg {
//class RacesList : public Aggregate {
class RacesList {
friend class RaceIterator;

public:
// RaceIterator createIterator() override;
RacesList();

//RaceIterator createIterator();
~RacesList();

void addRace(Race *);

//Race* removeRace();
Race *getHeadRace() const;

bool isEmpty();
Expand Down
74 changes: 44 additions & 30 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <transportation/Road.h>
#include <transportation/Fly.h>
#include <transportation/Ship.h>
#include <races/RacesList.h>
#include "ppl/factories/KidnapStudent.h"
#include "eng/EngTeam.h"
#include "rce/RacingDep.h"
Expand All @@ -18,23 +19,52 @@ int pr::Doc::transparency = 1;

void logDeptTesting();
void engTeamTesting();
void seasonRun();
void seasonRun(lg::Logistics*);
void testTyres();

void proto_FinalMain();

int main() {

//logDeptTesting();
logDeptTesting();
//engTeamTesting();
seasonRun();
//seasonRun();
// proto_FinalMain();
// testTyres();




}

void logDeptTesting() {
void proto_FinalMain() {
auto* racingDept = new rce::RacingDep();
auto* engDept = new eng::EngTeam;
auto* logDept = new lg::Logistics;

srand(time(0));
logDept->registerNotifier(racingDept);
logDept->registerNotifier(engDept);

seasonRun(logDept);

delete racingDept;
delete engDept;
delete logDept;

}


void seasonRun(lg::Logistics* a) {
a->doYearPlanning();
a->preSeasonPreparation();
a->raceSeason();
a->postSeasonDebrief();
}

void logDeptTesting() {
//Testing containers
/* auto* racingDept = new rce::RacingDep();
auto* engDept = new eng::EngTeam;
auto* a = new lg::Logistics;
lg::TransportHandler* curator = new lg::Road;
Expand All @@ -53,33 +83,17 @@ void logDeptTesting() {
delete racingDept;
delete engDept;
delete a;
delete a;*/
//Testing racingCalender destructor
lg::RacesList* r = new lg::RacesList;
r->addRace(new lg::Race("Australia", 0, 0,12));
r->addRace(new lg::Race("Benoni", 1, 1,21));
r->addRace(new lg::Race("Cambridge", 2, 0,112));
r->addRace(new lg::Race("Dallas", 0, 1,212));
r->printList();
delete r;
}

void seasonRun() {
auto* racingDept = new rce::RacingDep();
auto* engDept = new eng::EngTeam;
auto* a = new lg::Logistics;
a->registerNotifier(racingDept);
a->registerNotifier(engDept);
a->doYearPlanning();
a->preSeasonPreparation();
a->raceSeason();
a->postSeasonDebrief();
delete racingDept;
delete engDept;
delete a;
/*engDept->hireEmployees(50);
engDept->resetTickets();
engDept->setRiskLevel(lg::Aggressive);
int id = engDept->buildCar(50);
eng::Car* car = engDept->checkCarOutOfFactory(id);
car->print();*/

engDept.


}

void engTeamTesting() {
auto *e = new eng::EngTeam();
Expand Down

0 comments on commit d12d896

Please sign in to comment.