KSquare Utilities
Application.h
Go to the documentation of this file.
1 /* Application.h -- Generic Application class.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 #ifndef _APPLICATION_
7 #define _APPLICATION_
8 
9 #include "KKStr.h"
10 #include "RunLog.h"
11 
12 
13 
14 
15 namespace KKB
16 {
17  /**
18  *@class Application
19  *@brief The base class for all standalone application.
20  *@details This class is meant to be a general class that all standalone applications should be inherited
21  * from. It supports command line processing, and logging facilities. Derived classes will need to override
22  * the virtual methods "ApplicationName", "DisplayCommandLineParameters", and "ProcessCmdLineParameter". Right
23  * after an instance is created you need to call the "InitalizeApplication" method; this will start the processing
24  * of any encountered parameters. For each set of parameters encountered a call to "ProcessCmdLineParameter"
25  * will be made; this is where you can intercept and process parameters that are specific to your application.
26  * Any parameters that you do not recognized should be passed onto the base-class by calling
27  * "Application::ProcessCmdLineParameter".
28  */
30  {
31  public:
32  /**
33  *@brief Constructor for Application class that will start with a default logger(RunLog),
34  *@details After creating an instance of this class you initialize it by calling InitalizeApplication.
35  */
36  Application ();
37 
38 
39  /**
40  *@brief Copy Constructor for Application class.
41  *@param[in] _application Application instance to copy.
42  */
43  Application (const Application& _application);
44 
45 
46  /**
47  *@brief Constructor for Application class where we already have an existing logger '_log'.
48  *@details After creating an instance of this class you initialize it by calling InitalizeApplication.
49  *@param[in] _log A reference to a RunLog object.
50  */
51  Application (RunLog& _log);
52 
53 
54  virtual
55  ~Application ();
56 
57  /**
58  *@brief Returns 'true' if application has been aborted.
59  *@details You would typically call this method after you are done processing the command line to
60  * make sure that the application is to keep on running.
61  */
62  bool Abort () {return abort;}
63 
64  /**
65  *@brief Used to specify that the application is been aborted.
66  *@details If you have a reason to abort the processing of this application you would call this method to set the 'abort'
67  * flag to true. It will be the responsibility of the derived class to monitor the 'Abort' flag. If is set to
68  * true they should terminate as quickly as they can; they should also release any resources they have taken.
69  *@param[in] _abort Abort status to set; if set to true you are telling the application that the program needs to be terminated.
70  */
71  void Abort (bool _abort) {abort = _abort;}
72 
73  /** Specify the name of the application */
74  virtual
75  const char* ApplicationName ();
76 
77  void AssignLog (RunLog& _log); /**< @brief Replaces the Log file to write to. */
78 
79  virtual
80  KKStr BuildDate () const;
81 
82 
83 
84  /**
85  *@brief Initialized Application Instance; 1st method to be called after instance construction.
86  *@details This method will scan the command line parameters for the log file options (-L, -Log, or -LogFile) and use its
87  * parameter as the LogFile name. If none is provided it will assume the stdout as the Log File to write to. It will take
88  * ownership of this log file and delete it in its destructor. Right after calling this constructor you will need to
89  * call the method ProcessCmdLineParameters.
90  *@see RunLog
91  *@see ProcessCmdLineParameters
92  *@param[in] argc Number of arguments in argv.
93  *@param[in] argv List of asciiz strings; one string for each argument.
94  */
95  virtual
96  void InitalizeApplication (kkint32 argc,
97  char** argv
98  );
99 
100 
101  protected:
102  /**
103  *@brief Will display Command Lone parameters that the 'Application' class will manage.
104  *@details Derived classes that implement this method need to call their immediate base class version of this method
105  * to include these parameters.
106  */
107  virtual
109 
110 
111  /**
112  *@brief This method will get called once for each parameter specified in the command line.
113  *@details Derived classes should define this method to intercept parameters that they are interested in.
114  * Parameters are treated as pairs, Switch and Values where switches have a leading dash("-").
115  * The CmdLineExpander class will be used to expand any "-CmdFile" parameters.
116  *@param[in] parmSwitch The fill switch parameter.
117  *@param[in] parmValue Any value parameter that followed the switch parameter.
118  */
119  virtual
120  bool ProcessCmdLineParameter (const KKStr& parmSwitch,
121  const KKStr& parmValue
122  );
123 
124 
125  private:
126  /**
127  *@brief Processes all the command line parameters; will also expand the -CmdFile option.
128  *@details This method assumes that the command line consists of pairs of Switches and Operands. Switches are proceeded by the
129  * dash character("-"). For each pair it will call the 'ProcessCmdLineParameter' method which should be implemented by
130  * the derived class. Before calling 'ProcessCmdLineParameter' though it will scan the parameters for the "-CmdFile"
131  * switch. This switch specifies a text file that is to be read for additional command parameters.
132  *@param[in] argc Number of parameters.
133  *@param[in] argv The actual parameters.
134  */
135  void ProcessCmdLineParameters (kkint32 argc,
136  char** argv
137  );
138 
139 
140  bool abort;
141 
142  public:
144 
145  private:
146  std::vector<KKStrPair> expandedParameterPairs;
147  KKStr logFileName;
148  RunLogPtr ourLog; // We use this Log file if one is not provided,
149  }; /* Application */
150 } /* NameSpace KKB */
151 
152 #endif
Application(const Application &_application)
Copy Constructor for Application class.
Definition: Application.cpp:51
__int32 kkint32
Definition: KKBaseTypes.h:88
virtual void DisplayCommandLineParameters()
Will display Command Lone parameters that the &#39;Application&#39; class will manage.
void Abort(bool _abort)
Used to specify that the application is been aborted.
Definition: Application.h:71
virtual const char * ApplicationName()
Definition: Application.cpp:80
Application()
Constructor for Application class that will start with a default logger(RunLog),. ...
Definition: Application.cpp:39
virtual void InitalizeApplication(kkint32 argc, char **argv)
Initialized Application Instance; 1st method to be called after instance construction.
Definition: Application.cpp:69
Application(RunLog &_log)
Constructor for Application class where we already have an existing logger &#39;_log&#39;.
Definition: Application.cpp:29
virtual bool ProcessCmdLineParameter(const KKStr &parmSwitch, const KKStr &parmValue)
This method will get called once for each parameter specified in the command line.
KKTHread * KKTHreadPtr
virtual ~Application()
Definition: Application.cpp:61
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
virtual KKStr BuildDate() const
RunLog * RunLogPtr
Definition: RunLog.h:198
The base class for all standalone application.
Definition: Application.h:29
Used for logging messages.
Definition: RunLog.h:49
void AssignLog(RunLog &_log)
Replaces the Log file to write to.
Definition: Application.cpp:87
bool Abort()
Returns &#39;true&#39; if application has been aborted.
Definition: Application.h:62