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