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