source: molecuilder/src/World.cpp@ a1a532

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

moved Iterators for the World to a seperate file

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[2e8296]1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "World.hpp"
9
[d2d8f5]10#include "atom.hpp"
[120f8b]11#include "molecule.hpp"
12#include "periodentafel.hpp"
[86b917]13#include "Descriptors/AtomDescriptor.hpp"
[a5471c]14#include "Descriptors/AtomDescriptor_impl.hpp"
[5d4edf]15#include "Actions/ManipulateAtomsProcess.hpp"
[d2d8f5]16
17using namespace std;
[42918b]18
[2e8296]19/******************************* getter and setter ************************/
[120f8b]20periodentafel *&World::getPeriode(){
[2e8296]21 return periode;
22}
23
[323177]24atom* World::getAtom(AtomDescriptor descriptor){
[86b917]25 return descriptor.find();
26}
27
[323177]28vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[86b917]29 return descriptor.findAll();
30}
31
[bb89b9]32vector<atom*> World::getAllAtoms(){
33 return getAllAtoms(AllAtoms());
34}
35
[120f8b]36int World::numAtoms(){
37 return atoms.size();
38}
39
40int World::numMolecules(){
41 return molecules_deprecated->ListOfMolecules.size();
42}
43
[9ef76a]44/******************** Methods to change World state *********************/
45
[120f8b]46molecule* World::createMolecule(){
47 OBSERVE;
48 molecule *mol = NULL;
49 mol = new molecule(periode);
50 molecules_deprecated->insert(mol);
[a1a532]51 assert(!molecules.count(currMoleculeId));
52 molecules[currMoleculeId++] = mol;
[120f8b]53 mol->signOn(this);
54 return mol;
55}
56
[5d4edf]57
[7bfc19]58atom *World::createAtom(){
59 OBSERVE;
60 atom *res = NewAtom();
[a1a532]61 assert(!atoms.count(currAtomId));
[7bfc19]62 res->setId(currAtomId++);
63 res->setWorld(this);
64 atoms[res->getId()] = res;
65 return res;
66}
67
68int World::registerAtom(atom *atom){
69 OBSERVE;
[a1a532]70 assert(!atoms.count(currAtomId));
[7bfc19]71 atom->setId(currAtomId++);
72 atom->setWorld(this);
73 atoms[atom->getId()] = atom;
74 return atom->getId();
75}
76
77void World::destroyAtom(atom* atom){
78 OBSERVE;
79 int id = atom->getId();
80 destroyAtom(id);
81}
82
83void World::destroyAtom(int id) {
84 OBSERVE;
85 atom *atom = atoms[id];
86 assert(atom);
87 DeleteAtom(atom);
88 atoms.erase(id);
89}
90
[5d4edf]91ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
92 return new ManipulateAtomsProcess(op, descr,name,true);
93}
94
[bb89b9]95ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
96 return manipulateAtoms(op,name,AllAtoms());
97}
98
[9ef76a]99/********************* Internal Change methods for double Callback and Observer mechanism ********/
100
101void World::doManipulate(ManipulateAtomsProcess *proc){
102 proc->signOn(this);
103 {
104 OBSERVE;
105 proc->doManipulate(this);
106 }
107 proc->signOff(this);
108}
109
[a5471c]110/******************************* Iterators ********************************/
111
[a1a532]112/*
113 * Actual Implementation of the iterators can be found in WorldIterators.cpp
114 */
[a5471c]115
116World::AtomIterator World::getAtomIter(AtomDescriptor descr){
117 return AtomIterator(descr,this);
118}
[120f8b]119
[a1a532]120World::AtomSet::iterator World::atomEnd(){
[5d4edf]121 return atoms.end();
122}
123
[2e8296]124/******************************* Singleton Stuff **************************/
125
[42918b]126// TODO: Hide boost-thread using Autotools stuff when no threads are used
[2e8296]127World* World::theWorld = 0;
[42918b]128boost::mutex World::worldLock;
129
[2e8296]130
131
[323177]132World::World() :
[7bfc19]133 currAtomId(0),
[a1a532]134 currMoleculeId(0),
[120f8b]135 periode(new periodentafel),
[5d4edf]136 molecules_deprecated(new MoleculeListClass),
[a1a532]137 atoms(),
138 molecules()
[b53a7e]139{
140 molecules_deprecated->signOn(this);
141}
[2e8296]142
143World::~World()
[120f8b]144{
[7bfc19]145 delete molecules_deprecated;
[120f8b]146 delete periode;
[a1a532]147 AtomSet::iterator iter;
[7bfc19]148 for(iter=atoms.begin();iter!=atoms.end();++iter){
149 DeleteAtom((*iter).second);
150 }
151 atoms.clear();
[120f8b]152}
[2e8296]153
154World* World::get(){
155 // boost supports RAII-Style locking, so we don't need to unlock
156 boost::mutex::scoped_lock guard(worldLock);
157 if(!theWorld) {
158 theWorld = new World();
159 }
160 return theWorld;
161}
162
163void World::destroy(){
164 // boost supports RAII-Style locking, so we don't need to unlock
165 boost::mutex::scoped_lock guard(worldLock);
166 delete theWorld;
167 theWorld = 0;
168}
169
170World* World::reset(){
171 World* oldWorld = 0;
172 {
173 // boost supports RAII-Style locking, so we don't need to unlock
174 boost::mutex::scoped_lock guard(worldLock);
175
176 oldWorld = theWorld;
177 theWorld = new World();
178 // oldworld does not need protection any more,
179 // since we should have the only reference
180
181 // worldLock handles access to the pointer,
182 // not to the object
183 } // scope-end releases the lock
184
185 // we have to let all the observers know that the
186 // oldWorld was destroyed. oldWorld calls subjectKilled
187 // upon destruction. Every Observer getting that signal
188 // should see that it gets the updated new world
189 delete oldWorld;
190}
191
192/******************************* deprecated Legacy Stuff ***********************/
193
[120f8b]194MoleculeListClass *&World::getMolecules() {
195 return molecules_deprecated;
[2e8296]196}
Note: See TracBrowser for help on using the repository browser.