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