| 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 "molecule.hpp"
|
|---|
| 12 | #include "periodentafel.hpp"
|
|---|
| 13 | #include "Descriptors/AtomDescriptor.hpp"
|
|---|
| 14 | #include "Descriptors/AtomDescriptor_impl.hpp"
|
|---|
| 15 | #include "Actions/ManipulateAtomsProcess.hpp"
|
|---|
| 16 |
|
|---|
| 17 | using namespace std;
|
|---|
| 18 |
|
|---|
| 19 | /******************************* getter and setter ************************/
|
|---|
| 20 | periodentafel *&World::getPeriode(){
|
|---|
| 21 | return periode;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | atom* World::getAtom(AtomDescriptor descriptor){
|
|---|
| 25 | return descriptor.find();
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
|---|
| 29 | return descriptor.findAll();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | int World::numAtoms(){
|
|---|
| 33 | return atoms.size();
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | int World::numMolecules(){
|
|---|
| 37 | return molecules_deprecated->ListOfMolecules.size();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /******************** Methods to change World state *********************/
|
|---|
| 41 |
|
|---|
| 42 | molecule* World::createMolecule(){
|
|---|
| 43 | OBSERVE;
|
|---|
| 44 | molecule *mol = NULL;
|
|---|
| 45 | mol = new molecule(periode);
|
|---|
| 46 | molecules_deprecated->insert(mol);
|
|---|
| 47 | molecules.insert(mol);
|
|---|
| 48 | mol->signOn(this);
|
|---|
| 49 | return mol;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
|---|
| 54 | return new ManipulateAtomsProcess(op, descr,name,true);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
|---|
| 58 |
|
|---|
| 59 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
|---|
| 60 | proc->signOn(this);
|
|---|
| 61 | {
|
|---|
| 62 | OBSERVE;
|
|---|
| 63 | proc->doManipulate(this);
|
|---|
| 64 | }
|
|---|
| 65 | proc->signOff(this);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /******************************* Iterators ********************************/
|
|---|
| 69 |
|
|---|
| 70 | World::AtomIterator::AtomIterator(){
|
|---|
| 71 | state = World::get()->atomEnd();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | World::AtomIterator::AtomIterator(AtomDescriptor _descr, World* _world) :
|
|---|
| 75 | descr(_descr.get_impl()),
|
|---|
| 76 | world(_world),
|
|---|
| 77 | index(0)
|
|---|
| 78 | {
|
|---|
| 79 | state = world->atoms.begin();
|
|---|
| 80 | advanceState();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | World::AtomIterator::AtomIterator(const AtomIterator& rhs) :
|
|---|
| 84 | state(rhs.state),
|
|---|
| 85 | descr(rhs.descr),
|
|---|
| 86 | index(rhs.index),
|
|---|
| 87 | world(rhs.world)
|
|---|
| 88 | {}
|
|---|
| 89 |
|
|---|
| 90 | World::AtomIterator& World::AtomIterator::operator=(const AtomIterator& rhs)
|
|---|
| 91 | {
|
|---|
| 92 | if(&rhs!=this){
|
|---|
| 93 | state=rhs.state;
|
|---|
| 94 | descr=rhs.descr;
|
|---|
| 95 | index=rhs.index;
|
|---|
| 96 | world=rhs.world;
|
|---|
| 97 | }
|
|---|
| 98 | return *this;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | World::AtomIterator& World::AtomIterator::operator++(){
|
|---|
| 102 | ++state;
|
|---|
| 103 | ++index;
|
|---|
| 104 | advanceState();
|
|---|
| 105 | return *this;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | World::AtomIterator World::AtomIterator::operator++(int){
|
|---|
| 109 | AtomIterator res(*this);
|
|---|
| 110 | ++(*this);
|
|---|
| 111 | return res;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | bool World::AtomIterator::operator==(const AtomIterator& rhs){
|
|---|
| 115 | return state==rhs.state;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | bool World::AtomIterator::operator==(const World::AtomList::iterator& rhs){
|
|---|
| 119 | return state==rhs;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | bool World::AtomIterator::operator!=(const AtomIterator& rhs){
|
|---|
| 123 | return state!=rhs.state;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | bool World::AtomIterator::operator!=(const World::AtomList::iterator& rhs){
|
|---|
| 127 | return state!=rhs;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | atom* World::AtomIterator::operator*(){
|
|---|
| 131 | return (*state).second;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void World::AtomIterator::advanceState(){
|
|---|
| 135 | while((state!=world->atoms.end()) && (!descr->predicate(*state))){
|
|---|
| 136 | ++state;
|
|---|
| 137 | ++index;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | int World::AtomIterator::getCount(){
|
|---|
| 142 | return index;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
|
|---|
| 146 | return AtomIterator(descr,this);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | World::AtomList::iterator World::atomEnd(){
|
|---|
| 150 | return atoms.end();
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /******************************* Singleton Stuff **************************/
|
|---|
| 154 |
|
|---|
| 155 | // TODO: Hide boost-thread using Autotools stuff when no threads are used
|
|---|
| 156 | World* World::theWorld = 0;
|
|---|
| 157 | boost::mutex World::worldLock;
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | World::World() :
|
|---|
| 162 | dummyId(0),
|
|---|
| 163 | periode(new periodentafel),
|
|---|
| 164 | molecules_deprecated(new MoleculeListClass),
|
|---|
| 165 | atoms()
|
|---|
| 166 | {
|
|---|
| 167 | molecules_deprecated->signOn(this);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | World::~World()
|
|---|
| 171 | {
|
|---|
| 172 | delete periode;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | World* World::get(){
|
|---|
| 176 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 177 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 178 | if(!theWorld) {
|
|---|
| 179 | theWorld = new World();
|
|---|
| 180 | }
|
|---|
| 181 | return theWorld;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | void World::destroy(){
|
|---|
| 185 | // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
|
|---|
| 186 | theWorld->destroyLegacy();
|
|---|
| 187 | //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
|
|---|
| 188 |
|
|---|
| 189 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 190 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 191 | delete theWorld;
|
|---|
| 192 | theWorld = 0;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | World* World::reset(){
|
|---|
| 196 | // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
|
|---|
| 197 | theWorld->destroyLegacy();
|
|---|
| 198 | //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
|
|---|
| 199 |
|
|---|
| 200 | World* oldWorld = 0;
|
|---|
| 201 | {
|
|---|
| 202 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 203 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 204 |
|
|---|
| 205 | oldWorld = theWorld;
|
|---|
| 206 | theWorld = new World();
|
|---|
| 207 | // oldworld does not need protection any more,
|
|---|
| 208 | // since we should have the only reference
|
|---|
| 209 |
|
|---|
| 210 | // worldLock handles access to the pointer,
|
|---|
| 211 | // not to the object
|
|---|
| 212 | } // scope-end releases the lock
|
|---|
| 213 |
|
|---|
| 214 | // we have to let all the observers know that the
|
|---|
| 215 | // oldWorld was destroyed. oldWorld calls subjectKilled
|
|---|
| 216 | // upon destruction. Every Observer getting that signal
|
|---|
| 217 | // should see that it gets the updated new world
|
|---|
| 218 | delete oldWorld;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /******************************* deprecated Legacy Stuff ***********************/
|
|---|
| 222 |
|
|---|
| 223 | MoleculeListClass *&World::getMolecules() {
|
|---|
| 224 | return molecules_deprecated;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | // some legacy stuff to let the World know about items created outside
|
|---|
| 228 | void World::registerAtom(atom *theAtom){
|
|---|
| 229 | OBSERVE;
|
|---|
| 230 | atoms[dummyId++] = theAtom;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | void World::destroyLegacy(){
|
|---|
| 234 | //delete molecules_deprecated;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | void World::unregisterAtom(atom *theAtom){
|
|---|
| 238 | OBSERVE;
|
|---|
| 239 | atoms.erase(theAtom->getId());
|
|---|
| 240 | }
|
|---|