Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/World.cpp

    r24a5e0 r028c2e  
    1212#include "periodentafel.hpp"
    1313#include "Descriptors/AtomDescriptor.hpp"
    14 #include "Descriptors/AtomDescriptor_impl.hpp"
    15 #include "Actions/ManipulateAtomsProcess.hpp"
    1614
    1715using namespace std;
     
    3028}
    3129
    32 vector<atom*> World::getAllAtoms(){
    33   return getAllAtoms(AllAtoms());
    34 }
    35 
    3630int World::numAtoms(){
    3731  return atoms.size();
     
    4236}
    4337
    44 /******************** Methods to change World state *********************/
    45 
    4638molecule* World::createMolecule(){
    4739  OBSERVE;
    4840  molecule *mol = NULL;
    49   mol = NewMolecule();
    50   assert(!molecules.count(currMoleculeId));
    51   mol->setId(currMoleculeId++);
    52   // store the molecule by ID
    53   molecules[mol->getId()] = mol;
     41  mol = new molecule(periode);
     42  molecules_deprecated->insert(mol);
     43  molecules.insert(mol);
    5444  mol->signOn(this);
    5545  return mol;
    5646}
    5747
    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 
    71 
    72 atom *World::createAtom(){
    73   OBSERVE;
    74   atom *res = NewAtom();
    75   assert(!atoms.count(currAtomId));
    76   res->setId(currAtomId++);
    77   res->setWorld(this);
    78   // store the atom by ID
    79   atoms[res->getId()] = res;
    80   return res;
    81 }
    82 
    83 int World::registerAtom(atom *atom){
    84   OBSERVE;
    85   assert(!atoms.count(currAtomId));
    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 
    98 void World::destroyAtom(atomId_t id) {
    99   OBSERVE;
    100   atom *atom = atoms[id];
    101   assert(atom);
    102   DeleteAtom(atom);
    103   atoms.erase(id);
    104 }
    105 
    106 ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
    107   return new ManipulateAtomsProcess(op, descr,name,true);
    108 }
    109 
    110 ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
    111   return manipulateAtoms(op,name,AllAtoms());
    112 }
    113 
    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 
    125 /******************************* Iterators ********************************/
    126 
    127 /*
    128  * Actual Implementation of the iterators can be found in WorldIterators.cpp
    129  */
    130 
    131 World::AtomIterator World::getAtomIter(AtomDescriptor descr){
    132   return AtomIterator(descr,this);
    133 }
    134 
    135 World::AtomSet::iterator World::atomEnd(){
    136   return atoms.end();
    137 }
    13848
    13949/******************************* Singleton Stuff **************************/
     
    14353boost::mutex World::worldLock;
    14454
     55
     56
    14557World::World() :
    14658    periode(new periodentafel),
    147     atoms(),
    148     currAtomId(0),
    149     molecules(),
    150     currMoleculeId(0),
    151     molecules_deprecated(new MoleculeListClass(this))
     59    molecules_deprecated(new MoleculeListClass),
     60    dummyId(0)
    15261{
    15362  molecules_deprecated->signOn(this);
     
    15665World::~World()
    15766{
     67  molecules_deprecated->signOff(this);
    15868  delete molecules_deprecated;
    15969  delete periode;
    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);
    168   }
    169   atoms.clear();
    17070}
    17171
     
    18080
    18181void World::destroy(){
     82  // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
     83  theWorld->destroyLegacy();
     84  //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
     85
    18286  // boost supports RAII-Style locking, so we don't need to unlock
    18387  boost::mutex::scoped_lock guard(worldLock);
     
    18791
    18892World* World::reset(){
     93  // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
     94  theWorld->destroyLegacy();
     95  //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
     96
    18997  World* oldWorld = 0;
    19098  {
     
    206114  // should see that it gets the updated new world
    207115  delete oldWorld;
    208   return theWorld;
    209116}
    210117
     
    214121  return molecules_deprecated;
    215122}
     123
     124// some legacy stuff to let the World know about items created outside
     125void World::registerAtom(atom *theAtom){
     126  OBSERVE;
     127  atoms[dummyId++] = theAtom;
     128}
     129
     130void World::destroyLegacy(){
     131  //delete molecules_deprecated;
     132}
     133
     134void World::unregisterAtom(atom *theAtom){
     135  OBSERVE;
     136  atoms.erase(theAtom->getId());
     137}
Note: See TracChangeset for help on using the changeset viewer.