1 | /*
|
---|
2 | * World.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 3, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "World.hpp"
|
---|
9 |
|
---|
10 | #include "atom.hpp"
|
---|
11 | #include "molecule.hpp"
|
---|
12 | #include "periodentafel.hpp"
|
---|
13 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
14 | #include "Descriptors/AtomDescriptor_impl.hpp"
|
---|
15 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | /******************************* getter and setter ************************/
|
---|
20 | periodentafel *&World::getPeriode(){
|
---|
21 | return periode;
|
---|
22 | }
|
---|
23 |
|
---|
24 | atom* World::getAtom(AtomDescriptor descriptor){
|
---|
25 | return descriptor.find();
|
---|
26 | }
|
---|
27 |
|
---|
28 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
---|
29 | return descriptor.findAll();
|
---|
30 | }
|
---|
31 |
|
---|
32 | int World::numAtoms(){
|
---|
33 | return atoms.size();
|
---|
34 | }
|
---|
35 |
|
---|
36 | int World::numMolecules(){
|
---|
37 | return molecules_deprecated->ListOfMolecules.size();
|
---|
38 | }
|
---|
39 |
|
---|
40 | molecule* World::createMolecule(){
|
---|
41 | OBSERVE;
|
---|
42 | molecule *mol = NULL;
|
---|
43 | mol = new molecule(periode);
|
---|
44 | molecules_deprecated->insert(mol);
|
---|
45 | molecules.insert(mol);
|
---|
46 | mol->signOn(this);
|
---|
47 | return mol;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
---|
52 | return new ManipulateAtomsProcess(op, descr,name,true);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /******************************* Iterators ********************************/
|
---|
56 |
|
---|
57 | World::AtomIterator::AtomIterator(){
|
---|
58 | state = World::get()->atomEnd();
|
---|
59 | }
|
---|
60 |
|
---|
61 | World::AtomIterator::AtomIterator(AtomDescriptor _descr, World* _world) :
|
---|
62 | descr(_descr.get_impl()),
|
---|
63 | world(_world),
|
---|
64 | index(0)
|
---|
65 | {
|
---|
66 | state = world->atoms.begin();
|
---|
67 | advanceState();
|
---|
68 | }
|
---|
69 |
|
---|
70 | World::AtomIterator::AtomIterator(const AtomIterator& rhs) :
|
---|
71 | state(rhs.state),
|
---|
72 | descr(rhs.descr),
|
---|
73 | index(rhs.index),
|
---|
74 | world(rhs.world)
|
---|
75 | {}
|
---|
76 |
|
---|
77 | World::AtomIterator& World::AtomIterator::operator=(const AtomIterator& rhs)
|
---|
78 | {
|
---|
79 | if(&rhs!=this){
|
---|
80 | state=rhs.state;
|
---|
81 | descr=rhs.descr;
|
---|
82 | index=rhs.index;
|
---|
83 | world=rhs.world;
|
---|
84 | }
|
---|
85 | return *this;
|
---|
86 | }
|
---|
87 |
|
---|
88 | World::AtomIterator& World::AtomIterator::operator++(){
|
---|
89 | ++state;
|
---|
90 | ++index;
|
---|
91 | advanceState();
|
---|
92 | return *this;
|
---|
93 | }
|
---|
94 |
|
---|
95 | World::AtomIterator World::AtomIterator::operator++(int){
|
---|
96 | AtomIterator res(*this);
|
---|
97 | ++(*this);
|
---|
98 | return res;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool World::AtomIterator::operator==(const AtomIterator& rhs){
|
---|
102 | return state==rhs.state;
|
---|
103 | }
|
---|
104 |
|
---|
105 | bool World::AtomIterator::operator==(const World::AtomList::iterator& rhs){
|
---|
106 | return state==rhs;
|
---|
107 | }
|
---|
108 |
|
---|
109 | bool World::AtomIterator::operator!=(const AtomIterator& rhs){
|
---|
110 | return state!=rhs.state;
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool World::AtomIterator::operator!=(const World::AtomList::iterator& rhs){
|
---|
114 | return state!=rhs;
|
---|
115 | }
|
---|
116 |
|
---|
117 | atom* World::AtomIterator::operator*(){
|
---|
118 | return (*state).second;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void World::AtomIterator::advanceState(){
|
---|
122 | while((state!=world->atoms.end()) && (!descr->predicate(*state))){
|
---|
123 | ++state;
|
---|
124 | ++index;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | int World::AtomIterator::getCount(){
|
---|
129 | return index;
|
---|
130 | }
|
---|
131 |
|
---|
132 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
|
---|
133 | return AtomIterator(descr,this);
|
---|
134 | }
|
---|
135 |
|
---|
136 | World::AtomList::iterator World::atomEnd(){
|
---|
137 | return atoms.end();
|
---|
138 | }
|
---|
139 |
|
---|
140 | /******************************* Singleton Stuff **************************/
|
---|
141 |
|
---|
142 | // TODO: Hide boost-thread using Autotools stuff when no threads are used
|
---|
143 | World* World::theWorld = 0;
|
---|
144 | boost::mutex World::worldLock;
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | World::World() :
|
---|
149 | dummyId(0),
|
---|
150 | periode(new periodentafel),
|
---|
151 | molecules_deprecated(new MoleculeListClass),
|
---|
152 | atoms()
|
---|
153 | {
|
---|
154 | molecules_deprecated->signOn(this);
|
---|
155 | }
|
---|
156 |
|
---|
157 | World::~World()
|
---|
158 | {
|
---|
159 | delete periode;
|
---|
160 | }
|
---|
161 |
|
---|
162 | World* World::get(){
|
---|
163 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
164 | boost::mutex::scoped_lock guard(worldLock);
|
---|
165 | if(!theWorld) {
|
---|
166 | theWorld = new World();
|
---|
167 | }
|
---|
168 | return theWorld;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void World::destroy(){
|
---|
172 | // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
|
---|
173 | theWorld->destroyLegacy();
|
---|
174 | //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
|
---|
175 |
|
---|
176 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
177 | boost::mutex::scoped_lock guard(worldLock);
|
---|
178 | delete theWorld;
|
---|
179 | theWorld = 0;
|
---|
180 | }
|
---|
181 |
|
---|
182 | World* World::reset(){
|
---|
183 | // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
|
---|
184 | theWorld->destroyLegacy();
|
---|
185 | //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
|
---|
186 |
|
---|
187 | World* oldWorld = 0;
|
---|
188 | {
|
---|
189 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
190 | boost::mutex::scoped_lock guard(worldLock);
|
---|
191 |
|
---|
192 | oldWorld = theWorld;
|
---|
193 | theWorld = new World();
|
---|
194 | // oldworld does not need protection any more,
|
---|
195 | // since we should have the only reference
|
---|
196 |
|
---|
197 | // worldLock handles access to the pointer,
|
---|
198 | // not to the object
|
---|
199 | } // scope-end releases the lock
|
---|
200 |
|
---|
201 | // we have to let all the observers know that the
|
---|
202 | // oldWorld was destroyed. oldWorld calls subjectKilled
|
---|
203 | // upon destruction. Every Observer getting that signal
|
---|
204 | // should see that it gets the updated new world
|
---|
205 | delete oldWorld;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /******************************* deprecated Legacy Stuff ***********************/
|
---|
209 |
|
---|
210 | MoleculeListClass *&World::getMolecules() {
|
---|
211 | return molecules_deprecated;
|
---|
212 | }
|
---|
213 |
|
---|
214 | // some legacy stuff to let the World know about items created outside
|
---|
215 | void World::registerAtom(atom *theAtom){
|
---|
216 | OBSERVE;
|
---|
217 | atoms[dummyId++] = theAtom;
|
---|
218 | }
|
---|
219 |
|
---|
220 | void World::destroyLegacy(){
|
---|
221 | //delete molecules_deprecated;
|
---|
222 | }
|
---|
223 |
|
---|
224 | void World::unregisterAtom(atom *theAtom){
|
---|
225 | OBSERVE;
|
---|
226 | atoms.erase(theAtom->getId());
|
---|
227 | }
|
---|