source: molecuilder/src/World.cpp@ bb89b9

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

Added overloaded methods for all methods in the world taking an AtomDescriptor

  • Property mode set to 100644
File size: 5.7 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);
51 molecules.insert(mol);
52 mol->signOn(this);
53 return mol;
54}
55
[5d4edf]56
[7bfc19]57atom *World::createAtom(){
58 OBSERVE;
59 atom *res = NewAtom();
60 res->setId(currAtomId++);
61 res->setWorld(this);
62 atoms[res->getId()] = res;
63 return res;
64}
65
66int World::registerAtom(atom *atom){
67 OBSERVE;
68 atom->setId(currAtomId++);
69 atom->setWorld(this);
70 atoms[atom->getId()] = atom;
71 return atom->getId();
72}
73
74void World::destroyAtom(atom* atom){
75 OBSERVE;
76 int id = atom->getId();
77 destroyAtom(id);
78}
79
80void World::destroyAtom(int id) {
81 OBSERVE;
82 atom *atom = atoms[id];
83 assert(atom);
84 DeleteAtom(atom);
85 atoms.erase(id);
86}
87
[5d4edf]88ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
89 return new ManipulateAtomsProcess(op, descr,name,true);
90}
91
[bb89b9]92ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
93 return manipulateAtoms(op,name,AllAtoms());
94}
95
[9ef76a]96/********************* Internal Change methods for double Callback and Observer mechanism ********/
97
98void World::doManipulate(ManipulateAtomsProcess *proc){
99 proc->signOn(this);
100 {
101 OBSERVE;
102 proc->doManipulate(this);
103 }
104 proc->signOff(this);
105}
106
[a5471c]107/******************************* Iterators ********************************/
108
[5d4edf]109World::AtomIterator::AtomIterator(){
110 state = World::get()->atomEnd();
111}
112
[a5471c]113World::AtomIterator::AtomIterator(AtomDescriptor _descr, World* _world) :
114 descr(_descr.get_impl()),
[5d4edf]115 world(_world),
116 index(0)
[a5471c]117{
118 state = world->atoms.begin();
119 advanceState();
120}
121
122World::AtomIterator::AtomIterator(const AtomIterator& rhs) :
123 state(rhs.state),
[5d4edf]124 descr(rhs.descr),
125 index(rhs.index),
126 world(rhs.world)
[a5471c]127 {}
128
[5d4edf]129World::AtomIterator& World::AtomIterator::operator=(const AtomIterator& rhs)
130{
131 if(&rhs!=this){
132 state=rhs.state;
133 descr=rhs.descr;
134 index=rhs.index;
135 world=rhs.world;
136 }
137 return *this;
138}
139
[a5471c]140World::AtomIterator& World::AtomIterator::operator++(){
[5d4edf]141 ++state;
142 ++index;
[a5471c]143 advanceState();
[5d4edf]144 return *this;
145}
146
147World::AtomIterator World::AtomIterator::operator++(int){
148 AtomIterator res(*this);
149 ++(*this);
150 return res;
[a5471c]151}
152
153bool World::AtomIterator::operator==(const AtomIterator& rhs){
154 return state==rhs.state;
155}
156
[5d4edf]157bool World::AtomIterator::operator==(const World::AtomList::iterator& rhs){
158 return state==rhs;
159}
160
[a5471c]161bool World::AtomIterator::operator!=(const AtomIterator& rhs){
162 return state!=rhs.state;
163}
164
[5d4edf]165bool World::AtomIterator::operator!=(const World::AtomList::iterator& rhs){
166 return state!=rhs;
167}
168
[a5471c]169atom* World::AtomIterator::operator*(){
170 return (*state).second;
171}
172
173void World::AtomIterator::advanceState(){
[5d4edf]174 while((state!=world->atoms.end()) && (!descr->predicate(*state))){
175 ++state;
176 ++index;
177 }
178}
179
180int World::AtomIterator::getCount(){
181 return index;
[a5471c]182}
183
184World::AtomIterator World::getAtomIter(AtomDescriptor descr){
185 return AtomIterator(descr,this);
186}
[120f8b]187
[5d4edf]188World::AtomList::iterator World::atomEnd(){
189 return atoms.end();
190}
191
[2e8296]192/******************************* Singleton Stuff **************************/
193
[42918b]194// TODO: Hide boost-thread using Autotools stuff when no threads are used
[2e8296]195World* World::theWorld = 0;
[42918b]196boost::mutex World::worldLock;
197
[2e8296]198
199
[323177]200World::World() :
[7bfc19]201 currAtomId(0),
[120f8b]202 periode(new periodentafel),
[5d4edf]203 molecules_deprecated(new MoleculeListClass),
204 atoms()
[b53a7e]205{
206 molecules_deprecated->signOn(this);
207}
[2e8296]208
209World::~World()
[120f8b]210{
[7bfc19]211 delete molecules_deprecated;
[120f8b]212 delete periode;
[7bfc19]213 AtomList::iterator iter;
214 for(iter=atoms.begin();iter!=atoms.end();++iter){
215 DeleteAtom((*iter).second);
216 }
217 atoms.clear();
[120f8b]218}
[2e8296]219
220World* World::get(){
221 // boost supports RAII-Style locking, so we don't need to unlock
222 boost::mutex::scoped_lock guard(worldLock);
223 if(!theWorld) {
224 theWorld = new World();
225 }
226 return theWorld;
227}
228
229void World::destroy(){
230 // boost supports RAII-Style locking, so we don't need to unlock
231 boost::mutex::scoped_lock guard(worldLock);
232 delete theWorld;
233 theWorld = 0;
234}
235
236World* World::reset(){
237 World* oldWorld = 0;
238 {
239 // boost supports RAII-Style locking, so we don't need to unlock
240 boost::mutex::scoped_lock guard(worldLock);
241
242 oldWorld = theWorld;
243 theWorld = new World();
244 // oldworld does not need protection any more,
245 // since we should have the only reference
246
247 // worldLock handles access to the pointer,
248 // not to the object
249 } // scope-end releases the lock
250
251 // we have to let all the observers know that the
252 // oldWorld was destroyed. oldWorld calls subjectKilled
253 // upon destruction. Every Observer getting that signal
254 // should see that it gets the updated new world
255 delete oldWorld;
256}
257
258/******************************* deprecated Legacy Stuff ***********************/
259
[120f8b]260MoleculeListClass *&World::getMolecules() {
261 return molecules_deprecated;
[2e8296]262}
Note: See TracBrowser for help on using the repository browser.