[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 |
|
---|
[cbc5fb] | 18 | #include "defs.hpp"
|
---|
[5d1611] | 19 | #include "Patterns/Observer.hpp"
|
---|
| 20 | #include "Patterns/Cacheable.hpp"
|
---|
[23b547] | 21 | #include "Patterns/Singleton.hpp"
|
---|
| 22 |
|
---|
[5d1611] | 23 |
|
---|
| 24 | // forward declarations
|
---|
| 25 | class periodentafel;
|
---|
| 26 | class MoleculeListClass;
|
---|
[4d9c01] | 27 | class atom;
|
---|
[354859] | 28 | class molecule;
|
---|
[fc1b24] | 29 | class AtomDescriptor;
|
---|
[7a1ce5] | 30 | class AtomDescriptor_impl;
|
---|
[1c51c8] | 31 | class MoleculeDescriptor;
|
---|
| 32 | class MoleculeDescriptor_impl;
|
---|
[7c4e29] | 33 | class ManipulateAtomsProcess;
|
---|
[b54ac8] | 34 | template<typename T>
|
---|
| 35 | class AtomsCalculation;
|
---|
[5d1611] | 36 |
|
---|
[23b547] | 37 |
|
---|
| 38 |
|
---|
| 39 | class World : public Singleton<World>, public Observable
|
---|
[5d1611] | 40 | {
|
---|
[23b547] | 41 |
|
---|
| 42 | // Make access to constructor and destructor possible from inside the singleton
|
---|
| 43 | friend class Singleton<World>;
|
---|
| 44 |
|
---|
[b54ac8] | 45 | // necessary for coupling with descriptors
|
---|
[7a1ce5] | 46 | friend class AtomDescriptor_impl;
|
---|
[865a945] | 47 | friend class AtomDescriptor;
|
---|
[1c51c8] | 48 | friend class MoleculeDescriptor_impl;
|
---|
| 49 | friend class MoleculeDescriptor;
|
---|
[865a945] | 50 |
|
---|
[b54ac8] | 51 | // Actions, calculations etc associated with the World
|
---|
[7c4e29] | 52 | friend class ManipulateAtomsProcess;
|
---|
[b54ac8] | 53 | template<typename> friend class AtomsCalculation;
|
---|
[5d1611] | 54 | public:
|
---|
[23b547] | 55 |
|
---|
| 56 | // Types for Atom and Molecule structures
|
---|
[7042f45] | 57 | typedef std::map<atomId_t,atom*> AtomSet;
|
---|
| 58 | typedef std::map<moleculeId_t,molecule*> MoleculeSet;
|
---|
[5d1611] | 59 |
|
---|
| 60 | /***** getter and setter *****/
|
---|
[354859] | 61 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
---|
[02ee15] | 62 | /**
|
---|
| 63 | * returns the periodentafel for the world.
|
---|
| 64 | */
|
---|
[354859] | 65 | periodentafel *&getPeriode();
|
---|
[02ee15] | 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 | */
|
---|
[7a1ce5] | 71 | atom* getAtom(AtomDescriptor descriptor);
|
---|
[02ee15] | 72 |
|
---|
| 73 | /**
|
---|
| 74 | * returns a vector containing all atoms that match a given descriptor
|
---|
| 75 | */
|
---|
[7a1ce5] | 76 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
---|
[0e2a47] | 77 | std::vector<atom*> getAllAtoms();
|
---|
[b54ac8] | 78 |
|
---|
[02ee15] | 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 | */
|
---|
[0e2a47] | 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);
|
---|
[b54ac8] | 86 |
|
---|
[02ee15] | 87 | /**
|
---|
| 88 | * get the number of atoms in the World
|
---|
| 89 | */
|
---|
[354859] | 90 | int numAtoms();
|
---|
[02ee15] | 91 |
|
---|
[1c51c8] | 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 |
|
---|
[02ee15] | 103 | /**
|
---|
| 104 | * get the number of molecules in the World
|
---|
| 105 | */
|
---|
[354859] | 106 | int numMolecules();
|
---|
| 107 |
|
---|
| 108 | /***** Methods to work with the World *****/
|
---|
[02ee15] | 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 | */
|
---|
[354859] | 114 | molecule *createMolecule();
|
---|
[02ee15] | 115 |
|
---|
[cbc5fb] | 116 | void destroyMolecule(molecule*);
|
---|
| 117 | void destroyMolecule(moleculeId_t);
|
---|
| 118 |
|
---|
[02ee15] | 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 | */
|
---|
[46d958] | 123 | atom *createAtom();
|
---|
[02ee15] | 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 | */
|
---|
[46d958] | 129 | int registerAtom(atom*);
|
---|
[02ee15] | 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 | */
|
---|
[46d958] | 135 | void destroyAtom(atom*);
|
---|
[02ee15] | 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 | */
|
---|
[cbc5fb] | 141 | void destroyAtom(atomId_t);
|
---|
[865a945] | 142 |
|
---|
[88d586] | 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 |
|
---|
[02ee15] | 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 | */
|
---|
[7c4e29] | 155 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
[0e2a47] | 156 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
|
---|
[7c4e29] | 157 |
|
---|
[865a945] | 158 | protected:
|
---|
| 159 | /**** Iterators to use internal data structures */
|
---|
[1c51c8] | 160 |
|
---|
| 161 | // Atoms
|
---|
| 162 |
|
---|
[31af19] | 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 | {
|
---|
[865a945] | 169 | public:
|
---|
[31af19] | 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 |
|
---|
[7c4e29] | 179 | AtomIterator();
|
---|
[865a945] | 180 | AtomIterator(AtomDescriptor, World*);
|
---|
| 181 | AtomIterator(const AtomIterator&);
|
---|
[7c4e29] | 182 | AtomIterator& operator=(const AtomIterator&);
|
---|
| 183 | AtomIterator& operator++(); // prefix
|
---|
| 184 | AtomIterator operator++(int); // postfix with dummy parameter
|
---|
[865a945] | 185 | bool operator==(const AtomIterator&);
|
---|
[d2dbac0] | 186 | bool operator==(const AtomSet::iterator&);
|
---|
[865a945] | 187 | bool operator!=(const AtomIterator&);
|
---|
[d2dbac0] | 188 | bool operator!=(const AtomSet::iterator&);
|
---|
[865a945] | 189 | atom* operator*();
|
---|
[7c4e29] | 190 |
|
---|
| 191 | int getCount();
|
---|
[865a945] | 192 | protected:
|
---|
| 193 | void advanceState();
|
---|
[d2dbac0] | 194 | AtomSet::iterator state;
|
---|
[865a945] | 195 | boost::shared_ptr<AtomDescriptor_impl> descr;
|
---|
[7c4e29] | 196 | int index;
|
---|
[24a5e0] | 197 |
|
---|
| 198 | World* world;
|
---|
[865a945] | 199 | };
|
---|
| 200 |
|
---|
[02ee15] | 201 | /**
|
---|
| 202 | * returns an iterator over all Atoms matching a given descriptor.
|
---|
| 203 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
| 204 | */
|
---|
[865a945] | 205 | AtomIterator getAtomIter(AtomDescriptor descr);
|
---|
[02ee15] | 206 |
|
---|
| 207 | /**
|
---|
[d2dbac0] | 208 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator
|
---|
[02ee15] | 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 | */
|
---|
[d2dbac0] | 213 | AtomSet::iterator atomEnd();
|
---|
[865a945] | 214 |
|
---|
[1c51c8] | 215 | // Molecules
|
---|
| 216 |
|
---|
[31af19] | 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 | {
|
---|
[1c51c8] | 223 | public:
|
---|
[31af19] | 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 |
|
---|
[1c51c8] | 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 |
|
---|
[afb47f] | 269 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
| 270 | void doManipulate(ManipulateAtomsProcess *);
|
---|
| 271 |
|
---|
[5d1611] | 272 | private:
|
---|
[88d586] | 273 |
|
---|
| 274 | atomId_t getNextAtomId();
|
---|
| 275 | void releaseAtomId(atomId_t);
|
---|
| 276 | bool reserveAtomId(atomId_t);
|
---|
| 277 |
|
---|
[5d1611] | 278 | periodentafel *periode;
|
---|
[d2dbac0] | 279 | AtomSet atoms;
|
---|
[88d586] | 280 | std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
|
---|
[cbc5fb] | 281 | atomId_t currAtomId; //!< stores the next available Id for atoms
|
---|
[d2dbac0] | 282 | MoleculeSet molecules;
|
---|
[cbc5fb] | 283 | moleculeId_t currMoleculeId;
|
---|
[5d1611] | 284 | private:
|
---|
[02ee15] | 285 | /**
|
---|
| 286 | * private constructor to ensure creation of the world using
|
---|
| 287 | * the singleton pattern.
|
---|
| 288 | */
|
---|
[5d1611] | 289 | World();
|
---|
[02ee15] | 290 |
|
---|
| 291 | /**
|
---|
| 292 | * private destructor to ensure destruction of the world using the
|
---|
| 293 | * singleton pattern.
|
---|
| 294 | */
|
---|
[5d1611] | 295 | virtual ~World();
|
---|
| 296 |
|
---|
| 297 | /*****
|
---|
| 298 | * some legacy stuff that is include for now but will be removed later
|
---|
| 299 | *****/
|
---|
| 300 | public:
|
---|
[354859] | 301 | MoleculeListClass *&getMolecules();
|
---|
[4d9c01] | 302 |
|
---|
[5d1611] | 303 | private:
|
---|
[354859] | 304 | MoleculeListClass *molecules_deprecated;
|
---|
[5d1611] | 305 | };
|
---|
| 306 |
|
---|
| 307 | #endif /* WORLD_HPP_ */
|
---|