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