OpenMS
Param.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Marc Sturm, Clemens Groepl $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 #include <OpenMS/OpenMSConfig.h>
13 
14 #include <set>
15 #include <string>
16 #include <map>
17 
18 namespace OpenMS
19 {
20 
21  namespace Logger
22  {
23  class LogStream;
24  }
25 
43  class OPENMS_DLLAPI Param
44  {
45 public:
46 
48  struct OPENMS_DLLAPI ParamEntry
49  {
53  ParamEntry(const std::string& n, const ParamValue& v, const std::string& d, const std::vector<std::string>& t = std::vector<std::string>());
55  ParamEntry(const ParamEntry&) = default;
57  ParamEntry(ParamEntry&&) = default;
60 
62  ParamEntry& operator=(const ParamEntry&) = default;
64  ParamEntry& operator=(ParamEntry&&) & = default;
65 
67  bool isValid(std::string& message) const;
69  bool operator==(const ParamEntry& rhs) const;
70 
72  std::string name;
74  std::string description;
78  std::set<std::string> tags;
80 
81  double min_float;
82  double max_float;
83  int min_int;
84  int max_int;
85  std::vector<std::string> valid_strings;
87  };
88 
90  struct OPENMS_DLLAPI ParamNode
91  {
93  typedef std::vector<ParamNode>::iterator NodeIterator;
95  typedef std::vector<ParamEntry>::iterator EntryIterator;
97  typedef std::vector<ParamNode>::const_iterator ConstNodeIterator;
99  typedef std::vector<ParamEntry>::const_iterator ConstEntryIterator;
100 
104  ParamNode(const std::string& n, const std::string& d);
106  ParamNode(const ParamNode&) = default;
108  ParamNode(ParamNode&&) = default;
111 
113  ParamNode& operator=(const ParamNode&) = default;
115  ParamNode& operator=(ParamNode&&) & = default;
116 
118  bool operator==(const ParamNode& rhs) const;
119 
125  EntryIterator findEntry(const std::string& name);
131  NodeIterator findNode(const std::string& name);
137  ParamNode* findParentOf(const std::string& name);
143  ParamEntry* findEntryRecursive(const std::string& name);
144 
146  void insert(const ParamNode& node, const std::string& prefix = "");
148  void insert(const ParamEntry& entry, const std::string& prefix = "");
150  size_t size() const;
152  std::string suffix(const std::string& key) const;
153 
155  std::string name;
157  std::string description;
159  std::vector<ParamEntry> entries;
161  std::vector<ParamNode> nodes;
162  };
163 
164 public:
165 
167  class OPENMS_DLLAPI ParamIterator
168  {
169 public:
171  struct OPENMS_DLLAPI TraceInfo
172  {
174  inline TraceInfo(const std::string& n, const std::string& d, bool o) :
175  name(n),
176  description(d),
177  opened(o)
178  {
179  }
180 
182  std::string name;
184  std::string description;
186  bool opened;
187  };
188 
204  bool operator==(const ParamIterator& rhs) const;
206  bool operator!=(const ParamIterator& rhs) const;
208  std::string getName() const;
210  const std::vector<TraceInfo>& getTrace() const;
211 
212 protected:
216  int current_;
218  std::vector<const Param::ParamNode*> stack_;
220  std::vector<TraceInfo> trace_;
221 
222  };
223 
225  Param();
226 
228  Param(const Param&) = default;
229 
231  Param(Param&&) = default;
232 
235 
237  Param& operator=(const Param&) = default;
238 
240  Param& operator=(Param&&) & = default;
241 
243  bool operator==(const Param& rhs) const;
244 
247 
250 
252 
253 
262  void setValue(const std::string& key, const ParamValue& value, const std::string& description = "", const std::vector<std::string>& tags = std::vector<std::string>());
263 
269  const ParamValue& getValue(const std::string& key) const;
270 
276  ParamValue::ValueType getValueType(const std::string& key) const;
277 
283  const ParamEntry& getEntry(const std::string& key) const;
284 
291  bool exists(const std::string& key) const;
292 
299  bool hasSection(const std::string& key) const;
300 
307  ParamIterator findFirst(const std::string& leaf) const;
308 
316  ParamIterator findNext(const std::string& leaf, const ParamIterator& start_leaf) const;
318 
320 
321 
330  void addTag(const std::string& key, const std::string& tag);
331 
338  void addTags(const std::string& key, const std::vector<std::string>& tags);
339 
348  bool hasTag(const std::string& key, const std::string& tag) const;
349 
355  std::vector<std::string> getTags(const std::string& key) const;
356 
362  void clearTags(const std::string& key);
364 
365 
367 
368 
374  const std::string& getDescription(const std::string& key) const;
375 
384  void setSectionDescription(const std::string& key, const std::string& description);
385 
391  const std::string& getSectionDescription(const std::string& key) const;
392 
398  void addSection(const std::string& key, const std::string& description);
400 
402 
403 
405  size_t size() const;
406 
408  bool empty() const;
409 
411  void clear();
412 
415  void insert(const std::string& prefix, const Param& param);
416 
426  void remove(const std::string& key);
427 
436  void removeAll(const std::string& prefix);
437 
445  Param copy(const std::string& prefix, bool remove_prefix = false) const;
446 
456  Param copySubset(const Param& subset) const;
457 
463  bool update(const Param& p_outdated, const bool add_unknown = false);
464 
470  bool update(const Param& p_outdated, const bool add_unknown, Logger::LogStream& stream);
471 
472 
493  bool update(const Param& p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream& stream);
494 
500  void merge(const Param& toMerge);
501 
503 
504 
506 
507 
516  void setDefaults(const Param& defaults, const std::string& prefix = "", bool showMessage = false);
517 
537  void checkDefaults(const std::string& name, const Param& defaults, const std::string& prefix = "") const;
539 
541 
542 
550  void setValidStrings(const std::string& key, const std::vector<std::string>& strings);
551 
557  const std::vector<std::string>& getValidStrings(const std::string& key) const;
558 
566  void setMinInt(const std::string& key, int min);
567 
575  void setMaxInt(const std::string& key, int max);
576 
584  void setMinFloat(const std::string& key, double min);
585 
593  void setMaxFloat(const std::string& key, double max);
595 
597 
598 
616  void parseCommandLine(const int argc, const char** argv, const std::string& prefix = "");
617 
631  void parseCommandLine(const int argc, const char** argv, const std::map<std::string, std::string>& options_with_one_argument, const std::map<std::string, std::string>& options_without_argument, const std::map<std::string, std::string>& options_with_multiple_argument, const std::string& misc = "misc", const std::string& unknown = "unknown");
632 
634 
635 protected:
636 
642  ParamEntry& getEntry_(const std::string& key) const;
643 
645  Param(const Param::ParamNode& node);
646 
649  };
650 
652  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const Param& param);
653 
654 } // namespace OpenMS
655 
Log Stream Class.
Definition: LogStream.h:292
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition: ParamValue.h:29
ValueType
Supported types for ParamValue.
Definition: ParamValue.h:38
Forward const iterator for the Param class.
Definition: Param.h:168
bool operator!=(const ParamIterator &rhs) const
Equality operator.
ParamIterator operator++(int)
Postfix increment operator.
const Param::ParamEntry & operator*()
Dereferencing.
const Param::ParamNode * root_
Pointer to the root node.
Definition: Param.h:214
ParamIterator(const Param::ParamNode &root)
Constructor for begin iterator.
std::vector< TraceInfo > trace_
Node traversal data during last ++ operation.
Definition: Param.h:220
std::string getName() const
Returns the absolute path of the current element (including all sections)
const std::vector< TraceInfo > & getTrace() const
Returns the traceback of the opened and closed sections.
const Param::ParamEntry * operator->()
Dereferencing.
int current_
Index of the current ParamEntry (-1 means invalid)
Definition: Param.h:216
ParamIterator & operator++()
Prefix increment operator.
std::vector< const Param::ParamNode * > stack_
Pointers to the ParamNodes we are in.
Definition: Param.h:218
ParamIterator()
Default constructor used to create a past-the-end iterator.
bool operator==(const ParamIterator &rhs) const
Equality operator.
Management and storage of parameters / INI files.
Definition: Param.h:44
bool hasTag(const std::string &key, const std::string &tag) const
Returns if the parameter key has a tag.
bool update(const Param &p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream &stream)
Rescue parameter values from p_outdated to current param.
size_t size() const
Returns the number of entries (leafs).
ParamEntry & getEntry_(const std::string &key) const
Returns a mutable reference to a parameter entry.
void addTag(const std::string &key, const std::string &tag)
Adds the tag tag to the entry key.
void setValidStrings(const std::string &key, const std::vector< std::string > &strings)
Sets the valid strings for the parameter key.
void clearTags(const std::string &key)
Removes all tags from the entry key.
void remove(const std::string &key)
Remove the entry key or a section key (when suffix is ':')
Param()
Default constructor.
Param & operator=(Param &&) &=default
Move assignment operator.
Param(const Param &)=default
Copy constructor.
std::vector< std::string > getTags(const std::string &key) const
Returns the tags of entry key.
bool update(const Param &p_outdated, const bool add_unknown=false)
Rescue parameter values from p_outdated to current param.
Param copy(const std::string &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
bool operator==(const Param &rhs) const
Equality operator.
const ParamEntry & getEntry(const std::string &key) const
Returns the whole parameter entry.
void addSection(const std::string &key, const std::string &description)
Adds a parameter section under the path key with the given description.
ParamIterator begin() const
Begin iterator for the internal tree.
const std::string & getDescription(const std::string &key) const
Returns the description of a parameter.
bool empty() const
Returns if there are no entries.
Param(Param &&)=default
Move constructor.
Param & operator=(const Param &)=default
Assignment operator.
Param::ParamNode root_
Invisible root node that stores all the data.
Definition: Param.h:648
void setMaxFloat(const std::string &key, double max)
Sets the maximum value for the floating point or floating point list parameter key.
void insert(const std::string &prefix, const Param &param)
bool exists(const std::string &key) const
Tests if a parameter is set (expecting its fully qualified name, e.g., TextExporter:1:proteins_only)
void removeAll(const std::string &prefix)
Remove all entries that start with prefix.
ParamIterator findFirst(const std::string &leaf) const
Find leaf node by name (if it exists).
void setMaxInt(const std::string &key, int max)
Sets the maximum value for the integer or integer list parameter key.
ParamValue::ValueType getValueType(const std::string &key) const
Returns the type of a parameter.
void parseCommandLine(const int argc, const char **argv, const std::map< std::string, std::string > &options_with_one_argument, const std::map< std::string, std::string > &options_without_argument, const std::map< std::string, std::string > &options_with_multiple_argument, const std::string &misc="misc", const std::string &unknown="unknown")
Parses command line arguments to specified key locations.
const ParamValue & getValue(const std::string &key) const
Returns a value of a parameter.
void checkDefaults(const std::string &name, const Param &defaults, const std::string &prefix="") const
Checks the current parameter entries against given defaults.
ParamIterator end() const
End iterator for the internal tree.
void merge(const Param &toMerge)
Adds missing parameters from the given param toMerge to this param. Existing parameters will not be m...
void clear()
Deletes all entries.
ParamIterator findNext(const std::string &leaf, const ParamIterator &start_leaf) const
Find next leaf node by name (if it exists), not considering the start_leaf.
void addTags(const std::string &key, const std::vector< std::string > &tags)
Adds the tags in the list tags to the entry key.
void setMinInt(const std::string &key, int min)
Sets the minimum value for the integer or integer list parameter key.
const std::vector< std::string > & getValidStrings(const std::string &key) const
Gets he valid strings for the parameter key.
Param(const Param::ParamNode &node)
Constructor from a node which is used as root node.
bool hasSection(const std::string &key) const
Checks whether a section is present.
void parseCommandLine(const int argc, const char **argv, const std::string &prefix="")
Parses command line arguments.
~Param()
Destructor.
void setSectionDescription(const std::string &key, const std::string &description)
Sets a description for an existing section.
bool update(const Param &p_outdated, const bool add_unknown, Logger::LogStream &stream)
Rescue parameter values from p_outdated to current param.
Param copySubset(const Param &subset) const
Returns a new Param object containing all entries in the given subset.
void setValue(const std::string &key, const ParamValue &value, const std::string &description="", const std::vector< std::string > &tags=std::vector< std::string >())
Sets a value.
const std::string & getSectionDescription(const std::string &key) const
Returns the description corresponding to the section with name key.
void setMinFloat(const std::string &key, double min)
Sets the minimum value for the floating point or floating point list parameter key.
void setDefaults(const Param &defaults, const std::string &prefix="", bool showMessage=false)
Insert all values of defaults and adds the prefix prefix, if the values are not already set.
int verbose
Verbosity level ( "-v" is 1 and "-V" is 2 )
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:122
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
Parameter entry used to store the actual information inside of a Param entry.
Definition: Param.h:49
ParamEntry(const std::string &n, const ParamValue &v, const std::string &d, const std::vector< std::string > &t=std::vector< std::string >())
Constructor with name, description, value and advanced flag.
double max_float
Default: std::numeric_limits<double>::max()
Definition: Param.h:82
std::string description
Description of the entry.
Definition: Param.h:74
ParamEntry(ParamEntry &&)=default
Move constructor.
ParamEntry & operator=(ParamEntry &&) &=default
Move assignment operator.
ParamEntry & operator=(const ParamEntry &)=default
Assignment operator.
std::vector< std::string > valid_strings
Default: empty.
Definition: Param.h:85
double min_float
Default: - std::numeric_limits<double>::max()
Definition: Param.h:81
std::string name
Name of the entry.
Definition: Param.h:72
ParamValue value
Value associated with the entry.
Definition: Param.h:76
int min_int
Default: - std::numeric_limits<Int>::max()
Definition: Param.h:83
bool operator==(const ParamEntry &rhs) const
Equality operator (only name and value are compared)
int max_int
Default: std::numeric_limits<Int>::max()
Definition: Param.h:84
ParamEntry()
Default constructor.
std::set< std::string > tags
Tags list, used e.g. for advanced parameter tag.
Definition: Param.h:78
bool isValid(std::string &message) const
Check if 'value' fulfills restrictions.
ParamEntry(const ParamEntry &)=default
Copy constructor.
Struct that captures information on entered / left nodes for ParamIterator.
Definition: Param.h:172
std::string description
description of the node
Definition: Param.h:184
TraceInfo(const std::string &n, const std::string &d, bool o)
Constructor with name, description, and open flag.
Definition: Param.h:174
std::string name
name of the node
Definition: Param.h:182
bool opened
If it was opened (true) or closed (false)
Definition: Param.h:186
Node inside a Param object which is used to build the internal tree.
Definition: Param.h:91
ParamNode(const std::string &n, const std::string &d)
Constructor with name and description.
std::string suffix(const std::string &key) const
Returns the name suffix of a key (the part behind the last ':' character)
std::vector< ParamNode > nodes
Subnodes.
Definition: Param.h:161
ParamNode & operator=(const ParamNode &)=default
Assignment operator.
std::vector< ParamNode >::const_iterator ConstNodeIterator
Iterator for child nodes.
Definition: Param.h:97
size_t size() const
Returns the number of entries in the whole subtree.
ParamNode(const ParamNode &)=default
Copy constructor.
std::string description
Description of the node.
Definition: Param.h:157
NodeIterator findNode(const std::string &name)
Look up subnode of this node (local search)
ParamNode & operator=(ParamNode &&) &=default
Move assignment operator.
EntryIterator findEntry(const std::string &name)
Look up entry of this node (local search)
void insert(const ParamNode &node, const std::string &prefix="")
Inserts a node with the given prefix.
std::vector< ParamEntry >::iterator EntryIterator
Iterator for entries.
Definition: Param.h:95
ParamNode * findParentOf(const std::string &name)
Look up the parent node of the entry or node corresponding to name (tree search)
std::string name
Name of the node.
Definition: Param.h:155
ParamEntry * findEntryRecursive(const std::string &name)
Look up the entry corresponding to name (tree search)
std::vector< ParamNode >::iterator NodeIterator
Iterator for child nodes.
Definition: Param.h:93
void insert(const ParamEntry &entry, const std::string &prefix="")
Inserts an entry with the given prefix.
std::vector< ParamEntry >::const_iterator ConstEntryIterator
Iterator for entries.
Definition: Param.h:99
ParamNode()
Default constructor.
std::vector< ParamEntry > entries
Entries (leafs) in the node.
Definition: Param.h:159
bool operator==(const ParamNode &rhs) const
Equality operator (name, entries and subnodes are compared)
ParamNode(ParamNode &&)=default
Move constructor.