X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fapplication.cpp;h=139f94f2d88054d745ad9119b562dd7ec174d596;hb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4;hp=17611c1429ac2ef716c1687ba892b3b6356e6f74;hpb=fcd7272c28b6da5a68d41e5aac7be1dfd2eeab78;p=libs%2Fcore.git diff --git a/source/core/application.cpp b/source/core/application.cpp index 17611c1..139f94f 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -19,6 +19,10 @@ using namespace std; namespace Msp { +Application *Application::app_ = 0; +Application::Starter *Application::starter_ = 0; +void *Application::data_ = 0; + Application::Application(): exit_code(0) { } @@ -32,36 +36,38 @@ the library normally does it automatically at program startup. */ int Application::run(int argc, char **argv, void *data) { - static bool called=false; + static bool called = false; if(called) { cerr<<"Trying to call Application::run_app twice!\n"; return 125; } - called=true; + called = true; - if(!reg_app_) + if(!starter_) { - cerr<<"Trying to run with no application class registered!\n"; + cerr<<"Trying to run with no RegisteredApplication class!\n"; return 126; } - data_=data; + data_ = data; try { try { - app_=reg_app_->create_app(argc, argv); + app_ = starter_->create_app(argc, argv); } catch(const UsageError &e) { - reg_app_->usage(e.what(), argv[0], e.get_brief()); + starter_->usage(e.what(), argv[0], e.get_brief()); return 1; } - int result=app_->main(); - delete app_; + int result = app_->main(); + Application *a = app_; + app_ = 0; + delete a; return result; } catch(const exception &e) @@ -69,18 +75,18 @@ int Application::run(int argc, char **argv, void *data) delete app_; #ifdef WIN32 - string msg=Debug::demangle(typeid(e).name())+":\n"+e.what(); + string msg = Debug::demangle(typeid(e).name())+":\n"+e.what(); MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR); #else cerr<<"An uncaught exception occurred.\n"; cerr<<" type: "<(&e); + const Exception *exc = dynamic_cast(&e); if(exc && !exc->get_backtrace().get_frames().empty()) { cerr<<" backtrace:\n"; - const list &frames=exc->get_backtrace().get_frames(); + const list &frames = exc->get_backtrace().get_frames(); for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) cerr<<" "<<*i<<'\n'; } @@ -144,19 +150,12 @@ void Application::sighandler_(int s) } -Application::RegBase::RegBase() +Application::Starter::Starter() { - if(reg_app_) - { - cerr<<"Warning: registering the application twice\n"; - delete reg_app_; - } + if(starter_) + throw InvalidState("Can't create more than one Starter instance"); - reg_app_=this; + starter_ = this; } -Application *Application::app_=0; -Application::RegBase *Application::reg_app_=0; -void *Application::data_=0; - } // namespace Msp