Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    r4fb5a3 rcf1a07  
    1010
    1111#include <iostream>
    12 
    13 #include "Helpers/Assert.hpp"
     12#include <cassert>
    1413
    1514using namespace std;
     
    2524// See [Gamma et al, 1995] p. 297
    2625
    27 map<Observable*, int> Observable::depth;  //!< Map of Observables to the depth of the DAG of Observers
    28 map<Observable*,multimap<int,Observer*>*> Observable::callTable; //!< Table for each Observable of all its Observers
    29 set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers
     26map<Observable*, int> Observable::depth;
     27map<Observable*,multimap<int,Observer*>*> Observable::callTable;
     28set<Observable*> Observable::busyObservables;
    3029
    31 /** Attaching Sub-observables to Observables.
    32  * Increases entry in Observable::depth for this \a *publisher by one.
    33  *
    34  * The two functions \sa start_observer_internal() and \sa finish_observer_internal()
    35  * have to be used together at all time. Never use these functions directly
    36  * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
    37  * thus producing compiler-errors whenever only one is used.
    38  * \param *publisher reference of sub-observable
    39  */
     30// The two functions start_observer_internal and finish_observer_internal
     31// have to be used together at all time. Never use these functions directly
     32// START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
     33// thus producing compiler-errors whenever only one is used
     34
    4035void Observable::start_observer_internal(Observable *publisher){
    4136  // increase the count for this observable by one
     
    4540}
    4641
    47 /** Detaching Sub-observables from Observables.
    48  * Decreases entry in Observable::depth for this \a *publisher by one. If zero, we
    49  * start notifying all our Observers.
    50  *
    51  * The two functions start_observer_internal() and finish_observer_internal()
    52  * have to be used together at all time. Never use these functions directly
    53  * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
    54  * thus producing compiler-errors whenever only one is used.
    55  * \param *publisher reference of sub-observable
    56  */
    5742void Observable::finish_observer_internal(Observable *publisher){
    5843  // decrease the count for this observable
     
    6853}
    6954
    70 /** Constructor for Observable Protector.
    71  * Basically, calls start_observer_internal(). Hence use this class instead of
    72  * calling the function directly.
    73  *
    74  * \param *protege Observable to be protected.
    75  */
    7655Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
    7756  protege(_protege)
     
    8059}
    8160
    82 /** Destructor for Observable Protector.
    83  * Basically, calls finish_observer_internal(). Hence use this class instead of
    84  * calling the function directly.
    85  *
    86  * \param *protege Observable to be protected.
    87  */
    8861Observable::_Observable_protector::~_Observable_protector()
    8962{
     
    9366/************* Notification mechanism for observables **************/
    9467
    95 /** Notify all Observers of changes.
    96  * Puts \a *this into Observable::busyObservables, calls Observer::update() for all in callee_t
    97  * and removes from busy list.
    98  */
     68
    9969void Observable::notifyAll() {
    10070  // we are busy notifying others right now
     
    11787}
    11888
    119 /** Handles passing on updates from sub-Observables.
    120  * Mimicks basically the Observer::update() function.
    121  *
    122  * \param *publisher The \a *this we observe.
    123  */
     89// this handles passing on updates from sub-Observables
    12490void Observable::update(Observable *publisher) {
    12591  // circle detection
     
    12894    // observers, but still we are called by one of our sub-Observables
    12995    // we cannot be sure observation will still work at this point
    130     ASSERT(0,"Circle detected in observation-graph.\n"
    131              "Observation-graph always needs to be a DAG to work correctly!\n"
    132              "Please check your observation code and fix this!\n");
     96    cerr << "Circle detected in observation-graph." << endl;
     97    cerr << "Observation-graph always needs to be a DAG to work correctly!" << endl;
     98    cerr << "Please check your observation code and fix this!" << endl;
    13399    return;
    134100  }
     
    143109}
    144110
    145 /** Sign on an Observer to this Observable.
    146  * Puts \a *target into Observable::callTable list.
    147  * \param *target Observer
    148  * \param priority number in [-20,20]
    149  */
     111// methods to sign-on and off
    150112void Observable::signOn(Observer *target,int priority) {
    151   ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");
     113  assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]");
    152114  bool res = false;
    153115  callees_t *callees = 0;
     
    168130}
    169131
    170 /** Sign off an Observer from this Observable.
    171  * Removes \a *target from Observable::callTable list.
    172  * \param *target Observer
    173  */
    174132void Observable::signOff(Observer *target) {
    175   ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");
     133  assert(callTable.count(this) && "SignOff called for an Observable without Observers.");
    176134  callees_t *callees = callTable[this];
    177135  callees_t::iterator iter;
    178   callees_t::iterator deliter;
    179136  for(iter=callees->begin();iter!=callees->end();) {
    180137    if((*iter).second == target) {
     
    191148}
    192149
    193 bool Observable::isBlocked(){
    194   return depth.count(this) > 0;
    195 }
    196 
    197 /** Handles sub-observables that just got killed
    198  *  when an sub-observerable dies we usually don't need to do anything
    199  *  \param *publisher Sub-Observable.
    200  */
     150// when an sub-observerable dies we usually don't need to do anything
    201151void Observable::subjectKilled(Observable *publisher){
    202152}
    203153
    204 /** Constructor for class Observable.
    205  */
    206154Observable::Observable()
    207155{}
    208156
    209 /** Destructor for class Observable.
    210  * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled().
    211  */
     157// when an observable is deleted, we let all our observers know
    212158Observable::~Observable()
    213159{
     
    224170}
    225171
    226 /** Constructor for class Observer.
    227  */
    228172Observer::Observer()
    229173{}
    230174
    231 /** Destructor for class Observer.
    232  */
    233175Observer::~Observer()
    234176{}
Note: See TracChangeset for help on using the changeset viewer.