GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GApplication.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GApplication.hpp - GammaLib application base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2018 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GApplication.hpp
23  * @brief GammaLib application base class
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GAPPLICATION_HPP
28 #define GAPPLICATION_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <ctime>
32 #include <vector>
33 #include <string>
34 #include "GBase.hpp"
35 #include "GLog.hpp"
36 #include "GApplicationPars.hpp"
37 
38 
39 /***********************************************************************//**
40  * @class GApplication
41  *
42  * @brief GammaLib application interface definition
43  *
44  * This class provides the base class for ftools-like executables based on
45  * GammaLib. The ftools-like executables will be implemented as derived
46  * classes, and automatically have access to task parameters and the
47  * application logger.
48  ***************************************************************************/
49 class GApplication : public GBase {
50 
51 public:
52  // Constructors and destructors
53  GApplication(void);
54  GApplication(const std::string& name,
55  const std::string& version);
56  GApplication(const std::string& name,
57  const std::string& version,
58  const GApplicationPars& pars);
59  GApplication(const std::string& name,
60  const std::string& version,
61  int argc,
62  char* argv[]);
63  GApplication(const GApplication& app);
64  ~GApplication(void);
65 
66  // Operators
67  GApplication& operator=(const GApplication& app);
68  GApplicationPar& operator[](const int& index);
69  const GApplicationPar& operator[](const int& index) const;
70  GApplicationPar& operator[](const std::string& name);
71  const GApplicationPar& operator[](const std::string& name) const;
72 
73  // Methods
74  void clear(void);
75  GApplication* clone(void) const;
76  std::string classname(void) const;
77  const std::string& name(void) const;
78  const std::string& version(void) const;
79  double telapse(void) const;
80  double celapse(void) const;
81  void logFileOpen(const bool& clobber = true);
82  void logFileClose(void);
83  bool logTerse(void) const;
84  bool logNormal(void) const;
85  bool logExplicit(void) const;
86  bool logVerbose(void) const;
87  bool logDebug(void) const;
88  bool clobber(void) const;
89  bool has_par(const std::string& name) const;
90  const std::string& par_filename(void) const;
91  const std::string& log_filename(void) const;
92  void log_header(void);
93  void log_trailer(void);
94  void log_string(const GChatter& chatter,
95  const std::string& string,
96  const bool& linefeed = true);
97  void log_value(const GChatter& chatter,
98  const std::string& name,
99  const std::string& value);
100  void log_value(const GChatter& chatter,
101  const std::string& name,
102  const int& value);
103  void log_value(const GChatter& chatter,
104  const std::string& name,
105  const double& value);
106  void log_header1(const GChatter& chatter,
107  const std::string& header);
108  void log_header2(const GChatter& chatter,
109  const std::string& header);
110  void log_header3(const GChatter& chatter,
111  const std::string& header);
112  void log_parameters(const GChatter& chatter);
113  const bool& need_help(void) const;
114  const GApplicationPars& pars(void) const;
115  void pars(const GApplicationPars& pars);
116  const std::vector<std::string>& args(void) const;
117  std::string print(const GChatter& chatter = NORMAL) const;
118 
119  // Public members
120  GLog log; //!< Application logger
121 
122 protected:
123  // Protected methods
124  void init_members(void);
125  void copy_members(const GApplication& app);
126  void free_members(void);
127  void set_log_chatter(void);
128  void set_log_filename(void);
129 
130  // Protected data members
131  std::string m_name; //!< Application name
132  std::string m_version; //!< Application version
133  std::string m_parfile; //!< Parameter filename
134  std::string m_logfile; //!< Log filename
135  std::vector<std::string> m_args; //!< Command line arguments
136  std::time_t m_tstart; //!< Calendar start time of execution
137  std::clock_t m_cstart; //!< Clock start time of execution
138  GApplicationPars m_pars; //!< Application parameters
139  bool m_pars_loaded; //!< Application parameters loaded
140  bool m_need_help; //!< --help specified
141 };
142 
143 
144 /***********************************************************************//**
145  * @brief Return class name
146  *
147  * @return String containing the class name ("GApplication").
148  ***************************************************************************/
149 inline
150 std::string GApplication::classname(void) const
151 {
152  return ("GApplication");
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Parameter access operator
158  *
159  * @param[in] name Parameter name.
160  * @return Reference to application parameter.
161  *
162  * Returns a reference to the application parameter with the given @p name.
163  ***************************************************************************/
164 inline
165 GApplicationPar& GApplication::operator[](const std::string& name)
166 {
167  return (m_pars[name]);
168 }
169 
170 
171 /***********************************************************************//**
172  * @brief Parameter access operator (const version)
173  *
174  * @param[in] name Parameter name.
175  * @return Constant reference to application parameter
176  *
177  * Returns a const reference to the application parameter with the given
178  * @p name.
179  ***************************************************************************/
180 inline
181 const GApplicationPar& GApplication::operator[](const std::string& name) const
182 {
183  return (m_pars[name]);
184 }
185 
186 
187 /***********************************************************************//**
188  * @brief Parameter access operator
189  *
190  * @param[in] index Parameter index [0,...,pars().size()-1].
191  * @return Reference to application parameter
192  *
193  * Returns a reference to the application parameter with the given @p index.
194  * No range checking is performed for the index.
195  ***************************************************************************/
196 inline
198 {
199  return (m_pars[index]);
200 }
201 
202 
203 /***********************************************************************//**
204  * @brief Parameter access operator (const version)
205  *
206  * @param[in] index Parameter index [0,...,pars().size()-1].
207  * @return Constant reference to application parameter
208  *
209  * Returns a const reference to the application parameter with the given
210  * @p index. No range checking is performed for the index.
211  ***************************************************************************/
212 inline
213 const GApplicationPar& GApplication::operator[](const int& index) const
214 {
215  return (m_pars[index]);
216 }
217 
218 
219 /***********************************************************************//**
220  * @brief Return application name
221  *
222  * @return Application name.
223  ***************************************************************************/
224 inline
225 const std::string& GApplication::name(void) const
226 {
227  return m_name;
228 }
229 
230 
231 /***********************************************************************//**
232  * @brief Return application version
233  *
234  * @return Application version.
235  ***************************************************************************/
236 inline
237 const std::string& GApplication::version(void) const
238 {
239  return m_version;
240 }
241 
242 
243 /***********************************************************************//**
244  * @brief Signal if specified parameter exists
245  *
246  * @param[in] name Parameter name.
247  * @return True if an application parameter with the specified name exists.
248  ***************************************************************************/
249 inline
250 bool GApplication::has_par(const std::string& name) const
251 {
252  return (m_pars.contains(name));
253 }
254 
255 
256 /***********************************************************************//**
257  * @brief Returns parameter filename
258  *
259  * @return Parameter filename.
260  ***************************************************************************/
261 inline
262 const std::string& GApplication::par_filename(void) const
263 {
264  // Return
265  return (m_parfile);
266 }
267 
268 
269 /***********************************************************************//**
270  * @brief Returns log filename
271  *
272  * @return Log filename.
273  ***************************************************************************/
274 inline
275 const std::string& GApplication::log_filename(void) const
276 {
277  // Set logfile name from parameters (in case it has changed)
278  const_cast<GApplication*>((this))->set_log_filename();
279 
280  // Return
281  return (m_logfile);
282 }
283 
284 
285 /***********************************************************************//**
286  * @brief Signals if --help option has been specified
287  *
288  * @return True if --help option has been specified.
289  ***************************************************************************/
290 inline
291 const bool& GApplication::need_help(void) const
292 {
293  // Return
294  return (m_need_help);
295 }
296 
297 
298 /***********************************************************************//**
299  * @brief Return application parameters
300  *
301  * @return Application parameters.
302  ***************************************************************************/
303 inline
305 {
306  return m_pars;
307 }
308 
309 
310 /***********************************************************************//**
311  * @brief Set application parameters
312  *
313  * @param[in] pars Application parameters.
314  ***************************************************************************/
315 inline
317 {
318  m_pars = pars;
319  return;
320 }
321 
322 
323 /***********************************************************************//**
324  * @brief Return command line arguments
325  *
326  * @return Command line arguments.
327  ***************************************************************************/
328 inline
329 const std::vector<std::string>& GApplication::args(void) const
330 {
331  return m_args;
332 }
333 
334 #endif /* GAPPLICATION_HPP */
GApplication(void)
Void constructor.
bool logTerse(void) const
Signal terse logging.
Application parameter container class definition.
void set_log_chatter(void)
Set chattiness of logger.
Application parameter container class.
void log_header3(const GChatter &chatter, const std::string &header)
Write header 3 in log file.
void log_value(const GChatter &chatter, const std::string &name, const std::string &value)
Write parameter value in log file.
~GApplication(void)
Destructor.
const std::string & name(void) const
Return application name.
void set_log_filename(void)
Set log filename from &quot;logfile&quot; parameter.
void copy_members(const GApplication &app)
Copy class members.
std::time_t m_tstart
Calendar start time of execution.
GApplication * clone(void) const
Clone application.
const std::string & log_filename(void) const
Returns log filename.
bool has_par(const std::string &name) const
Signal if specified parameter exists.
std::clock_t m_cstart
Clock start time of execution.
void logFileClose(void)
Close log file.
const std::string & par_filename(void) const
Returns parameter filename.
Definition of interface for all GammaLib classes.
void log_header2(const GChatter &chatter, const std::string &header)
Write header 2 in log file.
std::string m_logfile
Log filename.
void log_header(void)
Write application header in log file.
void log_string(const GChatter &chatter, const std::string &string, const bool &linefeed=true)
Write string in log file.
double celapse(void) const
Return application elapsed time in CPU seconds.
void log_trailer(void)
Write application trailer in log file.
GApplication & operator=(const GApplication &app)
Assignment operator.
bool logExplicit(void) const
Signal explicit logging.
void init_members(void)
Initialise class members.
void clear(void)
Clear application.
bool logNormal(void) const
Signal normal logging.
std::vector< std::string > m_args
Command line arguments.
Information logger interface definition.
Definition: GLog.hpp:62
void log_header1(const GChatter &chatter, const std::string &header)
Write header 1 in log file.
bool clobber(void) const
Return clobber.
std::string classname(void) const
Return class name.
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
const bool & need_help(void) const
Signals if –help option has been specified.
double telapse(void) const
Return application elapsed time in calendar seconds.
GChatter
Definition: GTypemaps.hpp:33
void free_members(void)
Delete class members.
bool m_pars_loaded
Application parameters loaded.
std::string m_version
Application version.
std::string print(const GChatter &chatter=NORMAL) const
Print application.
bool m_need_help
–help specified
const std::string & version(void) const
Return application version.
void logFileOpen(const bool &clobber=true)
Open log file.
void log_parameters(const GChatter &chatter)
Write application parameters in log file.
const GApplicationPars & pars(void) const
Return application parameters.
GLog log
Application logger.
GApplicationPars m_pars
Application parameters.
Information logger class definition.
bool logDebug(void) const
Signal debug logging.
Application parameter class.
bool logVerbose(void) const
Signal verbose logging.
GammaLib application interface definition.
GApplicationPar & operator[](const int &index)
Parameter access operator.
std::string m_name
Application name.
const std::vector< std::string > & args(void) const
Return command line arguments.
std::string m_parfile
Parameter filename.
bool contains(const std::string &name) const
Check parameter exists.