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