/*
*
* Copyright (c) 1996
* Knowledge Science Institute, University of Calgary
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. The Knowledge Science Institute makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*/
#include
#include
//declaration
Subject::Subject() : Observers()
{
}
Subject::Subject(const Subject& s) : Observers() // Observers are NOT copied
{
}
Subject::~Subject()
{
notifyDestruction();
}
void Subject::attachObserver(Observer* o)
{
if (o) Observers.push_back(o);
}
void Subject::notifyDestruction()
{
for (list::iterator i=Observers.begin(); i!=Observers.end(); i++)
if (*i) (*i)->subjectDestroyed(this);
clearObservers();
}
void Subject::detachObserver(Observer* o)
{
//only the the first should be removed if multiple instances of o exist
for (list::iterator i=Observers.begin(); i!=Observers.end(); i++)
if ((*i)==o) {Observers.erase(i); return;}
error(0,'E',"Subject::detachObserver: called for unattached observer of type '%s'",typeid(o).name());
}
void Subject::notify()
{
for (list::iterator i=Observers.begin(); i!=Observers.end(); i++)
if (*i) (*i)->update((Subject*)this);
}
void Subject::clearObservers()
{
Observers.erase(Observers.begin(), Observers.end());
}