Compare commits
6
Commits
c681ebab2b
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5993dd1dc8 | ||
|
|
4e2e5f72a2 | ||
|
|
058179b643 | ||
|
|
a65e84e796 | ||
|
|
11b96e4410 | ||
|
|
6e0e45110a |
@@ -4,7 +4,7 @@
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
// TODO: declare the pure virtual function update()
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+6
-11
@@ -3,29 +3,25 @@
|
||||
#include "PelicanTimer.h"
|
||||
#include "Pole.h"
|
||||
|
||||
Pelican::Pelican(int x, int y)
|
||||
: pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), myPole(new Pole(x, y)),
|
||||
Pelican::Pelican()
|
||||
: pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5),
|
||||
state(GREEN_IDLE) {
|
||||
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
|
||||
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
notify();
|
||||
}
|
||||
|
||||
Pelican::~Pelican() { delete myPole; }
|
||||
Pelican::~Pelican() { }
|
||||
|
||||
void Pelican::press(void) {
|
||||
if (state == GREEN_IDLE) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
}
|
||||
notify();
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
@@ -60,6 +56,5 @@ void Pelican::timerExpired(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
notify();
|
||||
}
|
||||
|
||||
+4
-4
@@ -7,12 +7,14 @@
|
||||
#ifndef PELICAN_H
|
||||
#define PELICAN_H
|
||||
|
||||
#include "Subject.h"
|
||||
|
||||
class PelicanTimer;
|
||||
class Pole;
|
||||
|
||||
class Pelican {
|
||||
class Pelican : public Subject {
|
||||
public:
|
||||
Pelican(int x, int y);
|
||||
Pelican();
|
||||
~Pelican();
|
||||
|
||||
// call when the WAIT button has been pressed
|
||||
@@ -39,8 +41,6 @@ class Pelican {
|
||||
// indicates whether the WAIT light should be on
|
||||
bool pressed;
|
||||
|
||||
Pole *myPole;
|
||||
|
||||
// Timer that indicates when the lights should change
|
||||
PelicanTimer *myTimer;
|
||||
|
||||
|
||||
+7
-1
@@ -1,8 +1,9 @@
|
||||
#include "Pole.h"
|
||||
#include "Screen.h"
|
||||
|
||||
Pole::Pole(int x, int y) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)) {
|
||||
Pole::Pole(int x, int y, Pelican& pelican) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), pelican(pelican) {
|
||||
using namespace Screen; // we will discuss namespaces in the course
|
||||
pelican.attach(this);
|
||||
|
||||
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
|
||||
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
|
||||
@@ -57,3 +58,8 @@ void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
}
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
|
||||
void Pole::update() {
|
||||
setTrafficLights(pelican.getState());
|
||||
setWaitLight(pelican.isPressed());
|
||||
}
|
||||
|
||||
+6
-3
@@ -3,22 +3,25 @@
|
||||
#define POLE_H
|
||||
|
||||
#include "Light.h"
|
||||
#include "Observer.h"
|
||||
#include "Pelican.h"
|
||||
|
||||
class Pole {
|
||||
class Pole : public Observer {
|
||||
public:
|
||||
Pole(int x, int y);
|
||||
Pole(int x, int y, Pelican&);
|
||||
~Pole();
|
||||
|
||||
void setTrafficLights(Pelican::PelicanState state);
|
||||
void setWaitLight(bool pressed);
|
||||
|
||||
void update();
|
||||
|
||||
private:
|
||||
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
|
||||
|
||||
Light *lights[RED_MAN + 1];
|
||||
|
||||
Light *waitLight;
|
||||
Pelican& pelican;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
|
||||
|
||||
#include "Subject.h"
|
||||
#include "Observer.h"
|
||||
|
||||
// TODO: Implement the Subject::attach() member function that adds an Observer to the set
|
||||
void Subject::attach(Observer* observer) {
|
||||
m_observers.insert(observer);
|
||||
}
|
||||
|
||||
// TODO: Implement the Subject::notify() member function that calls all the update functions
|
||||
void Subject::notify() {
|
||||
for (auto* observer : m_observers) {
|
||||
observer->update();
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -8,11 +8,11 @@ class Observer;
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
// TODO: Declare the attach() function that adds an Observer to the set
|
||||
void attach(Observer* observer);
|
||||
protected:
|
||||
// TODO: Declare the notify() function that calls all the update functions
|
||||
void notify();
|
||||
private:
|
||||
// TODO: Define a set of Observer pointers
|
||||
std::set<Observer*> m_observers;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+4
-1
@@ -1,13 +1,16 @@
|
||||
|
||||
#include "Keyboard.h"
|
||||
#include "Pelican.h"
|
||||
#include "Pole.h"
|
||||
#include "TimerSet.h"
|
||||
|
||||
#include <cctype> // provides toupper()
|
||||
|
||||
int main() {
|
||||
Keyboard kbd;
|
||||
Pelican thePelican(30, 0);
|
||||
Pelican thePelican;
|
||||
Pole pole1(20, 0, thePelican);
|
||||
Pole pole2(40, 0, thePelican);
|
||||
|
||||
char c = 0;
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
#ifndef OBSERVER_H
|
||||
#define OBSERVER_H
|
||||
|
||||
class Hint;
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
virtual void update() = 0;
|
||||
virtual void update(const Hint&) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "Pelican.h"
|
||||
#include "PelicanTimer.h"
|
||||
#include "Subject.h"
|
||||
|
||||
Pelican::Pelican() : pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), state(GREEN_IDLE) {
|
||||
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
|
||||
@@ -12,11 +13,11 @@ void Pelican::press(void) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
notify();
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
notify();
|
||||
}
|
||||
notify(LightHint(state));
|
||||
notify(WaitHint(pressed));
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
@@ -51,5 +52,6 @@ void Pelican::timerExpired(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
notify();
|
||||
notify(LightHint(state));
|
||||
notify(WaitHint(pressed));
|
||||
}
|
||||
|
||||
@@ -48,4 +48,16 @@ class Pelican : public Subject {
|
||||
Pelican &operator=(const Pelican &);
|
||||
};
|
||||
|
||||
class LightHint : public Hint {
|
||||
public:
|
||||
LightHint(Pelican::PelicanState s) : state(s) { }
|
||||
Pelican::PelicanState state;
|
||||
};
|
||||
|
||||
class WaitHint : public Hint {
|
||||
public:
|
||||
WaitHint (bool p) : pressed(p) {}
|
||||
bool pressed;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+16
-4
@@ -1,6 +1,9 @@
|
||||
#include "Pole.h"
|
||||
#include "Pelican.h"
|
||||
#include "Screen.h"
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
|
||||
myPelican.attach(*this);
|
||||
|
||||
@@ -36,7 +39,8 @@ Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12
|
||||
write("_ /_________ /_ ", x, ++y, GRAY);
|
||||
write("/ / ", x, ++y, GRAY);
|
||||
|
||||
update();
|
||||
setTrafficLights(myPelican.getState());
|
||||
setWaitLight(myPelican.isPressed());
|
||||
}
|
||||
|
||||
Pole::~Pole() {
|
||||
@@ -62,7 +66,15 @@ void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
|
||||
void Pole::update() {
|
||||
setTrafficLights(myPelican.getState());
|
||||
setWaitLight(myPelican.isPressed());
|
||||
void Pole::update(const Hint& hint) {
|
||||
const WaitHint* waitHintPtr;
|
||||
waitHintPtr = dynamic_cast<const WaitHint*>(&hint);
|
||||
const LightHint* lightHintPtr;
|
||||
lightHintPtr = dynamic_cast<const LightHint*>(&hint);
|
||||
if (waitHintPtr != nullptr) {
|
||||
setWaitLight(waitHintPtr->pressed);
|
||||
}
|
||||
if (lightHintPtr != nullptr) {
|
||||
setTrafficLights(lightHintPtr->state);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ class Pole : public Observer {
|
||||
void setTrafficLights(Pelican::PelicanState state);
|
||||
void setWaitLight(bool pressed);
|
||||
|
||||
void update();
|
||||
void update(const Hint& hint);
|
||||
|
||||
private:
|
||||
Pelican &myPelican;
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
|
||||
|
||||
void Subject::notify() const {
|
||||
void Subject::notify(const Hint& hint) const {
|
||||
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
|
||||
(*iter)->update();
|
||||
(*iter)->update(hint);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-1
@@ -7,11 +7,16 @@ class Observer;
|
||||
|
||||
#include <set>
|
||||
|
||||
class Hint {
|
||||
public:
|
||||
virtual ~Hint() = default;
|
||||
};
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
typedef std::set<Observer *> ObsList;
|
||||
void attach(Observer &obs);
|
||||
void notify() const;
|
||||
void notify(const Hint&) const;
|
||||
|
||||
private:
|
||||
ObsList myObservers;
|
||||
|
||||
+13
-1
@@ -1,8 +1,11 @@
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
TimerSet::TimerSet() : previousTime(time(NULL)) {}
|
||||
TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
|
||||
pollingThread.detach();
|
||||
}
|
||||
|
||||
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
||||
|
||||
@@ -22,3 +25,12 @@ TimerSet &TimerSet::Instance() {
|
||||
static TimerSet pInstance;
|
||||
return pInstance;
|
||||
}
|
||||
|
||||
void TimerSet::poll10Hz() {
|
||||
using namespace std::chrono_literals;
|
||||
while (true) {
|
||||
auto const pause = 100ms;
|
||||
pollAllTimers();
|
||||
std::this_thread::sleep_for(pause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
class Timer;
|
||||
|
||||
@@ -15,6 +16,7 @@ class TimerSet {
|
||||
TimerSet();
|
||||
std::vector<Timer *> timers;
|
||||
std::time_t previousTime;
|
||||
std::thread pollingThread;
|
||||
|
||||
// Meyer's singleton
|
||||
public:
|
||||
@@ -24,6 +26,8 @@ class TimerSet {
|
||||
// prevent copying and assignment of the singleton
|
||||
TimerSet(const TimerSet &);
|
||||
TimerSet &operator=(const TimerSet &);
|
||||
|
||||
void poll10Hz();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -16,11 +16,6 @@ int main() {
|
||||
char c = 0;
|
||||
|
||||
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
|
||||
|
||||
while (!kbd.isCharAvail()) {
|
||||
TimerSet::Instance().pollAllTimers(); // no key press to process, poll the timers
|
||||
}
|
||||
|
||||
c = kbd.getChar(); // get the character
|
||||
if (c == ' ') {
|
||||
// it's a space, so press the WAIT button
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
#include "Person.h"
|
||||
#include "PersonException.h"
|
||||
#include "YoungPerson.h"
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
using std::cout;
|
||||
|
||||
std::shared_ptr<Person> Person::GetPerson(std::string name, int age) {
|
||||
if (age < 18) return std::make_shared<YoungPerson>(name, age);
|
||||
return std::make_shared<Person>(name, age);
|
||||
}
|
||||
|
||||
Person::Person(std::string name, int age) : name(name), age(age) {
|
||||
if (age < 0) {
|
||||
throw PersonException("negative age");
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#ifndef Person_h
|
||||
#define Person_h
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Person {
|
||||
@@ -15,6 +16,8 @@ class Person {
|
||||
|
||||
void setSalary(int sal);
|
||||
|
||||
static std::shared_ptr<Person> GetPerson(std::string name, int age);
|
||||
|
||||
private:
|
||||
Person(const Person &);
|
||||
|
||||
|
||||
+3
-9
@@ -1,5 +1,6 @@
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -14,7 +15,7 @@ using std::string;
|
||||
using std::exception;
|
||||
|
||||
int main() {
|
||||
std::vector<Person *> people;
|
||||
std::vector<std::shared_ptr<Person>> people;
|
||||
|
||||
string namePrompt = "Enter name: ";
|
||||
string name;
|
||||
@@ -38,7 +39,7 @@ int main() {
|
||||
throw PersonException("invalid age");
|
||||
}
|
||||
|
||||
Person *personPtr = new Person(name, age);
|
||||
auto personPtr = Person::GetPerson(name, age);
|
||||
|
||||
cout << salaryPrompt;
|
||||
cin >> salary;
|
||||
@@ -64,12 +65,5 @@ int main() {
|
||||
cin >> name;
|
||||
}
|
||||
|
||||
// delete all the person(s) in the vector
|
||||
for (std::vector<Person *>::iterator iter = people.begin(); iter != people.end(); ++iter) {
|
||||
delete *iter;
|
||||
}
|
||||
|
||||
cout << "There are " << Person::GetCount() << " Person(s) left dangling\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user