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 | vector<atom*> World::getAllAtoms(){
|
---|
33 | return getAllAtoms(AllAtoms());
|
---|
34 | }
|
---|
35 |
|
---|
36 | int World::numAtoms(){
|
---|
37 | return atoms.size();
|
---|
38 | }
|
---|
39 |
|
---|
40 | int World::numMolecules(){
|
---|
41 | return molecules_deprecated->ListOfMolecules.size();
|
---|
42 | }
|
---|
43 |
|
---|
44 | /******************** Methods to change World state *********************/
|
---|
45 |
|
---|
46 | molecule* World::createMolecule(){
|
---|
47 | OBSERVE;
|
---|
48 | molecule *mol = NULL;
|
---|
49 | mol = NewMolecule();
|
---|
50 | assert(!molecules.count(currMoleculeId));
|
---|
51 | mol->setId(currMoleculeId++);
|
---|
52 | // store the molecule by ID
|
---|
53 | molecules[mol->getId()] = mol;
|
---|
54 | mol->signOn(this);
|
---|
55 | return mol;
|
---|
56 | }
|
---|
57 |
|
---|
58 | void World::destroyMolecule(molecule* mol){
|
---|
59 | OBSERVE;
|
---|
60 | destroyMolecule(mol->getId());
|
---|
61 | }
|
---|
62 |
|
---|
63 | void World::destroyMolecule(moleculeId_t id){
|
---|
64 | OBSERVE;
|
---|
65 | molecule *mol = molecules[id];
|
---|
66 | assert(mol);
|
---|
67 | DeleteMolecule(mol);
|
---|
68 | molecules.erase(id);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | atom *World::createAtom(){
|
---|
73 | OBSERVE;
|
---|
74 | atom *res = NewAtom();
|
---|
75 | assert(!atoms.count(currAtomId));
|
---|
76 | res->setId(currAtomId++);
|
---|
77 | res->setWorld(this);
|
---|
78 | // store the atom by ID
|
---|
79 | atoms[res->getId()] = res;
|
---|
80 | return res;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int World::registerAtom(atom *atom){
|
---|
84 | OBSERVE;
|
---|
85 | assert(!atoms.count(currAtomId));
|
---|
86 | atom->setId(currAtomId++);
|
---|
87 | atom->setWorld(this);
|
---|
88 | atoms[atom->getId()] = atom;
|
---|
89 | return atom->getId();
|
---|
90 | }
|
---|
91 |
|
---|
92 | void World::destroyAtom(atom* atom){
|
---|
93 | OBSERVE;
|
---|
94 | int id = atom->getId();
|
---|
95 | destroyAtom(id);
|
---|
96 | }
|
---|
97 |
|
---|
98 | void World::destroyAtom(atomId_t id) {
|
---|
99 | OBSERVE;
|
---|
100 | atom *atom = atoms[id];
|
---|
101 | assert(atom);
|
---|
102 | DeleteAtom(atom);
|
---|
103 | atoms.erase(id);
|
---|
104 | }
|
---|
105 |
|
---|
106 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
---|
107 | return new ManipulateAtomsProcess(op, descr,name,true);
|
---|
108 | }
|
---|
109 |
|
---|
110 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
|
---|
111 | return manipulateAtoms(op,name,AllAtoms());
|
---|
112 | }
|
---|
113 |
|
---|
114 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
---|
115 |
|
---|
116 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
---|
117 | proc->signOn(this);
|
---|
118 | {
|
---|
119 | OBSERVE;
|
---|
120 | proc->doManipulate(this);
|
---|
121 | }
|
---|
122 | proc->signOff(this);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /******************************* Iterators ********************************/
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Actual Implementation of the iterators can be found in WorldIterators.cpp
|
---|
129 | */
|
---|
130 |
|
---|
131 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
|
---|
132 | return AtomIterator(descr,this);
|
---|
133 | }
|
---|
134 |
|
---|
135 | World::AtomSet::iterator World::atomEnd(){
|
---|
136 | return atoms.end();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /******************************* Singleton Stuff **************************/
|
---|
140 |
|
---|
141 | // TODO: Hide boost-thread using Autotools stuff when no threads are used
|
---|
142 | World* World::theWorld = 0;
|
---|
143 | boost::mutex World::worldLock;
|
---|
144 |
|
---|
145 | World::World() :
|
---|
146 | periode(new periodentafel),
|
---|
147 | atoms(),
|
---|
148 | currAtomId(0),
|
---|
149 | molecules(),
|
---|
150 | currMoleculeId(0),
|
---|
151 | molecules_deprecated(new MoleculeListClass(this))
|
---|
152 | {
|
---|
153 | molecules_deprecated->signOn(this);
|
---|
154 | }
|
---|
155 |
|
---|
156 | World::~World()
|
---|
157 | {
|
---|
158 | molecules_deprecated->signOff(this);
|
---|
159 | delete molecules_deprecated;
|
---|
160 | delete periode;
|
---|
161 | MoleculeSet::iterator molIter;
|
---|
162 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
|
---|
163 | DeleteMolecule((*molIter).second);
|
---|
164 | }
|
---|
165 | molecules.clear();
|
---|
166 | AtomSet::iterator atIter;
|
---|
167 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
|
---|
168 | DeleteAtom((*atIter).second);
|
---|
169 | }
|
---|
170 | atoms.clear();
|
---|
171 | }
|
---|
172 |
|
---|
173 | World* World::get(){
|
---|
174 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
175 | boost::mutex::scoped_lock guard(worldLock);
|
---|
176 | if(!theWorld) {
|
---|
177 | theWorld = new World();
|
---|
178 | }
|
---|
179 | return theWorld;
|
---|
180 | }
|
---|
181 |
|
---|
182 | void World::destroy(){
|
---|
183 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
184 | boost::mutex::scoped_lock guard(worldLock);
|
---|
185 | delete theWorld;
|
---|
186 | theWorld = 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | World* World::reset(){
|
---|
190 | World* oldWorld = 0;
|
---|
191 | {
|
---|
192 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
193 | boost::mutex::scoped_lock guard(worldLock);
|
---|
194 |
|
---|
195 | oldWorld = theWorld;
|
---|
196 | theWorld = new World();
|
---|
197 | // oldworld does not need protection any more,
|
---|
198 | // since we should have the only reference
|
---|
199 |
|
---|
200 | // worldLock handles access to the pointer,
|
---|
201 | // not to the object
|
---|
202 | } // scope-end releases the lock
|
---|
203 |
|
---|
204 | // we have to let all the observers know that the
|
---|
205 | // oldWorld was destroyed. oldWorld calls subjectKilled
|
---|
206 | // upon destruction. Every Observer getting that signal
|
---|
207 | // should see that it gets the updated new world
|
---|
208 | delete oldWorld;
|
---|
209 | return theWorld;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /******************************* deprecated Legacy Stuff ***********************/
|
---|
213 |
|
---|
214 | MoleculeListClass *&World::getMolecules() {
|
---|
215 | return molecules_deprecated;
|
---|
216 | }
|
---|