Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/World.cpp

    r028c2e r24a5e0  
    1212#include "periodentafel.hpp"
    1313#include "Descriptors/AtomDescriptor.hpp"
     14#include "Descriptors/AtomDescriptor_impl.hpp"
     15#include "Actions/ManipulateAtomsProcess.hpp"
    1416
    1517using namespace std;
     
    2830}
    2931
     32vector<atom*> World::getAllAtoms(){
     33  return getAllAtoms(AllAtoms());
     34}
     35
    3036int World::numAtoms(){
    3137  return atoms.size();
     
    3642}
    3743
     44/******************** Methods to change World state *********************/
     45
    3846molecule* World::createMolecule(){
    3947  OBSERVE;
    4048  molecule *mol = NULL;
    41   mol = new molecule(periode);
    42   molecules_deprecated->insert(mol);
    43   molecules.insert(mol);
     49  mol = NewMolecule();
     50  assert(!molecules.count(currMoleculeId));
     51  mol->setId(currMoleculeId++);
     52  // store the molecule by ID
     53  molecules[mol->getId()] = mol;
    4454  mol->signOn(this);
    4555  return mol;
    4656}
    4757
     58void World::destroyMolecule(molecule* mol){
     59  OBSERVE;
     60  destroyMolecule(mol->getId());
     61}
     62
     63void 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
     72atom *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
     83int 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
     92void World::destroyAtom(atom* atom){
     93  OBSERVE;
     94  int id = atom->getId();
     95  destroyAtom(id);
     96}
     97
     98void World::destroyAtom(atomId_t id) {
     99  OBSERVE;
     100  atom *atom = atoms[id];
     101  assert(atom);
     102  DeleteAtom(atom);
     103  atoms.erase(id);
     104}
     105
     106ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
     107  return new ManipulateAtomsProcess(op, descr,name,true);
     108}
     109
     110ManipulateAtomsProcess* 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
     116void 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
     131World::AtomIterator World::getAtomIter(AtomDescriptor descr){
     132  return AtomIterator(descr,this);
     133}
     134
     135World::AtomSet::iterator World::atomEnd(){
     136  return atoms.end();
     137}
    48138
    49139/******************************* Singleton Stuff **************************/
     
    53143boost::mutex World::worldLock;
    54144
    55 
    56 
    57145World::World() :
    58146    periode(new periodentafel),
    59     molecules_deprecated(new MoleculeListClass),
    60     dummyId(0)
     147    atoms(),
     148    currAtomId(0),
     149    molecules(),
     150    currMoleculeId(0),
     151    molecules_deprecated(new MoleculeListClass(this))
    61152{
    62153  molecules_deprecated->signOn(this);
     
    65156World::~World()
    66157{
    67   molecules_deprecated->signOff(this);
    68158  delete molecules_deprecated;
    69159  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();
    70170}
    71171
     
    80180
    81181void 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 
    86182  // boost supports RAII-Style locking, so we don't need to unlock
    87183  boost::mutex::scoped_lock guard(worldLock);
     
    91187
    92188World* 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 
    97189  World* oldWorld = 0;
    98190  {
     
    114206  // should see that it gets the updated new world
    115207  delete oldWorld;
     208  return theWorld;
    116209}
    117210
     
    121214  return molecules_deprecated;
    122215}
    123 
    124 // some legacy stuff to let the World know about items created outside
    125 void World::registerAtom(atom *theAtom){
    126   OBSERVE;
    127   atoms[dummyId++] = theAtom;
    128 }
    129 
    130 void World::destroyLegacy(){
    131   //delete molecules_deprecated;
    132 }
    133 
    134 void World::unregisterAtom(atom *theAtom){
    135   OBSERVE;
    136   atoms.erase(theAtom->getId());
    137 }
Note: See TracChangeset for help on using the changeset viewer.