GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GApplicationPars Class Reference

Application parameter container class. More...

#include <GApplicationPars.hpp>

Inheritance diagram for GApplicationPars:
GContainer GBase

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...
 
GApplicationParsoperator= (const GApplicationPars &pars)
 Assignment operator. More...
 
GApplicationParoperator[] (const int &index)
 Returns reference to parameter. More...
 
const GApplicationParoperator[] (const int &index) const
 Returns reference to parameter (const version) More...
 
GApplicationParoperator[] (const std::string &name)
 Returns reference to parameter. More...
 
const GApplicationParoperator[] (const std::string &name) const
 Returns reference to parameter (const version) More...
 
void clear (void)
 Clear parameter container. More...
 
GApplicationParsclone (void) const
 Clone parameter container. More...
 
std::string classname (void) const
 Return class name. More...
 
GApplicationParat (const int &index)
 Returns reference to parameter. More...
 
const GApplicationParat (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...
 
GApplicationParappend (const GApplicationPar &par)
 Append parameter to container. More...
 
void append_standard (void)
 Append standard parameters to container. More...
 
GApplicationParinsert (const int &index, const GApplicationPar &par)
 Insert parameter into container. More...
 
GApplicationParinsert (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< GApplicationParm_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
 

Detailed Description

Application parameter container class.

This class holds a collection of application parameters.

Definition at line 47 of file GApplicationPars.hpp.

Constructor & Destructor Documentation

GApplicationPars::GApplicationPars ( void  )

Void constructor.

Definition at line 81 of file GApplicationPars.cpp.

References init_members().

Referenced by clone().

GApplicationPars::GApplicationPars ( const GFilename filename)
explicit

Parameter file constructor.

Parameters
[in]filenameParameter 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.

Parameters
[in]filenameParameter filename.
[in]argsCommand line arguments.

Definition at line 114 of file GApplicationPars.cpp.

References init_members(), and load().

GApplicationPars::GApplicationPars ( const GApplicationPars pars)

Copy constructor.

Parameters
[in]parsParameter container.

Definition at line 133 of file GApplicationPars.cpp.

References copy_members(), and init_members().

GApplicationPars::~GApplicationPars ( void  )
virtual

Destructor.

Definition at line 149 of file GApplicationPars.cpp.

References free_members().

Member Function Documentation

GApplicationPar & GApplicationPars::append ( const GApplicationPar par)

Append parameter to container.

Parameters
[in]parParameter.
Returns
Reference to deep copy of appended parameter.
Exceptions
GException::invalid_valueParameter 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.

Parameters
[in]indexParameter index [0,...,size()-1].
Exceptions
GException::out_of_rangeParameter index is out of range.

Definition at line 287 of file GApplicationPars.cpp.

References G_AT, m_pars, and size().

const GApplicationPar & GApplicationPars::at ( const int &  index) const

Returns reference to parameter (const version)

Parameters
[in]indexParameter index [0,...,size()-1].
Exceptions
GException::out_of_rangeParameter index is out of range.

Definition at line 309 of file GApplicationPars.cpp.

References G_AT, m_pars, and size().

std::string GApplicationPars::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GApplicationPars").

Implements GBase.

Definition at line 130 of file GApplicationPars.hpp.

void GApplicationPars::clear ( void  )
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().

GApplicationPars * GApplicationPars::clone ( void  ) const
virtual

Clone parameter container.

Returns
Pointer to deep copy of 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.

Parameters
[in]nameParameter name.
Returns
True if parameter with specified name exists, false otherwise.

Determines whether a parameter with the specified name exists already in the parameter container.

Definition at line 617 of file GApplicationPars.cpp.

References get_index().

Referenced by GApplication::has_par(), load(), parse(), and GApplication::set_log_filename().

void GApplicationPars::copy_members ( const GApplicationPars pars)
protected

Copy class members.

Parameters
[in]parsObject from which members which should be copied.

Definition at line 964 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.

Parameters
[in]parsParameter container.

Append parameter container to the container.

Definition at line 567 of file GApplicationPars.cpp.

References append(), G_EXTEND, get_index(), is_empty(), reserve(), size(), and gammalib::str().

void GApplicationPars::free_members ( void  )
protected

Delete class members.

Definition at line 983 of file GApplicationPars.cpp.

Referenced by clear(), operator=(), and ~GApplicationPars().

int GApplicationPars::get_index ( const std::string &  name) const
protected

Return parameter index by name.

Parameters
[in]nameParameter name.
Returns
Parameter index (-1 if parameter name has not been found)

Returns parameter index based on the specified name. If no parameter with the specified name is found the method returns -1.

Definition at line 1641 of file GApplicationPars.cpp.

References m_pars, and size().

Referenced by append(), contains(), extend(), insert(), operator[](), remove(), and synchronise().

void GApplicationPars::init_members ( void  )
protected

Initialise class members.

Definition at line 943 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=().

std::string GApplicationPars::inpath ( const std::string &  filename) const
protected

Determine filepath for parameter file input.

Parameters
[in]filenameParameter 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 1007 of file GApplicationPars.cpp.

References gammalib::split().

Referenced by load().

GApplicationPar & GApplicationPars::insert ( const int &  index,
const GApplicationPar par 
)

Insert parameter into container.

Parameters
[in]indexParameter index [0,...,size()-1].
[in]parParameter.
Returns
Reference to deep copy of inserted 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.

Parameters
[in]nameParameter name.
[in]parParameter.
Returns
Reference to deep copy of inserted parameter.
Exceptions
GException::invalid_argumentParameter with specified name not found in container.
GException::invalid_valueName of parameter exists already in container.

Inserts a parameter into the container before the parameter with the specified name.

Definition at line 473 of file GApplicationPars.cpp.

References G_INSERT2, get_index(), and insert().

bool GApplicationPars::is_empty ( void  ) const
inlinevirtual

Signals if there are no parameters in container.

Returns
True if container is empty, false otherwise.

Signals if the parameter container does not contain any parameter.

Implements GContainer.

Definition at line 186 of file GApplicationPars.hpp.

References m_pars.

Referenced by extend(), and insert().

void GApplicationPars::load ( const GFilename filename)

Load parameters.

Parameters
[in]filenameParameter filename.
Exceptions
GException::par_file_not_foundParameter 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 644 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.

Parameters
[in]filenameParameter filename.
[in]argsCommand line arguments.
Exceptions
GException::bad_cmdline_argumentInvalid 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 706 of file GApplicationPars.cpp.

References contains(), G_LOAD2, and load().

GApplicationPars & GApplicationPars::operator= ( const GApplicationPars pars)

Assignment operator.

Parameters
[in]parsParameter container.
Returns
Parameter container.

Definition at line 171 of file GApplicationPars.cpp.

References copy_members(), free_members(), and init_members().

GApplicationPar & GApplicationPars::operator[] ( const int &  index)
inline

Returns reference to parameter.

Parameters
[in]indexParameter 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.

const GApplicationPar & GApplicationPars::operator[] ( const int &  index) const
inline

Returns reference to parameter (const version)

Parameters
[in]indexParameter 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.

Parameters
[in]nameParameter name.
Returns
Parameter.
Exceptions
GException::invalid_argumentParameter 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)

Parameters
[in]nameParameter name.
Returns
Parameter.
Exceptions
GException::invalid_argumentParameter with specified name not found in container.

Definition at line 228 of file GApplicationPars.cpp.

References G_ACCESS, get_index(), and m_pars.

std::string GApplicationPars::outpath ( const std::string &  filename) const
protected

Determine filepath for parameter file output.

Parameters
[in]filenameParameter filename.
Exceptions
GException::home_not_foundUnable to determine users home directory.
GException::could_not_create_pfilesUnable to create pfiles directory.
GException::pfiles_not_accessibleUnable 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 1190 of file GApplicationPars.cpp.

References G_OUTPATH, and gammalib::split().

Referenced by save().

std::string GApplicationPars::parline ( GApplicationPar par,
size_t *  start,
size_t *  stop 
) const
protected

Return parameter file line for a specific parameter.

Parameters
[in]parParameter.
[out]startColumn of value start.
[out]stopColumn of value stop.
Returns
Parameter file line (termined by
).

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 1670 of file GApplicationPars.cpp.

References GApplicationPar::max(), GApplicationPar::min(), GApplicationPar::mode(), GApplicationPar::name(), GApplicationPar::prompt(), GApplicationPar::type(), and GApplicationPar::value().

Referenced by append(), and insert().

void GApplicationPars::parse ( void  )
protected

Parse parameter file.

Exceptions
GException::par_file_syntax_errorSyntax 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 1435 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().

std::string GApplicationPars::pfiles_path ( const std::string &  filename) const
protected

Return path to parfile in $PFILES or $HOME/pfiles folder.

Parameters
[in]filenameParameter filename to search for.
Returns
Full parameter filename, including absolute access path.

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 1097 of file GApplicationPars.cpp.

References gammalib::split().

Referenced by load().

void GApplicationPars::pickle ( const std::vector< std::string > &  string)

Set class from pickled string vector.

Parameters
[in]stringString vector containing class information.

Definition at line 792 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.

Returns
String vector containing class information.

Definition at line 855 of file GApplicationPars.cpp.

References m_line, m_mode, m_parfile, m_pars, m_syspfiles, m_vstart, m_vstop, and gammalib::str().

std::string GApplicationPars::print ( const GChatter chatter = NORMAL) const
virtual

Print parameters.

Parameters
[in]chatterChattiness.
Returns
String containing parameter information.

Implements GBase.

Definition at line 911 of file GApplicationPars.cpp.

References m_pars, SILENT, and size().

void GApplicationPars::read ( const std::string &  filename)
protected

Read parameter file.

Parameters
[in]filenameParameter filename (absolut path).
Exceptions
GException::par_file_open_errorUnable 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 1269 of file GApplicationPars.cpp.

References G_READ, and m_parfile.

Referenced by load(), and synchronise().

void GApplicationPars::remove ( const int &  index)
virtual

Remove parameter from container.

Parameters
[in]indexParameter index [0,...,size()-1].
Exceptions
GException::out_of_rangeParameter index is out of range.

Remove parameter with specified index from container.

Implements GContainer.

Definition at line 501 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.

Parameters
[in]nameParameter name.
Exceptions
GException::invalid_argumentParameter with specified name not found in container.

Remove parameter with specified name from container.

Definition at line 539 of file GApplicationPars.cpp.

References G_REMOVE2, and get_index().

void GApplicationPars::reserve ( const int &  num)
inlinevirtual

Reserves space for parameters in container.

Parameters
[in]numNumber 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.

Parameters
[in]filenameParameter filename.
Exceptions
GException::par_file_not_foundNo valid directory to write the parameter file has been found.

Definition at line 768 of file GApplicationPars.cpp.

References G_SAVE, outpath(), update(), GFilename::url(), and write().

Referenced by GApplication::free_members().

int GApplicationPars::size ( void  ) const
inlinevirtual

Return number of parameters in container.

Returns
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().

void GApplicationPars::synchronise ( const std::string &  filename)
protected

Synchronise parameter file with the parameter values in a another parameter file.

Parameters
[in]filenameFile 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 1593 of file GApplicationPars.cpp.

References get_index(), m_pars, parse(), and read().

Referenced by load().

void GApplicationPars::syspfiles ( const std::string &  syspfiles)
inline

Set path to system pfiles.

Parameters
[in]syspfilesPath to system pfiles.

Sets the path to system parameter files.

Definition at line 215 of file GApplicationPars.hpp.

References m_syspfiles, and syspfiles().

const std::string & GApplicationPars::syspfiles ( void  ) const
inline

Return path to system pfiles.

Returns
Path to system pfiles.

Returns path to system parameter files.

Definition at line 230 of file GApplicationPars.hpp.

References m_syspfiles.

Referenced by syspfiles().

std::string GApplicationPars::syspfiles_path ( const std::string &  filename) const
protected

Return path to parfile in m_syspfiles folder.

Parameters
[in]filenameParameter filename to search for.
Returns
Full parameter filename, including absolute access path.

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 1155 of file GApplicationPars.cpp.

References m_syspfiles.

Referenced by load().

void GApplicationPars::update ( void  )
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 1563 of file GApplicationPars.cpp.

References m_line, m_parfile, m_pars, m_vstart, and m_vstop.

Referenced by save().

void GApplicationPars::write ( const std::string &  filename) const
protected

Write parameter file.

Parameters
[in]filenameParameter filename (absolut path).
Exceptions
GException::par_file_open_errorUnable 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 1344 of file GApplicationPars.cpp.

References G_WRITE, and m_parfile.

Referenced by save().

Friends And Related Function Documentation

friend class GApplication
friend

Definition at line 50 of file GApplicationPars.hpp.

Member Data Documentation

std::vector<int> GApplicationPars::m_line
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().

std::string GApplicationPars::m_mode
protected

Effective mode.

Definition at line 119 of file GApplicationPars.hpp.

Referenced by copy_members(), init_members(), parse(), and pickle().

std::vector<std::string> GApplicationPars::m_parfile
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().

std::string GApplicationPars::m_syspfiles
protected

Optional location of syspfiles.

Definition at line 120 of file GApplicationPars.hpp.

Referenced by copy_members(), init_members(), pickle(), syspfiles(), and syspfiles_path().

std::vector<size_t> GApplicationPars::m_vstart
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().

std::vector<size_t> GApplicationPars::m_vstop
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().


The documentation for this class was generated from the following files: