summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/WinterSpeak/general/observer
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/!NotAdopted/WinterSpeak/general/observer')
-rw-r--r--plugins/!NotAdopted/WinterSpeak/general/observer/observer.cpp19
-rw-r--r--plugins/!NotAdopted/WinterSpeak/general/observer/observer.h30
-rw-r--r--plugins/!NotAdopted/WinterSpeak/general/observer/subject.cpp77
-rw-r--r--plugins/!NotAdopted/WinterSpeak/general/observer/subject.h52
4 files changed, 178 insertions, 0 deletions
diff --git a/plugins/!NotAdopted/WinterSpeak/general/observer/observer.cpp b/plugins/!NotAdopted/WinterSpeak/general/observer/observer.cpp
new file mode 100644
index 0000000000..99e0b3a081
--- /dev/null
+++ b/plugins/!NotAdopted/WinterSpeak/general/observer/observer.cpp
@@ -0,0 +1,19 @@
+//==============================================================================
+// General Code, © 2002 Ryan Winter
+//==============================================================================
+
+#include "observer.h"
+
+#include "subject.h"
+
+#include <general/debug/debug.h>
+
+//------------------------------------------------------------------------------
+// public:
+//------------------------------------------------------------------------------
+Observer::~Observer()
+{
+ CLASSCERR("Observer::~Observer");
+}
+
+//==============================================================================
diff --git a/plugins/!NotAdopted/WinterSpeak/general/observer/observer.h b/plugins/!NotAdopted/WinterSpeak/general/observer/observer.h
new file mode 100644
index 0000000000..9f93ea91f1
--- /dev/null
+++ b/plugins/!NotAdopted/WinterSpeak/general/observer/observer.h
@@ -0,0 +1,30 @@
+#ifndef guard_general_observer_observer_observer_h
+#define guard_general_observer_observer_observer_h
+//==============================================================================
+// General Code, © 2002 Ryan Winter
+//==============================================================================
+
+class Subject;
+
+class Observer
+{
+ public:
+ virtual ~Observer();
+
+ //--------------------------------------------------------------------------
+ // Description : Called by a subject that this observer is observing
+ // to signify a change in state
+ // Parameters : subject - the subject that changed
+ //--------------------------------------------------------------------------
+ virtual void update(Subject &subject) = 0;
+};
+
+//==============================================================================
+//
+// Summary : Observer side of the observer pattern
+//
+// Description :
+//
+//==============================================================================
+
+#endif
diff --git a/plugins/!NotAdopted/WinterSpeak/general/observer/subject.cpp b/plugins/!NotAdopted/WinterSpeak/general/observer/subject.cpp
new file mode 100644
index 0000000000..e28c66c44b
--- /dev/null
+++ b/plugins/!NotAdopted/WinterSpeak/general/observer/subject.cpp
@@ -0,0 +1,77 @@
+//==============================================================================
+// General Code, © 2002 Ryan Winter
+//==============================================================================
+
+#include "subject.h"
+
+#include <general/debug/debug.h>
+
+//------------------------------------------------------------------------------
+// public:
+//------------------------------------------------------------------------------
+Subject::Subject()
+ :
+ m_observer_list()
+{
+ CLASSCERR("Subject::Subject");
+}
+
+//------------------------------------------------------------------------------
+Subject::~Subject()
+{
+ CLASSCERR("Subject::~Subject");
+}
+
+//------------------------------------------------------------------------------
+void
+Subject::notify()
+{
+ CLASSCERR("Subject::notify");
+
+ // call update for all observers in the list
+ for (std::list<Observer *>::iterator iter = m_observer_list.begin();
+ iter != m_observer_list.end();
+ ++iter)
+ {
+ (*iter)->update(*this);
+ }
+}
+
+//------------------------------------------------------------------------------
+void
+Subject::attach(Observer &observer)
+{
+ CLASSCERR("Subject::attach(" << &observer << ")");
+
+ for (std::list<Observer *>::iterator iter = m_observer_list.begin();
+ iter != m_observer_list.end();
+ ++iter)
+ {
+ if (*iter == &observer)
+ {
+ return;
+ }
+ }
+
+ m_observer_list.push_back(&observer);
+}
+
+//------------------------------------------------------------------------------
+void
+Subject::detach(const Observer &observer)
+{
+ CLASSCERR("Subject::detach(" << &observer << ")");
+
+ for (std::list<Observer *>::iterator iter = m_observer_list.begin();
+ iter != m_observer_list.end();
+ ++iter)
+ {
+ if (*iter == &observer)
+ {
+ m_observer_list.erase(iter);
+ }
+ }
+}
+
+//==============================================================================
+
diff --git a/plugins/!NotAdopted/WinterSpeak/general/observer/subject.h b/plugins/!NotAdopted/WinterSpeak/general/observer/subject.h
new file mode 100644
index 0000000000..a086e80765
--- /dev/null
+++ b/plugins/!NotAdopted/WinterSpeak/general/observer/subject.h
@@ -0,0 +1,52 @@
+#ifndef guard_general_observer_subject_h
+#define guard_general_observer_subject_h
+//==============================================================================
+// General Code, © 2002 Ryan Winter
+//==============================================================================
+
+#include "observer.h"
+
+#include <list>
+
+class Subject
+{
+ public:
+ Subject();
+ virtual ~Subject();
+
+ //--------------------------------------------------------------------------
+ // Description : Notify all observers of a state change
+ //--------------------------------------------------------------------------
+ void notify();
+
+ //--------------------------------------------------------------------------
+ // Description : Attach an observer to the subject
+ // Parameters : observer - the observer
+ //--------------------------------------------------------------------------
+ void attach(Observer &observer);
+
+ //--------------------------------------------------------------------------
+ // Description : Detach an observer from the subject
+ // Parameters : observer - the observer
+ //--------------------------------------------------------------------------
+ void detach(const Observer &observer);
+
+ private:
+ //--------------------------------------------------------------------------
+ // Description : Disallow assignment operator and copy constructor
+ //--------------------------------------------------------------------------
+ Subject(const Subject &rhs);
+ const Subject & operator=(const Subject &rhs);
+
+ std::list<Observer *> m_observer_list;
+};
+
+//==============================================================================
+//
+// Summary : Subject side of the observer pattern
+//
+// Description : Instances of this class are what is observered by observers
+//
+//==============================================================================
+
+#endif