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 |
|
---|
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 ManipulateAtomsProcess;
|
---|
30 | template<typename T>
|
---|
31 | class AtomsCalculation;
|
---|
32 |
|
---|
33 | class World : public Observable
|
---|
34 | {
|
---|
35 | // necessary for coupling with descriptors
|
---|
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*> AtomList;
|
---|
44 | public:
|
---|
45 |
|
---|
46 | /***** getter and setter *****/
|
---|
47 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
---|
48 | /**
|
---|
49 | * returns the periodentafel for the world.
|
---|
50 | */
|
---|
51 | periodentafel *&getPeriode();
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * returns the first atom that matches a given descriptor.
|
---|
55 | * Do not rely on ordering for descriptors that match more than one atom.
|
---|
56 | */
|
---|
57 | atom* getAtom(AtomDescriptor descriptor);
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * returns a vector containing all atoms that match a given descriptor
|
---|
61 | */
|
---|
62 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
---|
63 | std::vector<atom*> getAllAtoms();
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * returns a calculation that calls a given function on all atoms matching a descriptor.
|
---|
67 | * the calculation is not called at this point and can be used as an action, i.e. be stored in
|
---|
68 | * menus, be kept around for later use etc.
|
---|
69 | */
|
---|
70 | template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor);
|
---|
71 | template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string);
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * get the number of atoms in the World
|
---|
75 | */
|
---|
76 | int numAtoms();
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * get the number of molecules in the World
|
---|
80 | */
|
---|
81 | int numMolecules();
|
---|
82 |
|
---|
83 | /***** Methods to work with the World *****/
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique
|
---|
87 | * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly.
|
---|
88 | */
|
---|
89 | molecule *createMolecule();
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores
|
---|
93 | * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends.
|
---|
94 | */
|
---|
95 | atom *createAtom();
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests.
|
---|
99 | * Do not re-register Atoms already known to the world since this will cause double-frees.
|
---|
100 | */
|
---|
101 | int registerAtom(atom*);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
|
---|
105 | * atom directly since this will leave the pointer inside the world.
|
---|
106 | */
|
---|
107 | void destroyAtom(atom*);
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
|
---|
111 | * atom directly since this will leave the pointer inside the world.
|
---|
112 | */
|
---|
113 | void destroyAtom(int);
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
|
---|
117 | * called at this time, so it can be passed around, stored inside menuItems etc.
|
---|
118 | */
|
---|
119 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
120 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
|
---|
121 |
|
---|
122 | protected:
|
---|
123 | /**** Iterators to use internal data structures */
|
---|
124 | class AtomIterator {
|
---|
125 | public:
|
---|
126 | AtomIterator();
|
---|
127 | AtomIterator(AtomDescriptor, World*);
|
---|
128 | AtomIterator(const AtomIterator&);
|
---|
129 | AtomIterator& operator=(const AtomIterator&);
|
---|
130 | AtomIterator& operator++(); // prefix
|
---|
131 | AtomIterator operator++(int); // postfix with dummy parameter
|
---|
132 | bool operator==(const AtomIterator&);
|
---|
133 | bool operator==(const AtomList::iterator&);
|
---|
134 | bool operator!=(const AtomIterator&);
|
---|
135 | bool operator!=(const AtomList::iterator&);
|
---|
136 | atom* operator*();
|
---|
137 |
|
---|
138 | int getCount();
|
---|
139 | protected:
|
---|
140 | void advanceState();
|
---|
141 | World* world;
|
---|
142 | AtomList::iterator state;
|
---|
143 | boost::shared_ptr<AtomDescriptor_impl> descr;
|
---|
144 | int index;
|
---|
145 | };
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * returns an iterator over all Atoms matching a given descriptor.
|
---|
149 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
150 | */
|
---|
151 | AtomIterator getAtomIter(AtomDescriptor descr);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * returns an iterator to the end of the AtomList. Due to overloading this iterator
|
---|
155 | * can be compared to iterators produced by getAtomIter (see the mis-matching types).
|
---|
156 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
157 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
158 | */
|
---|
159 | AtomList::iterator atomEnd();
|
---|
160 |
|
---|
161 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
162 | void doManipulate(ManipulateAtomsProcess *);
|
---|
163 |
|
---|
164 | private:
|
---|
165 | periodentafel *periode;
|
---|
166 | AtomList atoms;
|
---|
167 | int currAtomId; //!< stores the next available Id for atoms
|
---|
168 | std::set<molecule*> molecules;
|
---|
169 |
|
---|
170 |
|
---|
171 | /***** singleton Stuff *****/
|
---|
172 | public:
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * get the currently active instance of the World.
|
---|
176 | */
|
---|
177 | static World* get();
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * destroy the currently active instance of the World.
|
---|
181 | */
|
---|
182 | static void destroy();
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * destroy the currently active instance of the World and immidiately
|
---|
186 | * create a new one. Use this to reset while somebody is still Observing
|
---|
187 | * the world and should reset the observed instance. All observers will be
|
---|
188 | * sent the subjectKille() message from the old world.
|
---|
189 | */
|
---|
190 | static World* reset();
|
---|
191 |
|
---|
192 | private:
|
---|
193 | /**
|
---|
194 | * private constructor to ensure creation of the world using
|
---|
195 | * the singleton pattern.
|
---|
196 | */
|
---|
197 | World();
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * private destructor to ensure destruction of the world using the
|
---|
201 | * singleton pattern.
|
---|
202 | */
|
---|
203 | virtual ~World();
|
---|
204 |
|
---|
205 | static World *theWorld;
|
---|
206 | // this mutex only saves the singleton pattern...
|
---|
207 | // use other mutexes to protect internal data as well
|
---|
208 | // this mutex handles access to the pointer, not to the object!!!
|
---|
209 | static boost::mutex worldLock;
|
---|
210 |
|
---|
211 | /*****
|
---|
212 | * some legacy stuff that is include for now but will be removed later
|
---|
213 | *****/
|
---|
214 | public:
|
---|
215 | MoleculeListClass *&getMolecules();
|
---|
216 |
|
---|
217 | private:
|
---|
218 | MoleculeListClass *molecules_deprecated;
|
---|
219 | };
|
---|
220 |
|
---|
221 | #endif /* WORLD_HPP_ */
|
---|