| [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 |
|
|---|
| [5d4edf] | 18 |
|
|---|
| [2e8296] | 19 | #include "Patterns/Observer.hpp"
|
|---|
| 20 | #include "Patterns/Cacheable.hpp"
|
|---|
| 21 |
|
|---|
| 22 | // forward declarations
|
|---|
| 23 | class periodentafel;
|
|---|
| 24 | class MoleculeListClass;
|
|---|
| [42918b] | 25 | class atom;
|
|---|
| [120f8b] | 26 | class molecule;
|
|---|
| [86b917] | 27 | class AtomDescriptor;
|
|---|
| [323177] | 28 | class AtomDescriptor_impl;
|
|---|
| [5d4edf] | 29 | class ManipulateAtomsProcess;
|
|---|
| [2e8296] | 30 |
|
|---|
| 31 | class World : public Observable
|
|---|
| 32 | {
|
|---|
| [323177] | 33 | friend class AtomDescriptor_impl;
|
|---|
| [a5471c] | 34 | friend class AtomDescriptor;
|
|---|
| 35 |
|
|---|
| [5d4edf] | 36 | friend class ManipulateAtomsProcess;
|
|---|
| 37 |
|
|---|
| [a5471c] | 38 | typedef std::map<int,atom*> AtomList;
|
|---|
| [2e8296] | 39 | public:
|
|---|
| 40 |
|
|---|
| 41 | /***** getter and setter *****/
|
|---|
| [120f8b] | 42 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
|---|
| 43 | periodentafel *&getPeriode();
|
|---|
| [323177] | 44 | atom* getAtom(AtomDescriptor descriptor);
|
|---|
| 45 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
|---|
| [120f8b] | 46 | int numAtoms();
|
|---|
| 47 | int numMolecules();
|
|---|
| 48 |
|
|---|
| 49 | /***** Methods to work with the World *****/
|
|---|
| 50 | molecule *createMolecule();
|
|---|
| [a5471c] | 51 |
|
|---|
| [5d4edf] | 52 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
|---|
| 53 |
|
|---|
| [a5471c] | 54 | protected:
|
|---|
| 55 | /**** Iterators to use internal data structures */
|
|---|
| 56 | class AtomIterator {
|
|---|
| 57 | public:
|
|---|
| [5d4edf] | 58 | AtomIterator();
|
|---|
| [a5471c] | 59 | AtomIterator(AtomDescriptor, World*);
|
|---|
| 60 | AtomIterator(const AtomIterator&);
|
|---|
| [5d4edf] | 61 | AtomIterator& operator=(const AtomIterator&);
|
|---|
| 62 | AtomIterator& operator++(); // prefix
|
|---|
| 63 | AtomIterator operator++(int); // postfix with dummy parameter
|
|---|
| [a5471c] | 64 | bool operator==(const AtomIterator&);
|
|---|
| [5d4edf] | 65 | bool operator==(const AtomList::iterator&);
|
|---|
| [a5471c] | 66 | bool operator!=(const AtomIterator&);
|
|---|
| [5d4edf] | 67 | bool operator!=(const AtomList::iterator&);
|
|---|
| [a5471c] | 68 | atom* operator*();
|
|---|
| [5d4edf] | 69 |
|
|---|
| 70 | int getCount();
|
|---|
| [a5471c] | 71 | protected:
|
|---|
| 72 | void advanceState();
|
|---|
| 73 | World* world;
|
|---|
| 74 | AtomList::iterator state;
|
|---|
| 75 | boost::shared_ptr<AtomDescriptor_impl> descr;
|
|---|
| [5d4edf] | 76 | int index;
|
|---|
| [a5471c] | 77 | };
|
|---|
| 78 |
|
|---|
| 79 | AtomIterator getAtomIter(AtomDescriptor descr);
|
|---|
| [5d4edf] | 80 | AtomList::iterator atomEnd();
|
|---|
| [a5471c] | 81 |
|
|---|
| [9ef76a] | 82 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
|---|
| 83 | void doManipulate(ManipulateAtomsProcess *);
|
|---|
| 84 |
|
|---|
| [2e8296] | 85 | private:
|
|---|
| 86 | periodentafel *periode;
|
|---|
| [a5471c] | 87 | AtomList atoms;
|
|---|
| [120f8b] | 88 | std::set<molecule*> molecules;
|
|---|
| [2e8296] | 89 |
|
|---|
| 90 |
|
|---|
| 91 | /***** singleton Stuff *****/
|
|---|
| 92 | public:
|
|---|
| 93 | static World* get();
|
|---|
| 94 | static void destroy();
|
|---|
| 95 | static World* reset();
|
|---|
| 96 |
|
|---|
| 97 | private:
|
|---|
| 98 | World();
|
|---|
| 99 | virtual ~World();
|
|---|
| 100 |
|
|---|
| 101 | static World *theWorld;
|
|---|
| 102 | // this mutex only saves the singleton pattern...
|
|---|
| 103 | // use other mutexes to protect internal data as well
|
|---|
| 104 | // this mutex handles access to the pointer, not to the object!!!
|
|---|
| 105 | static boost::mutex worldLock;
|
|---|
| 106 |
|
|---|
| 107 | /*****
|
|---|
| 108 | * some legacy stuff that is include for now but will be removed later
|
|---|
| 109 | *****/
|
|---|
| 110 | public:
|
|---|
| [120f8b] | 111 | MoleculeListClass *&getMolecules();
|
|---|
| [42918b] | 112 |
|
|---|
| 113 | // functions used for the WorldContent template mechanism
|
|---|
| [d2d8f5] | 114 | void registerAtom(atom *theAtom);
|
|---|
| 115 | void unregisterAtom(atom *theAtom);
|
|---|
| [2e8296] | 116 | private:
|
|---|
| [120f8b] | 117 | // this function cleans up anything that cannot be cleaned while the lock is active
|
|---|
| 118 | // at a later point all these cleanups have to be moved to the World Class so the deadlock and
|
|---|
| 119 | // race condition can both be avoided.
|
|---|
| 120 | void destroyLegacy();
|
|---|
| 121 |
|
|---|
| 122 | MoleculeListClass *molecules_deprecated;
|
|---|
| [323177] | 123 |
|
|---|
| 124 | // this is needed to assign unique IDs to atoms... so far
|
|---|
| 125 | // IDs are not assigned upon Atom creation, so we cannot query the ID
|
|---|
| 126 | // during construction. By using the dummy ID we can make sure all atoms
|
|---|
| 127 | // are actually stored in the map and don't overwrite each other.
|
|---|
| 128 | int dummyId;
|
|---|
| [2e8296] | 129 | };
|
|---|
| 130 |
|
|---|
| 131 | #endif /* WORLD_HPP_ */
|
|---|