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 | * builder_init.cpp
|
---|
10 | *
|
---|
11 | * Created on: Dec 15, 2010
|
---|
12 | * Author: heber
|
---|
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 "bondgraph.hpp"
|
---|
23 | #include "config.hpp"
|
---|
24 | #include "CodePatterns/Log.hpp"
|
---|
25 | #include "molecule.hpp"
|
---|
26 | #include "periodentafel.hpp"
|
---|
27 | #include "tesselationhelpers.hpp"
|
---|
28 | #include "UIElements/UIFactory.hpp"
|
---|
29 | #include "UIElements/Menu/MenuDescription.hpp"
|
---|
30 | #include "UIElements/TextUI/TextUIFactory.hpp"
|
---|
31 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
|
---|
32 | #include "UIElements/CommandLineUI/CommandLineParser.hpp"
|
---|
33 | #ifdef USE_GUI_QT
|
---|
34 | #include "UIElements/Qt4/QtUIFactory.hpp"
|
---|
35 | #endif
|
---|
36 | #include "UIElements/MainWindow.hpp"
|
---|
37 | #include "UIElements/Dialog.hpp"
|
---|
38 | //#include "Menu/ActionMenuItem.hpp"
|
---|
39 | #include "CodePatterns/Verbose.hpp"
|
---|
40 | #include "World.hpp"
|
---|
41 |
|
---|
42 | #include "Actions/ActionRegistry.hpp"
|
---|
43 | #include "Actions/ActionHistory.hpp"
|
---|
44 |
|
---|
45 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp"
|
---|
46 | #include "RandomNumbers/RandomNumberEngineFactory.hpp"
|
---|
47 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
48 |
|
---|
49 | #include "Parser/ChangeTracker.hpp"
|
---|
50 | #include "Parser/FormatParserStorage.hpp"
|
---|
51 |
|
---|
52 | #include "UIElements/UIFactory.hpp"
|
---|
53 | #include "UIElements/TextUI/TextUIFactory.hpp"
|
---|
54 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
|
---|
55 | #include "UIElements/MainWindow.hpp"
|
---|
56 | #include "UIElements/Dialog.hpp"
|
---|
57 |
|
---|
58 | #include "version.h"
|
---|
59 |
|
---|
60 | #include "builder_init.hpp"
|
---|
61 |
|
---|
62 | /** Print some initial information output the program.
|
---|
63 | *
|
---|
64 | */
|
---|
65 | void ProgramHeader()
|
---|
66 | {
|
---|
67 | // print version check and copyright notice
|
---|
68 | cout << MOLECUILDERVERSION << endl;
|
---|
69 | cout << "MoleCuilder comes with ABSOLUTELY NO WARRANTY; for details type" << endl;
|
---|
70 | cout << "`molecuilder --warranty'." << endl;
|
---|
71 | cout << "`MoleCuilder - to create and alter molecular systems." << endl;
|
---|
72 | cout << "Copyright (C) 2010 University Bonn. All rights reserved." << endl;
|
---|
73 | cout << endl;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** General stuff to intialize before UI.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | void initGeneral()
|
---|
80 | {
|
---|
81 | // while we are non interactive, we want to abort from asserts
|
---|
82 | ASSERT_DO(Assert::Abort);
|
---|
83 | ASSERT_HOOK(dumpMemory);
|
---|
84 |
|
---|
85 | ProgramHeader();
|
---|
86 |
|
---|
87 | setVerbosity(1);
|
---|
88 | // need to init the history before any action is created
|
---|
89 | ActionHistory::init();
|
---|
90 |
|
---|
91 | // from this moment on, we need to be sure to deeinitialize in the correct order
|
---|
92 | // this is handled by the cleanup function
|
---|
93 | atexit(cleanUp);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Initialize specific UIFactory.
|
---|
97 | *
|
---|
98 | * @param argc argument count
|
---|
99 | * @param argv argument array
|
---|
100 | */
|
---|
101 | void initUI(int argc, char **argv)
|
---|
102 | {
|
---|
103 | std::string BondGraphFileName("\n");
|
---|
104 | // Parse command line options and if present create respective UI
|
---|
105 | // construct bond graph
|
---|
106 | if (World::getInstance().getConfig()->BG == NULL) {
|
---|
107 | World::getInstance().getConfig()->BG = new BondGraph(World::getInstance().getConfig()->GetIsAngstroem());
|
---|
108 | if (boost::filesystem::exists(BondGraphFileName)) {
|
---|
109 | if (World::getInstance().getConfig()->BG->LoadBondLengthTable(BondGraphFileName)) {
|
---|
110 | DoLog(0) && (Log() << Verbose(0) << "Bond length table loaded successfully." << endl);
|
---|
111 | } else {
|
---|
112 | DoeLog(1) && (eLog()<< Verbose(1) << "Bond length table loading failed." << endl);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | // handle remaining arguments by CommandLineParser
|
---|
117 | if (argc>1) {
|
---|
118 | DoLog(0) && (Log() << Verbose(0) << "Setting UI to CommandLine." << endl);
|
---|
119 | CommandLineParser::getInstance().InitializeCommandArguments();
|
---|
120 | CommandLineParser::getInstance().Run(argc,argv);
|
---|
121 | UIFactory::registerFactory(new CommandLineUIFactory::description());
|
---|
122 | UIFactory::makeUserInterface("CommandLine");
|
---|
123 | } else {
|
---|
124 | // In the interactive mode, we can leave the user the choice in case of error
|
---|
125 | ASSERT_DO(Assert::Ask);
|
---|
126 | #ifdef USE_GUI_QT
|
---|
127 | DoLog(0) && (Log() << Verbose(0) << "Setting UI to Qt4." << endl);
|
---|
128 | UIFactory::registerFactory(new QtUIFactory::description());
|
---|
129 | UIFactory::makeUserInterface("Qt4");
|
---|
130 | #else
|
---|
131 | DoLog(0) && (Log() << Verbose(0) << "Setting UI to Text." << endl);
|
---|
132 | cout << MOLECUILDERVERSION << endl;
|
---|
133 | UIFactory::registerFactory(new TextUIFactory::description());
|
---|
134 | UIFactory::makeUserInterface("Text");
|
---|
135 | #endif
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Create MainWindow and displays.
|
---|
140 | * I.e. here all the Actions are parsed and done.
|
---|
141 | */
|
---|
142 | void doUI()
|
---|
143 | {
|
---|
144 | MainWindow *mainWindow = UIFactory::getInstance().makeMainWindow();
|
---|
145 | mainWindow->display();
|
---|
146 | delete mainWindow;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /** In this function all dynamicly allocated member variables to static/global
|
---|
151 | * variables are added to the ignore list of Memory/MemDebug.
|
---|
152 | *
|
---|
153 | * Use this to prevent their listing in the Memory::getState() at the end of the
|
---|
154 | * program. Check with valgrind that truely no memory leak occurs!
|
---|
155 | */
|
---|
156 | void AddStaticEntitiestoIgnoreList()
|
---|
157 | {
|
---|
158 | // zeroVec and unitVec are global variables (on the stack) but vectorContent
|
---|
159 | // within is situated on the heap and has to be ignored
|
---|
160 | Memory::ignore(zeroVec.get());
|
---|
161 | Memory::ignore(unitVec[0].get());
|
---|
162 | Memory::ignore(unitVec[1].get());
|
---|
163 | Memory::ignore(unitVec[2].get());
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Cleans all singleton instances in an orderly fashion.
|
---|
167 | * C++ does not guarantee any specific sequence of removal of single instances
|
---|
168 | * which have static/global variables. Some singletons depend on others hence we
|
---|
169 | * acertain a specific ordering here, which is is used via the atexit() hook.
|
---|
170 | */
|
---|
171 | void cleanUp()
|
---|
172 | {
|
---|
173 | RandomNumberDistributionFactory::purgeInstance();
|
---|
174 | RandomNumberEngineFactory::purgeInstance();
|
---|
175 | RandomNumberGeneratorFactory::purgeInstance();
|
---|
176 | FormatParserStorage::purgeInstance();
|
---|
177 | ChangeTracker::purgeInstance();
|
---|
178 | World::purgeInstance();
|
---|
179 | MenuDescription::purgeInstance();
|
---|
180 | UIFactory::purgeInstance();
|
---|
181 | ValueStorage::purgeInstance();
|
---|
182 | CommandLineParser::purgeInstance();
|
---|
183 | ActionRegistry::purgeInstance();
|
---|
184 | OptionRegistry::purgeInstance();
|
---|
185 | ActionHistory::purgeInstance();
|
---|
186 | // we have to remove these two static as otherwise their boost::shared_ptrs are still present
|
---|
187 | Action::removeStaticStateEntities();
|
---|
188 | // put some static variables' dynamic contents on the Memory::ignore map to avoid their
|
---|
189 | // admonishing lateron
|
---|
190 | AddStaticEntitiestoIgnoreList();
|
---|
191 | logger::purgeInstance();
|
---|
192 | errorLogger::purgeInstance();
|
---|
193 | #ifdef LOG_OBSERVER
|
---|
194 | cout << observerLog().getLog();
|
---|
195 | #endif
|
---|
196 | Memory::getState();
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** Dump current memory chunks.
|
---|
200 | *
|
---|
201 | */
|
---|
202 | void dumpMemory()
|
---|
203 | {
|
---|
204 | ofstream ost("molecuilder.memdump");
|
---|
205 | Memory::dumpMemory(ost);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /** Save the current World to output files and exit.
|
---|
209 | *
|
---|
210 | * @return retrieved from World::getExitFlag()
|
---|
211 | */
|
---|
212 | int saveAll()
|
---|
213 | {
|
---|
214 | int test[500000];
|
---|
215 | for (size_t i=0;i<500000;++i) {
|
---|
216 | test[i] = (int)i;
|
---|
217 | }
|
---|
218 | FormatParserStorage::getInstance().SaveAll();
|
---|
219 | ChangeTracker::getInstance().saveStatus();
|
---|
220 |
|
---|
221 | int ExitFlag = World::getInstance().getExitFlag();
|
---|
222 | return (ExitFlag == 1 ? 0 : ExitFlag);
|
---|
223 | }
|
---|