source: molecuilder/src/World.hpp@ cbc27f

Last change on this file since cbc27f was a5471c, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added iterator structure that allows iterating over selected atoms in the World.

  • Property mode set to 100644
File size: 2.8 KB
Line 
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
21class periodentafel;
22class MoleculeListClass;
23class atom;
24class molecule;
25class AtomDescriptor;
26class AtomDescriptor_impl;
27
28class World : public Observable
29{
30friend class AtomDescriptor_impl;
31friend class AtomDescriptor;
32
33typedef std::map<int,atom*> AtomList;
34public:
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
47protected:
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
66private:
67 periodentafel *periode;
68 AtomList atoms;
69 std::set<molecule*> molecules;
70
71
72 /***** singleton Stuff *****/
73public:
74 static World* get();
75 static void destroy();
76 static World* reset();
77
78private:
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 *****/
91public:
92 MoleculeListClass *&getMolecules();
93
94 // functions used for the WorldContent template mechanism
95 void registerAtom(atom *theAtom);
96 void unregisterAtom(atom *theAtom);
97private:
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_ */
Note: See TracBrowser for help on using the repository browser.