Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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_;
00123 unsigned timeout_;
00124 unsigned verbose_;
00125 bool fastExit_;
00126 volatile sig_atomic_t blocked_;
00127 volatile sig_atomic_t pending_;
00128 static Application* instance_s;
00129 static void sigHandler(int sig);
00130 };
00131
00132 }
00133 #endif