application.h
Go to the documentation of this file.
00001 //
00002 //  Copyright (c) Benjamin Kaufmann 2004
00003 //
00004 //  This is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version. 
00008 // 
00009 //  This file is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this file; if not, write to the Free Software
00016 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 //
00018 //
00019 // NOTE: ProgramOptions is inspired by Boost.Program_options
00020 //       see: www.boost.org/libs/program_options
00021 //
00022 #ifndef APP_OPTIONS_H_INCLUDED
00023 #define APP_OPTIONS_H_INCLUDED
00024 
00025 #ifdef _MSC_VER
00026 #pragma warning (disable : 4200) // nonstandard extension used : zero-sized array
00027 #pragma once
00028 #endif
00029 
00030 #include <string>
00031 #include <utility>
00032 #include "program_options.h"
00033 #include <stdio.h>
00034 #include <signal.h>
00035 
00036 namespace ProgramOptions {
00037 typedef std::vector<std::string> StringSeq;
00039 // Application base class
00041 #define WRITE_STDERR(type,sys,msg) ( fflush(stdout), fprintf(stderr, "*** %-5s: (%s): %s\n", (type),(sys),(msg)), fflush(stderr) )
00042 class Application {
00043 public:
00045         typedef std::pair<const char*, unsigned> HelpOpt;
00050 
00051         virtual const char* getName()       const   = 0;
00053         virtual const char* getVersion()    const   = 0;
00055         virtual const int*  getSignals()    const { return 0; }
00057         virtual const char* getUsage()      const { return "[options]"; }
00059         virtual HelpOpt     getHelpOption() const { return HelpOpt("Print help information and exit", 1); }
00061         virtual PosOption   getPositional() const { return 0; }
00063         virtual void        error(const char* msg) const { WRITE_STDERR("ERROR", getName(), msg); }
00065         virtual void        info(const char*  msg) const { WRITE_STDERR("Info",  getName(), msg); }
00067         virtual void        warn(const char*  msg) const { WRITE_STDERR("Warn",  getName(), msg); }
00069         
00074 
00075         int main(int argc, char** argv);
00077         void setExitCode(int n);
00079         int  getExitCode() const;
00081         static Application* getInstance();
00083         virtual void        printHelp(const OptionContext& root);
00085         virtual void        printVersion();
00087         virtual void        printUsage();
00089 protected:
00094 
00095         virtual void        initOptions(OptionContext& root) = 0;
00097         virtual void        validateOptions(const OptionContext& root, const ParsedOptions& parsed, const ParsedValues& values) = 0;
00099         virtual void        setup() = 0;
00101         virtual void        run()   = 0;
00103         virtual void        shutdown();
00105         virtual void        onUnhandledException();
00107         virtual bool        onSignal(int);
00109 protected:
00110         Application();
00111         virtual ~Application();
00112         void     shutdown(bool hasError);
00113         void     exit(int exitCode) const;
00114         unsigned verbose() const;
00115         void     setVerbose(unsigned v);
00116         void     killAlarm();
00117         int      blockSignals();
00118         void     unblockSignals(bool deliverPending);
00119         void     processSignal(int sigNum);
00120 private:
00121         bool getOptions(int argc, char** argv);
00122         int                   exitCode_;  // application's exit code
00123         unsigned              timeout_;   // active time limit or 0 for no limit
00124         unsigned              verbose_;   // active verbosity level
00125         bool                  fastExit_;  // force fast exit?
00126         volatile sig_atomic_t blocked_;   // temporarily block signals?
00127         volatile sig_atomic_t pending_;   // pending signal or 0 if no pending signal
00128         static Application*   instance_s; // running instance (only valid during run()).
00129         static void sigHandler(int sig);  // signal/timeout handler
00130 };
00131 
00132 }
00133 #endif


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:38