GammaLib 2.0.0
Loading...
Searching...
No Matches
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 _______________________________________________ */
39class GFitsHDU;
40class GFits;
41class 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 ***************************************************************************/
54class GApplication : public GBase {
55
56public:
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
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
135protected:
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
146protected:
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 ***************************************************************************/
177inline
178std::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 ***************************************************************************/
192inline
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 ***************************************************************************/
208inline
209const 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 ***************************************************************************/
224inline
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 ***************************************************************************/
240inline
241const 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 ***************************************************************************/
252inline
253const 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 ***************************************************************************/
264inline
265const 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 ***************************************************************************/
278inline
279void GApplication::add_celapse(const double& celapse)
280{
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 ***************************************************************************/
292inline
293bool 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 ***************************************************************************/
304inline
305const 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 ***************************************************************************/
317inline
318const 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 ***************************************************************************/
333inline
334const 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 ***************************************************************************/
346inline
347void 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 ***************************************************************************/
359inline
360const 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 ***************************************************************************/
372inline
374{
375 return m_pars;
376}
377
378
379/***********************************************************************//**
380 * @brief Set application parameters
381 *
382 * @param[in] pars Application parameters.
383 ***************************************************************************/
384inline
386{
387 m_pars = pars;
388 return;
389}
390
391
392/***********************************************************************//**
393 * @brief Return command line arguments
394 *
395 * @return Command line arguments.
396 ***************************************************************************/
397inline
398const std::vector<std::string>& GApplication::args(void) const
399{
400 return m_args;
401}
402
403#endif /* GAPPLICATION_HPP */
Application parameter container class definition.
Definition of interface for all GammaLib classes.
Information logger class definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Application parameter class.
Application parameter container class.
bool contains(const std::string &name) const
Check parameter exists.
GammaLib application interface definition.
void log_string(const GChatter &chatter, const std::string &string, const bool &linefeed=true)
Write string in log file.
GLog log
Application logger.
bool m_need_help
–help specified
double gCO2e(const std::string &country) const
Return application equivalent CO2 footprint (units: g CO2e)
void write_statistics(void)
Write application statistics.
void init_members(void)
Initialise class members.
std::vector< std::string > m_args
Command line arguments.
void set_log_chatter(void)
Set chattiness of logger.
void log_value(const GChatter &chatter, const std::string &name, const std::string &value)
Write parameter value in log file.
GApplication(void)
Void constructor.
std::string m_version
Application version.
void log_parameters(const GChatter &chatter)
Write application parameters in log file.
bool logTerse(void) const
Signal terse logging.
const bool & need_help(void) const
Signals if –help option has been specified.
const bool & statistics(void) const
Signals if statistics will be written.
std::string m_parfile
Parameter filename.
GApplicationPars m_pars
Application parameters.
static int & running()
bool clobber(void) const
Return clobber.
std::string print(const GChatter &chatter=NORMAL) const
Print application.
void copy_members(const GApplication &app)
Copy class members.
const std::string & log_filename(void) const
Returns log filename.
std::time_t m_tstart
Calendar start time of execution.
bool has_par(const std::string &name) const
Signal if specified parameter exists.
void stamp(GFitsHDU &hdu) const
Stamp FITS header with provenance information.
void add_celapse(const double &celapse)
Add CPU seconds to internal counter.
void set_log_filename(void)
Set log filename from "logfile" parameter.
bool m_pars_loaded
Application parameters loaded.
GApplication * clone(void) const
Clone application.
void logFileClose(void)
Close log file.
std::string m_logfile
Log filename.
void log_trailer(void)
Write application trailer in log file.
void clear(void)
Clear application.
void logFileOpen(const bool &clobber=true)
Open log file.
const std::vector< std::string > & args(void) const
Return command line arguments.
std::string m_name
Application name.
double celapse(void) const
Return application elapsed time in CPU seconds.
bool logNormal(void) const
Signal normal logging.
const std::string & version(void) const
Return application version.
const GApplicationPars & pars(void) const
Return application parameters.
void log_header2(const GChatter &chatter, const std::string &header)
Write header 2 in log file.
void set_statistics(void)
Set statistics according to parent child status.
void free_members(void)
Delete class members.
GApplicationPar & operator[](const int &index)
Parameter access operator.
void log_header3(const GChatter &chatter, const std::string &header)
Write header 3 in log file.
void log_header1(const GChatter &chatter, const std::string &header)
Write header 1 in log file.
bool m_statistics
Enable writing of statistics.
bool logDebug(void) const
Signal debug logging.
const std::string & par_filename(void) const
Returns parameter filename.
void log_header(void)
Write application header in log file.
double telapse(void) const
Return application elapsed time in calendar seconds.
~GApplication(void)
Destructor.
const std::string & name(void) const
Return application name.
std::string classname(void) const
Return class name.
bool logExplicit(void) const
Signal explicit logging.
GApplication & operator=(const GApplication &app)
Assignment operator.
bool logVerbose(void) const
Signal verbose logging.
double m_cstart
Clock start time of execution.
double m_celapse
Internal CPU seconds counter.
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Filename class.
Definition GFilename.hpp:62
Abstract FITS extension base class.
Definition GFitsHDU.hpp:51
FITS file class.
Definition GFits.hpp:63
Information logger interface definition.
Definition GLog.hpp:62