source: molecuilder/src/World.hpp@ 2704e2

Last change on this file since 2704e2 was 025ca2, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Made the World Iterators conform to STL-Structure

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[2e8296]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
[5d4edf]11#include <string>
[d2d8f5]12#include <map>
[86b917]13#include <vector>
[120f8b]14#include <set>
[5d4edf]15#include <boost/thread.hpp>
[a5471c]16#include <boost/shared_ptr.hpp>
[2e8296]17
[8d9d38]18#include "defs.hpp"
[2e8296]19#include "Patterns/Observer.hpp"
20#include "Patterns/Cacheable.hpp"
21
22// forward declarations
23class periodentafel;
24class MoleculeListClass;
[42918b]25class atom;
[120f8b]26class molecule;
[86b917]27class AtomDescriptor;
[323177]28class AtomDescriptor_impl;
[14d898]29class MoleculeDescriptor;
30class MoleculeDescriptor_impl;
[5d4edf]31class ManipulateAtomsProcess;
[01d28a]32template<typename T>
33class AtomsCalculation;
[2e8296]34
35class World : public Observable
36{
[01d28a]37// necessary for coupling with descriptors
[323177]38friend class AtomDescriptor_impl;
[a5471c]39friend class AtomDescriptor;
[14d898]40friend class MoleculeDescriptor_impl;
41friend class MoleculeDescriptor;
[a5471c]42
[01d28a]43// Actions, calculations etc associated with the World
[5d4edf]44friend class ManipulateAtomsProcess;
[01d28a]45template<typename> friend class AtomsCalculation;
[2e8296]46public:
[33bc66]47 typedef std::map<atomId_t,atom*> AtomSet;
48 typedef std::map<moleculeId_t,molecule*> MoleculeSet;
[2e8296]49
50 /***** getter and setter *****/
[120f8b]51 // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
[5bf941]52 /**
53 * returns the periodentafel for the world.
54 */
[120f8b]55 periodentafel *&getPeriode();
[5bf941]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 */
[323177]61 atom* getAtom(AtomDescriptor descriptor);
[5bf941]62
63 /**
64 * returns a vector containing all atoms that match a given descriptor
65 */
[323177]66 std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
[bb89b9]67 std::vector<atom*> getAllAtoms();
[01d28a]68
[5bf941]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 */
[bb89b9]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);
[01d28a]76
[5bf941]77 /**
78 * get the number of atoms in the World
79 */
[120f8b]80 int numAtoms();
[5bf941]81
[14d898]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
[5bf941]93 /**
94 * get the number of molecules in the World
95 */
[120f8b]96 int numMolecules();
97
98 /***** Methods to work with the World *****/
[5bf941]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 */
[120f8b]104 molecule *createMolecule();
[5bf941]105
[8d9d38]106 void destroyMolecule(molecule*);
107 void destroyMolecule(moleculeId_t);
108
[5bf941]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 */
[7bfc19]113 atom *createAtom();
[5bf941]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 */
[7bfc19]119 int registerAtom(atom*);
[5bf941]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 */
[7bfc19]125 void destroyAtom(atom*);
[5bf941]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 */
[8d9d38]131 void destroyAtom(atomId_t);
[a5471c]132
[3746aeb]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
[5bf941]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 */
[5d4edf]145 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
[bb89b9]146 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
[5d4edf]147
[a5471c]148protected:
149 /**** Iterators to use internal data structures */
[14d898]150
151 // Atoms
152
[025ca2]153 class AtomIterator :
154 public std::iterator<std::iterator_traits<AtomSet::iterator>::difference_type,
155 std::iterator_traits<AtomSet::iterator>::value_type,
156 std::iterator_traits<AtomSet::iterator>::pointer,
157 std::iterator_traits<AtomSet::iterator>::reference>
158 {
[a5471c]159 public:
[025ca2]160
161 typedef AtomSet::iterator _Iter;
162 typedef _Iter::value_type value_type;
163 typedef _Iter::difference_type difference_type;
164 typedef _Iter::pointer pointer;
165 typedef _Iter::reference reference;
166 typedef _Iter::iterator_category iterator_category;
167
168
[5d4edf]169 AtomIterator();
[a5471c]170 AtomIterator(AtomDescriptor, World*);
171 AtomIterator(const AtomIterator&);
[5d4edf]172 AtomIterator& operator=(const AtomIterator&);
173 AtomIterator& operator++(); // prefix
174 AtomIterator operator++(int); // postfix with dummy parameter
[a5471c]175 bool operator==(const AtomIterator&);
[a1a532]176 bool operator==(const AtomSet::iterator&);
[a5471c]177 bool operator!=(const AtomIterator&);
[a1a532]178 bool operator!=(const AtomSet::iterator&);
[a5471c]179 atom* operator*();
[5d4edf]180
181 int getCount();
[a5471c]182 protected:
183 void advanceState();
[a1a532]184 AtomSet::iterator state;
[a5471c]185 boost::shared_ptr<AtomDescriptor_impl> descr;
[5d4edf]186 int index;
[98a2987]187
188 World* world;
[a5471c]189 };
190
[5bf941]191 /**
192 * returns an iterator over all Atoms matching a given descriptor.
193 * used for internal purposes, like AtomProcesses and AtomCalculations.
194 */
[a5471c]195 AtomIterator getAtomIter(AtomDescriptor descr);
[5bf941]196
197 /**
[a1a532]198 * returns an iterator to the end of the AtomSet. Due to overloading this iterator
[5bf941]199 * can be compared to iterators produced by getAtomIter (see the mis-matching types).
200 * Thus it can be used to detect when such an iterator is at the end of the list.
201 * used for internal purposes, like AtomProcesses and AtomCalculations.
202 */
[a1a532]203 AtomSet::iterator atomEnd();
[a5471c]204
[14d898]205 // Molecules
206
[025ca2]207 class MoleculeIterator :
208 public std::iterator<std::iterator_traits<MoleculeSet::iterator>::difference_type,
209 std::iterator_traits<MoleculeSet::iterator>::value_type,
210 std::iterator_traits<MoleculeSet::iterator>::pointer,
211 std::iterator_traits<MoleculeSet::iterator>::reference>
212 {
[14d898]213 public:
[025ca2]214
215 typedef MoleculeSet::iterator _Iter;
216 typedef _Iter::value_type value_type;
217 typedef _Iter::difference_type difference_type;
218 typedef _Iter::pointer pointer;
219 typedef _Iter::reference reference;
220 typedef _Iter::iterator_category iterator_category;
221
[14d898]222 MoleculeIterator();
223 MoleculeIterator(MoleculeDescriptor, World*);
224 MoleculeIterator(const MoleculeIterator&);
225 MoleculeIterator& operator=(const MoleculeIterator&);
226 MoleculeIterator& operator++(); // prefix
227 MoleculeIterator operator++(int); // postfix with dummy parameter
228 bool operator==(const MoleculeIterator&);
229 bool operator==(const MoleculeSet::iterator&);
230 bool operator!=(const MoleculeIterator&);
231 bool operator!=(const MoleculeSet::iterator&);
232 molecule* operator*();
233
234 int getCount();
235 protected:
236 void advanceState();
237 MoleculeSet::iterator state;
238 boost::shared_ptr<MoleculeDescriptor_impl> descr;
239 int index;
240
241 World* world;
242 };
243
244 /**
245 * returns an iterator over all Molecules matching a given descriptor.
246 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
247 */
248 MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
249
250 /**
251 * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
252 * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
253 * Thus it can be used to detect when such an iterator is at the end of the list.
254 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
255 */
256 MoleculeSet::iterator moleculeEnd();
257
258
[9ef76a]259 /******* Internal manipulation routines for double callback and Observer mechanism ******/
260 void doManipulate(ManipulateAtomsProcess *);
261
[2e8296]262private:
[3746aeb]263
264 atomId_t getNextAtomId();
265 void releaseAtomId(atomId_t);
266 bool reserveAtomId(atomId_t);
267
[2e8296]268 periodentafel *periode;
[a1a532]269 AtomSet atoms;
[3746aeb]270 std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
[8d9d38]271 atomId_t currAtomId; //!< stores the next available Id for atoms
[a1a532]272 MoleculeSet molecules;
[8d9d38]273 moleculeId_t currMoleculeId;
[2e8296]274
275
276 /***** singleton Stuff *****/
277public:
[5bf941]278
279 /**
280 * get the currently active instance of the World.
281 */
[2e8296]282 static World* get();
[5bf941]283
284 /**
285 * destroy the currently active instance of the World.
286 */
[2e8296]287 static void destroy();
[5bf941]288
289 /**
290 * destroy the currently active instance of the World and immidiately
291 * create a new one. Use this to reset while somebody is still Observing
292 * the world and should reset the observed instance. All observers will be
293 * sent the subjectKille() message from the old world.
294 */
[2e8296]295 static World* reset();
296
297private:
[5bf941]298 /**
299 * private constructor to ensure creation of the world using
300 * the singleton pattern.
301 */
[2e8296]302 World();
[5bf941]303
304 /**
305 * private destructor to ensure destruction of the world using the
306 * singleton pattern.
307 */
[2e8296]308 virtual ~World();
309
310 static World *theWorld;
311 // this mutex only saves the singleton pattern...
312 // use other mutexes to protect internal data as well
313 // this mutex handles access to the pointer, not to the object!!!
314 static boost::mutex worldLock;
315
316 /*****
317 * some legacy stuff that is include for now but will be removed later
318 *****/
319public:
[120f8b]320 MoleculeListClass *&getMolecules();
[42918b]321
[2e8296]322private:
[120f8b]323 MoleculeListClass *molecules_deprecated;
[2e8296]324};
325
326#endif /* WORLD_HPP_ */
Note: See TracBrowser for help on using the repository browser.