source: molecuilder/src/World.hpp@ b918031

Last change on this file since b918031 was e04838, checked in by Frederik Heber <heber@…>, 15 years ago

class config added to World.

  • World now has the global configuration.
  • new function World::getConfig().
  • TextWindow::TextWindow() uses thr above to obtain the configuration.

Signed-off-by: Frederik Heber <heber@…>

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