OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
observable.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 /*
22  abstract observable concept
23 
24  Author: Ansgar Philippsen
25 */
26 
27 #ifndef IMG_OBSERVABLE_H
28 #define IMG_OBSERVABLE_H
29 
30 #include <list>
31 #include "extent.hh"
32 
33 namespace ost { namespace img {
34 
36 /*
37  manages a list of observers, which must
38  offer the methods ObserverRelease, ObserverInvalidate
39  and ObserverUpdate
40 */
41 template <class T>
42 class DLLEXPORT Observable {
43  typedef std::list<T *> ObserverList;
44  typedef typename ObserverList::iterator ObserverIter;
45  typedef typename ObserverList::const_iterator ObserverConstIter;
46 public:
48  list_.clear();
49  }
50 
51  /*
52  copy logic: the observers are not copied
53  */
54  Observable(const Observable& o) {
55  list_.clear();
56  }
57 
59  for(ObserverIter it=list_.begin();it!=list_.end();++it) {
60  (*it)->ObserverInvalidate();
61  (*it)->ObserverRelease();
62  }
63  }
64 
65  /*
66  assignement logic: the observers are not copied
67  */
69  list_.clear();
70  return *this;
71  }
72 
73  void Attach(T* d) {
74  list_.push_back(d);
75  }
76 
77  void Detach(T* o) {
78  list_.remove(o);
79  }
80 
81  void Notify() const {
82  for(ObserverConstIter it=list_.begin();it!=list_.end();++it)
83  (*it)->ObserverUpdate();
84  }
85  void Notify(const Extent& e) const {
86  for(ObserverConstIter it=list_.begin();it!=list_.end();++it)
87  (*it)->ObserverUpdate(e);
88  }
89  void Notify(const Point& p) const {
90  for(ObserverConstIter it=list_.begin();it!=list_.end();++it)
91  (*it)->ObserverUpdate(p);
92  }
93 
94  int GetListSize() const {
95  return list_.size();
96  }
97 
98  long MemSize() const {
99  return sizeof(list_) + list_.size()*sizeof(T*);
100  }
101 
102  private:
103  ObserverList list_;
104 };
105 
106 
107 }} // namespace
108 
109 #endif