Skip to content

Commit

Permalink
added missing documentation to ParamFile.hpp functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ozmorph committed Jun 18, 2020
1 parent 0c8c233 commit df5466d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 60 deletions.
57 changes: 18 additions & 39 deletions src/ParamFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,74 +90,50 @@ void ParamReader::extract_or_exit(std::string const& param, T& output)
}

template<typename T>
bool ParamReader::extract_multiple(std::string const& param, T* output, std::size_t N, T default_value)
bool ParamReader::extract_multiple_no_default(std::string const& param, T* output, std::size_t N)
{
if (!exists(param))
{
std::cerr << "Using default value: " << default_value << std::endl;
std::fill_n(output, N, default_value);
return false;
}

std::istringstream iss(m_param_value_map[param]);
for (auto i = 0; i < N; i++)
{
if (!raw_extract(iss, output[i]))
{
std::cerr << "ERROR: Got " << i << " out of " << N << " parameters for "
<< param << ".\nUsing default value: " << default_value << std::endl;
std::fill_n(output, N, default_value);
<< param << std::endl;
return false;
}
}
return true;
}

template<typename T>
bool ParamReader::cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value)
void ParamReader::extract_multiple_or_exit(std::string const& param, T* output, std::size_t N)
{
if (conditional)
return extract_multiple(param, output, N, default_value);
std::fill_n(output, N, default_value);
return true;
if (extract_multiple_no_default(param, output, N) == false)
std::exit(1);
}

template<typename T>
bool ParamReader::extract_multiple_no_default(std::string const& param, T* output, std::size_t N)
bool ParamReader::extract_multiple(std::string const& param, T* output, std::size_t N, T default_value)
{
if (!exists(param))
if (extract_multiple_no_default(param, output, N) == false)
{
std::cerr << "Using default value: " << default_value << std::endl;
std::fill_n(output, N, default_value);
return false;
}

std::istringstream iss(m_param_value_map[param]);
for (auto i = 0; i < N; i++)
{
if (!raw_extract(iss, output[i]))
{
std::cerr << "ERROR: Got " << i << " out of " << N << " parameters for "
<< param << std::endl;
return false;
}
}
return true;
}

template<typename T>
void ParamReader::extract_multiple_or_exit(std::string const& param, T* output, std::size_t N)
bool ParamReader::cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value)
{
if (!exists(param))
std::exit(1);

std::istringstream iss(m_param_value_map[param]);
for (auto i = 0; i < N; i++)
{
if (!raw_extract(iss, output[i]))
{
std::cerr << "ERROR: Got " << i << " out of " << N << " parameters for " << param << std::endl;
std::exit(1);
}
}
if (conditional)
return extract_multiple(param, output, N, default_value);
std::fill_n(output, N, default_value);
return true;
}

void ParamReader::extract_multiple_strings_or_exit(std::string const& param, std::vector<std::string>& output, std::size_t N)
Expand All @@ -175,7 +151,7 @@ void ParamReader::extract_multiple_strings_or_exit(std::string const& param, std
std::cerr << "ERROR: Only got " << i << " out of " << N << " parameters for " << param << std::endl;
std::exit(1);
}
output.emplace_back(token);
output.push_back(std::move(token));
}
}

Expand Down Expand Up @@ -262,15 +238,18 @@ void ParamReader::parse_param_file(std::string const& param_file)
// try to read the value of this parameter into a string
std::string value_line;
std::string tmp_line;
// read every line after the parameter name until there's no more or a break
while (std::getline(param_stream, tmp_line))
{
trim(tmp_line);
// break if the next line is empty or doesn't start with a letter, number, or '-' character
if (tmp_line.empty() || !(std::isalnum(tmp_line[0]) || tmp_line[0] == '-'))
break;
if (!value_line.empty())
value_line.push_back('\n');
value_line.append(tmp_line);
}
// if there is no value found for the parameter, print an error and continue
if (value_line.empty())
{
std::cerr << "ERROR: Value is missing for parameter: " << param_line << std::endl;
Expand Down
42 changes: 21 additions & 21 deletions src/ParamFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,54 @@ class ParamReader {
* Each parameter-value pair can be extracted out by one of the extract_*()
* functions by passing a storage variable of the type expected.
*
* Note: The parameter file names passed into this function can be empty.
* @note: The parameter file names passed into this function can be empty.
*/
ParamReader(std::string const& param_file, std::string const& preparam_file, std::string const& admin_file);

/**
* Extract a single parameter value or assign the default to `output`.
*
* @note: The parameter value is checked using the numeric limits of the
* output variable's type.
* Extract a single numeric value or assign the default to `output`.
*/
template<typename T>
bool extract(std::string const& param, T& output, T default_value);

/**
* Extract and assign a single parameter value to `output` or exit the program.
*
* @note: The parameter value is checked using the numeric limits of the
* output variable's type.
* Extract and assign a single numeric value to `output` or exit the program.
*/
template<typename T>
void extract_or_exit(std::string const& param, T& output);

/**
* Extract multiple parameter values or assign the default to `N` values in `output`.
*
* @note: The parameter values are checked using the numeric limits of the
* output variable's type.
* Extract multiple numeric values to `N` numbers in `output`.
*/
template<typename T>
bool extract_multiple(std::string const& param, T* output, std::size_t N, T default_value);
bool extract_multiple_no_default(std::string const& param, T* output, std::size_t N);

/**
* Extract and assign multiple numeric values to `N` numbers in `output` or exit the program.
*/
template<typename T>
bool cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value);
void extract_multiple_or_exit(std::string const& param, T* output, std::size_t N);

/**
* Extract multiple numeric values or assign the default to `N` numbers in `output`.
*/
template<typename T>
bool extract_multiple_no_default(std::string const& param, T* output, std::size_t N);
bool extract_multiple(std::string const& param, T* output, std::size_t N, T default_value);

/**
* Extract and assign multiple parameter values to `N` values in `output` or exit the program.
*
* @note: The parameter values are checked using the numeric limits of the
* output variable's type.
* Conditionally extract multiple numeric values or assign the default to `N` numbers in `output`.
*/
template<typename T>
void extract_multiple_or_exit(std::string const& param, T* output, std::size_t N);
bool cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value);

/**
* Extract exactly `N` strings and add them to `output` or exit the program.
*/
void extract_multiple_strings_or_exit(std::string const& param, std::vector<std::string>& output, std::size_t N);

/**
* Extract a matrix of strings with `num_cols` columns per row and add them to `output` or exit the program.
*/
void extract_string_matrix_or_exit(std::string const& param, std::vector<std::vector<std::string>>& output, std::size_t num_cols);

private:
Expand Down

0 comments on commit df5466d

Please sign in to comment.