| 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 "Descriptors/AtomDescriptor.hpp"
|
|---|
| 12 |
|
|---|
| 13 | using namespace std;
|
|---|
| 14 |
|
|---|
| 15 | /******************************* getter and setter ************************/
|
|---|
| 16 | periodentafel* World::getPeriode(){
|
|---|
| 17 | return periode;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | atom* World::getAtom(AtomDescriptor descriptor){
|
|---|
| 21 | return descriptor.find();
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
|---|
| 25 | return descriptor.findAll();
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /******************************* Singleton Stuff **************************/
|
|---|
| 29 |
|
|---|
| 30 | // TODO: Hide boost-thread using Autotools stuff when no threads are used
|
|---|
| 31 | World* World::theWorld = 0;
|
|---|
| 32 | boost::mutex World::worldLock;
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | World::World() :
|
|---|
| 37 | dummyId(0)
|
|---|
| 38 | {}
|
|---|
| 39 |
|
|---|
| 40 | World::~World()
|
|---|
| 41 | {}
|
|---|
| 42 |
|
|---|
| 43 | World* World::get(){
|
|---|
| 44 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 45 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 46 | if(!theWorld) {
|
|---|
| 47 | theWorld = new World();
|
|---|
| 48 | }
|
|---|
| 49 | return theWorld;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void World::destroy(){
|
|---|
| 53 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 54 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 55 | delete theWorld;
|
|---|
| 56 | theWorld = 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | World* World::reset(){
|
|---|
| 60 | World* oldWorld = 0;
|
|---|
| 61 | {
|
|---|
| 62 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 63 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 64 |
|
|---|
| 65 | oldWorld = theWorld;
|
|---|
| 66 | theWorld = new World();
|
|---|
| 67 | // oldworld does not need protection any more,
|
|---|
| 68 | // since we should have the only reference
|
|---|
| 69 |
|
|---|
| 70 | // worldLock handles access to the pointer,
|
|---|
| 71 | // not to the object
|
|---|
| 72 | } // scope-end releases the lock
|
|---|
| 73 |
|
|---|
| 74 | // we have to let all the observers know that the
|
|---|
| 75 | // oldWorld was destroyed. oldWorld calls subjectKilled
|
|---|
| 76 | // upon destruction. Every Observer getting that signal
|
|---|
| 77 | // should see that it gets the updated new world
|
|---|
| 78 | delete oldWorld;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /******************************* deprecated Legacy Stuff ***********************/
|
|---|
| 82 |
|
|---|
| 83 | MoleculeListClass *World::getMolecules() {
|
|---|
| 84 | return molecules;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | // some legacy stuff to let the World know about items created outside
|
|---|
| 88 | void World::registerAtom(atom *theAtom){
|
|---|
| 89 | OBSERVE;
|
|---|
| 90 | atoms[dummyId++] = theAtom;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void World::unregisterAtom(atom *theAtom){
|
|---|
| 94 | OBSERVE;
|
|---|
| 95 | atoms.erase(theAtom->getId());
|
|---|
| 96 | }
|
|---|