Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    rcf1a07 r4fb5a3  
    1010
    1111#include <iostream>
    12 #include <cassert>
     12
     13#include "Helpers/Assert.hpp"
    1314
    1415using namespace std;
     
    2425// See [Gamma et al, 1995] p. 297
    2526
    26 map<Observable*, int> Observable::depth;
    27 map<Observable*,multimap<int,Observer*>*> Observable::callTable;
    28 set<Observable*> Observable::busyObservables;
    29 
    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 
     27map<Observable*, int> Observable::depth;  //!< Map of Observables to the depth of the DAG of Observers
     28map<Observable*,multimap<int,Observer*>*> Observable::callTable; //!< Table for each Observable of all its Observers
     29set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers
     30
     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 */
    3540void Observable::start_observer_internal(Observable *publisher){
    3641  // increase the count for this observable by one
     
    4045}
    4146
     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 */
    4257void Observable::finish_observer_internal(Observable *publisher){
    4358  // decrease the count for this observable
     
    5368}
    5469
     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 */
    5576Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
    5677  protege(_protege)
     
    5980}
    6081
     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 */
    6188Observable::_Observable_protector::~_Observable_protector()
    6289{
     
    6693/************* Notification mechanism for observables **************/
    6794
    68 
     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 */
    6999void Observable::notifyAll() {
    70100  // we are busy notifying others right now
     
    87117}
    88118
    89 // this handles passing on updates from sub-Observables
     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 */
    90124void Observable::update(Observable *publisher) {
    91125  // circle detection
     
    94128    // observers, but still we are called by one of our sub-Observables
    95129    // we cannot be sure observation will still work at this point
    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;
     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");
    99133    return;
    100134  }
     
    109143}
    110144
    111 // methods to sign-on and off
     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 */
    112150void Observable::signOn(Observer *target,int priority) {
    113   assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]");
     151  ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");
    114152  bool res = false;
    115153  callees_t *callees = 0;
     
    130168}
    131169
     170/** Sign off an Observer from this Observable.
     171 * Removes \a *target from Observable::callTable list.
     172 * \param *target Observer
     173 */
    132174void Observable::signOff(Observer *target) {
    133   assert(callTable.count(this) && "SignOff called for an Observable without Observers.");
     175  ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");
    134176  callees_t *callees = callTable[this];
    135177  callees_t::iterator iter;
     178  callees_t::iterator deliter;
    136179  for(iter=callees->begin();iter!=callees->end();) {
    137180    if((*iter).second == target) {
     
    148191}
    149192
    150 // when an sub-observerable dies we usually don't need to do anything
     193bool 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 */
    151201void Observable::subjectKilled(Observable *publisher){
    152202}
    153203
     204/** Constructor for class Observable.
     205 */
    154206Observable::Observable()
    155207{}
    156208
    157 // when an observable is deleted, we let all our observers know
     209/** Destructor for class Observable.
     210 * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled().
     211 */
    158212Observable::~Observable()
    159213{
     
    170224}
    171225
     226/** Constructor for class Observer.
     227 */
    172228Observer::Observer()
    173229{}
    174230
     231/** Destructor for class Observer.
     232 */
    175233Observer::~Observer()
    176234{}
Note: See TracChangeset for help on using the changeset viewer.