source: molecuilder/src/World.hpp@ 5bf941

Last change on this file since 5bf941 was 5bf941, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Improved documentation of the World-class

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