advgetopt/conf_file.cpp
/** \brief Create and read a conf_file.
 *
 * This function creates a new conf_file object unless one with the same
 * filename already exists.
 *
 * If the configuration file was already loaded, then that pointer gets
 * returned instead of reloading the file. There is currently no API to
 * allow for the removal because another thread or function may have
 * the existing pointer cached and we want all instances of a configuration
 * file to be the same (i.e. if you update the value of a parameter then
 * that new value should be visible by all the users of that configuration
 * file.) Therefore, you can think of a configuration file as a global
 * variable.
 *
 * \note
 * Any number of call this function to load a given file always returns
 * exactly the same pointer.
 *
 * \todo
 * With the communicator, we will at some point implement a class
 * used to detect that a file changed, allowing us to get a signal
 * and reload the file as required. This get_conf_file() function
 * will greatly benefit from such since that way we can automatically
 * reload the configuration file. In other words, process A could
 * make a change, then process B reloads and sees the change that
 * process A made. Such an implementation will require a proper
 * locking mechanism of the configuration files while modifications
 * are being performed.
 *
 * \param[in] setup  The settings to be used in this configuration file reader.
 *
 * \return A pointer to the configuration file data.
 */
\brief Create and read a conf_file.
This function creates a new conf_file object unless one with the same
filename already exists.

If the configuration file was already loaded, then that pointer gets
returned instead of reloading the file. There is currently no API to
allow for the removal because another thread or function may have
the existing pointer cached and we want all instances of a configuration
file to be the same  Therefore, you can think of a configuration file as a global
variable.

\note
Any number of call this function to load a given file always returns
exactly the same pointer.

\todo
With the communicator, we will at some point implement a class
used to detect that a file changed, allowing us to get a signal
and reload the file as required. This get_conf_file() function
will greatly benefit from such since that way we can automatically
reload the configuration file. In other words, process A could
make a change, then process B reloads and sees the change that
process A made. Such an implementation will require a proper
locking mechanism of the configuration files while modifications
are being performed.

\param[in] setup  The settings to be used in this configuration file reader.

\return A pointer to the configuration file data.
\ brief Create and read a conf_file . This function creates a new conf_file object unless one with the same filename already exists . If the configuration file was already loaded then that pointer gets returned instead of reloading the file . There is currently no API to allow for the removal because another thread or function may have the existing pointer cached and we want all instances of a configuration file to be the same Therefore you can think of a configuration file as a global variable . \ note Any number of call this function to load a given file always returns exactly the same pointer . \ todo With the communicator we will at some point implement a class used to detect that a file changed allowing us to get a signal and reload the file as required . This get_conf_file () function will greatly benefit from such since that way we can automatically reload the configuration file . In other words process A could make a change then process B reloads and sees the change that process A made . Such an implementation will require a proper locking mechanism of the configuration files while modifications are being performed . \ param [ in ] setup The settings to be used in this configuration file reader . \ return A pointer to the configuration file data .
conf_file::pointer_t conf_file::get_conf_file(conf_file_setup const & setup)
{
    cppthread::guard lock(get_global_mutex());
    auto it(g_conf_files.find(setup.get_filename()));
    if(it != g_conf_files.end())
    {
        if(it->second->get_setup().get_config_url() != setup.get_config_url())
        {
            throw getopt_logic_error("trying to load configuration file \""
                                       + setup.get_config_url()
                                       + "\" but an existing configuration file with the same name was loaded with URL: \""
                                       + it->second->get_setup().get_config_url()
                                       + "\".");
        }
        return it->second;
    }
    conf_file::pointer_t cf(new conf_file(setup));
    g_conf_files[setup.get_filename()] = cf;
    return cf;
}
conf_file :: pointer_t conf_file :: get_conf_file ( conf_file_setup const & setup ) { cppthread :: guard lock ( get_global_mutex ( ) ) ; auto it ( g_conf_files . find ( setup . get_filename ( ) ) ) ; if ( it != g_conf_files . end ( ) ) { if ( it -> second -> get_setup ( ) . get_config_url ( ) != setup . get_config_url ( ) ) { throw getopt_logic_error ( " \" " + setup . get_config_url ( ) + " \" \" " + it -> second -> get_setup ( ) . get_config_url ( ) + " \" " ) ; } return it -> second ; } conf_file :: pointer_t cf ( new conf_file ( setup ) ) ; g_conf_files [ setup . get_filename ( ) ] = cf ; return cf ; }
\brief Create and read a conf_file.
\ brief Create and read a conf_file .
// TODO: look into not blocking forever? //