Ginga  0.13.6
 All Classes Namespaces Functions Variables
Observable.h
1 /******************************************************************************
2 Este arquivo eh parte da implementacao do ambiente declarativo do middleware
3 Ginga (Ginga-NCL).
4 
5 Direitos Autorais Reservados (c) 1989-2007 PUC-Rio/Laboratorio TeleMidia
6 
7 Este programa eh software livre; voce pode redistribui-lo e/ou modificah-lo sob
8 os termos da Licenca Publica Geral GNU versao 2 conforme publicada pela Free
9 Software Foundation.
10 
11 Este programa eh distribuido na expectativa de que seja util, porem, SEM
12 NENHUMA GARANTIA; nem mesmo a garantia implicita de COMERCIABILIDADE OU
13 ADEQUACAO A UMA FINALIDADE ESPECIFICA. Consulte a Licenca Publica Geral do
14 GNU versao 2 para mais detalhes.
15 
16 Voce deve ter recebido uma copia da Licenca Publica Geral do GNU versao 2 junto
17 com este programa; se nao, escreva para a Free Software Foundation, Inc., no
18 endereco 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
19 
20 Para maiores informacoes:
21 ncl @ telemidia.puc-rio.br
22 http://www.ncl.org.br
23 http://www.ginga.org.br
24 http://www.telemidia.puc-rio.br
25 ******************************************************************************
26 This file is part of the declarative environment of middleware Ginga (Ginga-NCL)
27 
28 Copyright: 1989-2007 PUC-RIO/LABORATORIO TELEMIDIA, All Rights Reserved.
29 
30 This program is free software; you can redistribute it and/or modify it under
31 the terms of the GNU General Public License version 2 as published by
32 the Free Software Foundation.
33 
34 This program is distributed in the hope that it will be useful, but WITHOUT ANY
35 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
36 PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
37 details.
38 
39 You should have received a copy of the GNU General Public License version 2
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 
43 For further information contact:
44 ncl @ telemidia.puc-rio.br
45 http://www.ncl.org.br
46 http://www.ginga.org.br
47 http://www.telemidia.puc-rio.br
48 *******************************************************************************/
49 
50 #ifndef _OBSERVABLE_H_
51 #define _OBSERVABLE_H_
52 
53 #include "Observer.h"
54 
55 #include <pthread.h>
56 #include <set>
57 using namespace std;
58 
59 class Observable {
60  private:
61  set<Observer*> observers;
62  pthread_mutex_t vM;
63 
64  public:
65  virtual ~Observable() {
66  pthread_mutex_lock(&vM);
67  observers.clear();
68  pthread_mutex_unlock(&vM);
69  pthread_mutex_destroy(&vM);
70  };
71 
72  protected:
73  void createObserversVector() {
74  pthread_mutex_init(&vM, NULL);
75  observers.clear();
76  };
77 
78  public:
79  void addObserver(Observer* object) {
80  pthread_mutex_lock(&vM);
81  observers.insert(object);
82  pthread_mutex_unlock(&vM);
83  };
84 
85  void removeObserver(Observer* object) {
86  set<Observer*>::iterator i;
87 
88  pthread_mutex_lock(&vM);
89  i = observers.find(object);
90  if (i != observers.end()) {
91  observers.erase(i);
92  }
93  pthread_mutex_unlock(&vM);
94  };
95 
96  virtual void notifyObservers(void* object) {
97  set<Observer*>::iterator i;
98 
99  pthread_mutex_lock(&vM);
100  i = observers.begin();
101  while (i != observers.end()) {
102  (*i)->update(this, object);
103  ++i;
104  }
105  pthread_mutex_unlock(&vM);
106  };
107 };
108 
109 #endif //_OBSERVABLE_H_
Definition: Observable.h:59
Definition: Observer.h:53