source: molecuilder/src/World.cpp@ 025ca2

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

Added methods to query Molecules by ID

  • Property mode set to 100644
File size: 7.0 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"
[14d898]15#include "Descriptors/MoleculeDescriptor.hpp"
16#include "Descriptors/MoleculeDescriptor_impl.hpp"
[5d4edf]17#include "Actions/ManipulateAtomsProcess.hpp"
[d2d8f5]18
19using namespace std;
[42918b]20
[2e8296]21/******************************* getter and setter ************************/
[120f8b]22periodentafel *&World::getPeriode(){
[2e8296]23 return periode;
24}
25
[14d898]26// Atoms
27
[323177]28atom* World::getAtom(AtomDescriptor descriptor){
[86b917]29 return descriptor.find();
30}
31
[323177]32vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[86b917]33 return descriptor.findAll();
34}
35
[bb89b9]36vector<atom*> World::getAllAtoms(){
37 return getAllAtoms(AllAtoms());
38}
39
[120f8b]40int World::numAtoms(){
41 return atoms.size();
42}
43
[14d898]44// Molecules
45
46molecule *World::getMolecule(MoleculeDescriptor descriptor){
47 return descriptor.find();
48}
49
50std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
51 return descriptor.findAll();
52}
53
[120f8b]54int World::numMolecules(){
55 return molecules_deprecated->ListOfMolecules.size();
56}
57
[9ef76a]58/******************** Methods to change World state *********************/
59
[120f8b]60molecule* World::createMolecule(){
61 OBSERVE;
62 molecule *mol = NULL;
[8d9d38]63 mol = NewMolecule();
[a1a532]64 assert(!molecules.count(currMoleculeId));
[8d9d38]65 mol->setId(currMoleculeId++);
[2e6496]66 // store the molecule by ID
[8d9d38]67 molecules[mol->getId()] = mol;
[120f8b]68 mol->signOn(this);
69 return mol;
70}
71
[8d9d38]72void World::destroyMolecule(molecule* mol){
73 OBSERVE;
74 destroyMolecule(mol->getId());
75}
76
77void World::destroyMolecule(moleculeId_t id){
78 OBSERVE;
79 molecule *mol = molecules[id];
80 assert(mol);
81 DeleteMolecule(mol);
82 molecules.erase(id);
83}
84
[5d4edf]85
[7bfc19]86atom *World::createAtom(){
87 OBSERVE;
[3746aeb]88 atomId_t id = getNextAtomId();
89 atom *res = NewAtom(id);
[7bfc19]90 res->setWorld(this);
[2e6496]91 // store the atom by ID
[7bfc19]92 atoms[res->getId()] = res;
93 return res;
94}
95
96int World::registerAtom(atom *atom){
97 OBSERVE;
[3746aeb]98 atomId_t id = getNextAtomId();
99 atom->setId(id);
[7bfc19]100 atom->setWorld(this);
101 atoms[atom->getId()] = atom;
102 return atom->getId();
103}
104
105void World::destroyAtom(atom* atom){
106 OBSERVE;
107 int id = atom->getId();
108 destroyAtom(id);
109}
110
[8d9d38]111void World::destroyAtom(atomId_t id) {
[7bfc19]112 OBSERVE;
113 atom *atom = atoms[id];
114 assert(atom);
115 DeleteAtom(atom);
116 atoms.erase(id);
[3746aeb]117 releaseAtomId(id);
118}
119
120bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
121 OBSERVE;
122 // in case this call did not originate from inside the atom, we redirect it,
123 // to also let it know that it has changed
124 if(!target){
125 target = atoms[oldId];
126 assert(target && "Atom with that ID not found");
127 return target->changeId(newId);
128 }
129 else{
130 if(reserveAtomId(newId)){
131 atoms.erase(oldId);
132 atoms.insert(pair<atomId_t,atom*>(newId,target));
133 return true;
134 }
135 else{
136 return false;
137 }
138 }
[7bfc19]139}
140
[5d4edf]141ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
142 return new ManipulateAtomsProcess(op, descr,name,true);
143}
144
[bb89b9]145ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
146 return manipulateAtoms(op,name,AllAtoms());
147}
148
[9ef76a]149/********************* Internal Change methods for double Callback and Observer mechanism ********/
150
151void World::doManipulate(ManipulateAtomsProcess *proc){
152 proc->signOn(this);
153 {
154 OBSERVE;
155 proc->doManipulate(this);
156 }
157 proc->signOff(this);
158}
[3746aeb]159/******************************* IDManagement *****************************/
160
[f058ef]161// Atoms
162
[3746aeb]163atomId_t World::getNextAtomId(){
164 // see if we can reuse some Id
165 if(atomIdPool.empty()){
166 return currAtomId++;
167 }
168 else{
169 // we give out the first ID from the pool
170 atomId_t id = *(atomIdPool.begin());
171 atomIdPool.erase(id);
172 }
173}
174
175void World::releaseAtomId(atomId_t id){
176 atomIdPool.insert(id);
177 // defragmentation of the pool
178 set<atomId_t>::reverse_iterator iter;
179 // go through all Ids in the pool that lie immediately below the border
180 while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
181 atomIdPool.erase(--currAtomId);
182 }
183}
[9ef76a]184
[3746aeb]185bool World::reserveAtomId(atomId_t id){
186 if(id>=currAtomId ){
187 // add all ids between the new one and current border as available
188 for(atomId_t pos=currAtomId; pos<id; ++pos){
189 atomIdPool.insert(pos);
190 }
191 currAtomId=id+1;
192 return true;
193 }
194 else if(atomIdPool.count(id)){
195 atomIdPool.erase(id);
196 return true;
197 }
198 else{
199 // this ID could not be reserved
200 return false;
201 }
202}
[f058ef]203
204// Molecules
205
[a5471c]206/******************************* Iterators ********************************/
207
[a1a532]208/*
209 * Actual Implementation of the iterators can be found in WorldIterators.cpp
210 */
[a5471c]211
212World::AtomIterator World::getAtomIter(AtomDescriptor descr){
213 return AtomIterator(descr,this);
214}
[120f8b]215
[a1a532]216World::AtomSet::iterator World::atomEnd(){
[5d4edf]217 return atoms.end();
218}
219
[14d898]220World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
221 return MoleculeIterator(descr,this);
222}
223
224World::MoleculeSet::iterator World::moleculeEnd(){
225 return molecules.end();
226}
227
[2e8296]228/******************************* Singleton Stuff **************************/
229
[42918b]230// TODO: Hide boost-thread using Autotools stuff when no threads are used
[2e8296]231World* World::theWorld = 0;
[42918b]232boost::mutex World::worldLock;
233
[323177]234World::World() :
[120f8b]235 periode(new periodentafel),
[a1a532]236 atoms(),
[98a2987]237 currAtomId(0),
238 molecules(),
239 currMoleculeId(0),
240 molecules_deprecated(new MoleculeListClass(this))
[b53a7e]241{
242 molecules_deprecated->signOn(this);
243}
[2e8296]244
245World::~World()
[120f8b]246{
[a0fcfb]247 molecules_deprecated->signOff(this);
[7bfc19]248 delete molecules_deprecated;
[120f8b]249 delete periode;
[8d9d38]250 MoleculeSet::iterator molIter;
251 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
252 DeleteMolecule((*molIter).second);
253 }
254 molecules.clear();
255 AtomSet::iterator atIter;
256 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
257 DeleteAtom((*atIter).second);
[7bfc19]258 }
259 atoms.clear();
[120f8b]260}
[2e8296]261
262World* World::get(){
263 // boost supports RAII-Style locking, so we don't need to unlock
264 boost::mutex::scoped_lock guard(worldLock);
265 if(!theWorld) {
266 theWorld = new World();
267 }
268 return theWorld;
269}
270
271void World::destroy(){
272 // boost supports RAII-Style locking, so we don't need to unlock
273 boost::mutex::scoped_lock guard(worldLock);
274 delete theWorld;
275 theWorld = 0;
276}
277
278World* World::reset(){
279 World* oldWorld = 0;
280 {
281 // boost supports RAII-Style locking, so we don't need to unlock
282 boost::mutex::scoped_lock guard(worldLock);
283
284 oldWorld = theWorld;
285 theWorld = new World();
286 // oldworld does not need protection any more,
287 // since we should have the only reference
288
289 // worldLock handles access to the pointer,
290 // not to the object
291 } // scope-end releases the lock
292
293 // we have to let all the observers know that the
294 // oldWorld was destroyed. oldWorld calls subjectKilled
295 // upon destruction. Every Observer getting that signal
296 // should see that it gets the updated new world
297 delete oldWorld;
[98a2987]298 return theWorld;
[2e8296]299}
300
301/******************************* deprecated Legacy Stuff ***********************/
302
[120f8b]303MoleculeListClass *&World::getMolecules() {
304 return molecules_deprecated;
[2e8296]305}
Note: See TracBrowser for help on using the repository browser.