source: molecuilder/src/World.hpp@ 239987d

Last change on this file since 239987d was 5738177, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added a generic Iterator that can be used to iterate only over certain parts of an internal data structure

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