JANA2
C++ framework for multi-threaded data processing
JParameterManager Class Reference
Inheritance diagram for JParameterManager:
[legend]
Collaboration diagram for JParameterManager:
[legend]

Public Member Functions

 JParameterManager ()
 Default constructor.
 
 JParameterManager (const JParameterManager &)
 Copy constructor. More...
 
 ~JParameterManager () override
 Destructor. More...
 
void SetLogger (JLogger logger)
 
bool Exists (std::string name)
 Test whether a parameter of some name exists. More...
 
JParameterFindParameter (std::string)
 Retrieves a JParameter object of some name. More...
 
void PrintParameters ()
 
void PrintParameters (int verbosity, int strictness)
 Prints parameters to stdout. More...
 
std::map< std::string, JParameter * > GetAllParameters ()
 Access entire map of parameters. More...
 
template<typename T >
JParameterGetParameter (std::string name, T &val)
 Retrieves a JParameter and stores its value. More...
 
template<typename T >
GetParameterValue (std::string name)
 Retrieves a parameter value. More...
 
template<typename T >
JParameterSetParameter (std::string name, T val)
 Sets a configuration parameter. More...
 
template<typename T >
JParameterSetDefaultParameter (std::string name, T &val, std::string description="")
 Retrieve a configuration parameter, if necessary creating it with the provided default value. More...
 
template<typename T >
RegisterParameter (std::string name, const T default_val, std::string description="")
 Retrieve a configuration parameter, if necessary creating it with the provided default value. More...
 
void FilterParameters (std::map< std::string, std::string > &parms, std::string filter="")
 Copy a list of all parameters into the supplied map, replacing its current contents. More...
 
void ReadConfigFile (std::string name)
 Load parameters from a configuration file. More...
 
void WriteConfigFile (std::string name)
 Write parameters out to an ASCII configuration file. More...
 
JLogger GetLogger (const std::string &prefix)
 
template<>
void Parse (const std::string &value, std::string &out)
 Specialization for string. More...
 
template<>
void Parse (const std::string &value, bool &val)
 Specialization for bool.
 
template<typename T , size_t N>
void Parse (const std::string &value, std::array< T, N > &val)
 
template<>
void Parse (const std::string &in, JLogger::Level &out)
 Specialization for JLogger::Level enum.
 
template<>
std::string Stringify (const float &value)
 
template<>
std::string Stringify (const double &value)
 
template<>
std::string Stringify (const long double &value)
 
template<>
std::string Stringify (const std::string &value)
 
template<>
std::string Stringify (const JInspector::Format &value)
 
template<>
void Parse (const std::string &value, JInspector::Format &val)
 
- Public Member Functions inherited from JService
virtual ~JService ()=default
 acquire_services is a callback which allows the user to configure a JService which relies on other JServices. More...
 
void DoInit (JServiceLocator *)
 
virtual void Init ()
 
virtual void acquire_services (JServiceLocator *)
 
- Public Member Functions inherited from jana::components::JComponent
void SetLevel (JEventLevel level)
 
void SetCallbackStyle (CallbackStyle style)
 
void SetPrefix (std::string prefix)
 
void SetTypeName (std::string type_name)
 For convenience, we provide a NAME_OF_THIS macro so that the user doesn't have to store the type name as a string, because that could get out of sync if automatic refactoring tools are used.
 
JApplicationGetApplication () const
 
JLoggerGetLogger ()
 
std::string GetPrefix () const
 
JEventLevel GetLevel () const
 
std::string GetLoggerName () const
 
std::string GetPluginName () const
 
void SetLoggerName (std::string logger_name)
 
void SetPluginName (std::string plugin_name)
 
std::string GetTypeName () const
 
virtual void Summarize (JComponentSummary &) const
 
CallbackStyle GetCallbackStyle () const
 
Status GetStatus () const
 
void SetApplication (JApplication *app)
 
void SetLogger (JLogger logger)
 
template<typename F >
void CallWithJExceptionWrapper (std::string func_name, F func)
 
void RegisterParameter (ParameterBase *parameter)
 
void RegisterService (ServiceBase *service)
 
void ConfigureAllParameters (std::map< std::string, std::string > fields)
 

