Skip to content

Commit

Permalink
Merge pull request #190 from JeffersonLab/davidl_RegisterParameter
Browse files Browse the repository at this point in the history
Add RegisterParameter and associated unit tests.
  • Loading branch information
faustus123 authored Feb 16, 2023
2 parents 9753127 + f7834e3 commit b74abf7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libraries/JANA/JApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class JApplication {
template <typename T>
JParameter* SetDefaultParameter(std::string name, T& val, std::string description="");

template <typename T>
T RegisterParameter(std::string name, const T default_val, std::string description="");

// Locating services

/// Use this in EventSources, Factories, or EventProcessors. Do not call this
Expand Down Expand Up @@ -173,6 +176,11 @@ JParameter* JApplication::SetDefaultParameter(std::string name, T& val, std::str
return m_params->SetDefaultParameter(name.c_str(), val, description);
}

template <typename T>
T JApplication::RegisterParameter(std::string name, const T default_val, std::string description) {
return m_params->RegisterParameter(name.c_str(), default_val, description);
}

template <typename T>
JParameter* JApplication::GetParameter(std::string name, T& result) {
return m_params->GetParameter(name, result);
Expand Down
23 changes: 23 additions & 0 deletions src/libraries/JANA/Services/JParameterManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class JParameterManager : public JService {
template<typename T>
JParameter* SetDefaultParameter(std::string name, T& val, std::string description = "");

template <typename T>
T RegisterParameter(std::string name, const T default_val, std::string description = "");

void FilterParameters(std::map<std::string,std::string> &parms, std::string filter="");

void ReadConfigFile(std::string name);
Expand Down Expand Up @@ -267,6 +270,26 @@ JParameter* JParameterManager::SetDefaultParameter(std::string name, T& val, std
return param;
}

/// @brief Retrieve a configuration parameter, if necessary creating it with the provided default value.
///
/// @param [in] name The parameter name. Must not contain spaces.
/// @param [in,out] default_val Value to set the desired default value to (returned if no value yet set for parameter).
/// @param [in] description Optional description, e.g. units, set or range of valid values, etc.
/// @return Current value of parameter
///
/// @details This is a convenience method that wraps SetDefaultParameter. The difference
/// is that this has the default passed by value (not by reference) and the value of the parameter is returned
/// by value. This allows a slightly different form for declaring configuration parameters with a default value.
/// e.g.
/// auto thresh = jpp->RegisterParameter("SystemA:threshold", 1.3, "threshold in MeV");
///
template <typename T>
inline T JParameterManager::RegisterParameter(std::string name, const T default_val, std::string description){
T val = default_val;
SetDefaultParameter(name.c_str(), val, description);
return val;
}


/// @brief Logic for parsing different types in a generic way
/// @throws JException in case parsing fails.
Expand Down
25 changes: 25 additions & 0 deletions src/programs/tests/JParameterManagerTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ TEST_CASE("JParameterManager_VectorParams") {
auto param = jpm.GetParameter("test", outputs);
REQUIRE(param->GetValue() == "22,49.2,42");
}
}

TEST_CASE("JParameterManager::RegisterParameter") {

JParameterManager jpm;

SECTION("Set/Get") {
int x_default = 44;
auto x_actual = jpm.RegisterParameter("testing:dummy_var", x_default);
REQUIRE(x_actual == x_default);
}

SECTION("Set/Get templated float") {
auto y_actual = jpm.RegisterParameter("testing:dummy_var2", 22.0);
REQUIRE(y_actual == 22.0);
}

SECTION("Set/Get default") {
jpm.SetParameter("testing:dummy_var", 22);
auto x_actual = jpm.RegisterParameter("testing:dummy_var", 44); // this should set the default value to 44 while keeping value at 22
auto x_default_str = jpm.FindParameter("testing:dummy_var")->GetDefault();
auto x_default = jpm.Parse<int>(x_default_str);
REQUIRE(x_actual == 22);
REQUIRE(x_default == 44);
}

}

Expand Down

0 comments on commit b74abf7

Please sign in to comment.