Compare commits
4
Commits
11b96e4410
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5993dd1dc8 | ||
|
|
4e2e5f72a2 | ||
|
|
058179b643 | ||
|
|
a65e84e796 |
+12
-10
@@ -1,10 +1,12 @@
|
||||
|
||||
#ifndef OBSERVER_H
|
||||
#define OBSERVER_H
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef OBSERVER_H
|
||||
#define OBSERVER_H
|
||||
|
||||
class Hint;
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
virtual void update(const Hint&) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+57
-55
@@ -1,55 +1,57 @@
|
||||
|
||||
|
||||
#include "Pelican.h"
|
||||
#include "PelicanTimer.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);
|
||||
}
|
||||
|
||||
void Pelican::press(void) {
|
||||
if (state == GREEN_IDLE) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
notify();
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
switch (state) {
|
||||
case RED_STATE:
|
||||
state = YELLOW_FLASHING;
|
||||
myTimer->timeoutRequest(FLASH_TIME);
|
||||
break;
|
||||
|
||||
case YELLOW_FLASHING:
|
||||
state = GREEN_TIMING;
|
||||
myTimer->timeoutRequest(GREEN_TIME);
|
||||
break;
|
||||
|
||||
case GREEN_TIMING:
|
||||
if (pressed) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
} else {
|
||||
state = GREEN_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case GREEN_IDLE:
|
||||
// should never happen
|
||||
break;
|
||||
|
||||
case YELLOW_STATE:
|
||||
state = RED_STATE;
|
||||
pressed = false;
|
||||
myTimer->timeoutRequest(RED_TIME);
|
||||
break;
|
||||
}
|
||||
|
||||
notify();
|
||||
}
|
||||
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
void Pelican::press(void) {
|
||||
if (state == GREEN_IDLE) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
}
|
||||
notify(LightHint(state));
|
||||
notify(WaitHint(pressed));
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
switch (state) {
|
||||
case RED_STATE:
|
||||
state = YELLOW_FLASHING;
|
||||
myTimer->timeoutRequest(FLASH_TIME);
|
||||
break;
|
||||
|
||||
case YELLOW_FLASHING:
|
||||
state = GREEN_TIMING;
|
||||
myTimer->timeoutRequest(GREEN_TIME);
|
||||
break;
|
||||
|
||||
case GREEN_TIMING:
|
||||
if (pressed) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
} else {
|
||||
state = GREEN_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case GREEN_IDLE:
|
||||
// should never happen
|
||||
break;
|
||||
|
||||
case YELLOW_STATE:
|
||||
state = RED_STATE;
|
||||
pressed = false;
|
||||
myTimer->timeoutRequest(RED_TIME);
|
||||
break;
|
||||
}
|
||||
|
||||
notify(LightHint(state));
|
||||
notify(WaitHint(pressed));
|
||||
}
|
||||
|
||||
+63
-51
@@ -1,51 +1,63 @@
|
||||
// ======================================================
|
||||
//
|
||||
// Represents a Pelican crossing
|
||||
//
|
||||
// ======================================================
|
||||
|
||||
#ifndef PELICAN_H
|
||||
#define PELICAN_H
|
||||
|
||||
#include "Subject.h"
|
||||
|
||||
class PelicanTimer;
|
||||
|
||||
class Pelican : public Subject {
|
||||
public:
|
||||
Pelican();
|
||||
|
||||
// call when the WAIT button has been pressed
|
||||
void press(void);
|
||||
|
||||
// states that a Pelican can be in
|
||||
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
|
||||
|
||||
bool isPressed() const { return pressed; }
|
||||
PelicanState getState() const { return state; }
|
||||
|
||||
private:
|
||||
// state that this Pelican is in
|
||||
PelicanState state;
|
||||
|
||||
// called by PelicanTimer when the lights need to change
|
||||
void timerExpired(void);
|
||||
|
||||
const unsigned int RED_TIME;
|
||||
const unsigned int YELLOW_TIME;
|
||||
const unsigned int GREEN_TIME;
|
||||
const unsigned int FLASH_TIME;
|
||||
|
||||
// indicates whether the WAIT light should be on
|
||||
bool pressed;
|
||||
|
||||
// Timer that indicates when the lights should change
|
||||
PelicanTimer *myTimer;
|
||||
|
||||
private:
|
||||
// prevent general use of copy ctor and operator=
|
||||
Pelican(const Pelican &);
|
||||
Pelican &operator=(const Pelican &);
|
||||
};
|
||||
|
||||
#endif
|
||||
// ======================================================
|
||||
//
|
||||
// Represents a Pelican crossing
|
||||
//
|
||||
// ======================================================
|
||||
|
||||
#ifndef PELICAN_H
|
||||
#define PELICAN_H
|
||||
|
||||
#include "Subject.h"
|
||||
|
||||
class PelicanTimer;
|
||||
|
||||
class Pelican : public Subject {
|
||||
public:
|
||||
Pelican();
|
||||
|
||||
// call when the WAIT button has been pressed
|
||||
void press(void);
|
||||
|
||||
// states that a Pelican can be in
|
||||
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
|
||||
|
||||
bool isPressed() const { return pressed; }
|
||||
PelicanState getState() const { return state; }
|
||||
|
||||
private:
|
||||
// state that this Pelican is in
|
||||
PelicanState state;
|
||||
|
||||
// called by PelicanTimer when the lights need to change
|
||||
void timerExpired(void);
|
||||
|
||||
const unsigned int RED_TIME;
|
||||
const unsigned int YELLOW_TIME;
|
||||
const unsigned int GREEN_TIME;
|
||||
const unsigned int FLASH_TIME;
|
||||
|
||||
// indicates whether the WAIT light should be on
|
||||
bool pressed;
|
||||
|
||||
// Timer that indicates when the lights should change
|
||||
PelicanTimer *myTimer;
|
||||
|
||||
private:
|
||||
// prevent general use of copy ctor and operator=
|
||||
Pelican(const Pelican &);
|
||||
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
|
||||
|
||||
+80
-68
@@ -1,68 +1,80 @@
|
||||
#include "Pole.h"
|
||||
#include "Screen.h"
|
||||
|
||||
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
|
||||
myPelican.attach(*this);
|
||||
|
||||
using namespace Screen; // we will discuss namespaces in the course
|
||||
|
||||
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
|
||||
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
|
||||
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
|
||||
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
|
||||
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
|
||||
|
||||
write(" _____ ", x, ++y, GRAY);
|
||||
write(" /___ /| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" | |/| ", x, ++y, GRAY);
|
||||
write(" |____| | ", x, ++y, GRAY);
|
||||
write(" | |/| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" |____|/ ", x, ++y, GRAY);
|
||||
write(" _||__ ", x, ++y, GRAY);
|
||||
write(" /___ /| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" |____|/ ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write("========||===========", x, ++y, GRAY);
|
||||
write(" / || / ", x, ++y, GRAY);
|
||||
write("_ /_________ /_ ", x, ++y, GRAY);
|
||||
write("/ / ", x, ++y, GRAY);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
Pole::~Pole() {
|
||||
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
|
||||
delete lights[i];
|
||||
delete waitLight;
|
||||
}
|
||||
|
||||
void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
|
||||
// red yellow green green man red man
|
||||
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
|
||||
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
|
||||
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
|
||||
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
|
||||
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
|
||||
};
|
||||
|
||||
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
|
||||
lights[i]->set(LightStates[state][i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
|
||||
void Pole::update() {
|
||||
setTrafficLights(myPelican.getState());
|
||||
setWaitLight(myPelican.isPressed());
|
||||
}
|
||||
#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);
|
||||
|
||||
using namespace Screen; // we will discuss namespaces in the course
|
||||
|
||||
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
|
||||
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
|
||||
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
|
||||
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
|
||||
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
|
||||
|
||||
write(" _____ ", x, ++y, GRAY);
|
||||
write(" /___ /| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" | |/| ", x, ++y, GRAY);
|
||||
write(" |____| | ", x, ++y, GRAY);
|
||||
write(" | |/| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" |____|/ ", x, ++y, GRAY);
|
||||
write(" _||__ ", x, ++y, GRAY);
|
||||
write(" /___ /| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" |____|/ ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write("========||===========", x, ++y, GRAY);
|
||||
write(" / || / ", x, ++y, GRAY);
|
||||
write("_ /_________ /_ ", x, ++y, GRAY);
|
||||
write("/ / ", x, ++y, GRAY);
|
||||
|
||||
setTrafficLights(myPelican.getState());
|
||||
setWaitLight(myPelican.isPressed());
|
||||
}
|
||||
|
||||
Pole::~Pole() {
|
||||
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
|
||||
delete lights[i];
|
||||
delete waitLight;
|
||||
}
|
||||
|
||||
void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
|
||||
// red yellow green green man red man
|
||||
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
|
||||
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
|
||||
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
|
||||
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
|
||||
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
|
||||
};
|
||||
|
||||
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
|
||||
lights[i]->set(LightStates[state][i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
+28
-28
@@ -1,28 +1,28 @@
|
||||
|
||||
#ifndef POLE_H
|
||||
#define POLE_H
|
||||
|
||||
#include "Light.h"
|
||||
#include "Observer.h"
|
||||
#include "Pelican.h"
|
||||
|
||||
class Pole : public Observer {
|
||||
public:
|
||||
Pole(int x, int y, Pelican &p);
|
||||
~Pole();
|
||||
|
||||
void setTrafficLights(Pelican::PelicanState state);
|
||||
void setWaitLight(bool pressed);
|
||||
|
||||
void update();
|
||||
|
||||
private:
|
||||
Pelican &myPelican;
|
||||
|
||||
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
|
||||
|
||||
Light *lights[RED_MAN + 1];
|
||||
Light *waitLight;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef POLE_H
|
||||
#define POLE_H
|
||||
|
||||
#include "Light.h"
|
||||
#include "Observer.h"
|
||||
#include "Pelican.h"
|
||||
|
||||
class Pole : public Observer {
|
||||
public:
|
||||
Pole(int x, int y, Pelican &p);
|
||||
~Pole();
|
||||
|
||||
void setTrafficLights(Pelican::PelicanState state);
|
||||
void setWaitLight(bool pressed);
|
||||
|
||||
void update(const Hint& hint);
|
||||
|
||||
private:
|
||||
Pelican &myPelican;
|
||||
|
||||
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
|
||||
|
||||
Light *lights[RED_MAN + 1];
|
||||
Light *waitLight;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+12
-12
@@ -1,12 +1,12 @@
|
||||
|
||||
|
||||
#include "Subject.h"
|
||||
#include "Observer.h"
|
||||
|
||||
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
|
||||
|
||||
void Subject::notify() const {
|
||||
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
|
||||
(*iter)->update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#include "Subject.h"
|
||||
#include "Observer.h"
|
||||
|
||||
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
|
||||
|
||||
void Subject::notify(const Hint& hint) const {
|
||||
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
|
||||
(*iter)->update(hint);
|
||||
}
|
||||
}
|
||||
|
||||
+25
-20
@@ -1,20 +1,25 @@
|
||||
|
||||
|
||||
#ifndef SUBJECT_H
|
||||
#define SUBJECT_H
|
||||
|
||||
class Observer;
|
||||
|
||||
#include <set>
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
typedef std::set<Observer *> ObsList;
|
||||
void attach(Observer &obs);
|
||||
void notify() const;
|
||||
|
||||
private:
|
||||
ObsList myObservers;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef SUBJECT_H
|
||||
#define SUBJECT_H
|
||||
|
||||
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 Hint&) const;
|
||||
|
||||
private:
|
||||
ObsList myObservers;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+36
-24
@@ -1,24 +1,36 @@
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#include <iostream>
|
||||
|
||||
TimerSet::TimerSet() : previousTime(time(NULL)) {}
|
||||
|
||||
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
||||
|
||||
void TimerSet::pollAllTimers() {
|
||||
std::time_t timeNow;
|
||||
if (time(&timeNow) > previousTime) {
|
||||
previousTime = timeNow;
|
||||
|
||||
for (auto it = timers.begin(); it != timers.end(); it++) {
|
||||
(*it)->tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TimerSet &TimerSet::Instance() {
|
||||
// Meyer's Singleton implementation
|
||||
static TimerSet pInstance;
|
||||
return pInstance;
|
||||
}
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
|
||||
pollingThread.detach();
|
||||
}
|
||||
|
||||
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
||||
|
||||
void TimerSet::pollAllTimers() {
|
||||
std::time_t timeNow;
|
||||
if (time(&timeNow) > previousTime) {
|
||||
previousTime = timeNow;
|
||||
|
||||
for (auto it = timers.begin(); it != timers.end(); it++) {
|
||||
(*it)->tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TimerSet &TimerSet::Instance() {
|
||||
// Meyer's Singleton implementation
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
+33
-29
@@ -1,29 +1,33 @@
|
||||
#ifndef TIMERSET_H
|
||||
#define TIMERSET_H
|
||||
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
|
||||
class Timer;
|
||||
|
||||
class TimerSet {
|
||||
public:
|
||||
void pollAllTimers();
|
||||
void addATimer(Timer *);
|
||||
|
||||
private:
|
||||
TimerSet();
|
||||
std::vector<Timer *> timers;
|
||||
std::time_t previousTime;
|
||||
|
||||
// Meyer's singleton
|
||||
public:
|
||||
static TimerSet &Instance();
|
||||
|
||||
private:
|
||||
// prevent copying and assignment of the singleton
|
||||
TimerSet(const TimerSet &);
|
||||
TimerSet &operator=(const TimerSet &);
|
||||
};
|
||||
|
||||
#endif
|
||||
#ifndef TIMERSET_H
|
||||
#define TIMERSET_H
|
||||
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
class Timer;
|
||||
|
||||
class TimerSet {
|
||||
public:
|
||||
void pollAllTimers();
|
||||
void addATimer(Timer *);
|
||||
|
||||
private:
|
||||
TimerSet();
|
||||
std::vector<Timer *> timers;
|
||||
std::time_t previousTime;
|
||||
std::thread pollingThread;
|
||||
|
||||
// Meyer's singleton
|
||||
public:
|
||||
static TimerSet &Instance();
|
||||
|
||||
private:
|
||||
// prevent copying and assignment of the singleton
|
||||
TimerSet(const TimerSet &);
|
||||
TimerSet &operator=(const TimerSet &);
|
||||
|
||||
void poll10Hz();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+27
-32
@@ -1,32 +1,27 @@
|
||||
|
||||
|
||||
#include "Keyboard.h"
|
||||
#include "Pelican.h"
|
||||
#include "Pole.h"
|
||||
#include "TimerSet.h"
|
||||
|
||||
#include <cctype> // provides toupper()
|
||||
|
||||
int main() {
|
||||
Keyboard kbd;
|
||||
Pelican thePelican;
|
||||
Pole pole1(20, 0, thePelican);
|
||||
Pole pole2(40, 0, thePelican);
|
||||
|
||||
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
|
||||
thePelican.press();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#include "Keyboard.h"
|
||||
#include "Pelican.h"
|
||||
#include "Pole.h"
|
||||
#include "TimerSet.h"
|
||||
|
||||
#include <cctype> // provides toupper()
|
||||
|
||||
int main() {
|
||||
Keyboard kbd;
|
||||
Pelican thePelican;
|
||||
Pole pole1(20, 0, thePelican);
|
||||
Pole pole2(40, 0, thePelican);
|
||||
|
||||
char c = 0;
|
||||
|
||||
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
|
||||
c = kbd.getChar(); // get the character
|
||||
if (c == ' ') {
|
||||
// it's a space, so press the WAIT button
|
||||
thePelican.press();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+44
-37
@@ -1,37 +1,44 @@
|
||||
#include "Person.h"
|
||||
#include "PersonException.h"
|
||||
#include "YoungPerson.h"
|
||||
#include <iostream>
|
||||
|
||||
using std::cout;
|
||||
|
||||
Person::Person(std::string name, int age) : name(name), age(age) {
|
||||
if (age < 0) {
|
||||
throw PersonException("negative age");
|
||||
}
|
||||
|
||||
cout << "Person ctor\n";
|
||||
Count++;
|
||||
}
|
||||
|
||||
Person::Person(const Person &) {
|
||||
cout << "Copy ctor\n";
|
||||
Count++;
|
||||
}
|
||||
|
||||
Person::~Person() {
|
||||
cout << "Dtor\n";
|
||||
Count--;
|
||||
}
|
||||
|
||||
void Person::setSalary(int sal) {
|
||||
if (sal < 0) {
|
||||
throw PersonException("negative salary");
|
||||
}
|
||||
|
||||
salary = sal;
|
||||
}
|
||||
|
||||
int Person::GetCount() { return Count; }
|
||||
|
||||
int Person::Count = 0;
|
||||
#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");
|
||||
}
|
||||
|
||||
cout << "Person ctor\n";
|
||||
Count++;
|
||||
}
|
||||
|
||||
Person::Person(const Person &) {
|
||||
cout << "Copy ctor\n";
|
||||
Count++;
|
||||
}
|
||||
|
||||
Person::~Person() {
|
||||
cout << "Dtor\n";
|
||||
Count--;
|
||||
}
|
||||
|
||||
void Person::setSalary(int sal) {
|
||||
if (sal < 0) {
|
||||
throw PersonException("negative salary");
|
||||
}
|
||||
|
||||
salary = sal;
|
||||
}
|
||||
|
||||
int Person::GetCount() { return Count; }
|
||||
|
||||
int Person::Count = 0;
|
||||
|
||||
+31
-28
@@ -1,28 +1,31 @@
|
||||
|
||||
|
||||
#ifndef Person_h
|
||||
#define Person_h
|
||||
|
||||
#include <string>
|
||||
|
||||
class Person {
|
||||
public:
|
||||
virtual ~Person();
|
||||
|
||||
static int GetCount();
|
||||
|
||||
Person(std::string name, int age);
|
||||
|
||||
void setSalary(int sal);
|
||||
|
||||
private:
|
||||
Person(const Person &);
|
||||
|
||||
std::string name;
|
||||
int age;
|
||||
int salary;
|
||||
|
||||
static int Count;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef Person_h
|
||||
#define Person_h
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Person {
|
||||
public:
|
||||
virtual ~Person();
|
||||
|
||||
static int GetCount();
|
||||
|
||||
Person(std::string name, int age);
|
||||
|
||||
void setSalary(int sal);
|
||||
|
||||
static std::shared_ptr<Person> GetPerson(std::string name, int age);
|
||||
|
||||
private:
|
||||
Person(const Person &);
|
||||
|
||||
std::string name;
|
||||
int age;
|
||||
int salary;
|
||||
|
||||
static int Count;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+69
-75
@@ -1,75 +1,69 @@
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Person.h"
|
||||
|
||||
#include "PersonException.h"
|
||||
#include <iostream>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::string;
|
||||
using std::exception;
|
||||
|
||||
int main() {
|
||||
std::vector<Person *> people;
|
||||
|
||||
string namePrompt = "Enter name: ";
|
||||
string name;
|
||||
string agePrompt = "Enter age: ";
|
||||
int age;
|
||||
string salaryPrompt = "Enter salary: ";
|
||||
int salary;
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
|
||||
while (name != "q") {
|
||||
try {
|
||||
cout << agePrompt;
|
||||
cin >> age;
|
||||
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid age");
|
||||
}
|
||||
|
||||
Person *personPtr = new Person(name, age);
|
||||
|
||||
cout << salaryPrompt;
|
||||
cin >> salary;
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid salary");
|
||||
}
|
||||
|
||||
personPtr->setSalary(salary);
|
||||
|
||||
people.push_back(personPtr);
|
||||
}
|
||||
|
||||
catch (exception &e) {
|
||||
cout << "PersonException thrown: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
cout << "There are " << Person::GetCount() << " Person(s), " << people.size() << " in vector\n";
|
||||
|
||||
cout << namePrompt;
|
||||
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;
|
||||
}
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Person.h"
|
||||
|
||||
#include "PersonException.h"
|
||||
#include <iostream>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::string;
|
||||
using std::exception;
|
||||
|
||||
int main() {
|
||||
std::vector<std::shared_ptr<Person>> people;
|
||||
|
||||
string namePrompt = "Enter name: ";
|
||||
string name;
|
||||
string agePrompt = "Enter age: ";
|
||||
int age;
|
||||
string salaryPrompt = "Enter salary: ";
|
||||
int salary;
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
|
||||
while (name != "q") {
|
||||
try {
|
||||
cout << agePrompt;
|
||||
cin >> age;
|
||||
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid age");
|
||||
}
|
||||
|
||||
auto personPtr = Person::GetPerson(name, age);
|
||||
|
||||
cout << salaryPrompt;
|
||||
cin >> salary;
|
||||
if (!cin) {
|
||||
cin.clear();
|
||||
string dummy;
|
||||
cin >> dummy;
|
||||
throw PersonException("invalid salary");
|
||||
}
|
||||
|
||||
personPtr->setSalary(salary);
|
||||
|
||||
people.push_back(personPtr);
|
||||
}
|
||||
|
||||
catch (exception &e) {
|
||||
cout << "PersonException thrown: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
cout << "There are " << Person::GetCount() << " Person(s), " << people.size() << " in vector\n";
|
||||
|
||||
cout << namePrompt;
|
||||
cin >> name;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user