source: molecuilder/src/World.cpp@ d639c7

Last change on this file since d639c7 was 4c60ef, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added generic singleton Pattern that can be inherited to any class making that class a singleton.

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