JayBeams  0.1
Another project to have fun coding.
Public Member Functions | List of all members
jb::config_files_locations< getenv_functor, validator_functor > Class Template Reference

Compute the directories where a configuration file can be found. More...

#include <config_files_location.hpp>

Inheritance diagram for jb::config_files_locations< getenv_functor, validator_functor >:
jb::config_files_locations_base

Public Member Functions

 config_files_locations (boost::filesystem::path const &argv0, char const *program_root_variable, getenv_functor getenv)
 Constructor. More...
 
 config_files_locations (boost::filesystem::path const &argv0, getenv_functor getenv)
 Constructor. More...
 
boost::filesystem::path find_configuration_file (std::string const &filename, validator_functor validator) const
 Find a configuration file in the computed search path. More...
 
boost::filesystem::path find_configuration_file (std::string const &filename) const
 Find a configuration file in the computed search path. More...
 
 config_files_locations (boost::filesystem::path const &argv0, char const *program_root_variable)
 Convenience constructors. More...
 
 config_files_locations (boost::filesystem::path const &argv0)
 
 config_files_locations (char const *argv0, char const *program_root_variable, getenv_functor getenv)
 
 config_files_locations (char const *argv0, char const *program_root_variable)
 
 config_files_locations (char const *argv0, getenv_functor getenv)
 
 config_files_locations (char const *argv0)
 
- Public Member Functions inherited from jb::config_files_locations_base
std::vector< boost::filesystem::path > const & search_path () const
 

Additional Inherited Members

- Protected Member Functions inherited from jb::config_files_locations_base
 config_files_locations_base (boost::filesystem::path const &argv0, std::function< char const *(char const *)> getenv, char const *program_root_variable)
 Constructor. More...
 
 config_files_locations_base (boost::filesystem::path const &argv0, std::function< char const *(char const *)> getenv)
 Constructor. More...
 

Detailed Description

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
class jb::config_files_locations< getenv_functor, validator_functor >

Compute the directories where a configuration file can be found.

JayBeams configuration files can be located in multiple places:

Each program defines its own '*_ROOT' environment variable, the

files are searched there if the variable is defined, otherwise

the files are found in the generic 'JAYBEAMS_ROOT' location, if

that environment variable is defined, otherwise

the files are found in the installation directory, if the

directory exists, otherwise,

the files are found relative to the program path name.

This class implements the algorithms to (a) compute the valid list of directories for a given program, and (b) select which configuration file to load.

We use some template parameters for dependency injection, it is hard to test the interaction with the file system (and the environment variables) without them.

Template Parameters
getenv_functorthe functor used to fetch the value of environment variables. This is used for dependency injection during testing.
validator_functorthe functor used to validate if a given path is readable. This is used for dependency injection during testing.

Definition at line 103 of file config_files_location.hpp.

Constructor & Destructor Documentation

◆ config_files_locations() [1/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( boost::filesystem::path const &  argv0,
char const *  program_root_variable,
getenv_functor  getenv 
)
inline

Constructor.

Build the search path given the program path and the name of its preferred environment variable.

Parameters
argv0the program path, typically argv[0] from main()
program_root_variablethe name of the preferred environment variable for this program, i.e., its *_ROOT variable.
getenvthe functor object to use

Definition at line 116 of file config_files_location.hpp.

◆ config_files_locations() [2/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( boost::filesystem::path const &  argv0,
getenv_functor  getenv 
)
inline

Constructor.

Build the search path given the program path. Used in programs that do not have a preferred *_ROOT environment variable.

Parameters
argv0the program path, typically argv[0] from main()
getenvthe functor object to use

Definition at line 131 of file config_files_location.hpp.

◆ config_files_locations() [3/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( boost::filesystem::path const &  argv0,
char const *  program_root_variable 
)
inline

Convenience constructors.

Apply common conversions and create functors as needed.

Definition at line 142 of file config_files_location.hpp.

◆ config_files_locations() [4/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( boost::filesystem::path const &  argv0)
inlineexplicit

Definition at line 146 of file config_files_location.hpp.

◆ config_files_locations() [5/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( char const *  argv0,
char const *  program_root_variable,
getenv_functor  getenv 
)
inline

Definition at line 149 of file config_files_location.hpp.

◆ config_files_locations() [6/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( char const *  argv0,
char const *  program_root_variable 
)
inline

Definition at line 155 of file config_files_location.hpp.

◆ config_files_locations() [7/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( char const *  argv0,
getenv_functor  getenv 
)
inline

Definition at line 160 of file config_files_location.hpp.

◆ config_files_locations() [8/8]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
jb::config_files_locations< getenv_functor, validator_functor >::config_files_locations ( char const *  argv0)
inlineexplicit

Definition at line 163 of file config_files_location.hpp.

Member Function Documentation

◆ find_configuration_file() [1/2]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
boost::filesystem::path jb::config_files_locations< getenv_functor, validator_functor >::find_configuration_file ( std::string const &  filename,
validator_functor  validator 
) const
inline

Find a configuration file in the computed search path.

Parameters
filenamethe basename of the configuration file.
validatorthe functor to use for validation
Returns
the path to load
Exceptions
std::runtime_errorif no suitable file was found.

Definition at line 178 of file config_files_location.hpp.

◆ find_configuration_file() [2/2]

template<typename getenv_functor = default_getenv, typename validator_functor = default_validator>
boost::filesystem::path jb::config_files_locations< getenv_functor, validator_functor >::find_configuration_file ( std::string const &  filename) const
inline

Find a configuration file in the computed search path.

Parameters
filenamethe basename of the configuration file.
Returns
the path to load
Exceptions
std::runtime_errorif no suitable file was found.

Definition at line 200 of file config_files_location.hpp.

References jb::bindir(), and jb::sysconfdir().


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