Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/builder.cpp

    rf4d063 rc82d0c  
    7373#endif
    7474
    75 #include "builder_init.hpp"
     75#include "Helpers/MemDebug.hpp"
     76
     77#include "bondgraph.hpp"
     78#include "config.hpp"
     79#include "Helpers/Log.hpp"
     80#include "molecule.hpp"
     81#include "periodentafel.hpp"
     82#include "tesselationhelpers.hpp"
     83#include "UIElements/UIFactory.hpp"
     84#include "UIElements/Menu/MenuDescription.hpp"
     85#include "UIElements/TextUI/TextUIFactory.hpp"
     86#include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
     87#include "UIElements/CommandLineUI/CommandLineParser.hpp"
     88#ifdef USE_GUI_QT
     89#include "UIElements/Qt4/QtUIFactory.hpp"
     90#endif
     91#include "UIElements/MainWindow.hpp"
     92#include "UIElements/Dialog.hpp"
     93//#include "Menu/ActionMenuItem.hpp"
     94#include "Helpers/Verbose.hpp"
     95#include "World.hpp"
     96
     97#include "Actions/ActionRegistry.hpp"
     98#include "Actions/ActionHistory.hpp"
     99
     100#include "Parser/ChangeTracker.hpp"
     101#include "Parser/FormatParserStorage.hpp"
     102
     103#include "UIElements/UIFactory.hpp"
     104#include "UIElements/TextUI/TextUIFactory.hpp"
     105#include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
     106#include "UIElements/MainWindow.hpp"
     107#include "UIElements/Dialog.hpp"
     108
     109#include "version.h"
     110
    76111
    77112/********************************************** Main routine **************************************/
     113
     114/** In this function all dynamicly allocated member variables to static/global
     115 * variables are added to the ignore list of Memory/MemDebug.
     116 *
     117 * Use this to prevent their listing in the Memory::getState() at the end of the
     118 * program. Check with valgrind that truely no memory leak occurs!
     119 */
     120void AddStaticEntitiestoIgnoreList()
     121{
     122  // zeroVec and unitVec are global variables (on the stack) but vectorContent
     123  // within is situated on the heap and has to be ignored
     124  Memory::ignore(zeroVec.get());
     125  Memory::ignore(unitVec[0].get());
     126  Memory::ignore(unitVec[1].get());
     127  Memory::ignore(unitVec[2].get());
     128}
     129
     130/** Cleans all singleton instances in an orderly fashion.
     131 * C++ does not guarantee any specific sequence of removal of single instances
     132 * which have static/global variables. Some singletons depend on others hence we
     133 * acertain a specific ordering here, which is is used via the atexit() hook.
     134 */
     135void cleanUp(){
     136  FormatParserStorage::purgeInstance();
     137  ChangeTracker::purgeInstance();
     138  World::purgeInstance();
     139  MenuDescription::purgeInstance();
     140  UIFactory::purgeInstance();
     141  ValueStorage::purgeInstance();
     142  CommandLineParser::purgeInstance();
     143  ActionRegistry::purgeInstance();
     144  OptionRegistry::purgeInstance();
     145  ActionHistory::purgeInstance();
     146  // we have to remove these two static as otherwise their boost::shared_ptrs are still present
     147  Action::removeStaticStateEntities();
     148  // put some static variables' dynamic contents on the Memory::ignore map to avoid their
     149  // admonishing lateron
     150  AddStaticEntitiestoIgnoreList();
     151  logger::purgeInstance();
     152  errorLogger::purgeInstance();
     153#ifdef LOG_OBSERVER
     154  cout << observerLog().getLog();
     155#endif
     156  Memory::getState();
     157}
     158
     159void dumpMemory(){
     160  ofstream ost("molecuilder.memdump");
     161  Memory::dumpMemory(ost);
     162}
    78163
    79164int main(int argc, char **argv)
    80165{
    81   initGeneral();
    82 
    83   initUI(argc,argv);
    84 
    85   doUI();
    86 
    87   return saveAll();
     166  // while we are non interactive, we want to abort from asserts
     167  ASSERT_DO(Assert::Abort);
     168  ASSERT_HOOK(dumpMemory);
     169  string line;
     170  char **Arguments = NULL;
     171  int ArgcSize = 0;
     172  int ExitFlag = 0;
     173  bool ArgumentsCopied = false;
     174  std::string BondGraphFileName("\n");
     175
     176  // print version check and copyright notice
     177  cout << MOLECUILDERVERSION << endl;
     178  cout << "MoleCuilder comes with ABSOLUTELY NO WARRANTY; for details type" << endl;
     179  cout << "`molecuilder --warranty'." << endl;
     180  cout << "`MoleCuilder - to create and alter molecular systems." << endl;
     181  cout << "Copyright (C) 2010  University Bonn. All rights reserved." << endl;
     182
     183  setVerbosity(0);
     184  // need to init the history before any action is created
     185  ActionHistory::init();
     186
     187  // from this moment on, we need to be sure to deeinitialize in the correct order
     188  // this is handled by the cleanup function
     189  atexit(cleanUp);
     190
     191  // Parse command line options and if present create respective UI
     192  {
     193    // construct bond graph
     194    if (World::getInstance().getConfig()->BG == NULL) {
     195      World::getInstance().getConfig()->BG = new BondGraph(World::getInstance().getConfig()->GetIsAngstroem());
     196      if (World::getInstance().getConfig()->BG->LoadBondLengthTable(BondGraphFileName)) {
     197        DoLog(0) && (Log() << Verbose(0) << "Bond length table loaded successfully." << endl);
     198      } else {
     199        DoeLog(1) && (eLog()<< Verbose(1) << "Bond length table loading failed." << endl);
     200      }
     201    }
     202    // handle remaining arguments by CommandLineParser
     203    if (argc>1) {
     204      CommandLineParser::getInstance().InitializeCommandArguments();
     205      CommandLineParser::getInstance().Run(argc,argv);
     206      DoLog(0) && (Log() << Verbose(0) << "Setting UI to CommandLine." << endl);
     207      UIFactory::registerFactory(new CommandLineUIFactory::description());
     208      UIFactory::makeUserInterface("CommandLine");
     209    } else {
     210      // In the interactive mode, we can leave the user the choice in case of error
     211      ASSERT_DO(Assert::Ask);
     212      #ifdef USE_GUI_QT
     213        DoLog(0) && (Log() << Verbose(0) << "Setting UI to Qt4." << endl);
     214        UIFactory::registerFactory(new QtUIFactory::description());
     215        UIFactory::makeUserInterface("Qt4");
     216      #else
     217        DoLog(0) && (Log() << Verbose(0) << "Setting UI to Text." << endl);
     218        cout << MOLECUILDERVERSION << endl;
     219        UIFactory::registerFactory(new TextUIFactory::description());
     220        UIFactory::makeUserInterface("Text");
     221      #endif
     222    }
     223  }
     224
     225  {
     226    MainWindow *mainWindow = UIFactory::getInstance().makeMainWindow();
     227    mainWindow->display();
     228    delete mainWindow;
     229  }
     230
     231  FormatParserStorage::getInstance().SaveAll();
     232  ChangeTracker::getInstance().saveStatus();
     233
     234  // free the new argv
     235  if (ArgumentsCopied) {
     236    for (int i=0; i<ArgcSize;i++)
     237      delete[](Arguments[i]);
     238    delete[](Arguments);
     239  }
     240  //delete[](ConfigFileName);
     241
     242  ExitFlag = World::getInstance().getExitFlag();
     243  return (ExitFlag == 1 ? 0 : ExitFlag);
    88244}
    89245
Note: See TracChangeset for help on using the changeset viewer.