1 /*
2
3 This file is part of the Maude 2 interpreter.
4
5 Copyright 1997-2003 SRI International, Menlo Park, CA 94025, USA.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20
21 */
22
23 //
24 // Implementation for class Entity.
25 //
26
27 // utility stuff
28 #include "macros.hh"
29 #include "vector.hh"
30
31 // forward declarations
32 #include "interface.hh"
33 #include "core.hh"
34 #include "strategyLanguage.hh"
35 #include "mixfix.hh"
36
37 // front end class definitions
38 #include "importModule.hh"
39 #include "syntacticPreModule.hh"
40 #include "view.hh"
41 #include "entity.hh"
42
43 #ifndef NO_ASSERT
44 ostream&
operator <<(ostream & s,const Entity * e)45 operator<<(ostream& s, const Entity* e)
46 {
47 if (const ImportModule* m = dynamic_cast<const ImportModule*>(e))
48 s << "module " << m;
49 else if (const View* v = dynamic_cast<const View*>(e))
50 s << "view " << v;
51 else
52 s << "unknown entity";
53 return s;
54 }
55
56 ostream&
operator <<(ostream & s,const Entity::User * u)57 operator<<(ostream& s, const Entity::User* u)
58 {
59 if (const PreModule* p = dynamic_cast<const PreModule*>(u))
60 s << "premodule " << p;
61 else if (const ImportModule* m = dynamic_cast<const ImportModule*>(u))
62 s << "module " << m;
63 else if (const View* v = dynamic_cast<const View*>(u))
64 s << "view " << v;
65 else
66 s << "unknown user";
67 return s;
68 }
69 #endif
70
71 void
addUser(User * user)72 Entity::addUser(User* user)
73 {
74 if (users.insert(user).second)
75 {
76 //DebugAdvisory("added " << user << " to user set for " << this);
77 }
78 else
79 {
80 //DebugAdvisory(user << " is already in user set for " << this);
81 }
82 }
83
84 void
removeUser(User * user)85 Entity::removeUser(User* user)
86 {
87 if (users.erase(user) == 1)
88 {
89 //DebugAdvisory("removed " << user << " from user set for " << this);
90 }
91 else
92 {
93 //DebugAdvisory("missing " << user << " in user set for " << this);
94 }
95 }
96
97 void
informUsers()98 Entity::informUsers()
99 {
100 //DebugAdvisory(this << " informs users");
101 //
102 // We need to be careful since informing a user will often cause the user
103 // and/or other users to be removed, invalidating iterators.
104 //
105 const UserSet::const_iterator e = users.end();
106 User* last = 0;
107 for (;;)
108 {
109 UserSet::iterator i = users.begin();
110 if (i == e)
111 break;
112 User* user = *i;
113 if (user == last)
114 users.erase(i);
115 else
116 {
117 user->regretToInform(this); // invalidates i
118 last = user;
119 }
120 }
121 }
122