All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Functions
util::fhicl Namespace Reference

Classes

struct  SequenceWrapper
 Helper to return a converted sequence from FHiCL configuration. More...
 

Functions

template<typename Optional >
std::optional< typename
Optional::value_type > 
getOptionalValue (Optional const &parameter)
 Returns the value of an optional parameter as std::optional. More...
 
template<typename T , typename Optional >
getOptionalValue (Optional const &parameter, T defValue)
 Returns the value of an optional parameter, or a default value. More...
 

Function Documentation

template<typename Optional >
std::optional< typename Optional::value_type > util::fhicl::getOptionalValue ( Optional const &  parameter)

Returns the value of an optional parameter as std::optional.

Template Parameters
OptionalFHiCL optional class (e.g. fhicl::OptionalAtom)
Parameters
parameterthe optional FHiCL parameter
Returns
the value of the parameter

This utility allows to carry the information whether an optional parameter was specified or not. It may also help with single-line initialization of data members from FHiCL optional parameters, e.g.

struct Config {
fhicl::Atom<unsigned int> A {
fhicl::Comment("parameter A")
};
fhicl::OptionalAtom<unsigned int> B {
fhicl::Comment("parameter B (default: same as A)")
};
};
unsigned int fA, fB;
MyClass(Config const& config)
: fA(config.A())
, fB(util::fhicl::getOptionalValue(config.B).value_or(fA))
{}

This feature will be rendered obsolete by Redmine Issue #23653.

Definition at line 188 of file FHiCLutils.h.

188  {
189 
190  using Value_t = typename Optional::value_type;
191 
192  if (!parameter.hasValue()) return std::nullopt;
193 
194  Value_t value;
195  parameter(value);
196  return { value };
197 
198 } // util::fhicl::getOptionalValue(Optional& parameter)
temporary value
template<typename T , typename Optional >
T util::fhicl::getOptionalValue ( Optional const &  parameter,
defValue 
)

Returns the value of an optional parameter, or a default value.

Template Parameters
Ttype of the value being returned
OptionalFHiCL optional class (e.g. fhicl::OptionalAtom)
Parameters
parameterthe optional FHiCL parameter
defValuedefault value to be returned
Returns
the value of the parameter, or defValue if absent

It is usually preferable to use a non-optional FHiCL parameter with a default value, but some types are not well suited for this. Also, this function does not buy much unless the parameter reading needs to be done in a single statement, like in the initialization list of a constructor:

struct Config {
fhicl::OptionalSequenceTable<BoardCfg> Boards {
fhicl::Name("MyStruct"),
fhicl::Comment("data configuration elements")
};
};
using AllBoardCfg_t = std::vector<BoardCfg>;
AllBoardCfg_t fBoards;
MyClass(Config const& config)
: fBoards
(util::fhicl::getOptionalValue<BoardCfg>(config.Boards, AllBoardCfg_t{}))
{}

Definition at line 203 of file FHiCLutils.h.

204  { parameter(defValue); return defValue; }