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 | atom *createAtom();
|
---|
61 | int registerAtom(atom*);
|
---|
62 | void destroyAtom(atom*);
|
---|
63 | void destroyAtom(int);
|
---|
64 |
|
---|
65 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
66 |
|
---|
67 | protected:
|
---|
68 | /**** Iterators to use internal data structures */
|
---|
69 | class AtomIterator {
|
---|
70 | public:
|
---|
71 | AtomIterator();
|
---|
72 | AtomIterator(AtomDescriptor, World*);
|
---|
73 | AtomIterator(const AtomIterator&);
|
---|
74 | AtomIterator& operator=(const AtomIterator&);
|
---|
75 | AtomIterator& operator++(); // prefix
|
---|
76 | AtomIterator operator++(int); // postfix with dummy parameter
|
---|
77 | bool operator==(const AtomIterator&);
|
---|
78 | bool operator==(const AtomList::iterator&);
|
---|
79 | bool operator!=(const AtomIterator&);
|
---|
80 | bool operator!=(const AtomList::iterator&);
|
---|
81 | atom* operator*();
|
---|
82 |
|
---|
83 | int getCount();
|
---|
84 | protected:
|
---|
85 | void advanceState();
|
---|
86 | World* world;
|
---|
87 | AtomList::iterator state;
|
---|
88 | boost::shared_ptr<AtomDescriptor_impl> descr;
|
---|
89 | int index;
|
---|
90 | };
|
---|
91 |
|
---|
92 | AtomIterator getAtomIter(AtomDescriptor descr);
|
---|
93 | AtomList::iterator atomEnd();
|
---|
94 |
|
---|
95 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
96 | void doManipulate(ManipulateAtomsProcess *);
|
---|
97 |
|
---|
98 | private:
|
---|
99 | periodentafel *periode;
|
---|
100 | AtomList atoms;
|
---|
101 | int currAtomId; //!< stores the next available Id for atoms
|
---|
102 | std::set<molecule*> molecules;
|
---|
103 |
|
---|
104 |
|
---|
105 | /***** singleton Stuff *****/
|
---|
106 | public:
|
---|
107 | static World* get();
|
---|
108 | static void destroy();
|
---|
109 | static World* reset();
|
---|
110 |
|
---|
111 | private:
|
---|
112 | World();
|
---|
113 | virtual ~World();
|
---|
114 |
|
---|
115 | static World *theWorld;
|
---|
116 | // this mutex only saves the singleton pattern...
|
---|
117 | // use other mutexes to protect internal data as well
|
---|
118 | // this mutex handles access to the pointer, not to the object!!!
|
---|
119 | static boost::mutex worldLock;
|
---|
120 |
|
---|
121 | /*****
|
---|
122 | * some legacy stuff that is include for now but will be removed later
|
---|
123 | *****/
|
---|
124 | public:
|
---|
125 | MoleculeListClass *&getMolecules();
|
---|
126 |
|
---|
127 | private:
|
---|
128 | MoleculeListClass *molecules_deprecated;
|
---|
129 | };
|
---|
130 |
|
---|
131 | #endif /* WORLD_HPP_ */
|
---|