GammaLib  2.1.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-2023 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  const std::string& unit = "");
108  void log_value(const GChatter& chatter,
109  const std::string& name,
110  const int& value,
111  const std::string& unit = "");
112  void log_value(const GChatter& chatter,
113  const std::string& name,
114  const double& value,
115  const std::string& unit = "");
116  void log_header1(const GChatter& chatter,
117  const std::string& header);
118  void log_header2(const GChatter& chatter,
119  const std::string& header);
120  void log_header3(const GChatter& chatter,
121  const std::string& header);
122  void log_parameters(const GChatter& chatter);
123  const bool& need_help(void) const;
124  void statistics(const bool& statistics);
125  const bool& statistics(void) const;
126  const GApplicationPars& pars(void) const;
127  void pars(const GApplicationPars& pars);
128  const std::vector<std::string>& args(void) const;
129  void stamp(GFitsHDU& hdu) const;
130  void stamp(GFits& fits) const;
131  void stamp(const GFilename& filename) const;
132  std::string print(const GChatter& chatter = NORMAL) const;
133 
134  // Public members
135  GLog log; //!< Application logger
136 
137 #ifndef SWIG
138 protected:
139 #endif
140  // Returns the number of running instance of this method. The number
141  // of running instances has been implement as static method to avoid
142  // the static initialization order fiasco of static members; using
143  // static methods we follow the "construct on first use idiom")
144  static int& running() {
145  static int m_running = 0;
146  return m_running;
147  }
148 
149 protected:
150  // Protected methods
151  void init_members(void);
152  void copy_members(const GApplication& app);
153  void free_members(void);
154  void set_statistics(void);
155  void set_log_chatter(void);
156  void set_log_filename(void);
157  void write_statistics(void);
158 
159  // Protected data members
160  std::string m_name; //!< Application name
161  std::string m_version; //!< Application version
162  std::string m_parfile; //!< Parameter filename
163  std::string m_logfile; //!< Log filename
164  std::vector<std::string> m_args; //!< Command line arguments
165  std::time_t m_tstart; //!< Calendar start time of execution
166  double m_cstart; //!< Clock start time of execution
167  double m_celapse; //!< Internal CPU seconds counter
168  GApplicationPars m_pars; //!< Application parameters
169  bool m_pars_loaded; //!< Application parameters loaded
170  bool m_need_help; //!< --help specified
171  bool m_statistics; //!< Enable writing of statistics
172 };
173 
174 
175 /***********************************************************************//**
176  * @brief Return class name
177  *
178  * @return String containing the class name ("GApplication").
179  ***************************************************************************/
180 inline
181 std::string GApplication::classname(void) const
182 {
183  return ("GApplication");
184 }
185 
186 
187 /***********************************************************************//**
188  * @brief Parameter access operator
189  *
190  * @param[in] name Parameter name.
191  * @return Reference to application parameter.
192  *
193  * Returns a reference to the application parameter with the given @p name.
194  ***************************************************************************/
195 inline
196 GApplicationPar& GApplication::operator[](const std::string& name)
197 {
198  return (m_pars[name]);
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Parameter access operator (const version)
204  *
205  * @param[in] name Parameter name.
206  * @return Constant reference to application parameter
207  *
208  * Returns a const reference to the application parameter with the given
209  * @p name.
210  ***************************************************************************/
211 inline
212 const GApplicationPar& GApplication::operator[](const std::string& name) const
213 {
214  return (m_pars[name]);
215 }
216 
217 
218 /***********************************************************************//**
219  * @brief Parameter access operator
220  *
221  * @param[in] index Parameter index [0,...,pars().size()-1].
222  * @return Reference to application parameter
223  *
224  * Returns a reference to the application parameter with the given @p index.
225  * No range checking is performed for the index.
226  ***************************************************************************/
227 inline
229 {
230  return (m_pars[index]);
231 }
232 
233 
234 /***********************************************************************//**
235  * @brief Parameter access operator (const version)
236  *
237  * @param[in] index Parameter index [0,...,pars().size()-1].
238  * @return Constant reference to application parameter
239  *
240  * Returns a const reference to the application parameter with the given
241  * @p index. No range checking is performed for the index.
242  ***************************************************************************/
243 inline
244 const GApplicationPar& GApplication::operator[](const int& index) const
245 {
246  return (m_pars[index]);
247 }
248 
249 
250 /***********************************************************************//**
251  * @brief Return application name
252  *
253  * @return Application name.
254  ***************************************************************************/
255 inline
256 const std::string& GApplication::name(void) const
257 {
258  return m_name;
259 }
260 
261 
262 /***********************************************************************//**
263  * @brief Return application version
264  *
265  * @return Application version.
266  ***************************************************************************/
267 inline
268 const std::string& GApplication::version(void) const
269 {
270  return m_version;
271 }
272 
273 
274 /***********************************************************************//**
275  * @brief Add CPU seconds to internal counter
276  *
277  * @param[in] celapse CPU seconds.
278  *
279  * Adds the @p celapse CPU seconds to the internal CPU seconds counter.
280  ***************************************************************************/
281 inline
282 void GApplication::add_celapse(const double& celapse)
283 {
284  m_celapse += celapse;
285  return;
286 }
287 
288 
289 /***********************************************************************//**
290  * @brief Signal if specified parameter exists
291  *
292  * @param[in] name Parameter name.
293  * @return True if an application parameter with the specified name exists.
294  ***************************************************************************/
295 inline
296 bool GApplication::has_par(const std::string& name) const
297 {
298  return (m_pars.contains(name));
299 }
300 
301 
302 /***********************************************************************//**
303  * @brief Returns parameter filename
304  *
305  * @return Parameter filename.
306  ***************************************************************************/
307 inline
308 const std::string& GApplication::par_filename(void) const
309 {
310  // Return
311  return (m_parfile);
312 }
313 
314 
315 /***********************************************************************//**
316  * @brief Returns log filename
317  *
318  * @return Log filename.
319  ***************************************************************************/
320 inline
321 const std::string& GApplication::log_filename(void) const
322 {
323  // Set logfile name from parameters (in case it has changed)
324  const_cast<GApplication*>((this))->set_log_filename();
325 
326  // Return
327  return (m_logfile);
328 }
329 
330 
331 /***********************************************************************//**
332  * @brief Signals if --help option has been specified
333  *
334  * @return True if --help option has been specified.
335  ***************************************************************************/
336 inline
337 const bool& GApplication::need_help(void) const
338 {
339  // Return
340  return (m_need_help);
341 }
342 
343 
344 /***********************************************************************//**
345  * @brief Set if statistics will be written
346  *
347  * @param[in] statistics Write statistics?
348  ***************************************************************************/
349 inline
350 void GApplication::statistics(const bool& statistics)
351 {
353  return;
354 }
355 
356 
357 /***********************************************************************//**
358  * @brief Signals if statistics will be written
359  *
360  * @return True if statistics will be written.
361  ***************************************************************************/
362 inline
363 const bool& GApplication::statistics(void) const
364 {
365  // Return
366  return (m_statistics);
367 }
368 
369 
370 /***********************************************************************//**
371  * @brief Return application parameters
372  *
373  * @return Application parameters.
374  ***************************************************************************/
375 inline
377 {
378  return m_pars;
379 }
380 
381 
382 /***********************************************************************//**
383  * @brief Set application parameters
384  *
385  * @param[in] pars Application parameters.
386  ***************************************************************************/
387 inline
389 {
390  m_pars = pars;
391  return;
392 }
393 
394 
395 /***********************************************************************//**
396  * @brief Return command line arguments
397  *
398  * @return Command line arguments.
399  ***************************************************************************/
400 inline
401 const std::vector<std::string>& GApplication::args(void) const
402 {
403  return m_args;
404 }
405 
406 #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 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.
void log_value(const GChatter &chatter, const std::string &name, const std::string &value, const std::string &unit="")
Write parameter value in log file.
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.