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