Static Public Member Functions

template<typename T >
static void Parse (const std::string &value, T &out)
 Basic implementation of Parse for C++14 and earlier.
 
template<typename T >
static void Parse (const std::string &value, std::vector< T > &out)
 Specialization for std::vector<std::string>
 
template<typename T , size_t arrSize>
static void Parse (const std::string &value, std::array< T, arrSize > &out)
 
template<typename T >
static std::string Stringify (const T &value)
 Basic implementation of Parse for C++14 and earlier.
 
template<typename T >
static std::string Stringify (const std::vector< T > &values)
 
template<typename T , size_t N>
static std::string Stringify (const std::array< T, N > &values)
 
template<typename T >
static bool Equals (const T &lhs, const T &rhs)
 
static std::string ToLower (const std::string &name)
 Converts a parameter name to all lowercase. More...
 

Additional Inherited Members

- Public Types inherited from jana::components::JComponent
enum class  Status {
  Uninitialized ,
  Initialized ,
  Opened ,
  Closed ,
  Finalized
}
 
enum class  CallbackStyle {
  LegacyMode ,
  ExpertMode ,
  DeclarativeMode
}
 
- Protected Attributes inherited from jana::components::JComponent
std::vector< ParameterBase * > m_parameters
 
std::vector< ServiceBase * > m_services
 
JEventLevel m_level = JEventLevel::PhysicsEvent
 
CallbackStyle m_callback_style = CallbackStyle::LegacyMode
 
std::string m_prefix
 
std::string m_plugin_name
 
std::string m_logger_name
 
std::string m_type_name
 
Status m_status = Status::Uninitialized
 
std::mutex m_mutex
 
JApplicationm_app = nullptr
 
JLogger m_logger
 

Constructor & Destructor Documentation

◆ JParameterManager()

JParameterManager::JParameterManager ( const JParameterManager other)

Copy constructor.

Does a deep copy of the JParameter objects to avoid double frees.

◆ ~JParameterManager()

JParameterManager::~JParameterManager ( )
override

Destructor.

Destroys all contained JParameter objects.

Member Function Documentation

◆ Exists()

bool JParameterManager::Exists ( std::string  name)

Test whether a parameter of some name exists.

Parameters
[in]namethe parameter name
Returns
whether that parameter was found

◆ FilterParameters()

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

Copy a list of all parameters into the supplied map, replacing its current contents.

Parameters
[in]filterEmpty by default. If the filter string is non-empty, it is used to filter out all parameters whose key does not start with the filter string. Furthermore, the filter string is removed from the keys.
[out]parmsA map of {key: string, parameter: string}. The contents of this map are replaced on each call. Parameter values are returned as strings rather than as JParameter objects, which means we don't have to worry about ownership. Returned parameters are marked as used.

◆ FindParameter()

JParameter * JParameterManager::FindParameter ( std::string  name)

Retrieves a JParameter object of some name.

Parameters
[in]namethe parameter name
Returns
a pointer to the JParameter.
Note
The JParameter pointer is still owned by the JParameterManager, so don't delete it.

◆ GetAllParameters()

std::map< std::string, JParameter * > JParameterManager::GetAllParameters ( )

Access entire map of parameters.

Returns
The parameter map

Use this to do things like writing all parameters out to a non-standard format. This creates a copy of the map. Any modifications you make to the map itself won't propagate back to the JParameterManager. However, any modifications you make to the enclosed JParameters will. Prefer using SetParameter, SetDefaultParameter, FindParameter, or FilterParameters instead.

◆ GetParameter()

template<typename T >
JParameter * JParameterManager::GetParameter ( std::string  name,
T &  val 
)

Retrieves a JParameter and stores its value.

Parameters
[in]nameThe name of the parameter to retrieve
[out]valReference to where the parameter value should be stored
Returns
The corresponding JParameter representation, which provides us with additional information such as the default value, or nullptr if not found.

References Parse(), and ToLower().

◆ GetParameterValue()

template<typename T >
T JParameterManager::GetParameterValue ( std::string  name)

Retrieves a parameter value.

