HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 00021 #include "Observer.h" 00022 00023 #include <list> 00024 #include <algorithm> 00025 #include <functional> 00026 00027 using std::shared_ptr; 00028 using std::list; 00029 using std::binary_function; 00030 using std::for_each; 00031 00032 template <class Parent> 00033 struct Subject<Parent>::Impl { 00034 list< shared_ptr< Observer<Parent> > > observers_; 00035 }; 00036 00037 template <class Parent> 00038 Subject<Parent>::Subject(): pImpl_(new Impl) { } 00039 00040 template <class Parent> 00041 Subject<Parent>::~Subject() { } 00042 00043 template <class Parent> 00044 void Subject<Parent>::attach(shared_ptr< Observer<Parent> > observer) { 00045 pImpl_->observers_.push_back(observer); 00046 } 00047 00048 template <class Parent> 00049 void Subject<Parent>::detach(shared_ptr< Observer<Parent> > observer) { 00050 pImpl_->observers_.remove(observer); 00051 } 00052 00053 template <class Parent> 00054 struct Update: public binary_function< shared_ptr< Observer<Parent> >, shared_ptr< const Subject<Parent> >, void > { 00055 void operator()(shared_ptr< Observer<Parent> > observer, shared_ptr< const Subject<Parent> > subject) const { 00056 observer->update(subject); 00057 } 00058 }; 00059 00060 template <class Parent> 00061 void Subject<Parent>::notify() { 00062 static Update<Parent> update; 00063 for_each(pImpl_->observers_.begin(), pImpl_->observers_.end(), 00064 std::bind2nd(update, this)); 00065 } 00066 00067 template <class Parent> 00068 Observer<Parent>::~Observer() { }