source: molecuilder/src/World.cpp@ b918031

Last change on this file since b918031 was e04838, checked in by Frederik Heber <heber@…>, 15 years ago

class config added to World.

  • World now has the global configuration.
  • new function World::getConfig().
  • TextWindow::TextWindow() uses thr above to obtain the configuration.

Signed-off-by: Frederik Heber <heber@…>

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