GammaLib  2.0.0
 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-2022 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 /* __ Forward declarations _______________________________________________ */
39 class GFitsHDU;
40 class GFits;
41 class GFilename;
42 
43 
44 /***********************************************************************//**
45  * @class GApplication
46  *
47  * @brief GammaLib application interface definition
48  *
49  * This class provides the base class for ftools-like executables based on
50  * GammaLib. The ftools-like executables will be implemented as derived
51  * classes, and automatically have access to task parameters and the
52  * application logger.
53  ***************************************************************************/
54 class GApplication : public GBase {
55 
56 public:
57  // Constructors and destructors
58  GApplication(void);
59  GApplication(const std::string& name,
60  const std::string& version);
61  GApplication(const std::string& name,
62  const std::string& version,
63  const GApplicationPars& pars);
64  GApplication(const std::string& name,
65  const std::string& version,
66  int argc,
67  char* argv[]);
68  GApplication(const GApplication& app);
69  ~GApplication(void);
70 
71  // Operators
72  GApplication& operator=(const GApplication& app);
73  GApplicationPar& operator[](const int& index);
74  const GApplicationPar& operator[](const int& index) const;
75  GApplicationPar& operator[](const std::string& name);
76  const GApplicationPar& operator[](const std::string& name) const;
77 
78  // Public methods
79  void clear(void);
80  GApplication* clone(void) const;
81  std::string classname(void) const;
82  const std::string& name(void) const;
83  const std::string& version(void) const;
84  double telapse(void) const;
85  double celapse(void) const;
86  double gCO2e(const std::string& country) const;
87  void add_celapse(const double& celapse);
88  void logFileOpen(const bool& clobber = true);
89  void logFileClose(void);
90  bool logTerse(void) const;
91  bool logNormal(void) const;
92  bool logExplicit(void) const;
93  bool logVerbose(void) const;
94  bool logDebug(void) const;
95  bool clobber(void) const;
96  bool has_par(const std::string& name) const;
97  const std::string& par_filename(void) const;
98  const std::string& log_filename(void) const;
99  void log_header(void);
100  void log_trailer(void);
101  void log_string(const GChatter& chatter,
102  const std::string& string,
103  const bool& linefeed = true);
104  void log_value(const GChatter& chatter,
105  const std::string& name,
106  const std::string& value);
107  void log_value(const GChatter& chatter,
108  const std::string& name,
109  const int& value);
110  void log_value(const GChatter& chatter,
111  const std::string& name,
112  const double& value);
113  void log_header1(const GChatter& chatter,
114  const std::string& header);
115  void log_header2(const GChatter& chatter,
116  const std::string& header);
117  void log_header3(const GChatter& chatter,
118  const std::string& header);
119  void log_parameters(const GChatter& chatter);
120  const bool& need_help(void) const;
121  void statistics(const bool& statistics);
122  const bool& statistics(void) const;
123  const GApplicationPars& pars(void) const;
124  void pars(const GApplicationPars& pars);
125  const std::vector<std::string>& args(void) const;
126  void stamp(GFitsHDU& hdu) const;
127  void stamp(GFits& fits) const;
128  void stamp(const GFilename& filename) const;
129  std::string print(const GChatter& chatter = NORMAL) const;
130 
131  // Public members
132  GLog log; //!< Application logger
133 
134 #ifndef SWIG
135 protected:
136 #endif
137  // Returns the number of running instance of this method. The number
138  // of running instances has been implement as static method to avoid
139  // the static initialization order fiasco of static members; using
140  // static methods we follow the "construct on first use idiom")
141  static int& running() {
142  static int m_running = 0;
143  return m_running;
144  }
145 
146 protected:
147  // Protected methods
148  void init_members(void);
149  void copy_members(const GApplication& app);
150  void free_members(void);
151  void set_statistics(void);
152  void set_log_chatter(void);
153  void set_log_filename(void);
154  void write_statistics(void);
155 
156  // Protected data members
157  std::string m_name; //!< Application name
158  std::string m_version; //!< Application version
159  std::string m_parfile; //!< Parameter filename
160  std::string m_logfile; //!< Log filename
161  std::vector<std::string> m_args; //!< Command line arguments
162  std::time_t m_tstart; //!< Calendar start time of execution
163  double m_cstart; //!< Clock start time of execution
164  double m_celapse; //!< Internal CPU seconds counter
165  GApplicationPars m_pars; //!< Application parameters
166  bool m_pars_loaded; //!< Application parameters loaded
167  bool m_need_help; //!< --help specified
168  bool m_statistics; //!< Enable writing of statistics
169 };
170 
171 
172 /***********************************************************************//**
173  * @brief Return class name
174  *
175  * @return String containing the class name ("GApplication").
176  ***************************************************************************/
177 inline
178 std::string GApplication::classname(void) const
179 {
180  return ("GApplication");
181 }
182 
183 
184 /***********************************************************************//**
185  * @brief Parameter access operator
186  *
187  * @param[in] name Parameter name.
188  * @return Reference to application parameter.
189  *
190  * Returns a reference to the application parameter with the given @p name.
191  ***************************************************************************/
192 inline
193 GApplicationPar& GApplication::operator[](const std::string& name)
194 {
195  return (m_pars[name]);
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief Parameter access operator (const version)
201  *
202  * @param[in] name Parameter name.
203  * @return Constant reference to application parameter
204  *
205  * Returns a const reference to the application parameter with the given
206  * @p name.
207  ***************************************************************************/
208 inline
209 const GApplicationPar& GApplication::operator[](const std::string& name) const
210 {
211  return (m_pars[name]);
212 }
213 
214 
215 /***********************************************************************//**
216  * @brief Parameter access operator
217  *
218  * @param[in] index Parameter index [0,...,pars().size()-1].
219  * @return Reference to application parameter
220  *
221  * Returns a reference to the application parameter with the given @p index.
222  * No range checking is performed for the index.
223  ***************************************************************************/
224 inline
226 {
227  return (m_pars[index]);
228 }
229 
230 
231 /***********************************************************************//**
232  * @brief Parameter access operator (const version)
233  *
234  * @param[in] index Parameter index [0,...,pars().size()-1].
235  * @return Constant reference to application parameter
236  *
237  * Returns a const reference to the application parameter with the given
238  * @p index. No range checking is performed for the index.
239  ***************************************************************************/
240 inline
241 const GApplicationPar& GApplication::operator[](const int& index) const
242 {
243  return (m_pars[index]);
244 }
245 
246 
247 /***********************************************************************//**
248  * @brief Return application name
249  *
250  * @return Application name.
251  ***************************************************************************/
252 inline
253 const std::string& GApplication::name(void) const
254 {
255  return m_name;
256 }
257 
258 
259 /***********************************************************************//**
260  * @brief Return application version
261  *
262  * @return Application version.
263  ***************************************************************************/
264 inline
265 const std::string& GApplication::version(void) const
266 {
267  return m_version;
268 }
269 
270 
271 /***********************************************************************//**
272  * @brief Add CPU seconds to internal counter
273  *
274  * @param[in] celapse CPU seconds.
275  *
276  * Adds the @p celapse CPU seconds to the internal CPU seconds counter.
277  ***************************************************************************/
278 inline
279 void GApplication::add_celapse(const double& celapse)
280 {
281  m_celapse += celapse;
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Signal if specified parameter exists
288  *
289  * @param[in] name Parameter name.
290  * @return True if an application parameter with the specified name exists.
291  ***************************************************************************/
292 inline
293 bool GApplication::has_par(const std::string& name) const
294 {
295  return (m_pars.contains(name));
296 }
297 
298 
299 /***********************************************************************//**
300  * @brief Returns parameter filename
301  *
302  * @return Parameter filename.
303  ***************************************************************************/
304 inline
305 const std::string& GApplication::par_filename(void) const
306 {
307  // Return
308  return (m_parfile);
309 }
310 
311 
312 /***********************************************************************//**
313  * @brief Returns log filename
314  *
315  * @return Log filename.
316  ***************************************************************************/
317 inline
318 const std::string& GApplication::log_filename(void) const
319 {
320  // Set logfile name from parameters (in case it has changed)
321  const_cast<GApplication*>((this))->set_log_filename();
322 
323  // Return
324  return (m_logfile);
325 }
326 
327 
328 /***********************************************************************//**
329  * @brief Signals if --help option has been specified
330  *
331  * @return True if --help option has been specified.
332  ***************************************************************************/
333 inline
334 const bool& GApplication::need_help(void) const
335 {
336  // Return
337  return (m_need_help);
338 }
339 
340 
341 /***********************************************************************//**
342  * @brief Set if statistics will be written
343  *
344  * @param[in] statistics Write statistics?
345  ***************************************************************************/
346 inline
347 void GApplication::statistics(const bool& statistics)
348 {
350  return;
351 }
352 
353 
354 /***********************************************************************//**
355  * @brief Signals if statistics will be written
356  *
357  * @return True if statistics will be written.
358  ***************************************************************************/
359 inline
360 const bool& GApplication::statistics(void) const
361 {
362  // Return
363  return (m_statistics);
364 }
365 
366 
367 /***********************************************************************//**
368  * @brief Return application parameters
369  *
370  * @return Application parameters.
371  ***************************************************************************/
372 inline
374 {
375  return m_pars;
376 }
377 
378 
379 /***********************************************************************//**
380  * @brief Set application parameters
381  *
382  * @param[in] pars Application parameters.
383  ***************************************************************************/
384 inline
386 {
387  m_pars = pars;
388  return;
389 }
390 
391 
392 /***********************************************************************//**
393  * @brief Return command line arguments
394  *
395  * @return Command line arguments.
396  ***************************************************************************/
397 inline
398 const std::vector<std::string>& GApplication::args(void) const
399 {
400  return m_args;
401 }
402 
403 #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.
void write_statistics(void)
Write application statistics.
~GApplication(void)
Destructor.
static int & running()
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.
double m_celapse
Internal CPU seconds counter.
std::time_t m_tstart
Calendar start time of execution.
const bool & statistics(void) const
Signals if statistics will be written.
GApplication * clone(void) const
Clone application.
Abstract FITS extension base class.
Definition: GFitsHDU.hpp:51
const std::string & log_filename(void) const
Returns log filename.
bool has_par(const std::string &name) const
Signal if specified parameter exists.
void logFileClose(void)
Close log file.
double m_cstart
Clock start time of execution.
const std::string & par_filename(void) const
Returns parameter filename.
Definition of interface for all GammaLib classes.
void add_celapse(const double &celapse)
Add CPU seconds to internal counter.
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.
FITS file class.
Definition: GFits.hpp:63
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 stamp(GFitsHDU &hdu) const
Stamp FITS header with provenance information.
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.
Filename class.
Definition: GFilename.hpp:62
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.
void set_statistics(void)
Set statistics according to parent child status.
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.
bool m_statistics
Enable writing of statistics.
const std::vector< std::string > & args(void) const
Return command line arguments.
double gCO2e(const std::string &country) const
Return application equivalent CO2 footprint (units: g CO2e)
std::string m_parfile
Parameter filename.
bool contains(const std::string &name) const
Check parameter exists.