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