00001 #pragma ident "$Id: Application.hpp 2974 2011-11-11 09:14:39Z yanweignss $"
00002
00008 #ifndef GPSTK_APPLICATION_HPP
00009 #define GPSTK_APPLICATION_HPP
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <string>
00034 #include <vector>
00035 #include "AppOption.hpp"
00036 #include "Logger.hpp"
00037 #include "DayTime.hpp"
00038 #include "Exception.hpp"
00039
00040 namespace gpstk
00041 {
00045 class Application
00046 {
00047 public:
00048
00049
00051 enum ExitCode
00052 {
00053 EXIT_OK = 0,
00054 EXIT_USAGE = 64,
00055 EXIT_DATAERR = 65,
00056 EXIT_NOINPUT = 66,
00057 EXIT_NOUSER = 67,
00058 EXIT_NOHOST = 68,
00059 EXIT_UNAVAILABLE = 69,
00060 EXIT_SOFTWARE = 70,
00061 EXIT_OSERR = 71,
00062 EXIT_OSFILE = 72,
00063 EXIT_CANTCREAT = 73,
00064 EXIT_IOERR = 74,
00065 EXIT_TEMPFAIL = 75,
00066 EXIT_PROTOCOL = 76,
00067 EXIT_NOPERM = 77,
00068 EXIT_CONFIG = 78
00069 };
00070
00071 public:
00072
00073 Application(const std::string& author="GPSTk",
00074 const std::string& version="1.0.0",
00075 const std::string& desc="",
00076 const std::string& useage="[options] ...");
00077
00078 Application(int argc, char* argv[]);
00079
00080 virtual ~Application(){}
00081
00082 void init(int argc, char* argv[]);
00083
00084 virtual int run();
00085
00086 Logger& logger() const;
00087
00088 LogStream logstream();
00089
00090 Application& version(const std::string& version="1.0.0")
00091 { appVersion = version; return (*this); }
00092
00093 Application& description(const std::string& desc="")
00094 { appDesc = desc; return (*this); }
00095
00096 Application& useage(const std::string& usage="[options] ...")
00097 { appUsage = usage; return (*this); }
00098
00099 Application& author(const std::string& aut="")
00100 { appAuthor = aut; return (*this); }
00101
00102 std::string name() { return appName; }
00103
00104 std::string version() { return appVersion; }
00105
00106 std::string description() { return appDesc; }
00107
00108 std::string useage() { return appUsage; }
00109
00110 std::string author() { return appAuthor; }
00111
00112 Application& appInfo(const std::string& author,
00113 const std::string& version="1.0.0",
00114 const std::string& description="",
00115 const std::string& useage="[options] <file> ...");
00116
00117 const OptionSet& options() const;
00118
00119 double toltalMilliseconds();
00120
00121 protected:
00122
00123 virtual void setupOptions(OptionSet& options){}
00124
00125 virtual void spinUp(){}
00126
00127 virtual void process(const std::vector<std::string>& args){}
00128
00129 virtual void shutDown(){}
00130
00131 protected:
00132 std::string commandName() const;
00133
00134 void handleOption(const std::string& name,
00135 const std::string& value);
00136
00137 void defineOptions(OptionSet& options);
00138
00139
00140 void handleDefaultOptions(const std::string& name,
00141 const std::string& value);
00142
00143 void stopOptionsProcessing();
00144
00145 void processOptions();
00146
00147 void initialize(Application& self);
00148
00149 int main(const std::vector<std::string>& args);
00150
00151 void uninitialize();
00152
00153 protected:
00154 static Application* _pInstance;
00155 bool _initialized;
00156
00157 std::string _command;
00158 std::vector<std::string> _args;
00159
00160 OptionSet _options;
00161 bool _stopOptionsProcessing;
00162
00163 bool helpRequested;
00164 bool unixStyle;
00165
00166 std::string appName;
00167 std::string appVersion;
00168 std::string appDesc;
00169 std::string appAuthor;
00170 std::string appUsage;
00171
00172 int verboseLevel;
00173
00174 DayTime runTime;
00175
00176 };
00177
00178
00179 #define GPSTK_APP_MAIN(App) \
00180 int main(int argc, char* argv[]) \
00181 { \
00182 App app; \
00183 try \
00184 { \
00185 app.init(argc, argv); \
00186 } \
00187 catch (Exception* e) \
00188 { \
00189 GPSTK_ERROR("",e->what()); \
00190 return Application::EXIT_CONFIG; \
00191 } \
00192 catch (std::exception* e) \
00193 { \
00194 GPSTK_ERROR("",e->what()); \
00195 return Application::EXIT_CONFIG; \
00196 } \
00197 return app.run(); \
00198 }
00199
00200 }
00201
00202
00203 #endif //GPSTK_APPLICATION_HPP
00204