| [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 | /* | 
|---|
| [765f16] | 9 | * FormatParser_common_common.cpp | 
|---|
| [ab4b55] | 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 |  | 
|---|
| [9131f3] | 22 | #include <iostream> | 
|---|
| [ab4b55] | 23 |  | 
|---|
| [765f16] | 24 | #include "CodePatterns/Observer.hpp" | 
|---|
|  | 25 | #include "World.hpp" | 
|---|
|  | 26 | #include "ChangeTracker.hpp" | 
|---|
|  | 27 | #include "FormatParser_common.hpp" | 
|---|
|  | 28 |  | 
|---|
| [ab4b55] | 29 | using namespace std; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | /** | 
|---|
|  | 32 | * Constructor. | 
|---|
|  | 33 | */ | 
|---|
| [765f16] | 34 | FormatParser_common::FormatParser_common(FormatParser_Parameters *_parameters) : | 
|---|
|  | 35 | Observer("FormatParser_common") | 
|---|
| [cd5047] | 36 | { | 
|---|
| [765f16] | 37 | parameters = _parameters; | 
|---|
| [2f40c0e] | 38 | ChangeTracker::getInstance().signOn(this); | 
|---|
| [38f991] | 39 | World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomInserted)); | 
|---|
|  | 40 | World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomRemoved)); | 
|---|
| [ab4b55] | 41 | } | 
|---|
|  | 42 |  | 
|---|
|  | 43 | /** | 
|---|
|  | 44 | * Destructor. | 
|---|
|  | 45 | */ | 
|---|
| [765f16] | 46 | FormatParser_common::~FormatParser_common() | 
|---|
|  | 47 | { | 
|---|
| [2f40c0e] | 48 | ChangeTracker::getInstance().signOff(this); | 
|---|
| [38f991] | 49 | World::getInstance().signOff(this, World::getInstance().getChannel(World::AtomInserted)); | 
|---|
|  | 50 | World::getInstance().signOff(this, World::getInstance().getChannel(World::AtomRemoved)); | 
|---|
| [765f16] | 51 | if (parameters != NULL) | 
|---|
|  | 52 | delete parameters; | 
|---|
| [ab4b55] | 53 | } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | /** | 
|---|
|  | 56 | * Update operation which can be invoked by the observable (which should be the | 
|---|
|  | 57 | * change tracker here). | 
|---|
|  | 58 | */ | 
|---|
| [765f16] | 59 | void FormatParser_common::update(Observable *publisher) { | 
|---|
| [ab4b55] | 60 | if (!saveStream) { | 
|---|
| [9131f3] | 61 | cerr << "Please invoke setOstream() so the parser knows where to save the World's data." << endl; | 
|---|
|  | 62 | return; | 
|---|
| [ab4b55] | 63 | } | 
|---|
|  | 64 |  | 
|---|
| [73916f] | 65 | std::vector<atom *> atoms = World::getInstance().getAllAtoms(); | 
|---|
|  | 66 | save(saveStream, atoms); | 
|---|
| [ab4b55] | 67 | } | 
|---|
|  | 68 |  | 
|---|
| [38f991] | 69 | /** | 
|---|
|  | 70 | * With this, each format parser is informed about specific changes in the World. | 
|---|
|  | 71 | */ | 
|---|
| [765f16] | 72 | void FormatParser_common::recieveNotification(Observable *publisher, Notification_ptr notification) { | 
|---|
| [38f991] | 73 | switch (notification->getChannelNo()) { | 
|---|
|  | 74 | case World::AtomInserted: | 
|---|
|  | 75 | AtomInserted(World::getInstance().lastChanged<atom>()->getId()); | 
|---|
|  | 76 | break; | 
|---|
|  | 77 | case World::AtomRemoved: | 
|---|
|  | 78 | AtomRemoved(World::getInstance().lastChanged<atom>()->getId()); | 
|---|
|  | 79 | break; | 
|---|
|  | 80 | default: | 
|---|
|  | 81 | ASSERT(0, | 
|---|
| [765f16] | 82 | "FormatParser_common::recieveNotification() - unknown notification " | 
|---|
| [38f991] | 83 | +toString(notification->getChannelNo())+" received."); | 
|---|
|  | 84 | break; | 
|---|
|  | 85 | } | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
| [ab4b55] | 88 | /** | 
|---|
|  | 89 | * The observable can tell when it dies. | 
|---|
|  | 90 | */ | 
|---|
| [765f16] | 91 | void FormatParser_common::subjectKilled(Observable *publisher) {} | 
|---|
| [ab4b55] | 92 |  | 
|---|
|  | 93 | /** | 
|---|
|  | 94 | * Sets the output stream for save, so the save() method can be invoked on update | 
|---|
|  | 95 | * automatically. | 
|---|
|  | 96 | * | 
|---|
|  | 97 | * \param ostream where to save the World's state | 
|---|
|  | 98 | */ | 
|---|
| [765f16] | 99 | void FormatParser_common::setOstream(ostream* output) { | 
|---|
| [ab4b55] | 100 | saveStream = output; | 
|---|
|  | 101 | } | 
|---|