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