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