Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/World.hpp

    r24a5e0 r354859  
    99#define WORLD_HPP_
    1010
    11 #include <string>
     11#include <boost/thread.hpp>
    1212#include <map>
    1313#include <vector>
    1414#include <set>
    15 #include <boost/thread.hpp>
    16 #include <boost/shared_ptr.hpp>
    1715
    18 #include "defs.hpp"
    1916#include "Patterns/Observer.hpp"
    2017#include "Patterns/Cacheable.hpp"
     
    2724class AtomDescriptor;
    2825class AtomDescriptor_impl;
    29 class ManipulateAtomsProcess;
    30 template<typename T>
    31 class AtomsCalculation;
    3226
    3327class World : public Observable
    3428{
    35 // necessary for coupling with descriptors
    3629friend class AtomDescriptor_impl;
    37 friend class AtomDescriptor;
    38 
    39 // Actions, calculations etc associated with the World
    40 friend class ManipulateAtomsProcess;
    41 template<typename> friend class AtomsCalculation;
    4230public:
    43   typedef std::map<atomId_t,atom*> AtomSet;
    44   typedef std::map<moleculeId_t,molecule*> MoleculeSet;
    4531
    4632  /***** getter and setter *****/
    4733  // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
    48   /**
    49    * returns the periodentafel for the world.
    50    */
    5134  periodentafel *&getPeriode();
    52 
    53   /**
    54    * returns the first atom that matches a given descriptor.
    55    * Do not rely on ordering for descriptors that match more than one atom.
    56    */
    5735  atom* getAtom(AtomDescriptor descriptor);
    58 
    59   /**
    60    * returns a vector containing all atoms that match a given descriptor
    61    */
    6236  std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
    63   std::vector<atom*> getAllAtoms();
    64 
    65   /**
    66    * returns a calculation that calls a given function on all atoms matching a descriptor.
    67    * the calculation is not called at this point and can be used as an action, i.e. be stored in
    68    * menus, be kept around for later use etc.
    69    */
    70   template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor);
    71   template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string);
    72 
    73   /**
    74    * get the number of atoms in the World
    75    */
    7637  int numAtoms();
    77 
    78   /**
    79    * get the number of molecules in the World
    80    */
    8138  int numMolecules();
    8239
    8340  /***** Methods to work with the World *****/
    84 
    85   /**
    86    * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique
    87    * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly.
    88    */
    8941  molecule *createMolecule();
    90 
    91   void destroyMolecule(molecule*);
    92   void destroyMolecule(moleculeId_t);
    93 
    94   /**
    95    * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores
    96    * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends.
    97    */
    98   atom *createAtom();
    99 
    100   /**
    101    * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests.
    102    * Do not re-register Atoms already known to the world since this will cause double-frees.
    103    */
    104   int registerAtom(atom*);
    105 
    106   /**
    107      * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
    108      * atom directly since this will leave the pointer inside the world.
    109    */
    110   void destroyAtom(atom*);
    111 
    112   /**
    113    * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
    114    * atom directly since this will leave the pointer inside the world.
    115    */
    116   void destroyAtom(atomId_t);
    117 
    118   /**
    119    * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
    120    * called at this time, so it can be passed around, stored inside menuItems etc.
    121    */
    122   ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
    123   ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
    124 
    125 protected:
    126   /**** Iterators to use internal data structures */
    127   class AtomIterator {
    128   public:
    129     AtomIterator();
    130     AtomIterator(AtomDescriptor, World*);
    131     AtomIterator(const AtomIterator&);
    132     AtomIterator& operator=(const AtomIterator&);
    133     AtomIterator& operator++();     // prefix
    134     AtomIterator  operator++(int);  // postfix with dummy parameter
    135     bool operator==(const AtomIterator&);
    136     bool operator==(const AtomSet::iterator&);
    137     bool operator!=(const AtomIterator&);
    138     bool operator!=(const AtomSet::iterator&);
    139     atom* operator*();
    140 
    141     int getCount();
    142   protected:
    143     void advanceState();
    144     AtomSet::iterator state;
    145     boost::shared_ptr<AtomDescriptor_impl>  descr;
    146     int index;
    147 
    148     World* world;
    149   };
    150 
    151   /**
    152    * returns an iterator over all Atoms matching a given descriptor.
    153    * used for internal purposes, like AtomProcesses and AtomCalculations.
    154    */
    155   AtomIterator getAtomIter(AtomDescriptor descr);
    156 
    157   /**
    158    * returns an iterator to the end of the AtomSet. Due to overloading this iterator
    159    * can be compared to iterators produced by getAtomIter (see the mis-matching types).
    160    * Thus it can be used to detect when such an iterator is at the end of the list.
    161    * used for internal purposes, like AtomProcesses and AtomCalculations.
    162    */
    163   AtomSet::iterator atomEnd();
    164 
    165   /******* Internal manipulation routines for double callback and Observer mechanism ******/
    166   void doManipulate(ManipulateAtomsProcess *);
    167 
    16842private:
    16943  periodentafel *periode;
    170   AtomSet atoms;
    171   atomId_t currAtomId; //!< stores the next available Id for atoms
    172   MoleculeSet molecules;
    173   moleculeId_t currMoleculeId;
     44  std::map<int,atom*> atoms;
     45  std::set<molecule*> molecules;
    17446
    17547
    17648  /***** singleton Stuff *****/
    17749public:
    178 
    179   /**
    180    * get the currently active instance of the World.
    181    */
    18250  static World* get();
    183 
    184   /**
    185    * destroy the currently active instance of the World.
    186    */
    18751  static void destroy();
    188 
    189   /**
    190    * destroy the currently active instance of the World and immidiately
    191    * create a new one. Use this to reset while somebody is still Observing
    192    * the world and should reset the observed instance. All observers will be
    193    * sent the subjectKille() message from the old world.
    194    */
    19552  static World* reset();
    19653
    19754private:
    198   /**
    199    * private constructor to ensure creation of the world using
    200    * the singleton pattern.
    201    */
    20255  World();
    203 
    204   /**
    205    * private destructor to ensure destruction of the world using the
    206    * singleton pattern.
    207    */
    20856  virtual ~World();
    20957
     
    22068  MoleculeListClass *&getMolecules();
    22169
     70  // functions used for the WorldContent template mechanism
     71  void registerAtom(atom *theAtom);
     72  void unregisterAtom(atom *theAtom);
    22273private:
     74  // this function cleans up anything that cannot be cleaned while the lock is active
     75  // at a later point all these cleanups have to be moved to the World Class so the deadlock and
     76  // race condition can both be avoided.
     77  void destroyLegacy();
     78
    22379  MoleculeListClass *molecules_deprecated;
     80
     81  // this is needed to assign unique IDs to atoms... so far
     82  // IDs are not assigned upon Atom creation, so we cannot query the ID
     83  // during construction. By using the dummy ID we can make sure all atoms
     84  // are actually stored in the map and don't overwrite each other.
     85  int dummyId;
    22486};
    22587
Note: See TracChangeset for help on using the changeset viewer.