Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip parsing from std::string type #191

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 29 additions & 1 deletion 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,9 +270,28 @@ 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.
template <typename T>
inline T JParameterManager::Parse(const std::string& value) {
std::stringstream ss(value);
Expand All @@ -278,6 +300,12 @@ inline T JParameterManager::Parse(const std::string& value) {
return val;
}

/// @brief Specialization for handling strings that don't need parsing
template <>
inline std::string JParameterManager::Parse(const std::string& value) {
return std::string(value);
}

/// @brief Specialization for bool
template <>
inline bool JParameterManager::Parse(const std::string& value) {
Expand Down
45 changes: 45 additions & 0 deletions src/programs/tests/JParameterManagerTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ TEST_CASE("JParameterManager::SetDefaultParameter") {
jpm.SetDefaultParameter("testing:dummy_var_2", zz);
REQUIRE(zz == 77);
}

SECTION("Multiple calls to check strings with spaces") {

// basic string test
std::string x = "MyStringValue";
jpm.SetDefaultParameter("testing:dummy_var", x);
REQUIRE(x == "MyStringValue");

// string with spaces
std::string y = "My String Value With Spaces";
auto p = jpm.SetDefaultParameter("testing:dummy_var2", y);
REQUIRE(p->GetValue() == "My String Value With Spaces");

// Stringify returns identical string
REQUIRE( jpm.Stringify("My String Value With Spaces") == "My String Value With Spaces" );

// Parse returns identical string
std::string z = "My String Value With Spaces";
REQUIRE( jpm.Parse<std::string>(z) == "My String Value With Spaces" );
}
}


Expand Down Expand Up @@ -206,6 +226,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