Changes in src/World.hpp [354859:d2dbac0]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/World.hpp
r354859 rd2dbac0 9 9 #define WORLD_HPP_ 10 10 11 #include < boost/thread.hpp>11 #include <string> 12 12 #include <map> 13 13 #include <vector> 14 14 #include <set> 15 #include <boost/thread.hpp> 16 #include <boost/shared_ptr.hpp> 17 15 18 16 19 #include "Patterns/Observer.hpp" … … 24 27 class AtomDescriptor; 25 28 class AtomDescriptor_impl; 29 class ManipulateAtomsProcess; 30 template<typename T> 31 class AtomsCalculation; 26 32 27 33 class World : public Observable 28 34 { 35 // necessary for coupling with descriptors 29 36 friend class AtomDescriptor_impl; 37 friend class AtomDescriptor; 38 39 // Actions, calculations etc associated with the World 40 friend class ManipulateAtomsProcess; 41 template<typename> friend class AtomsCalculation; 42 43 typedef std::map<int,atom*> AtomSet; 44 typedef std::map<int,molecule*> MoleculeSet; 30 45 public: 31 46 32 47 /***** getter and setter *****/ 33 48 // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object 49 /** 50 * returns the periodentafel for the world. 51 */ 34 52 periodentafel *&getPeriode(); 53 54 /** 55 * returns the first atom that matches a given descriptor. 56 * Do not rely on ordering for descriptors that match more than one atom. 57 */ 35 58 atom* getAtom(AtomDescriptor descriptor); 59 60 /** 61 * returns a vector containing all atoms that match a given descriptor 62 */ 36 63 std::vector<atom*> getAllAtoms(AtomDescriptor descriptor); 64 std::vector<atom*> getAllAtoms(); 65 66 /** 67 * returns a calculation that calls a given function on all atoms matching a descriptor. 68 * the calculation is not called at this point and can be used as an action, i.e. be stored in 69 * menus, be kept around for later use etc. 70 */ 71 template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor); 72 template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string); 73 74 /** 75 * get the number of atoms in the World 76 */ 37 77 int numAtoms(); 78 79 /** 80 * get the number of molecules in the World 81 */ 38 82 int numMolecules(); 39 83 40 84 /***** Methods to work with the World *****/ 85 86 /** 87 * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique 88 * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly. 89 */ 41 90 molecule *createMolecule(); 91 92 /** 93 * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores 94 * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends. 95 */ 96 atom *createAtom(); 97 98 /** 99 * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests. 100 * Do not re-register Atoms already known to the world since this will cause double-frees. 101 */ 102 int registerAtom(atom*); 103 104 /** 105 * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on 106 * atom directly since this will leave the pointer inside the world. 107 */ 108 void destroyAtom(atom*); 109 110 /** 111 * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on 112 * atom directly since this will leave the pointer inside the world. 113 */ 114 void destroyAtom(int); 115 116 /** 117 * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not 118 * called at this time, so it can be passed around, stored inside menuItems etc. 119 */ 120 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor); 121 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string); 122 123 protected: 124 /**** Iterators to use internal data structures */ 125 class AtomIterator { 126 public: 127 AtomIterator(); 128 AtomIterator(AtomDescriptor, World*); 129 AtomIterator(const AtomIterator&); 130 AtomIterator& operator=(const AtomIterator&); 131 AtomIterator& operator++(); // prefix 132 AtomIterator operator++(int); // postfix with dummy parameter 133 bool operator==(const AtomIterator&); 134 bool operator==(const AtomSet::iterator&); 135 bool operator!=(const AtomIterator&); 136 bool operator!=(const AtomSet::iterator&); 137 atom* operator*(); 138 139 int getCount(); 140 protected: 141 void advanceState(); 142 World* world; 143 AtomSet::iterator state; 144 boost::shared_ptr<AtomDescriptor_impl> descr; 145 int index; 146 }; 147 148 /** 149 * returns an iterator over all Atoms matching a given descriptor. 150 * used for internal purposes, like AtomProcesses and AtomCalculations. 151 */ 152 AtomIterator getAtomIter(AtomDescriptor descr); 153 154 /** 155 * returns an iterator to the end of the AtomSet. Due to overloading this iterator 156 * can be compared to iterators produced by getAtomIter (see the mis-matching types). 157 * Thus it can be used to detect when such an iterator is at the end of the list. 158 * used for internal purposes, like AtomProcesses and AtomCalculations. 159 */ 160 AtomSet::iterator atomEnd(); 161 162 /******* Internal manipulation routines for double callback and Observer mechanism ******/ 163 void doManipulate(ManipulateAtomsProcess *); 164 42 165 private: 43 166 periodentafel *periode; 44 std::map<int,atom*> atoms; 45 std::set<molecule*> molecules; 167 AtomSet atoms; 168 int currAtomId; //!< stores the next available Id for atoms 169 MoleculeSet molecules; 170 int currMoleculeId; 46 171 47 172 48 173 /***** singleton Stuff *****/ 49 174 public: 175 176 /** 177 * get the currently active instance of the World. 178 */ 50 179 static World* get(); 180 181 /** 182 * destroy the currently active instance of the World. 183 */ 51 184 static void destroy(); 185 186 /** 187 * destroy the currently active instance of the World and immidiately 188 * create a new one. Use this to reset while somebody is still Observing 189 * the world and should reset the observed instance. All observers will be 190 * sent the subjectKille() message from the old world. 191 */ 52 192 static World* reset(); 53 193 54 194 private: 195 /** 196 * private constructor to ensure creation of the world using 197 * the singleton pattern. 198 */ 55 199 World(); 200 201 /** 202 * private destructor to ensure destruction of the world using the 203 * singleton pattern. 204 */ 56 205 virtual ~World(); 57 206 … … 68 217 MoleculeListClass *&getMolecules(); 69 218 70 // functions used for the WorldContent template mechanism71 void registerAtom(atom *theAtom);72 void unregisterAtom(atom *theAtom);73 219 private: 74 // this function cleans up anything that cannot be cleaned while the lock is active75 // at a later point all these cleanups have to be moved to the World Class so the deadlock and76 // race condition can both be avoided.77 void destroyLegacy();78 79 220 MoleculeListClass *molecules_deprecated; 80 81 // this is needed to assign unique IDs to atoms... so far82 // IDs are not assigned upon Atom creation, so we cannot query the ID83 // during construction. By using the dummy ID we can make sure all atoms84 // are actually stored in the map and don't overwrite each other.85 int dummyId;86 221 }; 87 222
Note:
See TracChangeset
for help on using the changeset viewer.