54 MARLINMT_THROW(
"Sub-section '" + n +
"' already present in section '" +
name() +
"'" ) ;
69 std::transform(
_subsections.begin(),
_subsections.end(), names.begin(), [] (
const auto &iter) {
return iter.first ; } ) ;
82 std::vector<std::string> names(
_parameters.size() ) ;
83 std::transform(
_parameters.begin(),
_parameters.end(), names.begin(), [] (
const auto &iter) {
return iter.first ; } ) ;
109 return ( _constants.end() != _constants.find( cn ) ) ;
121 auto iter = _sections.find( sn ) ;
122 if( _sections.end() != iter ) {
125 return _sections.emplace( sn,
ConfigSection{sn} ).first->second ;
131 auto iter = _sections.find( sn ) ;
132 if( _sections.end() == iter ) {
135 return iter->second ;
141 auto iter = _sections.find( sn ) ;
142 if( _sections.end() == iter ) {
145 return iter->second ;
157 return ( _sections.find( n ) != _sections.end()) ;
163 size_t pos = str.find(
"${") ;
164 while( pos != std::string::npos ) {
165 size_t pos2 = str.find_first_of(
"}", pos+2 ) ;
166 if( pos2 == std::string::npos ) {
167 MARLINMT_THROW_T( ParseException,
"Couldn't parse constant value in string '" + str +
"'. It seems you have an open bracket '${' but it is not closed" ) ;
169 const std::string key( str.substr( pos+2 , pos2-pos-2 )) ;
170 auto iter = _constants.find( key ) ;
171 const std::string replacementValue( iter != _constants.end() ? iter->second :
"" ) ;
172 if( replacementValue.empty() ) {
173 MARLINMT_THROW_T( ParseException,
"Constant '" + key +
"' doesn't exists !" ) ;
175 str.replace( pos , (pos2+1-pos) , replacementValue ) ;
176 pos2 = pos + replacementValue.size() ;
177 pos = str.find(
"${", pos2) ;
184 std::string localprefix = prefix +
" |";
185 stream << prefix <<
" + Section: " << section.
name() << std::endl ;
187 for(
auto &pn : pnames ) {
188 stream << localprefix <<
" - " << pn <<
" = " << section.
parameter<std::string>( pn ) << std::endl ;
191 for(
auto &sec : sections ) {
198 if( not constants.empty() ) {
199 stream <<
"==== Constants =====" << std::endl ;
200 for(
auto &kv : constants ) {
201 stream <<
" * " << kv.first <<
" : " << kv.second << std::endl ;
205 for(
auto &sec : sections ) {
215 auto splitVals = details::split_string<std::string>( str,
":", 2 ) ;
216 return (2 == splitVals.size()) ? std::pair{ splitVals[0], splitVals[1] } : std::pair{
"", splitVals[0] } ;
224 if( pluginInput.first.empty() ) {
225 std::filesystem::path filepath = pluginInput.second ;
226 if( filepath.extension().string() ==
".xml" ) {
227 pluginInput.first =
"XMLConfigReader" ;
230 if( pluginInput.first.empty() ) {
231 MARLINMT_THROW(
"No config reader plugin found in config string. Please specify your config as 'plugin_name:input'" ) ;
234 if(
nullptr == reader ) {
235 MARLINMT_THROW(
"No ConfigReader plugin found for type '" + pluginInput.first +
"'" ) ;
237 reader->init( pluginInput.second ) ;
238 reader->read( cfg ) ;
246 if( pluginInput.first.empty() ) {
247 std::filesystem::path filepath = pluginInput.second ;
248 if( filepath.extension().string() ==
".xml" ) {
249 pluginInput.first =
"XMLConfigWriter" ;
252 if( pluginInput.first.empty() ) {
253 MARLINMT_THROW(
"No config writer plugin found in config string. Please specify your config as 'plugin_name:input'" ) ;
256 if(
nullptr == writer ) {
257 MARLINMT_THROW(
"No ConfigWriter plugin found for type '" + pluginInput.first +
"'" ) ;
259 writer->init( pluginInput.second ) ;
260 writer->write( cfg ) ;
const Metadata & metadata() const
Get the section metadata.
ConfigSection(const ConfigSection &)=default
ConfigSection & section(const std::string &n)
Get a subsection by name.
T parameter(const std::string &n) const
Get a parameter value as type T.
#define MARLINMT_THROW_T(ExceptionType, message)
const std::string & name() const
Get the section name.
std::vector< std::string > parameterNames() const
Get the list of parameter names.
std::ostream & operator<<(std::ostream &stream, const Configuration &cfg)
Stream operator.
ConfigSection & createSection(const std::string &sn)
Create a new section by name.
std::map< std::string, std::string > ParameterMap
bool hasSection(const std::string &n) const
Whether the subsection exists.
static void writeConfig(const std::string &str, Configuration &cfg)
Write the configuration using a ConfigWriter plugin.
std::vector< std::string > sections() const
Get the list of subsections.
std::map< std::string, std::string > ConstantsMap
Metadata _metadata
The section metdata map.
std::map< std::string, std::string > Metadata
ConfigReader base class Interface for reading configuration.
void printSection(const ConfigSection §ion, std::ostream &stream, const std::string &prefix)
static std::pair< std::string, std::string > splitPluginInput(const std::string &str)
Split the string to a pair "plugin, input".
const std::string _name
The section name.
bool empty() const
Whether the section is empty (no parameter, no subsection)
bool hasConstant(const std::string &cn) const
Whetehr the constant is registered in the configuration.
bool hasParameter(const std::string &n) const
Whether the parameter exists.
#define MARLINMT_THROW(message)
static void readConfig(const std::string &str, Configuration &cfg)
Read the configuration using a ConfigReader plugin.
ConfigSection & addSection(const std::string &n)
Create a new subsection.
ConfigSection & section(const std::string &sn)
Get a section by name.
ParameterMap _parameters
The parameter map.
ConfigWriter base class Interface for writing configuration.
std::vector< std::string > subsectionNames() const
Get the list of subsection names.
ConfigSectionMap _subsections
The subsection map.
std::unique_ptr< T > create(const std::string &name) const
Create a new plugin instance.
const ConstantsMap & constants() const
Get the constants map.
const ParameterMap & parameters() const
Get the raw parameter storage.
void replaceConstants(std::string &str) const
Replace all occurences of ${constant_name} in the input string where "constant_name" must match a reg...
ConfigSection class Holds a set of parameters and subsection.
bool hasSection(const std::string &n) const
Whether the section exists.
static PluginManager & instance()
Get the plugin manager instance.
std::vector< K > keys(const std::map< K, V > &m)