Parameters
[in]nameThe name of the parameter to retrieve
Returns
The parameter value
Exceptions
JExceptionin case the parameter is not found

References Parse(), and ToLower().

◆ Parse()

template<>
void JParameterManager::Parse ( const std::string &  value,
std::string &  out 
)
inline

Specialization for string.

The stream operator is not only redundant here, but it also splits the string (see Issue #191)

◆ PrintParameters()

void JParameterManager::PrintParameters ( int  verbosity,
int  strictness 
)

Prints parameters to stdout.

Parameters
[in]intverbosity 0: Don't show parameters table 1: Show parameters with user-provided values only 2: Show parameters with default values, except those marked "advanced" 3: Show parameters with default values, including those marked "advanced"
[in]intstrictness 0: Ignore unused parameters 1: Warn on unused parameters 2: Throw on unused parameters

◆ ReadConfigFile()

void JParameterManager::ReadConfigFile ( std::string  filename)

Load parameters from a configuration file.

Parameters
filenamePath to the configuration file

The file should have the form:

    key1 value1
    key2 value2
    ...
    

There should be a space between the key and the value. The key may contain no spaces. The value is taken as the rest of the line up to, but not including the newline. A key may be specified with no value and the value will be set to "1". A "#" charater will discard the remaining characters in a line up to the next newline. Lines starting with "#" are ignored completely. Lines with no characters (except for the newline) are ignored.

◆ RegisterParameter()

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

Retrieve a configuration parameter, if necessary creating it with the provided default value.

Parameters
[in]nameThe parameter name. Must not contain spaces.
[in,out]default_valValue to set the desired default value to (returned if no value yet set for parameter).
[in]descriptionOptional description, e.g. units, set or range of valid values, etc.
Returns
Current value of parameter

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");

References SetDefaultParameter().

◆ SetDefaultParameter()

template<typename T >
JParameter * JParameterManager::SetDefaultParameter ( std::string  name,
T &  val,
std::string  description = "" 
)

Retrieve a configuration parameter, if necessary creating it with the provided default value.

Parameters
[in]nameThe parameter name. Must not contain spaces.
[in,out]valReference to a variable which has been set to the desired default value.
[in]descriptionOptional description, e.g. units, set or range of valid values, etc.

Upon entry, the value in "val" should be set to the desired default value. It will be overwritten if a value for the parameter already exists because it was given by the user either on the command line or in a configuration file. If the parameter does not already exist, it is created and its value set to that of "val". Upon exit, "val" will always contain the value that should be used for event processing.

If a parameter with the given name already exists, it will be checked to see if the parameter already has a default value assigned (this is kept separate from the actual value of the parameter used and is maintained purely for bookkeeping purposes). If it does not have a default value, then the value of "val" upon entry is saved as the default. If it does have a default, then the value of the default is compared to the value of "val" upon entry. If the two do not match, then a warning message is printed to indicate to the user that two different default values are being set for this parameter.

Parameters specified on the command line using the "-Pkey=value" syntax will not have a default value at the time the parameter is created.

References Parse(), Stringify(), and ToLower().

Referenced by JSignalHandler::register_handlers(), and RegisterParameter().

◆ SetParameter()

template<typename T >
JParameter * JParameterManager::SetParameter ( std::string  name,
val 
)

Sets a configuration parameter.

Parameters
[in]nameThe parameter name
[in]valThe parameter value. This may be typed, or it may be a string.
Returns
Pointer to the JParameter that was either created or updated. Note that this is owned by this JParameterManager, so do not delete.

References Stringify(), and ToLower().

◆ ToLower()

std::string JParameterManager::ToLower ( const std::string &  name)
static

Converts a parameter name to all lowercase.

When accessing the m_parameters map, strings are always converted to lower case. This makes configuration parameters case-insensitive in effect, while still preserving the user's choice of case in the parameter name.

Referenced by GetParameter(), GetParameterValue(), SetDefaultParameter(), and SetParameter().

◆ WriteConfigFile()

void JParameterManager::WriteConfigFile ( std::string  filename)

Write parameters out to an ASCII configuration file.

Parameters
[in]filenamePath to the configuration file

The file is written in a format compatible with reading in via ReadConfigFile().


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