GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GApplicationPars.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GApplicationPars.hpp - Application parameters *
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 GApplicationPars.hpp
23  * @brief Application parameter container class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GAPPLICATIONPARS_HPP
28 #define GAPPLICATIONPARS_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include <string>
33 #include "GContainer.hpp"
34 #include "GApplicationPar.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 class GFilename;
38 
39 
40 /***********************************************************************//**
41  * @class GApplicationPars
42  *
43  * @brief Application parameter container class
44  *
45  * This class holds a collection of application parameters.
46  ***************************************************************************/
47 class GApplicationPars : public GContainer {
48 
49  // Friend classes
50  friend class GApplication;
51 
52 public:
53  // Constructors and destructors
54  GApplicationPars(void);
55  explicit GApplicationPars(const GFilename& filename);
56  GApplicationPars(const GFilename& filename,
57  const std::vector<std::string>& args);
59  virtual ~GApplicationPars(void);
60 
61  // Operators
63  GApplicationPar& operator[](const int& index);
64  const GApplicationPar& operator[](const int& index) const;
65  GApplicationPar& operator[](const std::string& name);
66  const GApplicationPar& operator[](const std::string& name) const;
67 
68  // Methods
69  void clear(void);
70  GApplicationPars* clone(void) const;
71  std::string classname(void) const;
72  GApplicationPar& at(const int& index);
73  const GApplicationPar& at(const int& index) const;
74  int size(void) const;
75  bool is_empty(void) const;
77  void append_standard(void);
78  GApplicationPar& insert(const int& index, const GApplicationPar& par);
79  GApplicationPar& insert(const std::string& name,
80  const GApplicationPar& par);
81  void remove(const int& index);
82  void remove(const std::string& name);
83  void reserve(const int& num);
84  void extend(const GApplicationPars& pars);
85  bool contains(const std::string& name) const;
86  void syspfiles(const std::string& syspfiles);
87  const std::string& syspfiles(void) const;
88  void load(const GFilename& filename);
89  void load(const GFilename& filename,
90  const std::vector<std::string>& args);
91  void save(const GFilename& filename);
92  void pickle(const std::vector<std::string>& string);
93  std::vector<std::string> pickle(void) const;
94  std::string print(const GChatter& chatter = NORMAL) const;
95 
96 protected:
97  // Protected methods
98  void init_members(void);
99  void copy_members(const GApplicationPars& pars);
100  void free_members(void);
101  std::string inpath(const std::string& filename) const;
102  std::string pfiles_path(const std::string& filename) const;
103  std::string syspfiles_path(const std::string& filename) const;
104  std::string outpath(const std::string& filename) const;
105  void read(const std::string& filename);
106  void write(const std::string& filename) const;
107  void parse(void);
108  void update(void);
109  void synchronise(const std::string& filename);
110  int get_index(const std::string& name) const;
111  std::string parline(GApplicationPar& par, size_t* start, size_t* stop) const;
112 
113  // Protected data members
114  std::vector<std::string> m_parfile; //!< Parameter file lines
115  std::vector<GApplicationPar> m_pars; //!< Parameters
116  std::vector<int> m_line; //!< Line number of parameter
117  std::vector<size_t> m_vstart; //!< Column of value start
118  std::vector<size_t> m_vstop; //!< Column of value stop
119  std::string m_mode; //!< Effective mode
120  std::string m_syspfiles; //!< Optional location of syspfiles
121 };
122 
123 
124 /***********************************************************************//**
125  * @brief Return class name
126  *
127  * @return String containing the class name ("GApplicationPars").
128  ***************************************************************************/
129 inline
130 std::string GApplicationPars::classname(void) const
131 {
132  return ("GApplicationPars");
133 }
134 
135 
136 /***********************************************************************//**
137  * @brief Returns reference to parameter
138  *
139  * @param[in] index Parameter index [0,...,size()-1].
140  *
141  * Returns a reference to the parameter with the specified @p index.
142  ***************************************************************************/
143 inline
145 {
146  return (m_pars[index]);
147 }
148 
149 
150 /***********************************************************************//**
151  * @brief Returns reference to parameter (const version)
152  *
153  * @param[in] index Parameter index [0,...,size()-1].
154  *
155  * Returns a reference to the parameter with the specified @p index.
156  ***************************************************************************/
157 inline
158 const GApplicationPar& GApplicationPars::operator[](const int& index) const
159 {
160  return (m_pars[index]);
161 }
162 
163 
164 /***********************************************************************//**
165  * @brief Return number of parameters in container
166  *
167  * @return Number of parameters in container.
168  *
169  * Returns the number of parameters in the parameter container.
170  ***************************************************************************/
171 inline
172 int GApplicationPars::size(void) const
173 {
174  return (int)m_pars.size();
175 }
176 
177 
178 /***********************************************************************//**
179  * @brief Signals if there are no parameters in container
180  *
181  * @return True if container is empty, false otherwise.
182  *
183  * Signals if the parameter container does not contain any parameter.
184  ***************************************************************************/
185 inline
187 {
188  return (m_pars.empty());
189 }
190 
191 
192 /***********************************************************************//**
193  * @brief Reserves space for parameters in container
194  *
195  * @param[in] num Number of parameters.
196  *
197  * Reserves space for @p num parameters in the container.
198  ***************************************************************************/
199 inline
200 void GApplicationPars::reserve(const int& num)
201 {
202  m_pars.reserve(num);
203  return;
204 }
205 
206 
207 /***********************************************************************//**
208  * @brief Set path to system pfiles
209  *
210  * @param[in] syspfiles Path to system pfiles.
211  *
212  * Sets the path to system parameter files.
213  ***************************************************************************/
214 inline
215 void GApplicationPars::syspfiles(const std::string& syspfiles)
216 {
218  return;
219 }
220 
221 
222 /***********************************************************************//**
223  * @brief Return path to system pfiles
224  *
225  * @return Path to system pfiles.
226  *
227  * Returns path to system parameter files.
228  ***************************************************************************/
229 inline
230 const std::string& GApplicationPars::syspfiles(void) const
231 {
232  return (m_syspfiles);
233 }
234 
235 #endif /* GAPPLICATIONPARS_HPP */
std::vector< size_t > m_vstart
Column of value start.
GApplicationPar & append(const GApplicationPar &par)
Append parameter to container.
Application parameter container class.
void init_members(void)
Initialise class members.
std::string m_mode
Effective mode.
std::string m_syspfiles
Optional location of syspfiles.
const std::string & name(void) const
Return application name.
std::string syspfiles_path(const std::string &filename) const
Return path to parfile in m_syspfiles folder.
std::string print(const GChatter &chatter=NORMAL) const
Print parameters.
GApplicationPars & operator=(const GApplicationPars &pars)
Assignment operator.
std::vector< GApplicationPar > m_pars
Parameters.
void parse(void)
Parse parameter file.
const std::string & syspfiles(void) const
Return path to system pfiles.
std::string parline(GApplicationPar &par, size_t *start, size_t *stop) const
Return parameter file line for a specific parameter.
void reserve(const int &num)
Reserves space for parameters in container.
Application parameter class definition.
std::string inpath(const std::string &filename) const
Determine filepath for parameter file input.
void synchronise(const std::string &filename)
Synchronise parameter file with the parameter values in a another parameter file. ...
void free_members(void)
Delete class members.
int size(void) const
Return number of parameters in container.
std::string classname(void) const
Return class name.
GApplicationPars(void)
Void constructor.
Filename class.
Definition: GFilename.hpp:62
void update(void)
Update parameter file.
virtual ~GApplicationPars(void)
Destructor.
std::vector< std::string > m_parfile
Parameter file lines.
GChatter
Definition: GTypemaps.hpp:33
void save(const GFilename &filename)
Save parameters.
std::string pfiles_path(const std::string &filename) const
Return path to parfile in $PFILES or $HOME/pfiles folder.
void copy_members(const GApplicationPars &pars)
Copy class members.
std::vector< std::string > pickle(void) const
Return pickled string vector.
const GApplicationPars & pars(void) const
Return application parameters.
GApplicationPar & at(const int &index)
Returns reference to parameter.
GApplicationPar & operator[](const int &index)
Returns reference to parameter.
void read(const std::string &filename)
Read parameter file.
std::string outpath(const std::string &filename) const
Determine filepath for parameter file output.
void write(const std::string &filename) const
Write parameter file.
Application parameter class.
GammaLib application interface definition.
std::vector< size_t > m_vstop
Column of value stop.
Definition of interface for container classes.
void append_standard(void)
Append standard parameters to container.
std::vector< int > m_line
Line number of parameter.
void extend(const GApplicationPars &pars)
Append parameter container.
const std::vector< std::string > & args(void) const
Return command line arguments.
int get_index(const std::string &name) const
Return parameter index by name.
bool is_empty(void) const
Signals if there are no parameters in container.
Interface class for container classes.
Definition: GContainer.hpp:52
void load(const GFilename &filename)
Load parameters.
GApplicationPar & insert(const int &index, const GApplicationPar &par)
Insert parameter into container.
GApplicationPars * clone(void) const
Clone parameter container.
bool contains(const std::string &name) const
Check parameter exists.
void clear(void)
Clear parameter container.