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