| [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 | 
 | 
|---|
| [7c4e29] | 11 | #include <string>
 | 
|---|
| [d346b6] | 12 | #include <map>
 | 
|---|
| [fc1b24] | 13 | #include <vector>
 | 
|---|
| [354859] | 14 | #include <set>
 | 
|---|
| [7c4e29] | 15 | #include <boost/thread.hpp>
 | 
|---|
| [865a945] | 16 | #include <boost/shared_ptr.hpp>
 | 
|---|
| [5d1611] | 17 | 
 | 
|---|
| [ead4e6] | 18 | #include "types.hpp"
 | 
|---|
| [6e97e5] | 19 | #include "Descriptors/SelectiveIterator.hpp"
 | 
|---|
| [5d1611] | 20 | #include "Patterns/Observer.hpp"
 | 
|---|
 | 21 | #include "Patterns/Cacheable.hpp"
 | 
|---|
| [23b547] | 22 | #include "Patterns/Singleton.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
| [5d1611] | 24 | 
 | 
|---|
 | 25 | // forward declarations
 | 
|---|
 | 26 | class periodentafel;
 | 
|---|
 | 27 | class MoleculeListClass;
 | 
|---|
| [4d9c01] | 28 | class atom;
 | 
|---|
| [354859] | 29 | class molecule;
 | 
|---|
| [fc1b24] | 30 | class AtomDescriptor;
 | 
|---|
| [7a1ce5] | 31 | class AtomDescriptor_impl;
 | 
|---|
| [1c51c8] | 32 | class MoleculeDescriptor;
 | 
|---|
 | 33 | class MoleculeDescriptor_impl;
 | 
|---|
| [7c4e29] | 34 | class ManipulateAtomsProcess;
 | 
|---|
| [b54ac8] | 35 | template<typename T>
 | 
|---|
 | 36 | class AtomsCalculation;
 | 
|---|
| [5d1611] | 37 | 
 | 
|---|
| [23b547] | 38 | 
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | class World : public Singleton<World>, public Observable
 | 
|---|
| [5d1611] | 41 | {
 | 
|---|
| [23b547] | 42 | 
 | 
|---|
 | 43 | // Make access to constructor and destructor possible from inside the singleton
 | 
|---|
 | 44 | friend class Singleton<World>;
 | 
|---|
 | 45 | 
 | 
|---|
| [b54ac8] | 46 | // necessary for coupling with descriptors
 | 
|---|
| [7a1ce5] | 47 | friend class AtomDescriptor_impl;
 | 
|---|
| [865a945] | 48 | friend class AtomDescriptor;
 | 
|---|
| [1c51c8] | 49 | friend class MoleculeDescriptor_impl;
 | 
|---|
 | 50 | friend class MoleculeDescriptor;
 | 
|---|
| [865a945] | 51 | 
 | 
|---|
| [b54ac8] | 52 | // Actions, calculations etc associated with the World
 | 
|---|
| [7c4e29] | 53 | friend class ManipulateAtomsProcess;
 | 
|---|
| [b54ac8] | 54 | template<typename> friend class AtomsCalculation;
 | 
|---|
| [5d1611] | 55 | public:
 | 
|---|
| [23b547] | 56 | 
 | 
|---|
 | 57 |   // Types for Atom and Molecule structures
 | 
|---|
| [7042f45] | 58 |   typedef std::map<atomId_t,atom*> AtomSet;
 | 
|---|
 | 59 |   typedef std::map<moleculeId_t,molecule*> MoleculeSet;
 | 
|---|
| [5d1611] | 60 | 
 | 
|---|
 | 61 |   /***** getter and setter *****/
 | 
|---|
| [354859] | 62 |   // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
 | 
|---|
| [02ee15] | 63 |   /**
 | 
|---|
 | 64 |    * returns the periodentafel for the world.
 | 
|---|
 | 65 |    */
 | 
|---|
| [354859] | 66 |   periodentafel *&getPeriode();
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [7a1ce5] | 72 |   atom* getAtom(AtomDescriptor descriptor);
 | 
|---|
| [02ee15] | 73 | 
 | 
|---|
 | 74 |   /**
 | 
|---|
 | 75 |    * returns a vector containing all atoms that match a given descriptor
 | 
|---|
 | 76 |    */
 | 
|---|
| [7a1ce5] | 77 |   std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
 | 
|---|
| [0e2a47] | 78 |   std::vector<atom*> getAllAtoms();
 | 
|---|
| [b54ac8] | 79 | 
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [0e2a47] | 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);
 | 
|---|
| [b54ac8] | 87 | 
 | 
|---|
| [02ee15] | 88 |   /**
 | 
|---|
 | 89 |    * get the number of atoms in the World
 | 
|---|
 | 90 |    */
 | 
|---|
| [354859] | 91 |   int numAtoms();
 | 
|---|
| [02ee15] | 92 | 
 | 
|---|
| [1c51c8] | 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 | 
 | 
|---|
| [02ee15] | 104 |   /**
 | 
|---|
 | 105 |    * get the number of molecules in the World
 | 
|---|
 | 106 |    */
 | 
|---|
| [354859] | 107 |   int numMolecules();
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |   /***** Methods to work with the World *****/
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [354859] | 115 |   molecule *createMolecule();
 | 
|---|
| [02ee15] | 116 | 
 | 
|---|
| [cbc5fb] | 117 |   void destroyMolecule(molecule*);
 | 
|---|
 | 118 |   void destroyMolecule(moleculeId_t);
 | 
|---|
 | 119 | 
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [46d958] | 124 |   atom *createAtom();
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [46d958] | 130 |   int registerAtom(atom*);
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [46d958] | 136 |   void destroyAtom(atom*);
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [cbc5fb] | 142 |   void destroyAtom(atomId_t);
 | 
|---|
| [865a945] | 143 | 
 | 
|---|
| [88d586] | 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 | 
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [7c4e29] | 156 |   ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
 | 
|---|
| [0e2a47] | 157 |   ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
 | 
|---|
| [7c4e29] | 158 | 
 | 
|---|
| [865a945] | 159 | protected:
 | 
|---|
 | 160 |   /**** Iterators to use internal data structures */
 | 
|---|
| [1c51c8] | 161 | 
 | 
|---|
 | 162 |   // Atoms
 | 
|---|
| [6e97e5] | 163 |   typedef SelectiveIterator<atom*,AtomSet,AtomDescriptor> AtomIterator;
 | 
|---|
| [865a945] | 164 | 
 | 
|---|
| [02ee15] | 165 |   /**
 | 
|---|
 | 166 |    * returns an iterator over all Atoms matching a given descriptor.
 | 
|---|
 | 167 |    * used for internal purposes, like AtomProcesses and AtomCalculations.
 | 
|---|
 | 168 |    */
 | 
|---|
| [865a945] | 169 |   AtomIterator getAtomIter(AtomDescriptor descr);
 | 
|---|
| [02ee15] | 170 | 
 | 
|---|
 | 171 |   /**
 | 
|---|
| [d2dbac0] | 172 |    * returns an iterator to the end of the AtomSet. Due to overloading this iterator
 | 
|---|
| [02ee15] | 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 |    */
 | 
|---|
| [6e97e5] | 177 |   AtomIterator atomEnd();
 | 
|---|
| [865a945] | 178 | 
 | 
|---|
| [1c51c8] | 179 |   // Molecules
 | 
|---|
 | 180 | 
 | 
|---|
| [6e97e5] | 181 |   typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor> MoleculeIterator;
 | 
|---|
| [1c51c8] | 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 |    */
 | 
|---|
| [6e97e5] | 195 |   MoleculeIterator moleculeEnd();
 | 
|---|
| [1c51c8] | 196 | 
 | 
|---|
 | 197 | 
 | 
|---|
| [afb47f] | 198 |   /******* Internal manipulation routines for double callback and Observer mechanism ******/
 | 
|---|
 | 199 |   void doManipulate(ManipulateAtomsProcess *);
 | 
|---|
 | 200 | 
 | 
|---|
| [5d1611] | 201 | private:
 | 
|---|
| [88d586] | 202 | 
 | 
|---|
 | 203 |   atomId_t getNextAtomId();
 | 
|---|
 | 204 |   void releaseAtomId(atomId_t);
 | 
|---|
 | 205 |   bool reserveAtomId(atomId_t);
 | 
|---|
 | 206 | 
 | 
|---|
| [5d1611] | 207 |   periodentafel *periode;
 | 
|---|
| [6e97e5] | 208 | public:
 | 
|---|
| [d2dbac0] | 209 |   AtomSet atoms;
 | 
|---|
| [6e97e5] | 210 | private:
 | 
|---|
| [88d586] | 211 |   std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
 | 
|---|
| [cbc5fb] | 212 |   atomId_t currAtomId; //!< stores the next available Id for atoms
 | 
|---|
| [d2dbac0] | 213 |   MoleculeSet molecules;
 | 
|---|
| [cbc5fb] | 214 |   moleculeId_t currMoleculeId;
 | 
|---|
| [5d1611] | 215 | private:
 | 
|---|
| [02ee15] | 216 |   /**
 | 
|---|
 | 217 |    * private constructor to ensure creation of the world using
 | 
|---|
 | 218 |    * the singleton pattern.
 | 
|---|
 | 219 |    */
 | 
|---|
| [5d1611] | 220 |   World();
 | 
|---|
| [02ee15] | 221 | 
 | 
|---|
 | 222 |   /**
 | 
|---|
 | 223 |    * private destructor to ensure destruction of the world using the
 | 
|---|
 | 224 |    * singleton pattern.
 | 
|---|
 | 225 |    */
 | 
|---|
| [5d1611] | 226 |   virtual ~World();
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 |   /*****
 | 
|---|
 | 229 |    * some legacy stuff that is include for now but will be removed later
 | 
|---|
 | 230 |    *****/
 | 
|---|
 | 231 | public:
 | 
|---|
| [354859] | 232 |   MoleculeListClass *&getMolecules();
 | 
|---|
| [4d9c01] | 233 | 
 | 
|---|
| [5d1611] | 234 | private:
 | 
|---|
| [354859] | 235 |   MoleculeListClass *molecules_deprecated;
 | 
|---|
| [5d1611] | 236 | };
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 | #endif /* WORLD_HPP_ */
 | 
|---|