OpenMS
Loading...
Searching...
No Matches
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 <cstddef>
15#include <iterator>
16#include <set>
17#include <string>
18#include <map>
19
20namespace OpenMS
21{
22
23 namespace Logger
24 {
25 class LogStream;
26 }
27
45 class OPENMS_DLLAPI Param
46 {
47public:
48
50 struct OPENMS_DLLAPI ParamEntry
51 {
55 ParamEntry(const std::string& n, const ParamValue& v, const std::string& d, const std::vector<std::string>& t = std::vector<std::string>());
57 ParamEntry(const ParamEntry&) = default;
59 ParamEntry(ParamEntry&&) = default;
62
64 ParamEntry& operator=(const ParamEntry&) = default;
67
69 bool isValid(std::string& message) const;
71 bool operator==(const ParamEntry& rhs) const;
72
74 std::string name;
76 std::string description;
80 std::set<std::string> tags;
82
83 double min_float;
84 double max_float;
85 int min_int;
86 int max_int;
87 std::vector<std::string> valid_strings;
89 };
90
92 struct OPENMS_DLLAPI ParamNode
93 {
95 typedef std::vector<ParamNode>::iterator NodeIterator;
97 typedef std::vector<ParamEntry>::iterator EntryIterator;
99 typedef std::vector<ParamNode>::const_iterator ConstNodeIterator;
101 typedef std::vector<ParamEntry>::const_iterator ConstEntryIterator;
102
106 ParamNode(const std::string& n, const std::string& d);
108 ParamNode(const ParamNode&) = default;
110 ParamNode(ParamNode&&) = default;
113
115 ParamNode& operator=(const ParamNode&) = default;
118
120 bool operator==(const ParamNode& rhs) const;
121
127 EntryIterator findEntry(const std::string& name);
133 NodeIterator findNode(const std::string& name);
139 ParamNode* findParentOf(const std::string& name);
145 ParamEntry* findEntryRecursive(const std::string& name);
146
148 void insert(const ParamNode& node, const std::string& prefix = "");
150 void insert(const ParamEntry& entry, const std::string& prefix = "");
152 size_t size() const;
154 std::string suffix(const std::string& key) const;
155
157 std::string name;
159 std::string description;
161 std::vector<ParamEntry> entries;
163 std::vector<ParamNode> nodes;
164 };
165
166public:
167
169 class OPENMS_DLLAPI ParamIterator
170 {
171public:
172 // Iterator type definitions for C++ standard library compatibility
173 using iterator_category = std::forward_iterator_tag;
175 using difference_type = std::ptrdiff_t;
178
180 struct OPENMS_DLLAPI TraceInfo
181 {
183 inline TraceInfo(const std::string& n, const std::string& d, bool o) :
184 name(n),
185 description(d),
186 opened(o)
187 {
188 }
189
191 std::string name;
193 std::string description;
195 bool opened;
196 };
197
213 bool operator==(const ParamIterator& rhs) const;
215 bool operator!=(const ParamIterator& rhs) const;
217 std::string getName() const;
219 const std::vector<TraceInfo>& getTrace() const;
220
221protected:
227 std::vector<const Param::ParamNode*> stack_;
229 std::vector<TraceInfo> trace_;
230
231 };
232
235
237 Param(const Param&) = default;
238
240 Param(Param&&) = default;
241
244
246 Param& operator=(const Param&) = default;
247
249 Param& operator=(Param&&) & = default;
250
252 bool operator==(const Param& rhs) const;
253
256
259
261
262
271 void setValue(const std::string& key, const ParamValue& value, const std::string& description = "", const std::vector<std::string>& tags = std::vector<std::string>());
272
278 const ParamValue& getValue(const std::string& key) const;
279
285 ParamValue::ValueType getValueType(const std::string& key) const;
286
292 const ParamEntry& getEntry(const std::string& key) const;
293
300 bool exists(const std::string& key) const;
301
308 bool hasSection(const std::string& key) const;
309
316 ParamIterator findFirst(const std::string& leaf) const;
317
325 ParamIterator findNext(const std::string& leaf, const ParamIterator& start_leaf) const;
327
329
330
339 void addTag(const std::string& key, const std::string& tag);
340
347 void addTags(const std::string& key, const std::vector<std::string>& tags);
348
357 bool hasTag(const std::string& key, const std::string& tag) const;
358
364 std::vector<std::string> getTags(const std::string& key) const;
365
371 void clearTags(const std::string& key);
373
374
376
377
383 const std::string& getDescription(const std::string& key) const;
384
393 void setSectionDescription(const std::string& key, const std::string& description);
394
400 const std::string& getSectionDescription(const std::string& key) const;
401
407 void addSection(const std::string& key, const std::string& description);
409
411
412
414 size_t size() const;
415
417 bool empty() const;
418
420 void clear();
421
424 void insert(const std::string& prefix, const Param& param);
425
435 void remove(const std::string& key);
436
445 void removeAll(const std::string& prefix);
446
454 Param copy(const std::string& prefix, bool remove_prefix = false) const;
455
465 Param copySubset(const Param& subset) const;
466
472 bool update(const Param& p_outdated, const bool add_unknown = false);
473
479 bool update(const Param& p_outdated, const bool add_unknown, Logger::LogStream& stream);
480
481
502 bool update(const Param& p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream& stream);
503
509 void merge(const Param& toMerge);
510
512
513
515
516
525 void setDefaults(const Param& defaults, const std::string& prefix = "", bool showMessage = false);
526
546 void checkDefaults(const std::string& name, const Param& defaults, const std::string& prefix = "") const;
548
550
551
559 void setValidStrings(const std::string& key, const std::vector<std::string>& strings);
560
566 const std::vector<std::string>& getValidStrings(const std::string& key) const;
567
575 void setMinInt(const std::string& key, int min);
576
584 void setMaxInt(const std::string& key, int max);
585
593 void setMinFloat(const std::string& key, double min);
594
602 void setMaxFloat(const std::string& key, double max);
604
606
607
625 void parseCommandLine(const int argc, const char** argv, const std::string& prefix = "");
626
640 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");
641
643
644protected:
645
651 ParamEntry& getEntry_(const std::string& key) const;
652
655
658 };
659
661 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const Param& param);
662
663} // namespace OpenMS
664
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:31
ValueType
Supported types for ParamValue.
Definition ParamValue.h:40
Forward const iterator for the Param class.
Definition Param.h:170
bool operator!=(const ParamIterator &rhs) const
Equality operator.
ParamIterator operator++(int)
Postfix increment operator.
const Param::ParamNode * root_
Pointer to the root node.
Definition Param.h:223
ParamIterator(const Param::ParamNode &root)
Constructor for begin iterator.
std::vector< TraceInfo > trace_
Node traversal data during last ++ operation.
Definition Param.h:229
std::forward_iterator_tag iterator_category
Definition Param.h:173
const std::vector< TraceInfo > & getTrace() const
Returns the traceback of the opened and closed sections.
std::string getName() const
Returns the absolute path of the current element (including all sections)
const Param::ParamEntry * operator->()
Dereferencing.
int current_
Index of the current ParamEntry (-1 means invalid)
Definition Param.h:225
const Param::ParamEntry & operator*()
Dereferencing.
std::vector< const Param::ParamNode * > stack_
Pointers to the ParamNodes we are in.
Definition Param.h:227
std::ptrdiff_t difference_type
Definition Param.h:175
ParamIterator & operator++()
Prefix increment operator.
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:46
const ParamEntry & getEntry(const std::string &key) const
Returns the whole parameter entry.
Param & operator=(Param &&) &=default
Move assignment operator.
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.
const ParamValue & getValue(const std::string &key) const
Returns a value of a parameter.
size_t size() const
Returns the number of entries (leafs).
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(const Param &)=default
Copy constructor.
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.
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.
bool empty() const
Returns if there are no entries.
Param(Param &&)=default
Move constructor.
const std::string & getDescription(const std::string &key) const
Returns the description of a parameter.
ParamEntry & getEntry_(const std::string &key) const
Returns a mutable reference to a parameter entry.
Param::ParamNode root_
Invisible root node that stores all the data.
Definition Param.h:657
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.
const std::vector< std::string > & getValidStrings(const std::string &key) const
Gets he valid strings for the 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.
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.
std::vector< std::string > getTags(const std::string &key) const
Returns the tags of entry 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 & operator=(const Param &)=default
Assignment operator.
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.
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:51
ParamEntry & operator=(const ParamEntry &)=default
Assignment operator.
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:84
std::string description
Description of the entry.
Definition Param.h:76
ParamEntry(ParamEntry &&)=default
Move constructor.
ParamEntry & operator=(ParamEntry &&) &=default
Move assignment operator.
std::vector< std::string > valid_strings
Default: empty.
Definition Param.h:87
double min_float
Default: - std::numeric_limits<double>::max()
Definition Param.h:83
std::string name
Name of the entry.
Definition Param.h:74
ParamValue value
Value associated with the entry.
Definition Param.h:78
int min_int
Default: - std::numeric_limits<Int>::max()
Definition Param.h:85
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:86
ParamEntry()
Default constructor.
std::set< std::string > tags
Tags list, used e.g. for advanced parameter tag.
Definition Param.h:80
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:181
std::string description
description of the node
Definition Param.h:193
TraceInfo(const std::string &n, const std::string &d, bool o)
Constructor with name, description, and open flag.
Definition Param.h:183
std::string name
name of the node
Definition Param.h:191
bool opened
If it was opened (true) or closed (false)
Definition Param.h:195
Node inside a Param object which is used to build the internal tree.
Definition Param.h:93
ParamNode(const std::string &n, const std::string &d)
Constructor with name and description.
ParamEntry * findEntryRecursive(const std::string &name)
Look up the entry corresponding to name (tree search)
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:163
std::vector< ParamNode >::const_iterator ConstNodeIterator
Iterator for child nodes.
Definition Param.h:99
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:159
NodeIterator findNode(const std::string &name)
Look up subnode of this node (local search)
EntryIterator findEntry(const std::string &name)
Look up entry of this node (local search)
ParamNode & operator=(const ParamNode &)=default
Assignment operator.
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:97
std::string name
Name of the node.
Definition Param.h:157
std::vector< ParamNode >::iterator NodeIterator
Iterator for child nodes.
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)
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:101
ParamNode & operator=(ParamNode &&) &=default
Move assignment operator.
ParamNode()
Default constructor.
std::vector< ParamEntry > entries
Entries (leafs) in the node.
Definition Param.h:161
bool operator==(const ParamNode &rhs) const
Equality operator (name, entries and subnodes are compared)
ParamNode(ParamNode &&)=default
Move constructor.