| [5d1611] | 1 | /* | 
|---|
|  | 2 | * World.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Feb 3, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #include "World.hpp" | 
|---|
|  | 9 |  | 
|---|
| [d346b6] | 10 | #include "atom.hpp" | 
|---|
| [354859] | 11 | #include "molecule.hpp" | 
|---|
|  | 12 | #include "periodentafel.hpp" | 
|---|
| [fc1b24] | 13 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| [865a945] | 14 | #include "Descriptors/AtomDescriptor_impl.hpp" | 
|---|
| [7c4e29] | 15 | #include "Actions/ManipulateAtomsProcess.hpp" | 
|---|
| [d346b6] | 16 |  | 
|---|
|  | 17 | using namespace std; | 
|---|
| [4d9c01] | 18 |  | 
|---|
| [5d1611] | 19 | /******************************* getter and setter ************************/ | 
|---|
| [354859] | 20 | periodentafel *&World::getPeriode(){ | 
|---|
| [5d1611] | 21 | return periode; | 
|---|
|  | 22 | } | 
|---|
|  | 23 |  | 
|---|
| [7a1ce5] | 24 | atom* World::getAtom(AtomDescriptor descriptor){ | 
|---|
| [fc1b24] | 25 | return descriptor.find(); | 
|---|
|  | 26 | } | 
|---|
|  | 27 |  | 
|---|
| [7a1ce5] | 28 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){ | 
|---|
| [fc1b24] | 29 | return descriptor.findAll(); | 
|---|
|  | 30 | } | 
|---|
|  | 31 |  | 
|---|
| [0e2a47] | 32 | vector<atom*> World::getAllAtoms(){ | 
|---|
|  | 33 | return getAllAtoms(AllAtoms()); | 
|---|
|  | 34 | } | 
|---|
|  | 35 |  | 
|---|
| [354859] | 36 | int World::numAtoms(){ | 
|---|
|  | 37 | return atoms.size(); | 
|---|
|  | 38 | } | 
|---|
|  | 39 |  | 
|---|
|  | 40 | int World::numMolecules(){ | 
|---|
|  | 41 | return molecules_deprecated->ListOfMolecules.size(); | 
|---|
|  | 42 | } | 
|---|
|  | 43 |  | 
|---|
| [afb47f] | 44 | /******************** Methods to change World state *********************/ | 
|---|
|  | 45 |  | 
|---|
| [354859] | 46 | molecule* World::createMolecule(){ | 
|---|
|  | 47 | OBSERVE; | 
|---|
|  | 48 | molecule *mol = NULL; | 
|---|
| [cbc5fb] | 49 | mol = NewMolecule(); | 
|---|
| [d2dbac0] | 50 | assert(!molecules.count(currMoleculeId)); | 
|---|
| [cbc5fb] | 51 | mol->setId(currMoleculeId++); | 
|---|
| [244d26] | 52 | // store the molecule by ID | 
|---|
| [cbc5fb] | 53 | molecules[mol->getId()] = mol; | 
|---|
| [354859] | 54 | mol->signOn(this); | 
|---|
|  | 55 | return mol; | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
| [cbc5fb] | 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 |  | 
|---|
| [7c4e29] | 71 |  | 
|---|
| [46d958] | 72 | atom *World::createAtom(){ | 
|---|
|  | 73 | OBSERVE; | 
|---|
|  | 74 | atom *res = NewAtom(); | 
|---|
| [d2dbac0] | 75 | assert(!atoms.count(currAtomId)); | 
|---|
| [46d958] | 76 | res->setId(currAtomId++); | 
|---|
|  | 77 | res->setWorld(this); | 
|---|
| [244d26] | 78 | // store the atom by ID | 
|---|
| [46d958] | 79 | atoms[res->getId()] = res; | 
|---|
|  | 80 | return res; | 
|---|
|  | 81 | } | 
|---|
|  | 82 |  | 
|---|
|  | 83 | int World::registerAtom(atom *atom){ | 
|---|
|  | 84 | OBSERVE; | 
|---|
| [d2dbac0] | 85 | assert(!atoms.count(currAtomId)); | 
|---|
| [46d958] | 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 |  | 
|---|
| [cbc5fb] | 98 | void World::destroyAtom(atomId_t id) { | 
|---|
| [46d958] | 99 | OBSERVE; | 
|---|
|  | 100 | atom *atom = atoms[id]; | 
|---|
|  | 101 | assert(atom); | 
|---|
|  | 102 | DeleteAtom(atom); | 
|---|
|  | 103 | atoms.erase(id); | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
| [7c4e29] | 106 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){ | 
|---|
|  | 107 | return new ManipulateAtomsProcess(op, descr,name,true); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
| [0e2a47] | 110 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){ | 
|---|
|  | 111 | return manipulateAtoms(op,name,AllAtoms()); | 
|---|
|  | 112 | } | 
|---|
|  | 113 |  | 
|---|
| [afb47f] | 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 |  | 
|---|
| [865a945] | 125 | /******************************* Iterators ********************************/ | 
|---|
|  | 126 |  | 
|---|
| [d2dbac0] | 127 | /* | 
|---|
|  | 128 | * Actual Implementation of the iterators can be found in WorldIterators.cpp | 
|---|
|  | 129 | */ | 
|---|
| [865a945] | 130 |  | 
|---|
|  | 131 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){ | 
|---|
|  | 132 | return AtomIterator(descr,this); | 
|---|
|  | 133 | } | 
|---|
| [354859] | 134 |  | 
|---|
| [d2dbac0] | 135 | World::AtomSet::iterator World::atomEnd(){ | 
|---|
| [7c4e29] | 136 | return atoms.end(); | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
| [5d1611] | 139 | /******************************* Singleton Stuff **************************/ | 
|---|
|  | 140 |  | 
|---|
| [4d9c01] | 141 | // TODO: Hide boost-thread using Autotools stuff when no threads are used | 
|---|
| [5d1611] | 142 | World* World::theWorld = 0; | 
|---|
| [4d9c01] | 143 | boost::mutex World::worldLock; | 
|---|
|  | 144 |  | 
|---|
| [7a1ce5] | 145 | World::World() : | 
|---|
| [354859] | 146 | periode(new periodentafel), | 
|---|
| [d2dbac0] | 147 | atoms(), | 
|---|
| [24a5e0] | 148 | currAtomId(0), | 
|---|
|  | 149 | molecules(), | 
|---|
|  | 150 | currMoleculeId(0), | 
|---|
|  | 151 | molecules_deprecated(new MoleculeListClass(this)) | 
|---|
| [7dad10] | 152 | { | 
|---|
|  | 153 | molecules_deprecated->signOn(this); | 
|---|
|  | 154 | } | 
|---|
| [5d1611] | 155 |  | 
|---|
|  | 156 | World::~World() | 
|---|
| [354859] | 157 | { | 
|---|
| [028c2e] | 158 | molecules_deprecated->signOff(this); | 
|---|
| [46d958] | 159 | delete molecules_deprecated; | 
|---|
| [354859] | 160 | delete periode; | 
|---|
| [cbc5fb] | 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); | 
|---|
| [46d958] | 169 | } | 
|---|
|  | 170 | atoms.clear(); | 
|---|
| [354859] | 171 | } | 
|---|
| [5d1611] | 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; | 
|---|
| [24a5e0] | 209 | return theWorld; | 
|---|
| [5d1611] | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 | /******************************* deprecated Legacy Stuff ***********************/ | 
|---|
|  | 213 |  | 
|---|
| [354859] | 214 | MoleculeListClass *&World::getMolecules() { | 
|---|
|  | 215 | return molecules_deprecated; | 
|---|
| [5d1611] | 216 | } | 
|---|