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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "CubitObserver.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 "CubitObservable.hpp"
#include "CubitEvent.hpp"
#include "AppUtil.hpp"

CubitObserver::CubitObserver()
{
  observableCount = 0;
}

CubitObserver::~CubitObserver()
{
  assert(observableCount == 0);
}

CubitStatus CubitObserver::register_observable(CubitObservable *observable)
{
  if (observable == NULL)
    return CUBIT_FAILURE;

    //- add this observer to the observable's list, and if successful,
    //- increment the observable count
  CubitStatus success = observable->add_observer(this);
  
  if (success == CUBIT_SUCCESS)
    observableCount++;
  
  return success;
}

CubitStatus CubitObserver::unregister_observable(CubitObservable *observable)
{
  if (observable == NULL)
    return CUBIT_FAILURE;

    //- remove this observer from the observable's list, and if successful,
    //- decrement the observable count
  CubitStatus success = CUBIT_SUCCESS;

    //- only call the remove function on the observable if we're not being called
    //- from the observable
  success = observable->remove_observer(this);<--- Variable 'success' is reassigned a value before the old one has been used.

  if (success == CUBIT_SUCCESS)
    observableCount--;
  
  return success;
}

CubitStatus CubitObserver::register_observer(CubitObserver* obs)
{
    AppUtil::instance()->event_dispatcher().add_observer(obs);
    return CUBIT_SUCCESS;
}

CubitStatus CubitObserver::unregister_observer(CubitObserver* obs)
{
    AppUtil::instance()->event_dispatcher().remove_observer(obs);
    return CUBIT_SUCCESS;
}