]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/application.cpp
Invent a value for argv[0] if not present
[libs/core.git] / source / core / application.cpp
index 7e92dbdf3d552574fbb279b17abc7bf56edfbec2..aedaa4b6f25c027429f8bff1a5c069078206e7df 100644 (file)
-/*
-This file is part of libmspcore
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
+#include <cstring>
+#include <typeinfo>
 #include <signal.h>
-#include <iostream>
-#include "../time/units.h"
+#include <msp/debug/demangle.h>
+#include <msp/debug/errorreporter.h>
+#include <msp/fs/dir.h>
+#include <msp/fs/path.h>
+#include <msp/fs/utils.h>
+#include <msp/io/print.h>
 #include "application.h"
-#include "error.h"
+#include "getopt.h"
 
 using namespace std;
 
 namespace Msp {
 
-/**
-Constructs an instance of the registered application class and runs it.  If the
-application throws a UsageError, the static usage() function is called.
+Application *Application::app_ = 0;
+Application::Starter *Application::starter_ = 0;
+const char *Application::argv0_ = 0;
+string Application::name_;
+void *Application::data_ = 0;
 
-This function can only be called once.  The global main() function provided by
-the library normally does it automatically at program startup.
-*/
-int Application::run(int argc, char **argv)
+Application::Application(const string &n):
+       exit_code(0)
 {
-       static bool called=false;
-       if(called)
-       {
-               cerr<<"Trying to call Application::run_app twice!\n";
-               return 125;
-       }
-       called=true;
+       if(app_)
+               throw logic_error("instance already exists");
+
+       if(!n.empty())
+               name_ = n;
+       else
+               name_ = FS::basename(argv0_);
+}
 
-       if(!reg_app_)
+int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *))
+{
+       if(!starter_)
        {
-               cerr<<"Trying to run with no application class registered!\n";
+               IO::cerr.write("Application::run called with no RegisteredApplication class!\n");
                return 126;
        }
 
+       set_startup_info(argv[0], data);
+
        try
        {
-               app_=reg_app_->create_app(argc, argv);
+               try
+               {
+                       app_ = starter_->create_app(argc, argv);
+               }
+               catch(const usage_error &e)
+               {
+                       IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
+                       return 1;
+               }
+
+               if(created_callback)
+                       created_callback(data);
+
+               int result = app_->main();
+               Application *a = app_;
+               app_ = 0;
+               delete a;
+               return result;
        }
-       catch(const UsageError &e)
+       catch(const exception &e)
        {
-               reg_app_->usage(e.what(), argv[0], e.get_brief());
-               return 1;
-       }
+               bool handled = false;
+               if(const Debug::ErrorReporter *er = Debug::ErrorReporter::get_current())
+                       handled = er->report_uncaught_exception(e);
 
-       int result=app_->main();
-       delete app_;
-       return result;
-}
+               if(!handled)
+               {
+                       IO::print(IO::cerr, "An uncaught exception occurred.\n");
+                       IO::print(IO::cerr, "  type:   %s\n", Debug::demangle(typeid(e).name()));
+                       IO::print(IO::cerr, "  what(): %s\n", e.what());
+               }
 
-/**
-Prints a message describing the usage of the application.  The default version
-will blame the programmer for being lazy.
+               delete app_;
+               app_ = 0;
 
-@param   reason  Why the function was called
-@param   argv0   The value of argv[0], to be used in the message
-@param   brief   Whether to print a brief or long usage message
-*/
-void Application::usage(const char *reason, const char *, bool)
-{
-       if(reason)
-               cerr<<reason<<'\n';
-       cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
+               return 124;
+       }
 }
 
-Application::Application():
-       exit_code(0),
-       loop_mode_(TICK_SLEEP)
-{ }
-
-/**
-Default main loop.  Behavior depends on loop_mode_.  A custom main loop should
-monitor the done member variable and return exit_code.
-*/
-int Application::main()
+void Application::set_startup_info(const char *argv0, void *data)
 {
-       if(loop_mode_==NONE)
-               return 0;
+       if(argv0_)
+               throw logic_error("startup info already set");
 
-       done=false;
-       while(!done)
+       static FS::Path exe;
+
+       if(!argv0 || !*argv0)
        {
-               if(loop_mode_==SLEEP)
-               {
-                       sleep_sem_.wait();
-                       if(!done)
-                               tick();
-               }
-               else if(loop_mode_==TICK_SLEEP)
-               {
-                       tick();
-                       sleep(Time::msec);
-               }
-               else if(loop_mode_==TICK_YIELD)
-               {
-                       tick();
-#ifdef WIN32
-                       Sleep(0);
+#ifdef _WIN32
+               argv0 = "application.exe";
 #else
-                       sched_yield();
+               argv0 = "./application";
 #endif
-               }
        }
 
-       return exit_code;
-}
+       bool has_slash = strchr(argv0, FS::DIRSEP);
+       if(!has_slash)
+               exe = FS::path_lookup(argv0);
+       if(exe.empty())
+               exe = FS::realpath(argv0);
 
-/**
-Sets the specified signal to be delivered to the sighandler member function.
-*/
-void Application::catch_signal(int s)
-{
-       signal(s, &sighandler_);
+       argv0_ = exe.c_str();
+       data_ = data;
 }
 
-/**
-Changes the main loop mode.
-*/
-void Application::set_loop_mode(LoopMode l)
+int Application::main()
 {
-       LoopMode old_mode=loop_mode_;
-       loop_mode_=l;
-       if(old_mode==SLEEP)
-               sleep_sem_.signal();
+       done = false;
+       while(!done)
+               tick();
+
+       return exit_code;
 }
 
-/**
-Causes the tick() function to be executed once if loop mode is SLEEP.  Has no
-effect with other loop modes.
-*/
-void Application::induce_tick()
+void Application::catch_signal(int s)
 {
-       if(loop_mode_==SLEEP)
-               sleep_sem_.signal();
+       signal(s, &sighandler_);
 }
 
-/**
-Causes the application to exit gracefully with the given exit code.
-*/
 void Application::exit(int c)
 {
-       done=true;
-       exit_code=c;
-       if(loop_mode_==SLEEP)
-               sleep_sem_.signal();
+       done = true;
+       exit_code = c;
 }
 
-/**
-Static wrapper function to call a member function of the Application instance.
-*/
 void Application::sighandler_(int s)
 {
        app_->sighandler(s);
 }
 
-Application::RegBase::RegBase()
+
+Application::Starter::Starter()
 {
-       if(reg_app_)
-       {
-               cerr<<"Warning: registering the application twice\n";
-               delete reg_app_;
-       }
+       if(starter_)
+               throw logic_error("Can't create more than one Starter instance");
 
-       reg_app_=this;
+       starter_ = this;
 }
 
-Application *Application::app_=0;
-Application::RegBase *Application::reg_app_=0;
-
 } // namespace Msp