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