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 |
|
---|
22 | // forward declarations
|
---|
23 | class periodentafel;
|
---|
24 | class MoleculeListClass;
|
---|
25 | class atom;
|
---|
26 | class molecule;
|
---|
27 | class AtomDescriptor;
|
---|
28 | class AtomDescriptor_impl;
|
---|
29 | class MoleculeDescriptor;
|
---|
30 | class MoleculeDescriptor_impl;
|
---|
31 | class ManipulateAtomsProcess;
|
---|
32 | template<typename T>
|
---|
33 | class AtomsCalculation;
|
---|
34 |
|
---|
35 | class World : public Observable
|
---|
36 | {
|
---|
37 | // necessary for coupling with descriptors
|
---|
38 | friend class AtomDescriptor_impl;
|
---|
39 | friend class AtomDescriptor;
|
---|
40 | friend class MoleculeDescriptor_impl;
|
---|
41 | friend class MoleculeDescriptor;
|
---|
42 |
|
---|
43 | // Actions, calculations etc associated with the World
|
---|
44 | friend class ManipulateAtomsProcess;
|
---|
45 | template<typename> friend class AtomsCalculation;
|
---|
46 | public:
|
---|
47 | typedef std::map<atomId_t,atom*> AtomSet;
|
---|
48 | typedef std::map<moleculeId_t,molecule*> MoleculeSet;
|
---|
49 |
|
---|
50 | /***** getter and setter *****/
|
---|
51 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
---|
52 | /**
|
---|
53 | * returns the periodentafel for the world.
|
---|
54 | */
|
---|
55 | periodentafel *&getPeriode();
|
---|
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 | */
|
---|
61 | atom* getAtom(AtomDescriptor descriptor);
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * returns a vector containing all atoms that match a given descriptor
|
---|
65 | */
|
---|
66 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
---|
67 | std::vector<atom*> getAllAtoms();
|
---|
68 |
|
---|
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 | */
|
---|
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);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * get the number of atoms in the World
|
---|
79 | */
|
---|
80 | int numAtoms();
|
---|
81 |
|
---|
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 |
|
---|
93 | /**
|
---|
94 | * get the number of molecules in the World
|
---|
95 | */
|
---|
96 | int numMolecules();
|
---|
97 |
|
---|
98 | /***** Methods to work with the World *****/
|
---|
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 | */
|
---|
104 | molecule *createMolecule();
|
---|
105 |
|
---|
106 | void destroyMolecule(molecule*);
|
---|
107 | void destroyMolecule(moleculeId_t);
|
---|
108 |
|
---|
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 | */
|
---|
113 | atom *createAtom();
|
---|
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 | */
|
---|
119 | int registerAtom(atom*);
|
---|
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 | */
|
---|
125 | void destroyAtom(atom*);
|
---|
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 | */
|
---|
131 | void destroyAtom(atomId_t);
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
|
---|
135 | * called at this time, so it can be passed around, stored inside menuItems etc.
|
---|
136 | */
|
---|
137 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
138 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
|
---|
139 |
|
---|
140 | protected:
|
---|
141 | /**** Iterators to use internal data structures */
|
---|
142 |
|
---|
143 | // Atoms
|
---|
144 |
|
---|
145 | class AtomIterator {
|
---|
146 | public:
|
---|
147 | AtomIterator();
|
---|
148 | AtomIterator(AtomDescriptor, World*);
|
---|
149 | AtomIterator(const AtomIterator&);
|
---|
150 | AtomIterator& operator=(const AtomIterator&);
|
---|
151 | AtomIterator& operator++(); // prefix
|
---|
152 | AtomIterator operator++(int); // postfix with dummy parameter
|
---|
153 | bool operator==(const AtomIterator&);
|
---|
154 | bool operator==(const AtomSet::iterator&);
|
---|
155 | bool operator!=(const AtomIterator&);
|
---|
156 | bool operator!=(const AtomSet::iterator&);
|
---|
157 | atom* operator*();
|
---|
158 |
|
---|
159 | int getCount();
|
---|
160 | protected:
|
---|
161 | void advanceState();
|
---|
162 | AtomSet::iterator state;
|
---|
163 | boost::shared_ptr<AtomDescriptor_impl> descr;
|
---|
164 | int index;
|
---|
165 |
|
---|
166 | World* world;
|
---|
167 | };
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * returns an iterator over all Atoms matching a given descriptor.
|
---|
171 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
172 | */
|
---|
173 | AtomIterator getAtomIter(AtomDescriptor descr);
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator
|
---|
177 | * can be compared to iterators produced by getAtomIter (see the mis-matching types).
|
---|
178 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
179 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
180 | */
|
---|
181 | AtomSet::iterator atomEnd();
|
---|
182 |
|
---|
183 | // Molecules
|
---|
184 |
|
---|
185 | class MoleculeIterator {
|
---|
186 | public:
|
---|
187 | MoleculeIterator();
|
---|
188 | MoleculeIterator(MoleculeDescriptor, World*);
|
---|
189 | MoleculeIterator(const MoleculeIterator&);
|
---|
190 | MoleculeIterator& operator=(const MoleculeIterator&);
|
---|
191 | MoleculeIterator& operator++(); // prefix
|
---|
192 | MoleculeIterator operator++(int); // postfix with dummy parameter
|
---|
193 | bool operator==(const MoleculeIterator&);
|
---|
194 | bool operator==(const MoleculeSet::iterator&);
|
---|
195 | bool operator!=(const MoleculeIterator&);
|
---|
196 | bool operator!=(const MoleculeSet::iterator&);
|
---|
197 | molecule* operator*();
|
---|
198 |
|
---|
199 | int getCount();
|
---|
200 | protected:
|
---|
201 | void advanceState();
|
---|
202 | MoleculeSet::iterator state;
|
---|
203 | boost::shared_ptr<MoleculeDescriptor_impl> descr;
|
---|
204 | int index;
|
---|
205 |
|
---|
206 | World* world;
|
---|
207 | };
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * returns an iterator over all Molecules matching a given descriptor.
|
---|
211 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
212 | */
|
---|
213 | MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
|
---|
217 | * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
|
---|
218 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
219 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
220 | */
|
---|
221 | MoleculeSet::iterator moleculeEnd();
|
---|
222 |
|
---|
223 |
|
---|
224 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
225 | void doManipulate(ManipulateAtomsProcess *);
|
---|
226 |
|
---|
227 | private:
|
---|
228 | periodentafel *periode;
|
---|
229 | AtomSet atoms;
|
---|
230 | atomId_t currAtomId; //!< stores the next available Id for atoms
|
---|
231 | MoleculeSet molecules;
|
---|
232 | moleculeId_t currMoleculeId;
|
---|
233 |
|
---|
234 |
|
---|
235 | /***** singleton Stuff *****/
|
---|
236 | public:
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * get the currently active instance of the World.
|
---|
240 | */
|
---|
241 | static World* get();
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * destroy the currently active instance of the World.
|
---|
245 | */
|
---|
246 | static void destroy();
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * destroy the currently active instance of the World and immidiately
|
---|
250 | * create a new one. Use this to reset while somebody is still Observing
|
---|
251 | * the world and should reset the observed instance. All observers will be
|
---|
252 | * sent the subjectKille() message from the old world.
|
---|
253 | */
|
---|
254 | static World* reset();
|
---|
255 |
|
---|
256 | private:
|
---|
257 | /**
|
---|
258 | * private constructor to ensure creation of the world using
|
---|
259 | * the singleton pattern.
|
---|
260 | */
|
---|
261 | World();
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * private destructor to ensure destruction of the world using the
|
---|
265 | * singleton pattern.
|
---|
266 | */
|
---|
267 | virtual ~World();
|
---|
268 |
|
---|
269 | static World *theWorld;
|
---|
270 | // this mutex only saves the singleton pattern...
|
---|
271 | // use other mutexes to protect internal data as well
|
---|
272 | // this mutex handles access to the pointer, not to the object!!!
|
---|
273 | static boost::mutex worldLock;
|
---|
274 |
|
---|
275 | /*****
|
---|
276 | * some legacy stuff that is include for now but will be removed later
|
---|
277 | *****/
|
---|
278 | public:
|
---|
279 | MoleculeListClass *&getMolecules();
|
---|
280 |
|
---|
281 | private:
|
---|
282 | MoleculeListClass *molecules_deprecated;
|
---|
283 | };
|
---|
284 |
|
---|
285 | #endif /* WORLD_HPP_ */
|
---|