| [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 |  | 
|---|
| [cbc5fb] | 18 | #include "defs.hpp" | 
|---|
| [5d1611] | 19 | #include "Patterns/Observer.hpp" | 
|---|
|  | 20 | #include "Patterns/Cacheable.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | // forward declarations | 
|---|
|  | 23 | class periodentafel; | 
|---|
|  | 24 | class MoleculeListClass; | 
|---|
| [4d9c01] | 25 | class atom; | 
|---|
| [354859] | 26 | class molecule; | 
|---|
| [fc1b24] | 27 | class AtomDescriptor; | 
|---|
| [7a1ce5] | 28 | class AtomDescriptor_impl; | 
|---|
| [7c4e29] | 29 | class ManipulateAtomsProcess; | 
|---|
| [b54ac8] | 30 | template<typename T> | 
|---|
|  | 31 | class AtomsCalculation; | 
|---|
| [5d1611] | 32 |  | 
|---|
|  | 33 | class World : public Observable | 
|---|
|  | 34 | { | 
|---|
| [b54ac8] | 35 | // necessary for coupling with descriptors | 
|---|
| [7a1ce5] | 36 | friend class AtomDescriptor_impl; | 
|---|
| [865a945] | 37 | friend class AtomDescriptor; | 
|---|
|  | 38 |  | 
|---|
| [b54ac8] | 39 | // Actions, calculations etc associated with the World | 
|---|
| [7c4e29] | 40 | friend class ManipulateAtomsProcess; | 
|---|
| [b54ac8] | 41 | template<typename> friend class AtomsCalculation; | 
|---|
| [5d1611] | 42 | public: | 
|---|
| [7042f45] | 43 | typedef std::map<atomId_t,atom*> AtomSet; | 
|---|
|  | 44 | typedef std::map<moleculeId_t,molecule*> MoleculeSet; | 
|---|
| [5d1611] | 45 |  | 
|---|
|  | 46 | /***** getter and setter *****/ | 
|---|
| [354859] | 47 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object | 
|---|
| [02ee15] | 48 | /** | 
|---|
|  | 49 | * returns the periodentafel for the world. | 
|---|
|  | 50 | */ | 
|---|
| [354859] | 51 | periodentafel *&getPeriode(); | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [7a1ce5] | 57 | atom* getAtom(AtomDescriptor descriptor); | 
|---|
| [02ee15] | 58 |  | 
|---|
|  | 59 | /** | 
|---|
|  | 60 | * returns a vector containing all atoms that match a given descriptor | 
|---|
|  | 61 | */ | 
|---|
| [7a1ce5] | 62 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor); | 
|---|
| [0e2a47] | 63 | std::vector<atom*> getAllAtoms(); | 
|---|
| [b54ac8] | 64 |  | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [0e2a47] | 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); | 
|---|
| [b54ac8] | 72 |  | 
|---|
| [02ee15] | 73 | /** | 
|---|
|  | 74 | * get the number of atoms in the World | 
|---|
|  | 75 | */ | 
|---|
| [354859] | 76 | int numAtoms(); | 
|---|
| [02ee15] | 77 |  | 
|---|
|  | 78 | /** | 
|---|
|  | 79 | * get the number of molecules in the World | 
|---|
|  | 80 | */ | 
|---|
| [354859] | 81 | int numMolecules(); | 
|---|
|  | 82 |  | 
|---|
|  | 83 | /***** Methods to work with the World *****/ | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [354859] | 89 | molecule *createMolecule(); | 
|---|
| [02ee15] | 90 |  | 
|---|
| [cbc5fb] | 91 | void destroyMolecule(molecule*); | 
|---|
|  | 92 | void destroyMolecule(moleculeId_t); | 
|---|
|  | 93 |  | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [46d958] | 98 | atom *createAtom(); | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [46d958] | 104 | int registerAtom(atom*); | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [46d958] | 110 | void destroyAtom(atom*); | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [cbc5fb] | 116 | void destroyAtom(atomId_t); | 
|---|
| [865a945] | 117 |  | 
|---|
| [02ee15] | 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 | */ | 
|---|
| [7c4e29] | 122 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor); | 
|---|
| [0e2a47] | 123 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string); | 
|---|
| [7c4e29] | 124 |  | 
|---|
| [865a945] | 125 | protected: | 
|---|
|  | 126 | /**** Iterators to use internal data structures */ | 
|---|
|  | 127 | class AtomIterator { | 
|---|
|  | 128 | public: | 
|---|
| [7c4e29] | 129 | AtomIterator(); | 
|---|
| [865a945] | 130 | AtomIterator(AtomDescriptor, World*); | 
|---|
|  | 131 | AtomIterator(const AtomIterator&); | 
|---|
| [7c4e29] | 132 | AtomIterator& operator=(const AtomIterator&); | 
|---|
|  | 133 | AtomIterator& operator++();     // prefix | 
|---|
|  | 134 | AtomIterator  operator++(int);  // postfix with dummy parameter | 
|---|
| [865a945] | 135 | bool operator==(const AtomIterator&); | 
|---|
| [d2dbac0] | 136 | bool operator==(const AtomSet::iterator&); | 
|---|
| [865a945] | 137 | bool operator!=(const AtomIterator&); | 
|---|
| [d2dbac0] | 138 | bool operator!=(const AtomSet::iterator&); | 
|---|
| [865a945] | 139 | atom* operator*(); | 
|---|
| [7c4e29] | 140 |  | 
|---|
|  | 141 | int getCount(); | 
|---|
| [865a945] | 142 | protected: | 
|---|
|  | 143 | void advanceState(); | 
|---|
|  | 144 | World* world; | 
|---|
| [d2dbac0] | 145 | AtomSet::iterator state; | 
|---|
| [865a945] | 146 | boost::shared_ptr<AtomDescriptor_impl>  descr; | 
|---|
| [7c4e29] | 147 | int index; | 
|---|
| [865a945] | 148 | }; | 
|---|
|  | 149 |  | 
|---|
| [02ee15] | 150 | /** | 
|---|
|  | 151 | * returns an iterator over all Atoms matching a given descriptor. | 
|---|
|  | 152 | * used for internal purposes, like AtomProcesses and AtomCalculations. | 
|---|
|  | 153 | */ | 
|---|
| [865a945] | 154 | AtomIterator getAtomIter(AtomDescriptor descr); | 
|---|
| [02ee15] | 155 |  | 
|---|
|  | 156 | /** | 
|---|
| [d2dbac0] | 157 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator | 
|---|
| [02ee15] | 158 | * can be compared to iterators produced by getAtomIter (see the mis-matching types). | 
|---|
|  | 159 | * Thus it can be used to detect when such an iterator is at the end of the list. | 
|---|
|  | 160 | * used for internal purposes, like AtomProcesses and AtomCalculations. | 
|---|
|  | 161 | */ | 
|---|
| [d2dbac0] | 162 | AtomSet::iterator atomEnd(); | 
|---|
| [865a945] | 163 |  | 
|---|
| [afb47f] | 164 | /******* Internal manipulation routines for double callback and Observer mechanism ******/ | 
|---|
|  | 165 | void doManipulate(ManipulateAtomsProcess *); | 
|---|
|  | 166 |  | 
|---|
| [5d1611] | 167 | private: | 
|---|
|  | 168 | periodentafel *periode; | 
|---|
| [d2dbac0] | 169 | AtomSet atoms; | 
|---|
| [cbc5fb] | 170 | atomId_t currAtomId; //!< stores the next available Id for atoms | 
|---|
| [d2dbac0] | 171 | MoleculeSet molecules; | 
|---|
| [cbc5fb] | 172 | moleculeId_t currMoleculeId; | 
|---|
| [5d1611] | 173 |  | 
|---|
|  | 174 |  | 
|---|
|  | 175 | /***** singleton Stuff *****/ | 
|---|
|  | 176 | public: | 
|---|
| [02ee15] | 177 |  | 
|---|
|  | 178 | /** | 
|---|
|  | 179 | * get the currently active instance of the World. | 
|---|
|  | 180 | */ | 
|---|
| [5d1611] | 181 | static World* get(); | 
|---|
| [02ee15] | 182 |  | 
|---|
|  | 183 | /** | 
|---|
|  | 184 | * destroy the currently active instance of the World. | 
|---|
|  | 185 | */ | 
|---|
| [5d1611] | 186 | static void destroy(); | 
|---|
| [02ee15] | 187 |  | 
|---|
|  | 188 | /** | 
|---|
|  | 189 | * destroy the currently active instance of the World and immidiately | 
|---|
|  | 190 | * create a new one. Use this to reset while somebody is still Observing | 
|---|
|  | 191 | * the world and should reset the observed instance. All observers will be | 
|---|
|  | 192 | * sent the subjectKille() message from the old world. | 
|---|
|  | 193 | */ | 
|---|
| [5d1611] | 194 | static World* reset(); | 
|---|
|  | 195 |  | 
|---|
|  | 196 | private: | 
|---|
| [02ee15] | 197 | /** | 
|---|
|  | 198 | * private constructor to ensure creation of the world using | 
|---|
|  | 199 | * the singleton pattern. | 
|---|
|  | 200 | */ | 
|---|
| [5d1611] | 201 | World(); | 
|---|
| [02ee15] | 202 |  | 
|---|
|  | 203 | /** | 
|---|
|  | 204 | * private destructor to ensure destruction of the world using the | 
|---|
|  | 205 | * singleton pattern. | 
|---|
|  | 206 | */ | 
|---|
| [5d1611] | 207 | virtual ~World(); | 
|---|
|  | 208 |  | 
|---|
|  | 209 | static World *theWorld; | 
|---|
|  | 210 | // this mutex only saves the singleton pattern... | 
|---|
|  | 211 | // use other mutexes to protect internal data as well | 
|---|
|  | 212 | // this mutex handles access to the pointer, not to the object!!! | 
|---|
|  | 213 | static boost::mutex worldLock; | 
|---|
|  | 214 |  | 
|---|
|  | 215 | /***** | 
|---|
|  | 216 | * some legacy stuff that is include for now but will be removed later | 
|---|
|  | 217 | *****/ | 
|---|
|  | 218 | public: | 
|---|
| [354859] | 219 | MoleculeListClass *&getMolecules(); | 
|---|
| [4d9c01] | 220 |  | 
|---|
| [5d1611] | 221 | private: | 
|---|
| [354859] | 222 | MoleculeListClass *molecules_deprecated; | 
|---|
| [5d1611] | 223 | }; | 
|---|
|  | 224 |  | 
|---|
|  | 225 | #endif /* WORLD_HPP_ */ | 
|---|