1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "CubitEventDispatcher.hpp"<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#include "CubitObserver.hpp"
#include "CubitObservable.hpp"
#include <algorithm>

CubitEventDispatcher::CubitEventDispatcher()
{
}

CubitEventDispatcher::~CubitEventDispatcher()
{
}

void CubitEventDispatcher::send_event(CubitObservable* observable, const CubitEvent& event)
{
  if(observable)
  {
      observable->notify_observers(&event);
  }

  std::vector<CubitObserver*>::iterator iter;
  for(iter = mObservers.begin(); iter != mObservers.end(); ++iter)
  {
    (*iter)->notify_observer(&event);
  }
}

void CubitEventDispatcher::add_observer(CubitObserver* obs)
{
  std::vector<CubitObserver*>::iterator iter;
  iter = std::find(mObservers.begin(), mObservers.end(), obs);
  if(iter == mObservers.end())
  {
    mObservers.push_back(obs);
  }
}

void CubitEventDispatcher::remove_observer(CubitObserver* obs)
{
  mObservers.erase(
      std::remove(mObservers.begin(), mObservers.end(), obs),
      mObservers.end()
      );
}