OpenMS  3.0.0
Param.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2022.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Marc Sturm, Clemens Groepl $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 #include <OpenMS/OpenMSConfig.h>
39 
40 #include <set>
41 #include <string>
42 #include <map>
43 
44 namespace OpenMS
45 {
46 
47  namespace Logger
48  {
49  class LogStream;
50  }
51 
69  class OPENMS_DLLAPI Param
70  {
71 public:
72 
74  struct OPENMS_DLLAPI ParamEntry
75  {
77  ParamEntry();
79  ParamEntry(const std::string& n, const ParamValue& v, const std::string& d, const std::vector<std::string>& t = std::vector<std::string>());
81  ParamEntry(const ParamEntry&) = default;
83  ParamEntry(ParamEntry&&) = default;
85  ~ParamEntry();
86 
88  ParamEntry& operator=(const ParamEntry&) = default;
90  ParamEntry& operator=(ParamEntry&&) & = default;
91 
93  bool isValid(std::string& message) const;
95  bool operator==(const ParamEntry& rhs) const;
96 
98  std::string name;
100  std::string description;
104  std::set<std::string> tags;
106 
107  double min_float;
108  double max_float;
109  int min_int;
110  int max_int;
111  std::vector<std::string> valid_strings;
112 
113  };
114 
116  struct OPENMS_DLLAPI ParamNode
117  {
119  typedef std::vector<ParamNode>::iterator NodeIterator;
121  typedef std::vector<ParamEntry>::iterator EntryIterator;
123  typedef std::vector<ParamNode>::const_iterator ConstNodeIterator;
125  typedef std::vector<ParamEntry>::const_iterator ConstEntryIterator;
126 
128  ParamNode();
130  ParamNode(const std::string& n, const std::string& d);
132  ParamNode(const ParamNode&) = default;
134  ParamNode(ParamNode&&) = default;
136  ~ParamNode();
137 
139  ParamNode& operator=(const ParamNode&) = default;
141  ParamNode& operator=(ParamNode&&) & = default;
142 
144  bool operator==(const ParamNode& rhs) const;
145 
151  EntryIterator findEntry(const std::string& name);
157  NodeIterator findNode(const std::string& name);
163  ParamNode* findParentOf(const std::string& name);
169  ParamEntry* findEntryRecursive(const std::string& name);
170 
172  void insert(const ParamNode& node, const std::string& prefix = "");
174  void insert(const ParamEntry& entry, const std::string& prefix = "");
176  size_t size() const;
178  std::string suffix(const std::string& key) const;
179 
181  std::string name;
183  std::string description;
185  std::vector<ParamEntry> entries;
187  std::vector<ParamNode> nodes;
188  };
189 
190 public:
191 
193  class OPENMS_DLLAPI ParamIterator
194  {
195 public:
197  struct OPENMS_DLLAPI TraceInfo
198  {
200  inline TraceInfo(const std::string& n, const std::string& d, bool o) :
201  name(n),
202  description(d),
203  opened(o)
204  {
205  }
206 
208  std::string name;
210  std::string description;
212  bool opened;
213  };
214 
216  ParamIterator();
218  ParamIterator(const Param::ParamNode& root);
220  ~ParamIterator();
222  const Param::ParamEntry& operator*();
224  const Param::ParamEntry* operator->();
226  ParamIterator& operator++();
228  ParamIterator operator++(int);
230  bool operator==(const ParamIterator& rhs) const;
232  bool operator!=(const ParamIterator& rhs) const;
234  std::string getName() const;
236  const std::vector<TraceInfo>& getTrace() const;
237 
238 protected:
242  int current_;
244  std::vector<const Param::ParamNode*> stack_;
246  std::vector<TraceInfo> trace_;
247 
248  };
249 
251  Param();
252 
254  Param(const Param&) = default;
255 
257  Param(Param&&) = default;
258 
260  ~Param();
261 
263  Param& operator=(const Param&) = default;
264 
266  Param& operator=(Param&&) & = default;
267 
269  bool operator==(const Param& rhs) const;
270 
272  ParamIterator begin() const;
273 
275  ParamIterator end() const;
276 
278 
279 
288  void setValue(const std::string& key, const ParamValue& value, const std::string& description = "", const std::vector<std::string>& tags = std::vector<std::string>());
289 
295  const ParamValue& getValue(const std::string& key) const;
296 
302  ParamValue::ValueType getValueType(const std::string& key) const;
303 
309  const ParamEntry& getEntry(const std::string& key) const;
310 
317  bool exists(const std::string& key) const;
318 
325  bool hasSection(const std::string& key) const;
326 
333  ParamIterator findFirst(const std::string& leaf) const;
334 
342  ParamIterator findNext(const std::string& leaf, const ParamIterator& start_leaf) const;
344 
346 
347 
356  void addTag(const std::string& key, const std::string& tag);
357 
364  void addTags(const std::string& key, const std::vector<std::string>& tags);
365 
374  bool hasTag(const std::string& key, const std::string& tag) const;
375 
381  std::vector<std::string> getTags(const std::string& key) const;
382 
388  void clearTags(const std::string& key);
390 
391 
393 
394 
400  const std::string& getDescription(const std::string& key) const;
401 
410  void setSectionDescription(const std::string& key, const std::string& description);
411 
417  const std::string& getSectionDescription(const std::string& key) const;
418 
424  void addSection(const std::string& key, const std::string& description);
426 
428 
429 
431  size_t size() const;
432 
434  bool empty() const;
435 
437  void clear();
438 
441  void insert(const std::string& prefix, const Param& param);
442 
452  void remove(const std::string& key);
453 
462  void removeAll(const std::string& prefix);
463 
471  Param copy(const std::string& prefix, bool remove_prefix = false) const;
472 
482  Param copySubset(const Param& subset) const;
483 
489  bool update(const Param& p_outdated, const bool add_unknown = false);
490 
496  bool update(const Param& p_outdated, const bool add_unknown, Logger::LogStream& stream);
497 
498 
519  bool update(const Param& p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream& stream);
520 
526  void merge(const Param& toMerge);
527 
529 
530 
532 
533 
542  void setDefaults(const Param& defaults, const std::string& prefix = "", bool showMessage = false);
543 
563  void checkDefaults(const std::string& name, const Param& defaults, const std::string& prefix = "") const;
565 
567 
568 
576  void setValidStrings(const std::string& key, const std::vector<std::string>& strings);
577 
583  const std::vector<std::string>& getValidStrings(const std::string& key) const;
584 
592  void setMinInt(const std::string& key, int min);
593 
601  void setMaxInt(const std::string& key, int max);
602 
610  void setMinFloat(const std::string& key, double min);
611 
619  void setMaxFloat(const std::string& key, double max);
621 
623 
624 
642  void parseCommandLine(const int argc, const char** argv, const std::string& prefix = "");
643 
657  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");
658 
660 
661 protected:
662 
668  ParamEntry& getEntry_(const std::string& key) const;
669 
671  Param(const Param::ParamNode& node);
672 
675  };
676 
678  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const Param& param);
679 
680 } // namespace OpenMS
681 
OpenMS::Param::ParamEntry::valid_strings
std::vector< std::string > valid_strings
Default: empty.
Definition: Param.h:111
OpenMS::Param::ParamNode::entries
std::vector< ParamEntry > entries
Entries (leafs) in the node.
Definition: Param.h:185
OpenMS::Param::ParamEntry::value
ParamValue value
Value associated with the entry.
Definition: Param.h:102
OpenMS::Param::ParamNode::EntryIterator
std::vector< ParamEntry >::iterator EntryIterator
Iterator for entries.
Definition: Param.h:121
OpenMS::Param::ParamIterator::root_
const Param::ParamNode * root_
Pointer to the root node.
Definition: Param.h:240
OpenMS::Param::ParamEntry
Parameter entry used to store the actual information inside of a Param entry.
Definition: Param.h:74
OpenMS::Param::ParamEntry::min_int
int min_int
Default: - std::numeric_limits<Int>::max()
Definition: Param.h:109
OpenMS::StringUtils::suffix
static String suffix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:156
OpenMS::operator*
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:426
OpenMS::Param::root_
Param::ParamNode root_
Invisible root node that stores all the data.
Definition: Param.h:674
OpenMS::Param::ParamNode::description
std::string description
Description of the node.
Definition: Param.h:183
KDTree::operator!=
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
OpenMS::Param::ParamIterator::TraceInfo::TraceInfo
TraceInfo(const std::string &n, const std::string &d, bool o)
Constructor with name, description, and open flag.
Definition: Param.h:200
OpenMS::Param::ParamEntry::description
std::string description
Description of the entry.
Definition: Param.h:100
OpenMS::ParamValue
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition: ParamValue.h:54
OpenMS::Param::ParamNode::name
std::string name
Name of the node.
Definition: Param.h:181
OpenMS::ParamValue::ValueType
ValueType
Supported types for ParamValue.
Definition: ParamValue.h:63
OpenMS::Param::ParamIterator::TraceInfo::opened
bool opened
If it was opened (true) or closed (false)
Definition: Param.h:212
OpenMS::Param::ParamIterator
Forward const iterator for the Param class.
Definition: Param.h:193
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::Param::ParamNode::NodeIterator
std::vector< ParamNode >::iterator NodeIterator
Iterator for child nodes.
Definition: Param.h:119
OpenMS::Param::ParamEntry::max_float
double max_float
Default: std::numeric_limits<double>::max()
Definition: Param.h:108
OpenMS::StringUtils::remove
static String & remove(String &this_s, char what)
Definition: StringUtilsSimple.h:595
OpenMS::Param::ParamIterator::TraceInfo::description
std::string description
description of the node
Definition: Param.h:210
OpenMS::Param::ParamEntry::min_float
double min_float
Default: - std::numeric_limits<double>::max()
Definition: Param.h:107
OpenMS::Param::ParamEntry::max_int
int max_int
Default: std::numeric_limits<Int>::max()
Definition: Param.h:110
OpenMS::Param::ParamIterator::current_
int current_
Index of the current ParamEntry (-1 means invalid)
Definition: Param.h:242
OpenMS::Param::ParamNode::ConstNodeIterator
std::vector< ParamNode >::const_iterator ConstNodeIterator
Iterator for child nodes.
Definition: Param.h:123
OpenMS::Param::ParamIterator::trace_
std::vector< TraceInfo > trace_
Node traversal data during last ++ operation.
Definition: Param.h:246
ParamValue.h
OpenMS::Param::ParamNode::ConstEntryIterator
std::vector< ParamEntry >::const_iterator ConstEntryIterator
Iterator for entries.
Definition: Param.h:125
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::Logger::LogStream
Log Stream Class.
Definition: LogStream.h:311
OpenMS::Param::ParamEntry::tags
std::set< std::string > tags
Tags list, used e.g. for advanced parameter tag.
Definition: Param.h:104
OpenMS::Internal::ClassTest::verbose
int verbose
Verbosity level ( "-v" is 1 and "-V" is 2 )
OpenMS::Internal::operator==
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
OpenMS::Param::ParamIterator::TraceInfo
Struct that captures information on entered / left nodes for ParamIterator.
Definition: Param.h:197
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:69
OpenMS::Param::ParamNode
Node inside a Param object which is used to build the internal tree.
Definition: Param.h:116
OpenMS::StringUtils::prefix
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:147
OpenMS::Param::ParamIterator::stack_
std::vector< const Param::ParamNode * > stack_
Pointers to the ParamNodes we are in.
Definition: Param.h:244
OpenMS::Param::ParamNode::nodes
std::vector< ParamNode > nodes
Subnodes.
Definition: Param.h:187
OpenMS::Param::ParamIterator::TraceInfo::name
std::string name
name of the node
Definition: Param.h:208
OpenMS::Param::ParamEntry::name
std::string name
Name of the entry.
Definition: Param.h:98