Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/World.cpp

    r70378e r387b36  
    11/*
    2  * world.cpp
     2 * World.cpp
    33 *
    4  *  Created on: Mar 3, 2010
     4 *  Created on: Feb 3, 2010
    55 *      Author: crueger
    66 */
     
    88#include "World.hpp"
    99
    10 double *World::cell_size = 0;
    11 
    12 /** Constructor of World.
    13  *
    14  */
    15 World::World()
     10#include "atom.hpp"
     11#include "config.hpp"
     12#include "molecule.hpp"
     13#include "periodentafel.hpp"
     14#include "Descriptors/AtomDescriptor.hpp"
     15#include "Descriptors/AtomDescriptor_impl.hpp"
     16#include "Descriptors/MoleculeDescriptor.hpp"
     17#include "Descriptors/MoleculeDescriptor_impl.hpp"
     18#include "Descriptors/SelectiveIterator_impl.hpp"
     19#include "Actions/ManipulateAtomsProcess.hpp"
     20
     21#include "Patterns/Singleton_impl.hpp"
     22
     23using namespace std;
     24
     25/******************************* getter and setter ************************/
     26periodentafel *&World::getPeriode(){
     27  return periode;
     28}
     29
     30config *&World::getConfig(){
     31  return configuration;
     32}
     33
     34// Atoms
     35
     36atom* World::getAtom(AtomDescriptor descriptor){
     37  return descriptor.find();
     38}
     39
     40vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
     41  return descriptor.findAll();
     42}
     43
     44vector<atom*> World::getAllAtoms(){
     45  return getAllAtoms(AllAtoms());
     46}
     47
     48int World::numAtoms(){
     49  return atoms.size();
     50}
     51
     52// Molecules
     53
     54molecule *World::getMolecule(MoleculeDescriptor descriptor){
     55  return descriptor.find();
     56}
     57
     58std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
     59  return descriptor.findAll();
     60}
     61
     62std::vector<molecule*> World::getAllMolecules(){
     63  return getAllMolecules(AllMolecules());
     64}
     65
     66int World::numMolecules(){
     67  return molecules_deprecated->ListOfMolecules.size();
     68}
     69
     70// system
     71
     72double * World::getDomain() {
     73  return cell_size;
     74}
     75
     76void World::setDomain(double * matrix)
     77{
     78
     79}
     80
     81std::string World::getDefaultName() {
     82  return defaultName;
     83}
     84
     85void World::setDefaultName(std::string name)
     86{
     87  defaultName = name;
     88};
     89
     90int World::getExitFlag() {
     91  return ExitFlag;
     92}
     93
     94void World::setExitFlag(int flag) {
     95  if (ExitFlag < flag)
     96    ExitFlag = flag;
     97}
     98
     99/******************** Methods to change World state *********************/
     100
     101molecule* World::createMolecule(){
     102  OBSERVE;
     103  molecule *mol = NULL;
     104  mol = NewMolecule();
     105  assert(!molecules.count(currMoleculeId));
     106  mol->setId(currMoleculeId++);
     107  // store the molecule by ID
     108  molecules[mol->getId()] = mol;
     109  mol->signOn(this);
     110  return mol;
     111}
     112
     113void World::destroyMolecule(molecule* mol){
     114  OBSERVE;
     115  destroyMolecule(mol->getId());
     116}
     117
     118void World::destroyMolecule(moleculeId_t id){
     119  OBSERVE;
     120  molecule *mol = molecules[id];
     121  assert(mol);
     122  DeleteMolecule(mol);
     123  molecules.erase(id);
     124}
     125
     126double *World::cell_size = NULL;
     127
     128atom *World::createAtom(){
     129  OBSERVE;
     130  atomId_t id = getNextAtomId();
     131  atom *res = NewAtom(id);
     132  res->setWorld(this);
     133  // store the atom by ID
     134  atoms[res->getId()] = res;
     135  return res;
     136}
     137
     138
     139int World::registerAtom(atom *atom){
     140  OBSERVE;
     141  atomId_t id = getNextAtomId();
     142  atom->setId(id);
     143  atom->setWorld(this);
     144  atoms[atom->getId()] = atom;
     145  return atom->getId();
     146}
     147
     148void World::destroyAtom(atom* atom){
     149  OBSERVE;
     150  int id = atom->getId();
     151  destroyAtom(id);
     152}
     153
     154void World::destroyAtom(atomId_t id) {
     155  OBSERVE;
     156  atom *atom = atoms[id];
     157  assert(atom);
     158  DeleteAtom(atom);
     159  atoms.erase(id);
     160  releaseAtomId(id);
     161}
     162
     163bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
     164  OBSERVE;
     165  // in case this call did not originate from inside the atom, we redirect it,
     166  // to also let it know that it has changed
     167  if(!target){
     168    target = atoms[oldId];
     169    assert(target && "Atom with that ID not found");
     170    return target->changeId(newId);
     171  }
     172  else{
     173    if(reserveAtomId(newId)){
     174      atoms.erase(oldId);
     175      atoms.insert(pair<atomId_t,atom*>(newId,target));
     176      return true;
     177    }
     178    else{
     179      return false;
     180    }
     181  }
     182}
     183
     184ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
     185  return new ManipulateAtomsProcess(op, descr,name,true);
     186}
     187
     188ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
     189  return manipulateAtoms(op,name,AllAtoms());
     190}
     191
     192/********************* Internal Change methods for double Callback and Observer mechanism ********/
     193
     194void World::doManipulate(ManipulateAtomsProcess *proc){
     195  proc->signOn(this);
     196  {
     197    OBSERVE;
     198    proc->doManipulate(this);
     199  }
     200  proc->signOff(this);
     201}
     202/******************************* IDManagement *****************************/
     203
     204// Atoms
     205
     206atomId_t World::getNextAtomId(){
     207  // see if we can reuse some Id
     208  if(atomIdPool.empty()){
     209    return currAtomId++;
     210  }
     211  else{
     212    // we give out the first ID from the pool
     213    atomId_t id = *(atomIdPool.begin());
     214    atomIdPool.erase(id);
     215    return id;
     216  }
     217}
     218
     219void World::releaseAtomId(atomId_t id){
     220  atomIdPool.insert(id);
     221  // defragmentation of the pool
     222  set<atomId_t>::reverse_iterator iter;
     223  // go through all Ids in the pool that lie immediately below the border
     224  while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
     225    atomIdPool.erase(--currAtomId);
     226  }
     227}
     228
     229bool World::reserveAtomId(atomId_t id){
     230  if(id>=currAtomId ){
     231    // add all ids between the new one and current border as available
     232    for(atomId_t pos=currAtomId; pos<id; ++pos){
     233      atomIdPool.insert(pos);
     234    }
     235    currAtomId=id+1;
     236    return true;
     237  }
     238  else if(atomIdPool.count(id)){
     239    atomIdPool.erase(id);
     240    return true;
     241  }
     242  else{
     243    // this ID could not be reserved
     244    return false;
     245  }
     246}
     247
     248// Molecules
     249
     250/******************************* Iterators ********************************/
     251
     252// Build the AtomIterator from template
     253CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
     254
     255
     256World::AtomIterator World::getAtomIter(AtomDescriptor descr){
     257  return AtomIterator(descr,atoms);
     258}
     259
     260World::AtomIterator World::atomEnd(){
     261  return AtomIterator(AllAtoms(),atoms,atoms.end());
     262}
     263
     264// build the MoleculeIterator from template
     265CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
     266
     267World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
     268  return MoleculeIterator(descr,molecules);
     269}
     270
     271World::MoleculeIterator World::moleculeEnd(){
     272  return MoleculeIterator(AllMolecules(),molecules,molecules.end());
     273}
     274
     275/******************************* Singleton Stuff **************************/
     276
     277World::World() :
     278    periode(new periodentafel),
     279    configuration(new config),
     280    ExitFlag(0),
     281    atoms(),
     282    currAtomId(0),
     283    molecules(),
     284    currMoleculeId(0),
     285    molecules_deprecated(new MoleculeListClass(this))
    16286{
    17287  cell_size = new double[6];
    18 };
    19 
    20 /** Destructor of World.
    21  *
    22  */
     288  cell_size[0] = 20.;
     289  cell_size[1] = 0.;
     290  cell_size[2] = 20.;
     291  cell_size[3] = 0.;
     292  cell_size[4] = 0.;
     293  cell_size[5] = 20.;
     294  defaultName = "none";
     295  molecules_deprecated->signOn(this);
     296}
     297
    23298World::~World()
    24299{
    25   delete[](cell_size);
    26 };
    27 
    28 
    29 // TODO: Hide boost-thread using Autotools stuff when no threads are used
    30 World* World::theWorld = 0;
    31 
    32 
    33 World* World::get(){
    34   // boost supports RAII-Style locking, so we don't need to unlock
    35   if(!theWorld) {
    36     theWorld = new World();
    37   }
    38   return theWorld;
    39 }
    40 
    41 void World::destroy(){
    42   delete theWorld;
    43   theWorld = 0;
    44 }
    45 
    46 World* World::reset(){
    47   World* oldWorld = 0;
    48   {
    49     oldWorld = theWorld;
    50     theWorld = new World();
    51     // oldworld does not need protection any more,
    52     // since we should have the only reference
    53 
    54     // worldLock handles access to the pointer,
    55     // not to the object
    56   } // scope-end releases the lock
    57 
    58   // we have to let all the observers know that the
    59   // oldWorld was destroyed. oldWorld calls subjectKilled
    60   // upon destruction. Every Observer getting that signal
    61   // should see that it gets the updated new world
    62   delete oldWorld;
    63 }
     300  molecules_deprecated->signOff(this);
     301  delete[] cell_size;
     302  delete molecules_deprecated;
     303  delete periode;
     304  delete configuration;
     305  MoleculeSet::iterator molIter;
     306  for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
     307    DeleteMolecule((*molIter).second);
     308  }
     309  molecules.clear();
     310  AtomSet::iterator atIter;
     311  for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
     312    DeleteAtom((*atIter).second);
     313  }
     314  atoms.clear();
     315}
     316
     317// Explicit instantiation of the singleton mechanism at this point
     318
     319CONSTRUCT_SINGLETON(World)
     320
     321/******************************* deprecated Legacy Stuff ***********************/
     322
     323MoleculeListClass *&World::getMolecules() {
     324  return molecules_deprecated;
     325}
Note: See TracChangeset for help on using the changeset viewer.