Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/World.hpp

    r046783 r6e97e5  
    11/*
    2  * world.hpp
     2 * World.hpp
    33 *
    4  *  Created on: Mar 3, 2010
    5  *      Author: heber
     4 *  Created on: Feb 3, 2010
     5 *      Author: crueger
    66 */
    77
     
    99#define WORLD_HPP_
    1010
    11 using namespace std;
    12 
    13 /*********************************************** includes ***********************************/
    14 
    15 // include config.h
    16 #ifdef HAVE_CONFIG_H
    17 #include <config.h>
    18 #endif
    19 
    20 
    21 /****************************************** forward declarations *****************************/
    22 
    23 /********************************************** Class World *******************************/
    24 
    25 class World
     11#include <string>
     12#include <map>
     13#include <vector>
     14#include <set>
     15#include <boost/thread.hpp>
     16#include <boost/shared_ptr.hpp>
     17
     18#include "types.hpp"
     19#include "Descriptors/SelectiveIterator.hpp"
     20#include "Patterns/Observer.hpp"
     21#include "Patterns/Cacheable.hpp"
     22#include "Patterns/Singleton.hpp"
     23
     24
     25// forward declarations
     26class periodentafel;
     27class MoleculeListClass;
     28class atom;
     29class molecule;
     30class AtomDescriptor;
     31class AtomDescriptor_impl;
     32class MoleculeDescriptor;
     33class MoleculeDescriptor_impl;
     34class ManipulateAtomsProcess;
     35template<typename T>
     36class AtomsCalculation;
     37
     38
     39
     40class World : public Singleton<World>, public Observable
    2641{
    27   /***** singleton Stuff *****/
     42
     43// Make access to constructor and destructor possible from inside the singleton
     44friend class Singleton<World>;
     45
     46// necessary for coupling with descriptors
     47friend class AtomDescriptor_impl;
     48friend class AtomDescriptor;
     49friend class MoleculeDescriptor_impl;
     50friend class MoleculeDescriptor;
     51
     52// Actions, calculations etc associated with the World
     53friend class ManipulateAtomsProcess;
     54template<typename> friend class AtomsCalculation;
    2855public:
    29   static World* get();
    30   static void destroy();
    31   static World* reset();
    32 
    33   static double *cell_size;
    34   static char *DefaultName;
    35 
    36 private:
     56
     57  // Types for Atom and Molecule structures
     58  typedef std::map<atomId_t,atom*> AtomSet;
     59  typedef std::map<moleculeId_t,molecule*> MoleculeSet;
     60
     61  /***** getter and setter *****/
     62  // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
     63  /**
     64   * returns the periodentafel for the world.
     65   */
     66  periodentafel *&getPeriode();
     67
     68  /**
     69   * returns the first atom that matches a given descriptor.
     70   * Do not rely on ordering for descriptors that match more than one atom.
     71   */
     72  atom* getAtom(AtomDescriptor descriptor);
     73
     74  /**
     75   * returns a vector containing all atoms that match a given descriptor
     76   */
     77  std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
     78  std::vector<atom*> getAllAtoms();
     79
     80  /**
     81   * returns a calculation that calls a given function on all atoms matching a descriptor.
     82   * the calculation is not called at this point and can be used as an action, i.e. be stored in
     83   * menus, be kept around for later use etc.
     84   */
     85  template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor);
     86  template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string);
     87
     88  /**
     89   * get the number of atoms in the World
     90   */
     91  int numAtoms();
     92
     93  /**
     94   * returns the first molecule that matches a given descriptor.
     95   * Do not rely on ordering for descriptors that match more than one molecule.
     96   */
     97  molecule *getMolecule(MoleculeDescriptor descriptor);
     98
     99  /**
     100   * returns a vector containing all molecules that match a given descriptor
     101   */
     102  std::vector<molecule*> getAllMolecules(MoleculeDescriptor descriptor);
     103
     104  /**
     105   * get the number of molecules in the World
     106   */
     107  int numMolecules();
     108
     109  /***** Methods to work with the World *****/
     110
     111  /**
     112   * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique
     113   * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly.
     114   */
     115  molecule *createMolecule();
     116
     117  void destroyMolecule(molecule*);
     118  void destroyMolecule(moleculeId_t);
     119
     120  /**
     121   * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores
     122   * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends.
     123   */
     124  atom *createAtom();
     125
     126  /**
     127   * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests.
     128   * Do not re-register Atoms already known to the world since this will cause double-frees.
     129   */
     130  int registerAtom(atom*);
     131
     132  /**
     133     * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
     134     * atom directly since this will leave the pointer inside the world.
     135   */
     136  void destroyAtom(atom*);
     137
     138  /**
     139   * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
     140   * atom directly since this will leave the pointer inside the world.
     141   */
     142  void destroyAtom(atomId_t);
     143
     144  /**
     145   * used when changing an atom Id.
     146   * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
     147   *
     148   * Return value indicates wether the change could be done or not.
     149   */
     150  bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0);
     151
     152  /**
     153   * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
     154   * called at this time, so it can be passed around, stored inside menuItems etc.
     155   */
     156  ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
     157  ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
     158
     159protected:
     160  /**** Iterators to use internal data structures */
     161
     162  // Atoms
     163  typedef SelectiveIterator<atom*,AtomSet,AtomDescriptor> AtomIterator;
     164
     165  /**
     166   * returns an iterator over all Atoms matching a given descriptor.
     167   * used for internal purposes, like AtomProcesses and AtomCalculations.
     168   */
     169  AtomIterator getAtomIter(AtomDescriptor descr);
     170
     171  /**
     172   * returns an iterator to the end of the AtomSet. Due to overloading this iterator
     173   * can be compared to iterators produced by getAtomIter (see the mis-matching types).
     174   * Thus it can be used to detect when such an iterator is at the end of the list.
     175   * used for internal purposes, like AtomProcesses and AtomCalculations.
     176   */
     177  AtomIterator atomEnd();
     178
     179  // Molecules
     180
     181  typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor> MoleculeIterator;
     182
     183  /**
     184   * returns an iterator over all Molecules matching a given descriptor.
     185   * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
     186   */
     187  MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
     188
     189  /**
     190   * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
     191   * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
     192   * Thus it can be used to detect when such an iterator is at the end of the list.
     193   * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
     194   */
     195  MoleculeIterator moleculeEnd();
     196
     197
     198  /******* Internal manipulation routines for double callback and Observer mechanism ******/
     199  void doManipulate(ManipulateAtomsProcess *);
     200
     201private:
     202
     203  atomId_t getNextAtomId();
     204  void releaseAtomId(atomId_t);
     205  bool reserveAtomId(atomId_t);
     206
     207  periodentafel *periode;
     208public:
     209  AtomSet atoms;
     210private:
     211  std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
     212  atomId_t currAtomId; //!< stores the next available Id for atoms
     213  MoleculeSet molecules;
     214  moleculeId_t currMoleculeId;
     215private:
     216  /**
     217   * private constructor to ensure creation of the world using
     218   * the singleton pattern.
     219   */
    37220  World();
     221
     222  /**
     223   * private destructor to ensure destruction of the world using the
     224   * singleton pattern.
     225   */
    38226  virtual ~World();
    39227
    40   static World *theWorld;
     228  /*****
     229   * some legacy stuff that is include for now but will be removed later
     230   *****/
     231public:
     232  MoleculeListClass *&getMolecules();
     233
     234private:
     235  MoleculeListClass *molecules_deprecated;
    41236};
    42237
Note: See TracChangeset for help on using the changeset viewer.