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

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

Made the periodentafel use STL-containers instead of custom llists

  • Property mode set to 100644
File size: 9.6 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
[5dba7a]18#include "types.hpp"
[2e8296]19#include "Patterns/Observer.hpp"
20#include "Patterns/Cacheable.hpp"
[4c60ef]21#include "Patterns/Singleton.hpp"
22
[2e8296]23
24// forward declarations
25class periodentafel;
26class MoleculeListClass;
[42918b]27class atom;
[120f8b]28class molecule;
[86b917]29class AtomDescriptor;
[323177]30class AtomDescriptor_impl;
[14d898]31class MoleculeDescriptor;
32class MoleculeDescriptor_impl;
[5d4edf]33class ManipulateAtomsProcess;
[01d28a]34template<typename T>
35class AtomsCalculation;
[2e8296]36
[4c60ef]37
38
39class World : public Singleton<World>, public Observable
[2e8296]40{
[4c60ef]41
42// Make access to constructor and destructor possible from inside the singleton
43friend class Singleton<World>;
44
[01d28a]45// necessary for coupling with descriptors
[323177]46friend class AtomDescriptor_impl;
[a5471c]47friend class AtomDescriptor;
[14d898]48friend class MoleculeDescriptor_impl;
49friend class MoleculeDescriptor;
[a5471c]50
[01d28a]51// Actions, calculations etc associated with the World
[5d4edf]52friend class ManipulateAtomsProcess;
[01d28a]53template<typename> friend class AtomsCalculation;
[2e8296]54public:
[4c60ef]55
56 // Types for Atom and Molecule structures
[33bc66]57 typedef std::map<atomId_t,atom*> AtomSet;
58 typedef std::map<moleculeId_t,molecule*> MoleculeSet;
[2e8296]59
60 /***** getter and setter *****/
[120f8b]61 // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
[5bf941]62 /**
63 * returns the periodentafel for the world.
64 */
[120f8b]65 periodentafel *&getPeriode();
[5bf941]66
67 /**
68 * returns the first atom that matches a given descriptor.
69 * Do not rely on ordering for descriptors that match more than one atom.
70 */
[323177]71 atom* getAtom(AtomDescriptor descriptor);
[5bf941]72
73 /**
74 * returns a vector containing all atoms that match a given descriptor
75 */
[323177]76 std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
[bb89b9]77 std::vector<atom*> getAllAtoms();
[01d28a]78
[5bf941]79 /**
80 * returns a calculation that calls a given function on all atoms matching a descriptor.
81 * the calculation is not called at this point and can be used as an action, i.e. be stored in
82 * menus, be kept around for later use etc.
83 */
[bb89b9]84 template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor);
85 template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string);
[01d28a]86
[5bf941]87 /**
88 * get the number of atoms in the World
89 */
[120f8b]90 int numAtoms();
[5bf941]91
[14d898]92 /**
93 * returns the first molecule that matches a given descriptor.
94 * Do not rely on ordering for descriptors that match more than one molecule.
95 */
96 molecule *getMolecule(MoleculeDescriptor descriptor);
97
98 /**
99 * returns a vector containing all molecules that match a given descriptor
100 */
101 std::vector<molecule*> getAllMolecules(MoleculeDescriptor descriptor);
102
[5bf941]103 /**
104 * get the number of molecules in the World
105 */
[120f8b]106 int numMolecules();
107
108 /***** Methods to work with the World *****/
[5bf941]109
110 /**
111 * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique
112 * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly.
113 */
[120f8b]114 molecule *createMolecule();
[5bf941]115
[8d9d38]116 void destroyMolecule(molecule*);
117 void destroyMolecule(moleculeId_t);
118
[5bf941]119 /**
120 * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores
121 * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends.
122 */
[7bfc19]123 atom *createAtom();
[5bf941]124
125 /**
126 * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests.
127 * Do not re-register Atoms already known to the world since this will cause double-frees.
128 */
[7bfc19]129 int registerAtom(atom*);
[5bf941]130
131 /**
132 * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
133 * atom directly since this will leave the pointer inside the world.
134 */
[7bfc19]135 void destroyAtom(atom*);
[5bf941]136
137 /**
138 * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
139 * atom directly since this will leave the pointer inside the world.
140 */
[8d9d38]141 void destroyAtom(atomId_t);
[a5471c]142
[3746aeb]143 /**
144 * used when changing an atom Id.
145 * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
146 *
147 * Return value indicates wether the change could be done or not.
148 */
149 bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0);
150
[5bf941]151 /**
152 * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
153 * called at this time, so it can be passed around, stored inside menuItems etc.
154 */
[5d4edf]155 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
[bb89b9]156 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
[5d4edf]157
[a5471c]158protected:
159 /**** Iterators to use internal data structures */
[14d898]160
161 // Atoms
162
[025ca2]163 class AtomIterator :
164 public std::iterator<std::iterator_traits<AtomSet::iterator>::difference_type,
165 std::iterator_traits<AtomSet::iterator>::value_type,
166 std::iterator_traits<AtomSet::iterator>::pointer,
167 std::iterator_traits<AtomSet::iterator>::reference>
168 {
[a5471c]169 public:
[025ca2]170
171 typedef AtomSet::iterator _Iter;
172 typedef _Iter::value_type value_type;
173 typedef _Iter::difference_type difference_type;
174 typedef _Iter::pointer pointer;
175 typedef _Iter::reference reference;
176 typedef _Iter::iterator_category iterator_category;
177
178
[5d4edf]179 AtomIterator();
[a5471c]180 AtomIterator(AtomDescriptor, World*);
181 AtomIterator(const AtomIterator&);
[5d4edf]182 AtomIterator& operator=(const AtomIterator&);
183 AtomIterator& operator++(); // prefix
184 AtomIterator operator++(int); // postfix with dummy parameter
[a5471c]185 bool operator==(const AtomIterator&);
[a1a532]186 bool operator==(const AtomSet::iterator&);
[a5471c]187 bool operator!=(const AtomIterator&);
[a1a532]188 bool operator!=(const AtomSet::iterator&);
[a5471c]189 atom* operator*();
[5d4edf]190
191 int getCount();
[a5471c]192 protected:
193 void advanceState();
[a1a532]194 AtomSet::iterator state;
[a5471c]195 boost::shared_ptr<AtomDescriptor_impl> descr;
[5d4edf]196 int index;
[98a2987]197
198 World* world;
[a5471c]199 };
200
[5bf941]201 /**
202 * returns an iterator over all Atoms matching a given descriptor.
203 * used for internal purposes, like AtomProcesses and AtomCalculations.
204 */
[a5471c]205 AtomIterator getAtomIter(AtomDescriptor descr);
[5bf941]206
207 /**
[a1a532]208 * returns an iterator to the end of the AtomSet. Due to overloading this iterator
[5bf941]209 * can be compared to iterators produced by getAtomIter (see the mis-matching types).
210 * Thus it can be used to detect when such an iterator is at the end of the list.
211 * used for internal purposes, like AtomProcesses and AtomCalculations.
212 */
[a1a532]213 AtomSet::iterator atomEnd();
[a5471c]214
[14d898]215 // Molecules
216
[025ca2]217 class MoleculeIterator :
218 public std::iterator<std::iterator_traits<MoleculeSet::iterator>::difference_type,
219 std::iterator_traits<MoleculeSet::iterator>::value_type,
220 std::iterator_traits<MoleculeSet::iterator>::pointer,
221 std::iterator_traits<MoleculeSet::iterator>::reference>
222 {
[14d898]223 public:
[025ca2]224
225 typedef MoleculeSet::iterator _Iter;
226 typedef _Iter::value_type value_type;
227 typedef _Iter::difference_type difference_type;
228 typedef _Iter::pointer pointer;
229 typedef _Iter::reference reference;
230 typedef _Iter::iterator_category iterator_category;
231
[14d898]232 MoleculeIterator();
233 MoleculeIterator(MoleculeDescriptor, World*);
234 MoleculeIterator(const MoleculeIterator&);
235 MoleculeIterator& operator=(const MoleculeIterator&);
236 MoleculeIterator& operator++(); // prefix
237 MoleculeIterator operator++(int); // postfix with dummy parameter
238 bool operator==(const MoleculeIterator&);
239 bool operator==(const MoleculeSet::iterator&);
240 bool operator!=(const MoleculeIterator&);
241 bool operator!=(const MoleculeSet::iterator&);
242 molecule* operator*();
243
244 int getCount();
245 protected:
246 void advanceState();
247 MoleculeSet::iterator state;
248 boost::shared_ptr<MoleculeDescriptor_impl> descr;
249 int index;
250
251 World* world;
252 };
253
254 /**
255 * returns an iterator over all Molecules matching a given descriptor.
256 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
257 */
258 MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
259
260 /**
261 * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
262 * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
263 * Thus it can be used to detect when such an iterator is at the end of the list.
264 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
265 */
266 MoleculeSet::iterator moleculeEnd();
267
268
[9ef76a]269 /******* Internal manipulation routines for double callback and Observer mechanism ******/
270 void doManipulate(ManipulateAtomsProcess *);
271
[2e8296]272private:
[3746aeb]273
274 atomId_t getNextAtomId();
275 void releaseAtomId(atomId_t);
276 bool reserveAtomId(atomId_t);
277
[2e8296]278 periodentafel *periode;
[a1a532]279 AtomSet atoms;
[3746aeb]280 std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
[8d9d38]281 atomId_t currAtomId; //!< stores the next available Id for atoms
[a1a532]282 MoleculeSet molecules;
[8d9d38]283 moleculeId_t currMoleculeId;
[2e8296]284private:
[5bf941]285 /**
286 * private constructor to ensure creation of the world using
287 * the singleton pattern.
288 */
[2e8296]289 World();
[5bf941]290
291 /**
292 * private destructor to ensure destruction of the world using the
293 * singleton pattern.
294 */
[2e8296]295 virtual ~World();
296
297 /*****
298 * some legacy stuff that is include for now but will be removed later
299 *****/
300public:
[120f8b]301 MoleculeListClass *&getMolecules();
[42918b]302
[2e8296]303private:
[120f8b]304 MoleculeListClass *molecules_deprecated;
[2e8296]305};
306
307#endif /* WORLD_HPP_ */
Note: See TracBrowser for help on using the repository browser.