GammaLib
2.0.0
|
Application parameter container class. More...
#include <GApplicationPars.hpp>
Public Member Functions | |
GApplicationPars (void) | |
Void constructor. More... | |
GApplicationPars (const GFilename &filename) | |
Parameter file constructor. More... | |
GApplicationPars (const GFilename &filename, const std::vector< std::string > &args) | |
Parameter constructor. More... | |
GApplicationPars (const GApplicationPars &pars) | |
Copy constructor. More... | |
virtual | ~GApplicationPars (void) |
Destructor. More... | |
GApplicationPars & | operator= (const GApplicationPars &pars) |
Assignment operator. More... | |
GApplicationPar & | operator[] (const int &index) |
Returns reference to parameter. More... | |
const GApplicationPar & | operator[] (const int &index) const |
Returns reference to parameter (const version) More... | |
GApplicationPar & | operator[] (const std::string &name) |
Returns reference to parameter. More... | |
const GApplicationPar & | operator[] (const std::string &name) const |
Returns reference to parameter (const version) More... | |
void | clear (void) |
Clear parameter container. More... | |
GApplicationPars * | clone (void) const |
Clone parameter container. More... | |
std::string | classname (void) const |
Return class name. More... | |
GApplicationPar & | at (const int &index) |
Returns reference to parameter. More... | |
const GApplicationPar & | at (const int &index) const |
Returns reference to parameter (const version) More... | |
int | size (void) const |
Return number of parameters in container. More... | |
bool | is_empty (void) const |
Signals if there are no parameters in container. More... | |
GApplicationPar & | append (const GApplicationPar &par) |
Append parameter to container. More... | |
void | append_standard (void) |
Append standard parameters to container. More... | |
GApplicationPar & | insert (const int &index, const GApplicationPar &par) |
Insert parameter into container. More... | |
GApplicationPar & | insert (const std::string &name, const GApplicationPar &par) |
Insert parameter into container. More... | |
void | remove (const int &index) |
Remove parameter from container. More... | |
void | remove (const std::string &name) |
Remove parameter from container. More... | |
void | reserve (const int &num) |
Reserves space for parameters in container. More... | |
void | extend (const GApplicationPars &pars) |
Append parameter container. More... | |
bool | contains (const std::string &name) const |
Check parameter exists. More... | |
void | syspfiles (const std::string &syspfiles) |
Set path to system pfiles. More... | |
const std::string & | syspfiles (void) const |
Return path to system pfiles. More... | |
void | load (const GFilename &filename) |
Load parameters. More... | |
void | load (const GFilename &filename, const std::vector< std::string > &args) |
Load parameters. More... | |
void | save (const GFilename &filename) |
Save parameters. More... | |
void | pickle (const std::vector< std::string > &string) |
Set class from pickled string vector. More... | |
std::vector< std::string > | pickle (void) const |
Return pickled string vector. More... | |
std::string | print (const GChatter &chatter=NORMAL) const |
Print parameters. More... | |
Public Member Functions inherited from GContainer | |
virtual | ~GContainer (void) |
Destructor. More... | |
Public Member Functions inherited from GBase | |
virtual | ~GBase (void) |
Destructor. More... | |
Protected Member Functions | |
void | init_members (void) |
Initialise class members. More... | |
void | copy_members (const GApplicationPars &pars) |
Copy class members. More... | |
void | free_members (void) |
Delete class members. More... | |
std::string | inpath (const std::string &filename) const |
Determine filepath for parameter file input. More... | |
std::string | pfiles_path (const std::string &filename) const |
Return path to parfile in $PFILES or $HOME/pfiles folder. More... | |
std::string | syspfiles_path (const std::string &filename) const |
Return path to parfile in m_syspfiles folder. More... | |
std::string | outpath (const std::string &filename) const |
Determine filepath for parameter file output. More... | |
void | read (const std::string &filename) |
Read parameter file. More... | |
void | write (const std::string &filename) const |
Write parameter file. More... | |
void | parse (void) |
Parse parameter file. More... | |
void | update (void) |
Update parameter file. More... | |
void | synchronise (const std::string &filename) |
Synchronise parameter file with the parameter values in a another parameter file. More... | |
int | get_index (const std::string &name) const |
Return parameter index by name. More... | |
std::string | parline (GApplicationPar &par, size_t *start, size_t *stop) const |
Return parameter file line for a specific parameter. More... | |
Protected Attributes | |
std::vector< std::string > | m_parfile |
Parameter file lines. More... | |
std::vector< GApplicationPar > | m_pars |
Parameters. More... | |
std::vector< int > | m_line |
Line number of parameter. More... | |
std::vector< size_t > | m_vstart |
Column of value start. More... | |
std::vector< size_t > | m_vstop |
Column of value stop. More... | |
std::string | m_mode |
Effective mode. More... | |
std::string | m_syspfiles |
Optional location of syspfiles. More... | |
Friends | |
class | GApplication |
Application parameter container class.
This class holds a collection of application parameters.
Definition at line 47 of file GApplicationPars.hpp.
GApplicationPars::GApplicationPars | ( | void | ) |
Void constructor.
Definition at line 81 of file GApplicationPars.cpp.
References init_members().
Referenced by clone().
|
explicit |
Parameter file constructor.
[in] | filename | Parameter filename. |
Definition at line 96 of file GApplicationPars.cpp.
References init_members(), and load().
GApplicationPars::GApplicationPars | ( | const GFilename & | filename, |
const std::vector< std::string > & | args | ||
) |
Parameter constructor.
[in] | filename | Parameter filename. |
[in] | args | Command line arguments. |
Definition at line 114 of file GApplicationPars.cpp.
References init_members(), and load().
GApplicationPars::GApplicationPars | ( | const GApplicationPars & | pars | ) |
Copy constructor.
[in] | pars | Parameter container. |
Definition at line 133 of file GApplicationPars.cpp.
References copy_members(), and init_members().
|
virtual |
GApplicationPar & GApplicationPars::append | ( | const GApplicationPar & | par | ) |
Append parameter to container.
[in] | par | Parameter. |
GException::invalid_value | Parameter with same name exists already in container. |
This method appends one parameter to the parameter container. The parameter provided to the method can be deleted after calling this method.
Definition at line 335 of file GApplicationPars.cpp.
References G_APPEND, get_index(), m_line, m_parfile, m_pars, m_vstart, m_vstop, GApplicationPar::name(), parline(), and gammalib::str().
Referenced by append_standard(), and extend().
void GApplicationPars::append_standard | ( | void | ) |
Append standard parameters to container.
This method appends the standard parameters to the parameter container. Standard parameters are: "chatter", "clobber", "debug" and "mode".
Definition at line 376 of file GApplicationPars.cpp.
References append().
GApplicationPar & GApplicationPars::at | ( | const int & | index | ) |
Returns reference to parameter.
[in] | index | Parameter index [0,...,size()-1]. |
GException::out_of_range | Parameter index is out of range. |
Definition at line 287 of file GApplicationPars.cpp.
const GApplicationPar & GApplicationPars::at | ( | const int & | index | ) | const |
Returns reference to parameter (const version)
[in] | index | Parameter index [0,...,size()-1]. |
GException::out_of_range | Parameter index is out of range. |
Definition at line 309 of file GApplicationPars.cpp.
|
inlinevirtual |
Return class name.
Implements GBase.
Definition at line 130 of file GApplicationPars.hpp.
|
virtual |
Clear parameter container.
Implements GBase.
Definition at line 255 of file GApplicationPars.cpp.
References free_members(), and init_members().
Referenced by GApplication::init_members(), and pickle().
|
virtual |
Clone parameter container.
Implements GBase.
Definition at line 273 of file GApplicationPars.cpp.
References GApplicationPars().
bool GApplicationPars::contains | ( | const std::string & | name | ) | const |
Check parameter exists.
[in] | name | Parameter name. |
Determines whether a parameter with the specified name exists already in the parameter container.
Definition at line 618 of file GApplicationPars.cpp.
References get_index().
Referenced by GApplication::has_par(), load(), parse(), and GApplication::set_log_filename().
|
protected |
Copy class members.
[in] | pars | Object from which members which should be copied. |
Definition at line 979 of file GApplicationPars.cpp.
References m_line, m_mode, m_parfile, m_pars, m_syspfiles, m_vstart, and m_vstop.
Referenced by GApplicationPars(), and operator=().
void GApplicationPars::extend | ( | const GApplicationPars & | pars | ) |
Append parameter container.
[in] | pars | Parameter container. |
GException::invalid_value | Parameter with the same name exists already. |
Append parameter container to the container.
Definition at line 569 of file GApplicationPars.cpp.
References append(), G_EXTEND, get_index(), is_empty(), reserve(), size(), and gammalib::str().
|
protected |
Delete class members.
Definition at line 998 of file GApplicationPars.cpp.
Referenced by clear(), operator=(), and ~GApplicationPars().
|
protected |
Return parameter index by name.
[in] | name | Parameter name. |
Returns parameter index based on the specified name
. If no parameter with the specified name
is found the method returns -1.
Definition at line 1693 of file GApplicationPars.cpp.
References m_pars, and size().
Referenced by append(), contains(), extend(), insert(), operator[](), remove(), and synchronise().
|
protected |
Initialise class members.
Definition at line 958 of file GApplicationPars.cpp.
References m_line, m_mode, m_parfile, m_pars, m_syspfiles, m_vstart, and m_vstop.
Referenced by clear(), GApplicationPars(), and operator=().
|
protected |
Determine filepath for parameter file input.
[in] | filename | Parameter filename to search for. |
Locates parameter file for input. The parameter file is first searched in the directories that are listed in the PFILES environment variable. Directories may be separated by : or by ; in PFILES.
If the PFILES environment variable is not set the parameter file is searched in the users pfiles directory.
If still no parameter file is found, the parameter file is searched (in the given order) in ${GAMMALIB}/syspfiles and in ${prefix}/syspfiles, where ${prefix} is the path to the GammaLib installation.
Definition at line 1022 of file GApplicationPars.cpp.
References gammalib::getenv(), and gammalib::split().
Referenced by load().
GApplicationPar & GApplicationPars::insert | ( | const int & | index, |
const GApplicationPar & | par | ||
) |
Insert parameter into container.
[in] | index | Parameter index [0,...,size()-1]. |
[in] | par | Parameter. |
Inserts a parameter into the container before the parameter with the specified index
.
Definition at line 399 of file GApplicationPars.cpp.
References G_INSERT1, get_index(), is_empty(), m_line, m_parfile, m_pars, m_vstart, m_vstop, GApplicationPar::name(), parline(), size(), and gammalib::str().
Referenced by insert().
GApplicationPar & GApplicationPars::insert | ( | const std::string & | name, |
const GApplicationPar & | par | ||
) |
Insert parameter into container.
[in] | name | Parameter name. |
[in] | par | Parameter. |
GException::invalid_argument | Parameter with specified name not found in container. |
Inserts a parameter into the container before the parameter with the specified name
.
Definition at line 471 of file GApplicationPars.cpp.
References G_INSERT2, get_index(), and insert().
|
inlinevirtual |
Signals if there are no parameters in container.
Signals if the parameter container does not contain any parameter.
Implements GContainer.
Definition at line 186 of file GApplicationPars.hpp.
References m_pars.
void GApplicationPars::load | ( | const GFilename & | filename | ) |
Load parameters.
[in] | filename | Parameter filename. |
GException::invalid_argument | Parameter file not found. |
Loads all parameters from parameter file.
If the syspfiles path is set then load the parameter file from that location and update the parameters using a paramater file that is found in the users "pfiles" directory.
Otherwise, search the parameter file in the usual location (see the GApplicationPars::inpath method for more information).
Definition at line 645 of file GApplicationPars.cpp.
References G_LOAD1, inpath(), m_parfile, parse(), pfiles_path(), read(), synchronise(), syspfiles_path(), and GFilename::url().
Referenced by GApplication::clear(), GApplication::GApplication(), GApplicationPars(), and load().
void GApplicationPars::load | ( | const GFilename & | filename, |
const std::vector< std::string > & | args | ||
) |
Load parameters.
[in] | filename | Parameter filename. |
[in] | args | Command line arguments. |
GException::invalid_argument | Invalid command line argument encountered. |
Loads all parameters from parameter file. Parameters are overwritten by the values specified in the command line arguments.
Definition at line 710 of file GApplicationPars.cpp.
References contains(), G_LOAD2, and load().
GApplicationPars & GApplicationPars::operator= | ( | const GApplicationPars & | pars | ) |
Assignment operator.
[in] | pars | Parameter container. |
Definition at line 171 of file GApplicationPars.cpp.
References copy_members(), free_members(), and init_members().
|
inline |
Returns reference to parameter.
[in] | index | Parameter index [0,...,size()-1]. |
Returns a reference to the parameter with the specified index
.
Definition at line 144 of file GApplicationPars.hpp.
References m_pars.
|
inline |
Returns reference to parameter (const version)
[in] | index | Parameter index [0,...,size()-1]. |
Returns a reference to the parameter with the specified index
.
Definition at line 158 of file GApplicationPars.hpp.
References m_pars.
GApplicationPar & GApplicationPars::operator[] | ( | const std::string & | name | ) |
Returns reference to parameter.
[in] | name | Parameter name. |
GException::invalid_argument | Parameter with specified name not found in container. |
Definition at line 201 of file GApplicationPars.cpp.
References G_ACCESS, get_index(), and m_pars.
const GApplicationPar & GApplicationPars::operator[] | ( | const std::string & | name | ) | const |
Returns reference to parameter (const version)
[in] | name | Parameter name. |
GException::invalid_argument | Parameter with specified name not found in container. |
Definition at line 228 of file GApplicationPars.cpp.
References G_ACCESS, get_index(), and m_pars.
|
protected |
Determine filepath for parameter file output.
[in] | filename | Parameter filename. |
GException::runtime_error | Unable to determine users home directory. Unable to create pfiles directory. Unable to make pfiles directory accessible to user. |
Searchs for first writable directory listed in PFILES environment variable. If PFILES is not set then use pfiles directory in users home directory. If pfiles directory does not exist then create it. If directory exists but is not writable then make it writable.
Definition at line 1201 of file GApplicationPars.cpp.
References G_OUTPATH, gammalib::getenv(), and gammalib::split().
Referenced by save().
|
protected |
Return parameter file line for a specific parameter.
[in] | par | Parameter. |
[out] | start | Column of value start. |
[out] | stop | Column of value stop. |
Constructs the parameter file line for a specific parameter and returns the line as a string. The line is terminated by a
character.
Definition at line 1722 of file GApplicationPars.cpp.
References GApplicationPar::max(), GApplicationPar::min(), GApplicationPar::mode(), GApplicationPar::name(), GApplicationPar::prompt(), GApplicationPar::type(), and GApplicationPar::value().
|
protected |
Parse parameter file.
GException::invalid_value | Syntax error encountered in parameter file. |
The parameter type has to be one b,i,r,s,f,fr,fw,fe,fn. The fr,fw,fe,fn types test for read access, write access, file existence, and file absence, respectively. The parameter mode has to be one of a,h,l,q,hl,ql,lh,lq. For mode 'a' the effective mode equals to the value given by the mode parameter, if it exists. Without the presence of the mode parameter the effective mode will be 'h'.
Definition at line 1475 of file GApplicationPars.cpp.
References contains(), G_PARSE, m_line, m_mode, m_parfile, m_pars, m_vstart, m_vstop, gammalib::str(), gammalib::strip_chars(), gammalib::strip_whitespace(), and GExceptionHandler::what().
Referenced by load(), and synchronise().
|
protected |
Return path to parfile in $PFILES or $HOME/pfiles folder.
[in] | filename | Parameter filename to search for. |
Locates parameter file in the directories that are listed in the PFILES environment variable. Directories may be separated by : or by ; in the PFILES environment variable.
If the PFILES environment variable is not set the parameter file is searched in the users pfiles directory.
If parameter file was not found, or the parameter file is not read-accessible, an empty string is returned.
Definition at line 1110 of file GApplicationPars.cpp.
References gammalib::getenv(), and gammalib::split().
Referenced by load().
void GApplicationPars::pickle | ( | const std::vector< std::string > & | string | ) |
Set class from pickled string vector.
[in] | string | String vector containing class information. |
Definition at line 809 of file GApplicationPars.cpp.
References clear(), m_line, m_mode, m_parfile, m_pars, m_syspfiles, m_vstart, m_vstop, GApplicationPar::pickle(), and gammalib::toint().
std::vector< std::string > GApplicationPars::pickle | ( | void | ) | const |
Return pickled string vector.
Definition at line 870 of file GApplicationPars.cpp.
References m_line, m_mode, m_parfile, m_pars, m_syspfiles, m_vstart, m_vstop, and gammalib::str().
|
protected |
Read parameter file.
[in] | filename | Parameter filename (absolut path). |
GException::file_error | Unable to open parameter file (read access requested). |
Read all lines of the parameter file. Each line is terminated by a newline character. The file will be locked to avoid simultaneous access by another process.
Definition at line 1285 of file GApplicationPars.cpp.
References G_READ, and m_parfile.
Referenced by load(), and synchronise().
|
virtual |
Remove parameter from container.
[in] | index | Parameter index [0,...,size()-1]. |
GException::out_of_range | Parameter index is out of range. |
Remove parameter with specified index
from container.
Implements GContainer.
Definition at line 500 of file GApplicationPars.cpp.
References G_REMOVE1, m_line, m_parfile, m_pars, m_vstart, m_vstop, and size().
void GApplicationPars::remove | ( | const std::string & | name | ) |
Remove parameter from container.
[in] | name | Parameter name. |
GException::invalid_argument | Parameter with specified name not found in container. |
Remove parameter with specified name
from container.
Definition at line 538 of file GApplicationPars.cpp.
References G_REMOVE2, and get_index().
|
inlinevirtual |
Reserves space for parameters in container.
[in] | num | Number of parameters. |
Reserves space for num
parameters in the container.
Implements GContainer.
Definition at line 200 of file GApplicationPars.hpp.
References m_pars.
Referenced by extend().
void GApplicationPars::save | ( | const GFilename & | filename | ) |
Save parameters.
[in] | filename | Parameter filename. |
GException::file_error | No valid directory to write the parameter file has been found. |
Definition at line 782 of file GApplicationPars.cpp.
References G_LOAD2, outpath(), update(), GFilename::url(), and write().
Referenced by GApplication::free_members().
|
inlinevirtual |
Return number of parameters in container.
Returns the number of parameters in the parameter container.
Implements GContainer.
Definition at line 172 of file GApplicationPars.hpp.
References m_pars.
Referenced by at(), extend(), get_index(), insert(), GApplication::log_parameters(), print(), GApplication::print(), and remove().
|
protected |
Synchronise parameter file with the parameter values in a another parameter file.
[in] | filename | File name of other parameter file. |
Copies over all values from another parameter file that correspond to parameters with the same name and type, and that are signaled as "learn" in the other parameter file.
Definition at line 1645 of file GApplicationPars.cpp.
References get_index(), m_pars, parse(), and read().
Referenced by load().
|
inline |
Set path to system pfiles.
[in] | syspfiles | Path to system pfiles. |
Sets the path to system parameter files.
Definition at line 215 of file GApplicationPars.hpp.
References m_syspfiles, and syspfiles().
|
inline |
Return path to system pfiles.
Returns path to system parameter files.
Definition at line 230 of file GApplicationPars.hpp.
References m_syspfiles.
Referenced by syspfiles().
|
protected |
Return path to parfile in m_syspfiles folder.
[in] | filename | Parameter filename to search for. |
Locates parameter file in the directory that is specified by the m_syspfiles string. If the string is empty, the specified file is not found or read accessible, an empty string is returned.
Definition at line 1168 of file GApplicationPars.cpp.
References m_syspfiles.
Referenced by load().
|
protected |
Update parameter file.
Update lines of parameter file according to the parameter values. This method handles correctly formatted parameter files by replacing the value at its original location within the line (preserving additional whitespace). Updating is only done of the parameter mode is 'learn'.
Definition at line 1615 of file GApplicationPars.cpp.
References m_line, m_parfile, m_pars, m_vstart, and m_vstop.
Referenced by save().
|
protected |
Write parameter file.
[in] | filename | Parameter filename (absolut path). |
GException::file_error | Unable to open parameter file (write access requested). |
Writes all lines of the parameter file. The file will be locked to avoid simultaneous access by another process.
Definition at line 1372 of file GApplicationPars.cpp.
References G_WRITE, and m_parfile.
Referenced by save().
|
friend |
Definition at line 50 of file GApplicationPars.hpp.
|
protected |
Line number of parameter.
Definition at line 116 of file GApplicationPars.hpp.
Referenced by append(), copy_members(), init_members(), insert(), parse(), pickle(), remove(), and update().
|
protected |
Effective mode.
Definition at line 119 of file GApplicationPars.hpp.
Referenced by copy_members(), init_members(), parse(), and pickle().
|
protected |
Parameter file lines.
Definition at line 114 of file GApplicationPars.hpp.
Referenced by append(), copy_members(), init_members(), insert(), load(), parse(), pickle(), read(), remove(), update(), and write().
|
protected |
Parameters.
Definition at line 115 of file GApplicationPars.hpp.
Referenced by append(), at(), copy_members(), get_index(), init_members(), insert(), is_empty(), GApplication::log_parameters(), operator[](), parse(), pickle(), print(), GApplication::print(), remove(), reserve(), size(), synchronise(), and update().
|
protected |
Optional location of syspfiles.
Definition at line 120 of file GApplicationPars.hpp.
Referenced by copy_members(), init_members(), pickle(), syspfiles(), and syspfiles_path().
|
protected |
Column of value start.
Definition at line 117 of file GApplicationPars.hpp.
Referenced by append(), copy_members(), init_members(), insert(), parse(), pickle(), remove(), and update().
|
protected |
Column of value stop.
Definition at line 118 of file GApplicationPars.hpp.
Referenced by append(), copy_members(), init_members(), insert(), parse(), pickle(), remove(), and update().