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