Compare commits

...
4 Commits
Author SHA1 Message Date
portersky 5993dd1dc8 feat(cmat): Completed Ex61 Part 2 2026-10-08 10:16:23 +02:00
portersky 4e2e5f72a2 feat(cmat): Completed Ex61 Part 1 2026-10-08 10:08:48 +02:00
portersky 058179b643 feat(cmat): Completed Ex51 2026-10-07 15:13:55 +02:00
porterskyandQwen 3.8 27B a65e84e796 feat(cmat): pass hints to Ex42 observers
Extend the Ex42 observer pattern with Hint objects so each
observer receives the reason for the update instead of
querying the subject:

- Subject::notify() and Observer::update() now take a
  const Hint& argument.
- Add LightHint and WaitHint carrying the traffic light
  state and WAIT button state, defined in Pelican.h.
- Pole dispatches on hint type using dynamic_cast.
- The Pole constructor sets its initial lights directly
  from the Pelican's current state, since no notification
  is sent at startup. Stopgap until the initial update
  is handled properly.

Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
2026-10-07 11:31:16 +02:00
13 changed files with 517 additions and 469 deletions
+3 -1
View File
@@ -2,9 +2,11 @@
#ifndef OBSERVER_H #ifndef OBSERVER_H
#define OBSERVER_H #define OBSERVER_H
class Hint;
class Observer { class Observer {
public: public:
virtual void update() = 0; virtual void update(const Hint&) = 0;
}; };
#endif #endif
+5 -3
View File
@@ -2,6 +2,7 @@
#include "Pelican.h" #include "Pelican.h"
#include "PelicanTimer.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) { 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); myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
@@ -12,11 +13,11 @@ void Pelican::press(void) {
state = YELLOW_STATE; state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME); myTimer->timeoutRequest(YELLOW_TIME);
pressed = true; pressed = true;
notify();
} else if (state != RED_STATE) { } else if (state != RED_STATE) {
pressed = true; pressed = true;
notify();
} }
notify(LightHint(state));
notify(WaitHint(pressed));
} }
void Pelican::timerExpired(void) { void Pelican::timerExpired(void) {
@@ -51,5 +52,6 @@ void Pelican::timerExpired(void) {
break; break;
} }
notify(); notify(LightHint(state));
notify(WaitHint(pressed));
} }
+12
View File
@@ -48,4 +48,16 @@ class Pelican : public Subject {
Pelican &operator=(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 #endif
+16 -4
View File
@@ -1,6 +1,9 @@
#include "Pole.h" #include "Pole.h"
#include "Pelican.h"
#include "Screen.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) { Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
myPelican.attach(*this); 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);
write("/ / ", x, ++y, GRAY); write("/ / ", x, ++y, GRAY);
update(); setTrafficLights(myPelican.getState());
setWaitLight(myPelican.isPressed());
} }
Pole::~Pole() { 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::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
void Pole::update() { void Pole::update(const Hint& hint) {
setTrafficLights(myPelican.getState()); const WaitHint* waitHintPtr;
setWaitLight(myPelican.isPressed()); 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
View File
@@ -14,7 +14,7 @@ class Pole : public Observer {
void setTrafficLights(Pelican::PelicanState state); void setTrafficLights(Pelican::PelicanState state);
void setWaitLight(bool pressed); void setWaitLight(bool pressed);
void update(); void update(const Hint& hint);
private: private:
Pelican &myPelican; Pelican &myPelican;
+2 -2
View File
@@ -5,8 +5,8 @@
void Subject::attach(Observer &obs) { myObservers.insert(&obs); } 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) { for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
(*iter)->update(); (*iter)->update(hint);
} }
} }
+6 -1
View File
@@ -7,11 +7,16 @@ class Observer;
#include <set> #include <set>
class Hint {
public:
virtual ~Hint() = default;
};
class Subject { class Subject {
public: public:
typedef std::set<Observer *> ObsList; typedef std::set<Observer *> ObsList;
void attach(Observer &obs); void attach(Observer &obs);
void notify() const; void notify(const Hint&) const;
private: private:
ObsList myObservers; ObsList myObservers;
+13 -1
View File
@@ -1,8 +1,11 @@
#include "TimerSet.h" #include "TimerSet.h"
#include "Timer.h" #include "Timer.h"
#include <iostream> #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); } void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
@@ -22,3 +25,12 @@ TimerSet &TimerSet::Instance() {
static TimerSet pInstance; static TimerSet pInstance;
return pInstance; return pInstance;
} }
void TimerSet::poll10Hz() {
using namespace std::chrono_literals;
while (true) {
auto const pause = 100ms;
pollAllTimers();
std::this_thread::sleep_for(pause);
}
}
+4
View File
@@ -3,6 +3,7 @@
#include <ctime> #include <ctime>
#include <vector> #include <vector>
#include <thread>
class Timer; class Timer;
@@ -15,6 +16,7 @@ class TimerSet {
TimerSet(); TimerSet();
std::vector<Timer *> timers; std::vector<Timer *> timers;
std::time_t previousTime; std::time_t previousTime;
std::thread pollingThread;
// Meyer's singleton // Meyer's singleton
public: public:
@@ -24,6 +26,8 @@ class TimerSet {
// prevent copying and assignment of the singleton // prevent copying and assignment of the singleton
TimerSet(const TimerSet &); TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &); TimerSet &operator=(const TimerSet &);
void poll10Hz();
}; };
#endif #endif
-5
View File
@@ -16,11 +16,6 @@ int main() {
char c = 0; char c = 0;
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed 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 c = kbd.getChar(); // get the character
if (c == ' ') { if (c == ' ') {
// it's a space, so press the WAIT button // it's a space, so press the WAIT button
+7
View File
@@ -1,10 +1,17 @@
#include "Person.h" #include "Person.h"
#include "PersonException.h" #include "PersonException.h"
#include "YoungPerson.h" #include "YoungPerson.h"
#include <cstddef>
#include <iostream> #include <iostream>
#include <memory>
using std::cout; 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) { Person::Person(std::string name, int age) : name(name), age(age) {
if (age < 0) { if (age < 0) {
throw PersonException("negative age"); throw PersonException("negative age");
+3
View File
@@ -3,6 +3,7 @@
#ifndef Person_h #ifndef Person_h
#define Person_h #define Person_h
#include <memory>
#include <string> #include <string>
class Person { class Person {
@@ -15,6 +16,8 @@ class Person {
void setSalary(int sal); void setSalary(int sal);
static std::shared_ptr<Person> GetPerson(std::string name, int age);
private: private:
Person(const Person &); Person(const Person &);
+3 -9
View File
@@ -1,5 +1,6 @@
#include <exception> #include <exception>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -14,7 +15,7 @@ using std::string;
using std::exception; using std::exception;
int main() { int main() {
std::vector<Person *> people; std::vector<std::shared_ptr<Person>> people;
string namePrompt = "Enter name: "; string namePrompt = "Enter name: ";
string name; string name;
@@ -38,7 +39,7 @@ int main() {
throw PersonException("invalid age"); throw PersonException("invalid age");
} }
Person *personPtr = new Person(name, age); auto personPtr = Person::GetPerson(name, age);
cout << salaryPrompt; cout << salaryPrompt;
cin >> salary; cin >> salary;
@@ -64,12 +65,5 @@ int main() {
cin >> name; 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; return 0;
} }