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