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 |
|
---|
8 | /*
|
---|
9 | * FormatParser.cpp
|
---|
10 | *
|
---|
11 | * Created on: Mar 1, 2010
|
---|
12 | * Author: metzler
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "FormatParser.hpp"
|
---|
23 | #include <iostream>
|
---|
24 |
|
---|
25 | using namespace std;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructor.
|
---|
29 | */
|
---|
30 | FormatParser::FormatParser() :
|
---|
31 | Observer("FormatParser"),
|
---|
32 | parameters(NULL),
|
---|
33 | saveStream(NULL)
|
---|
34 | {
|
---|
35 | ChangeTracker::getInstance().signOn(this);
|
---|
36 | World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomInserted));
|
---|
37 | World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomRemoved));
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Destructor.
|
---|
42 | */
|
---|
43 | FormatParser::~FormatParser() {
|
---|
44 | ChangeTracker::getInstance().signOff(this);
|
---|
45 | World::getInstance().signOff(this, World::getInstance().getChannel(World::AtomInserted));
|
---|
46 | World::getInstance().signOff(this, World::getInstance().getChannel(World::AtomRemoved));
|
---|
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) {
|
---|
55 | cerr << "Please invoke setOstream() so the parser knows where to save the World's data." << endl;
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
60 | save(saveStream, atoms);
|
---|
61 | }
|
---|
62 |
|
---|
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 |
|
---|
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 | }
|
---|