| [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"
|
|---|
| [7c4e29] | 15 | #include "Actions/ManipulateAtomsProcess.hpp"
|
|---|
| [d346b6] | 16 |
|
|---|
| 17 | using namespace std;
|
|---|
| [4d9c01] | 18 |
|
|---|
| [5d1611] | 19 | /******************************* getter and setter ************************/
|
|---|
| [354859] | 20 | periodentafel *&World::getPeriode(){
|
|---|
| [5d1611] | 21 | return periode;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| [7a1ce5] | 24 | atom* World::getAtom(AtomDescriptor descriptor){
|
|---|
| [fc1b24] | 25 | return descriptor.find();
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| [7a1ce5] | 28 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
|---|
| [fc1b24] | 29 | return descriptor.findAll();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| [0e2a47] | 32 | vector<atom*> World::getAllAtoms(){
|
|---|
| 33 | return getAllAtoms(AllAtoms());
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| [354859] | 36 | int World::numAtoms(){
|
|---|
| 37 | return atoms.size();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | int World::numMolecules(){
|
|---|
| 41 | return molecules_deprecated->ListOfMolecules.size();
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| [afb47f] | 44 | /******************** Methods to change World state *********************/
|
|---|
| 45 |
|
|---|
| [354859] | 46 | molecule* World::createMolecule(){
|
|---|
| 47 | OBSERVE;
|
|---|
| 48 | molecule *mol = NULL;
|
|---|
| [cbc5fb] | 49 | mol = NewMolecule();
|
|---|
| [d2dbac0] | 50 | assert(!molecules.count(currMoleculeId));
|
|---|
| [cbc5fb] | 51 | mol->setId(currMoleculeId++);
|
|---|
| [244d26] | 52 | // store the molecule by ID
|
|---|
| [cbc5fb] | 53 | molecules[mol->getId()] = mol;
|
|---|
| [354859] | 54 | mol->signOn(this);
|
|---|
| 55 | return mol;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| [cbc5fb] | 58 | void World::destroyMolecule(molecule* mol){
|
|---|
| 59 | OBSERVE;
|
|---|
| 60 | destroyMolecule(mol->getId());
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void World::destroyMolecule(moleculeId_t id){
|
|---|
| 64 | OBSERVE;
|
|---|
| 65 | molecule *mol = molecules[id];
|
|---|
| 66 | assert(mol);
|
|---|
| 67 | DeleteMolecule(mol);
|
|---|
| 68 | molecules.erase(id);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| [7c4e29] | 71 |
|
|---|
| [46d958] | 72 | atom *World::createAtom(){
|
|---|
| 73 | OBSERVE;
|
|---|
| 74 | atom *res = NewAtom();
|
|---|
| [d2dbac0] | 75 | assert(!atoms.count(currAtomId));
|
|---|
| [46d958] | 76 | res->setId(currAtomId++);
|
|---|
| 77 | res->setWorld(this);
|
|---|
| [244d26] | 78 | // store the atom by ID
|
|---|
| [46d958] | 79 | atoms[res->getId()] = res;
|
|---|
| 80 | return res;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int World::registerAtom(atom *atom){
|
|---|
| 84 | OBSERVE;
|
|---|
| [d2dbac0] | 85 | assert(!atoms.count(currAtomId));
|
|---|
| [46d958] | 86 | atom->setId(currAtomId++);
|
|---|
| 87 | atom->setWorld(this);
|
|---|
| 88 | atoms[atom->getId()] = atom;
|
|---|
| 89 | return atom->getId();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void World::destroyAtom(atom* atom){
|
|---|
| 93 | OBSERVE;
|
|---|
| 94 | int id = atom->getId();
|
|---|
| 95 | destroyAtom(id);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| [cbc5fb] | 98 | void World::destroyAtom(atomId_t id) {
|
|---|
| [46d958] | 99 | OBSERVE;
|
|---|
| 100 | atom *atom = atoms[id];
|
|---|
| 101 | assert(atom);
|
|---|
| 102 | DeleteAtom(atom);
|
|---|
| 103 | atoms.erase(id);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| [7c4e29] | 106 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
|---|
| 107 | return new ManipulateAtomsProcess(op, descr,name,true);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [0e2a47] | 110 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
|
|---|
| 111 | return manipulateAtoms(op,name,AllAtoms());
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| [afb47f] | 114 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
|---|
| 115 |
|
|---|
| 116 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
|---|
| 117 | proc->signOn(this);
|
|---|
| 118 | {
|
|---|
| 119 | OBSERVE;
|
|---|
| 120 | proc->doManipulate(this);
|
|---|
| 121 | }
|
|---|
| 122 | proc->signOff(this);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| [865a945] | 125 | /******************************* Iterators ********************************/
|
|---|
| 126 |
|
|---|
| [d2dbac0] | 127 | /*
|
|---|
| 128 | * Actual Implementation of the iterators can be found in WorldIterators.cpp
|
|---|
| 129 | */
|
|---|
| [865a945] | 130 |
|
|---|
| 131 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
|
|---|
| 132 | return AtomIterator(descr,this);
|
|---|
| 133 | }
|
|---|
| [354859] | 134 |
|
|---|
| [d2dbac0] | 135 | World::AtomSet::iterator World::atomEnd(){
|
|---|
| [7c4e29] | 136 | return atoms.end();
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| [5d1611] | 139 | /******************************* Singleton Stuff **************************/
|
|---|
| 140 |
|
|---|
| [4d9c01] | 141 | // TODO: Hide boost-thread using Autotools stuff when no threads are used
|
|---|
| [5d1611] | 142 | World* World::theWorld = 0;
|
|---|
| [4d9c01] | 143 | boost::mutex World::worldLock;
|
|---|
| 144 |
|
|---|
| [7a1ce5] | 145 | World::World() :
|
|---|
| [46d958] | 146 | currAtomId(0),
|
|---|
| [d2dbac0] | 147 | currMoleculeId(0),
|
|---|
| [354859] | 148 | periode(new periodentafel),
|
|---|
| [cbc5fb] | 149 | molecules_deprecated(new MoleculeListClass(this)),
|
|---|
| [d2dbac0] | 150 | atoms(),
|
|---|
| 151 | molecules()
|
|---|
| [7dad10] | 152 | {
|
|---|
| 153 | molecules_deprecated->signOn(this);
|
|---|
| 154 | }
|
|---|
| [5d1611] | 155 |
|
|---|
| 156 | World::~World()
|
|---|
| [354859] | 157 | {
|
|---|
| [46d958] | 158 | delete molecules_deprecated;
|
|---|
| [354859] | 159 | delete periode;
|
|---|
| [cbc5fb] | 160 | MoleculeSet::iterator molIter;
|
|---|
| 161 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
|
|---|
| 162 | DeleteMolecule((*molIter).second);
|
|---|
| 163 | }
|
|---|
| 164 | molecules.clear();
|
|---|
| 165 | AtomSet::iterator atIter;
|
|---|
| 166 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
|
|---|
| 167 | DeleteAtom((*atIter).second);
|
|---|
| [46d958] | 168 | }
|
|---|
| 169 | atoms.clear();
|
|---|
| [354859] | 170 | }
|
|---|
| [5d1611] | 171 |
|
|---|
| 172 | World* World::get(){
|
|---|
| 173 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 174 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 175 | if(!theWorld) {
|
|---|
| 176 | theWorld = new World();
|
|---|
| 177 | }
|
|---|
| 178 | return theWorld;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | void World::destroy(){
|
|---|
| 182 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 183 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 184 | delete theWorld;
|
|---|
| 185 | theWorld = 0;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | World* World::reset(){
|
|---|
| 189 | World* oldWorld = 0;
|
|---|
| 190 | {
|
|---|
| 191 | // boost supports RAII-Style locking, so we don't need to unlock
|
|---|
| 192 | boost::mutex::scoped_lock guard(worldLock);
|
|---|
| 193 |
|
|---|
| 194 | oldWorld = theWorld;
|
|---|
| 195 | theWorld = new World();
|
|---|
| 196 | // oldworld does not need protection any more,
|
|---|
| 197 | // since we should have the only reference
|
|---|
| 198 |
|
|---|
| 199 | // worldLock handles access to the pointer,
|
|---|
| 200 | // not to the object
|
|---|
| 201 | } // scope-end releases the lock
|
|---|
| 202 |
|
|---|
| 203 | // we have to let all the observers know that the
|
|---|
| 204 | // oldWorld was destroyed. oldWorld calls subjectKilled
|
|---|
| 205 | // upon destruction. Every Observer getting that signal
|
|---|
| 206 | // should see that it gets the updated new world
|
|---|
| 207 | delete oldWorld;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /******************************* deprecated Legacy Stuff ***********************/
|
|---|
| 211 |
|
|---|
| [354859] | 212 | MoleculeListClass *&World::getMolecules() {
|
|---|
| 213 | return molecules_deprecated;
|
|---|
| [5d1611] | 214 | }
|
|---|