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