Nymph Game Engine
Chaiscript based Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
observer.h
Go to the documentation of this file.
1 #ifndef OBSERVER_H
2 #define OBSERVER_H
3 #include <easylogging++.h>
4 #include <memory>
5 #include <queue>
6 #include "events/event.h"
7 #include "events/event_type.h"
8 
9 namespace Events {
13  class Observer {
14  private:
15  std::queue<std::shared_ptr<Event>> events;
16  protected:
17  std::shared_ptr<Event> getEvent() {
18  if(eventsWaiting()) {
19  auto e = events.front();
20  events.pop();
21  return e;
22  }
23  else {
24  return nullptr;
25  }
26  }
27 
28  bool eventsWaiting() const noexcept {
29  return !events.empty();
30  }
31 
32  public:
33  virtual ~Observer() {}
34 
41  void onNotify(std::shared_ptr<Event> event) { events.push(event); }
48  virtual void onNotifyNow(std::shared_ptr<Event> event) = 0;
49 
55  virtual void handleQueuedEvent(std::shared_ptr<Event> event) = 0;
56 
61  while(eventsWaiting()) {
63  }
64  }
65  };
66 }
67 
68 #endif
virtual void onNotifyNow(std::shared_ptr< Event > event)=0
When receiving an event that is immediate, onNotifyNow is used. It acts as an interrupt to make sure ...
virtual void handleQueuedEvent(std::shared_ptr< Event > event)=0
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Interface to be notified of an item's changes.
Definition: observer.h:13
void processEventQueue()
This should be called once ever loop for every Observer to properly use queued events.
Definition: observer.h:60
std::shared_ptr< Event > getEvent()
Definition: observer.h:17
virtual ~Observer()
Definition: observer.h:33
void onNotify(std::shared_ptr< Event > event)
When receiving an event that is non-immediate, onNotify is used. The event is added to the event queu...
Definition: observer.h:41
bool eventsWaiting() const noexcept
Definition: observer.h:28