source: molecuilder/src/World.hpp@ d50264

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

Added -Wall flag and fixed several small hickups

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