| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [ab4b55] | 8 | /*
 | 
|---|
 | 9 |  * FormatParser.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Mar 1, 2010
 | 
|---|
 | 12 |  *      Author: metzler
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 21 | 
 | 
|---|
| [ab4b55] | 22 | #include "FormatParser.hpp"
 | 
|---|
| [9131f3] | 23 | #include <iostream>
 | 
|---|
| [ab4b55] | 24 | 
 | 
|---|
 | 25 | using namespace std;
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | /**
 | 
|---|
 | 28 |  * Constructor.
 | 
|---|
 | 29 |  */
 | 
|---|
| [cd5047] | 30 | FormatParser::FormatParser() :
 | 
|---|
| [97b825] | 31 |   Observer("FormatParser"),
 | 
|---|
| [c1db05] | 32 |   parameters(NULL),
 | 
|---|
| [97b825] | 33 |   saveStream(NULL)
 | 
|---|
| [cd5047] | 34 | {
 | 
|---|
| [2f40c0e] | 35 |   ChangeTracker::getInstance().signOn(this);
 | 
|---|
| [38f991] | 36 |   World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomInserted));
 | 
|---|
 | 37 |   World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomRemoved));
 | 
|---|
| [ab4b55] | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | /**
 | 
|---|
 | 41 |  * Destructor.
 | 
|---|
 | 42 |  */
 | 
|---|
 | 43 | FormatParser::~FormatParser() {
 | 
|---|
| [2f40c0e] | 44 |   ChangeTracker::getInstance().signOff(this);
 | 
|---|
| [38f991] | 45 |   World::getInstance().signOff(this, World::getInstance().getChannel(World::AtomInserted));
 | 
|---|
 | 46 |   World::getInstance().signOff(this, World::getInstance().getChannel(World::AtomRemoved));
 | 
|---|
| [ab4b55] | 47 | }
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | /**
 | 
|---|
 | 50 |  * Update operation which can be invoked by the observable (which should be the
 | 
|---|
 | 51 |  * change tracker here).
 | 
|---|
 | 52 |  */
 | 
|---|
 | 53 | void FormatParser::update(Observable *publisher) {
 | 
|---|
 | 54 |   if (!saveStream) {
 | 
|---|
| [9131f3] | 55 |     cerr << "Please invoke setOstream() so the parser knows where to save the World's data." << endl;
 | 
|---|
 | 56 |     return;
 | 
|---|
| [ab4b55] | 57 |   }
 | 
|---|
 | 58 | 
 | 
|---|
| [73916f] | 59 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
 | 60 |   save(saveStream, atoms);
 | 
|---|
| [ab4b55] | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
| [38f991] | 63 | /**
 | 
|---|
 | 64 |  * With this, each format parser is informed about specific changes in the World.
 | 
|---|
 | 65 |  */
 | 
|---|
 | 66 | void FormatParser::recieveNotification(Observable *publisher, Notification_ptr notification) {
 | 
|---|
 | 67 |   switch (notification->getChannelNo()) {
 | 
|---|
 | 68 |     case World::AtomInserted:
 | 
|---|
 | 69 |       AtomInserted(World::getInstance().lastChanged<atom>()->getId());
 | 
|---|
 | 70 |       break;
 | 
|---|
 | 71 |     case World::AtomRemoved:
 | 
|---|
 | 72 |       AtomRemoved(World::getInstance().lastChanged<atom>()->getId());
 | 
|---|
 | 73 |       break;
 | 
|---|
 | 74 |     default:
 | 
|---|
 | 75 |       ASSERT(0,
 | 
|---|
 | 76 |           "FormatParser::recieveNotification() - unknown notification "
 | 
|---|
 | 77 |           +toString(notification->getChannelNo())+" received.");
 | 
|---|
 | 78 |       break;
 | 
|---|
 | 79 |   }
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
| [ab4b55] | 82 | /**
 | 
|---|
 | 83 |  * The observable can tell when it dies.
 | 
|---|
 | 84 |  */
 | 
|---|
 | 85 | void FormatParser::subjectKilled(Observable *publisher) {}
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | /**
 | 
|---|
 | 89 |  * Sets the output stream for save, so the save() method can be invoked on update
 | 
|---|
 | 90 |  * automatically.
 | 
|---|
 | 91 |  *
 | 
|---|
 | 92 |  * \param ostream where to save the World's state
 | 
|---|
 | 93 |  */
 | 
|---|
 | 94 | void FormatParser::setOstream(ostream* output) {
 | 
|---|
 | 95 |   saveStream = output;
 | 
|---|
 | 96 | }
 | 
|---|