[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"
|
---|
[1c51c8] | 15 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 16 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
|
---|
[7c4e29] | 17 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
[d346b6] | 18 |
|
---|
[23b547] | 19 | #include "Patterns/Singleton_impl.hpp"
|
---|
| 20 |
|
---|
[d346b6] | 21 | using namespace std;
|
---|
[4d9c01] | 22 |
|
---|
[5d1611] | 23 | /******************************* getter and setter ************************/
|
---|
[354859] | 24 | periodentafel *&World::getPeriode(){
|
---|
[5d1611] | 25 | return periode;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[1c51c8] | 28 | // Atoms
|
---|
| 29 |
|
---|
[7a1ce5] | 30 | atom* World::getAtom(AtomDescriptor descriptor){
|
---|
[fc1b24] | 31 | return descriptor.find();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[7a1ce5] | 34 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
---|
[fc1b24] | 35 | return descriptor.findAll();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[0e2a47] | 38 | vector<atom*> World::getAllAtoms(){
|
---|
| 39 | return getAllAtoms(AllAtoms());
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[354859] | 42 | int World::numAtoms(){
|
---|
| 43 | return atoms.size();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[1c51c8] | 46 | // Molecules
|
---|
| 47 |
|
---|
| 48 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
|
---|
| 49 | return descriptor.find();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
|
---|
| 53 | return descriptor.findAll();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[354859] | 56 | int World::numMolecules(){
|
---|
| 57 | return molecules_deprecated->ListOfMolecules.size();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[afb47f] | 60 | /******************** Methods to change World state *********************/
|
---|
| 61 |
|
---|
[354859] | 62 | molecule* World::createMolecule(){
|
---|
| 63 | OBSERVE;
|
---|
| 64 | molecule *mol = NULL;
|
---|
[cbc5fb] | 65 | mol = NewMolecule();
|
---|
[d2dbac0] | 66 | assert(!molecules.count(currMoleculeId));
|
---|
[cbc5fb] | 67 | mol->setId(currMoleculeId++);
|
---|
[244d26] | 68 | // store the molecule by ID
|
---|
[cbc5fb] | 69 | molecules[mol->getId()] = mol;
|
---|
[354859] | 70 | mol->signOn(this);
|
---|
| 71 | return mol;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[cbc5fb] | 74 | void World::destroyMolecule(molecule* mol){
|
---|
| 75 | OBSERVE;
|
---|
| 76 | destroyMolecule(mol->getId());
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void World::destroyMolecule(moleculeId_t id){
|
---|
| 80 | OBSERVE;
|
---|
| 81 | molecule *mol = molecules[id];
|
---|
| 82 | assert(mol);
|
---|
| 83 | DeleteMolecule(mol);
|
---|
| 84 | molecules.erase(id);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[7c4e29] | 87 |
|
---|
[46d958] | 88 | atom *World::createAtom(){
|
---|
| 89 | OBSERVE;
|
---|
[88d586] | 90 | atomId_t id = getNextAtomId();
|
---|
| 91 | atom *res = NewAtom(id);
|
---|
[46d958] | 92 | res->setWorld(this);
|
---|
[244d26] | 93 | // store the atom by ID
|
---|
[46d958] | 94 | atoms[res->getId()] = res;
|
---|
| 95 | return res;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | int World::registerAtom(atom *atom){
|
---|
| 99 | OBSERVE;
|
---|
[88d586] | 100 | atomId_t id = getNextAtomId();
|
---|
| 101 | atom->setId(id);
|
---|
[46d958] | 102 | atom->setWorld(this);
|
---|
| 103 | atoms[atom->getId()] = atom;
|
---|
| 104 | return atom->getId();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void World::destroyAtom(atom* atom){
|
---|
| 108 | OBSERVE;
|
---|
| 109 | int id = atom->getId();
|
---|
| 110 | destroyAtom(id);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[cbc5fb] | 113 | void World::destroyAtom(atomId_t id) {
|
---|
[46d958] | 114 | OBSERVE;
|
---|
| 115 | atom *atom = atoms[id];
|
---|
| 116 | assert(atom);
|
---|
| 117 | DeleteAtom(atom);
|
---|
| 118 | atoms.erase(id);
|
---|
[88d586] | 119 | releaseAtomId(id);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
|
---|
| 123 | OBSERVE;
|
---|
| 124 | // in case this call did not originate from inside the atom, we redirect it,
|
---|
| 125 | // to also let it know that it has changed
|
---|
| 126 | if(!target){
|
---|
| 127 | target = atoms[oldId];
|
---|
| 128 | assert(target && "Atom with that ID not found");
|
---|
| 129 | return target->changeId(newId);
|
---|
| 130 | }
|
---|
| 131 | else{
|
---|
| 132 | if(reserveAtomId(newId)){
|
---|
| 133 | atoms.erase(oldId);
|
---|
| 134 | atoms.insert(pair<atomId_t,atom*>(newId,target));
|
---|
| 135 | return true;
|
---|
| 136 | }
|
---|
| 137 | else{
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
[46d958] | 141 | }
|
---|
| 142 |
|
---|
[7c4e29] | 143 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
---|
| 144 | return new ManipulateAtomsProcess(op, descr,name,true);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[0e2a47] | 147 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
|
---|
| 148 | return manipulateAtoms(op,name,AllAtoms());
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[afb47f] | 151 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
---|
| 152 |
|
---|
| 153 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
---|
| 154 | proc->signOn(this);
|
---|
| 155 | {
|
---|
| 156 | OBSERVE;
|
---|
| 157 | proc->doManipulate(this);
|
---|
| 158 | }
|
---|
| 159 | proc->signOff(this);
|
---|
| 160 | }
|
---|
[88d586] | 161 | /******************************* IDManagement *****************************/
|
---|
| 162 |
|
---|
[57adc7] | 163 | // Atoms
|
---|
| 164 |
|
---|
[88d586] | 165 | atomId_t World::getNextAtomId(){
|
---|
| 166 | // see if we can reuse some Id
|
---|
| 167 | if(atomIdPool.empty()){
|
---|
| 168 | return currAtomId++;
|
---|
| 169 | }
|
---|
| 170 | else{
|
---|
| 171 | // we give out the first ID from the pool
|
---|
| 172 | atomId_t id = *(atomIdPool.begin());
|
---|
| 173 | atomIdPool.erase(id);
|
---|
[23b547] | 174 | return id;
|
---|
[88d586] | 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void World::releaseAtomId(atomId_t id){
|
---|
| 179 | atomIdPool.insert(id);
|
---|
| 180 | // defragmentation of the pool
|
---|
| 181 | set<atomId_t>::reverse_iterator iter;
|
---|
| 182 | // go through all Ids in the pool that lie immediately below the border
|
---|
| 183 | while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
|
---|
| 184 | atomIdPool.erase(--currAtomId);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
[afb47f] | 187 |
|
---|
[88d586] | 188 | bool World::reserveAtomId(atomId_t id){
|
---|
| 189 | if(id>=currAtomId ){
|
---|
| 190 | // add all ids between the new one and current border as available
|
---|
| 191 | for(atomId_t pos=currAtomId; pos<id; ++pos){
|
---|
| 192 | atomIdPool.insert(pos);
|
---|
| 193 | }
|
---|
| 194 | currAtomId=id+1;
|
---|
| 195 | return true;
|
---|
| 196 | }
|
---|
| 197 | else if(atomIdPool.count(id)){
|
---|
| 198 | atomIdPool.erase(id);
|
---|
| 199 | return true;
|
---|
| 200 | }
|
---|
| 201 | else{
|
---|
| 202 | // this ID could not be reserved
|
---|
| 203 | return false;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[57adc7] | 206 |
|
---|
| 207 | // Molecules
|
---|
| 208 |
|
---|
[865a945] | 209 | /******************************* Iterators ********************************/
|
---|
| 210 |
|
---|
[d2dbac0] | 211 | /*
|
---|
| 212 | * Actual Implementation of the iterators can be found in WorldIterators.cpp
|
---|
| 213 | */
|
---|
[865a945] | 214 |
|
---|
| 215 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
|
---|
| 216 | return AtomIterator(descr,this);
|
---|
| 217 | }
|
---|
[354859] | 218 |
|
---|
[d2dbac0] | 219 | World::AtomSet::iterator World::atomEnd(){
|
---|
[7c4e29] | 220 | return atoms.end();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[1c51c8] | 223 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
|
---|
| 224 | return MoleculeIterator(descr,this);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | World::MoleculeSet::iterator World::moleculeEnd(){
|
---|
| 228 | return molecules.end();
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[5d1611] | 231 | /******************************* Singleton Stuff **************************/
|
---|
| 232 |
|
---|
[7a1ce5] | 233 | World::World() :
|
---|
[354859] | 234 | periode(new periodentafel),
|
---|
[d2dbac0] | 235 | atoms(),
|
---|
[24a5e0] | 236 | currAtomId(0),
|
---|
| 237 | molecules(),
|
---|
| 238 | currMoleculeId(0),
|
---|
| 239 | molecules_deprecated(new MoleculeListClass(this))
|
---|
[7dad10] | 240 | {
|
---|
| 241 | molecules_deprecated->signOn(this);
|
---|
| 242 | }
|
---|
[5d1611] | 243 |
|
---|
| 244 | World::~World()
|
---|
[354859] | 245 | {
|
---|
[028c2e] | 246 | molecules_deprecated->signOff(this);
|
---|
[46d958] | 247 | delete molecules_deprecated;
|
---|
[354859] | 248 | delete periode;
|
---|
[cbc5fb] | 249 | MoleculeSet::iterator molIter;
|
---|
| 250 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
|
---|
| 251 | DeleteMolecule((*molIter).second);
|
---|
| 252 | }
|
---|
| 253 | molecules.clear();
|
---|
| 254 | AtomSet::iterator atIter;
|
---|
| 255 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
|
---|
| 256 | DeleteAtom((*atIter).second);
|
---|
[46d958] | 257 | }
|
---|
| 258 | atoms.clear();
|
---|
[354859] | 259 | }
|
---|
[5d1611] | 260 |
|
---|
[23b547] | 261 | // Explicit instantiation of the singleton mechanism at this point
|
---|
[5d1611] | 262 |
|
---|
[23b547] | 263 | CONSTRUCT_SINGLETON(World)
|
---|
[5d1611] | 264 |
|
---|
| 265 | /******************************* deprecated Legacy Stuff ***********************/
|
---|
| 266 |
|
---|
[354859] | 267 | MoleculeListClass *&World::getMolecules() {
|
---|
| 268 | return molecules_deprecated;
|
---|
[5d1611] | 269 | }
|
---|