Changes in src/World.cpp [028c2e:24a5e0]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/World.cpp
r028c2e r24a5e0 12 12 #include "periodentafel.hpp" 13 13 #include "Descriptors/AtomDescriptor.hpp" 14 #include "Descriptors/AtomDescriptor_impl.hpp" 15 #include "Actions/ManipulateAtomsProcess.hpp" 14 16 15 17 using namespace std; … … 28 30 } 29 31 32 vector<atom*> World::getAllAtoms(){ 33 return getAllAtoms(AllAtoms()); 34 } 35 30 36 int World::numAtoms(){ 31 37 return atoms.size(); … … 36 42 } 37 43 44 /******************** Methods to change World state *********************/ 45 38 46 molecule* World::createMolecule(){ 39 47 OBSERVE; 40 48 molecule *mol = NULL; 41 mol = new molecule(periode); 42 molecules_deprecated->insert(mol); 43 molecules.insert(mol); 49 mol = NewMolecule(); 50 assert(!molecules.count(currMoleculeId)); 51 mol->setId(currMoleculeId++); 52 // store the molecule by ID 53 molecules[mol->getId()] = mol; 44 54 mol->signOn(this); 45 55 return mol; 46 56 } 47 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 } 48 138 49 139 /******************************* Singleton Stuff **************************/ … … 53 143 boost::mutex World::worldLock; 54 144 55 56 57 145 World::World() : 58 146 periode(new periodentafel), 59 molecules_deprecated(new MoleculeListClass), 60 dummyId(0) 147 atoms(), 148 currAtomId(0), 149 molecules(), 150 currMoleculeId(0), 151 molecules_deprecated(new MoleculeListClass(this)) 61 152 { 62 153 molecules_deprecated->signOn(this); … … 65 156 World::~World() 66 157 { 67 molecules_deprecated->signOff(this);68 158 delete molecules_deprecated; 69 159 delete periode; 160 MoleculeSet::iterator molIter; 161 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){ 162 DeleteMolecule((*molIter).second); 163 } 164 molecules.clear(); 165 AtomSet::iterator atIter; 166 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){ 167 DeleteAtom((*atIter).second); 168 } 169 atoms.clear(); 70 170 } 71 171 … … 80 180 81 181 void World::destroy(){ 82 // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise83 theWorld->destroyLegacy();84 //WARNING: at this point we have a small race condition, when sombody now tries to access the world.85 86 182 // boost supports RAII-Style locking, so we don't need to unlock 87 183 boost::mutex::scoped_lock guard(worldLock); … … 91 187 92 188 World* World::reset(){ 93 // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise94 theWorld->destroyLegacy();95 //WARNING: at this point we have a small race condition, when sombody now tries to access the world.96 97 189 World* oldWorld = 0; 98 190 { … … 114 206 // should see that it gets the updated new world 115 207 delete oldWorld; 208 return theWorld; 116 209 } 117 210 … … 121 214 return molecules_deprecated; 122 215 } 123 124 // some legacy stuff to let the World know about items created outside125 void World::registerAtom(atom *theAtom){126 OBSERVE;127 atoms[dummyId++] = theAtom;128 }129 130 void World::destroyLegacy(){131 //delete molecules_deprecated;132 }133 134 void World::unregisterAtom(atom *theAtom){135 OBSERVE;136 atoms.erase(theAtom->getId());137 }
Note:
See TracChangeset
for help on using the changeset viewer.