OpenMS  2.4.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-2018.
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 
37 #include <OpenMS/CONCEPT/Types.h>
41 #include <OpenMS/OpenMSConfig.h>
42 
43 #include <iosfwd>
44 #include <set>
45 
46 namespace OpenMS
47 {
48 
49  template <class Key, class T>
50  class Map;
51 
52  namespace Logger
53  {
54  class LogStream;
55  }
56 
74  class OPENMS_DLLAPI Param
75  {
76 public:
77 
79  struct OPENMS_DLLAPI ParamEntry
80  {
82  ParamEntry();
84  ParamEntry(const String& n, const DataValue& v, const String& d, const StringList& t = StringList());
86  ParamEntry(const ParamEntry& other);
88  ~ParamEntry();
89 
91  bool isValid(String& message) const;
93  bool operator==(const ParamEntry& rhs) const;
94 
102  std::set<String> tags;
104 
105  double min_float;
106  double max_float;
109  std::vector<String> valid_strings;
110 
111  };
112 
114  struct OPENMS_DLLAPI ParamNode
115  {
117  typedef std::vector<ParamNode>::iterator NodeIterator;
119  typedef std::vector<ParamEntry>::iterator EntryIterator;
121  typedef std::vector<ParamNode>::const_iterator ConstNodeIterator;
123  typedef std::vector<ParamEntry>::const_iterator ConstEntryIterator;
124 
126  ParamNode();
128  ParamNode(const String& n, const String& d);
130  ~ParamNode();
132  bool operator==(const ParamNode& rhs) const;
133 
139  EntryIterator findEntry(const String& name);
145  NodeIterator findNode(const String& name);
151  ParamNode* findParentOf(const String& name);
157  ParamEntry* findEntryRecursive(const String& name);
158 
160  void insert(const ParamNode& node, const String& prefix = "");
162  void insert(const ParamEntry& entry, const String& prefix = "");
164  Size size() const;
166  String suffix(const String& key) const;
167 
173  std::vector<ParamEntry> entries;
175  std::vector<ParamNode> nodes;
176  };
177 
178 public:
179 
181  class OPENMS_DLLAPI ParamIterator
182  {
183 public:
185  struct OPENMS_DLLAPI TraceInfo
186  {
188  inline TraceInfo(const String& n, const String& d, bool o) :
189  name(n),
190  description(d),
191  opened(o)
192  {
193  }
194 
200  bool opened;
201  };
202 
204  ParamIterator();
206  ParamIterator(const Param::ParamNode& root);
208  ~ParamIterator();
210  const Param::ParamEntry& operator*();
212  const Param::ParamEntry* operator->();
214  ParamIterator& operator++();
216  ParamIterator operator++(int);
218  bool operator==(const ParamIterator& rhs) const;
220  bool operator!=(const ParamIterator& rhs) const;
222  String getName() const;
224  const std::vector<TraceInfo>& getTrace() const;
225 
226 protected:
232  std::vector<const Param::ParamNode*> stack_;
234  std::vector<TraceInfo> trace_;
235 
236  };
237 
239  Param();
240 
242  Param(const Param& rhs);
243 
245  ~Param();
246 
248  Param& operator=(const Param& rhs);
249 
251  bool operator==(const Param& rhs) const;
252 
254  ParamIterator begin() const;
255 
257  ParamIterator end() const;
258 
260 
261 
270  void setValue(const String& key, const DataValue& value, const String& description = "", const StringList& tags = StringList());
271 
277  const DataValue& getValue(const String& key) const;
278 
284  const ParamEntry& getEntry(const String& key) const;
285 
292  bool exists(const String& key) const;
293 
300  ParamIterator findFirst(const String& leaf) const;
301 
309  ParamIterator findNext(const String& leaf, const ParamIterator& start_leaf) const;
310 
311 
312 
314 
316 
317 
324  void addTag(const String& key, const String& tag);
325 
332  void addTags(const String& key, const StringList& tags);
333 
342  bool hasTag(const String& key, const String& tag) const;
343 
349  StringList getTags(const String& key) const;
350 
356  void clearTags(const String& key);
358 
359 
361 
362 
368  const String& getDescription(const String& key) const;
369 
378  void setSectionDescription(const String& key, const String& description);
379 
385  const String& getSectionDescription(const String& key) const;
386 
392  void addSection(const String& key, const String& description);
394 
396 
397 
399  Size size() const;
400 
402  bool empty() const;
403 
405  void clear();
406 
409  void insert(const String& prefix, const Param& param);
410 
420  void remove(const String& key);
421 
430  void removeAll(const String& prefix);
431 
439  Param copy(const String& prefix, bool remove_prefix = false) const;
440 
446  bool update(const Param& p_outdated, const bool add_unknown = false);
447 
453  bool update(const Param& p_outdated, const bool add_unknown, Logger::LogStream& stream);
454 
455 
476  bool update(const Param& p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream& stream);
477 
483  void merge(const Param& toMerge);
484 
486 
487 
489 
490 
499  void setDefaults(const Param& defaults, const String& prefix = "", bool showMessage = false);
500 
520  void checkDefaults(const String& name, const Param& defaults, const String& prefix = "") const;
522 
524 
525 
533  void setValidStrings(const String& key, const std::vector<String>& strings);
534 
542  void setMinInt(const String& key, Int min);
543 
551  void setMaxInt(const String& key, Int max);
552 
560  void setMinFloat(const String& key, double min);
561 
569  void setMaxFloat(const String& key, double max);
571 
573 
574 
592  void parseCommandLine(const int argc, const char** argv, const String& prefix = "");
593 
607  void parseCommandLine(const int argc, const char** argv, const Map<String, String>& options_with_one_argument, const Map<String, String>& options_without_argument, const Map<String, String>& options_with_multiple_argument, const String& misc = "misc", const String& unknown = "unknown");
608 
610 
611 protected:
617  ParamEntry& getEntry_(const String& key) const;
618 
620  Param(const Param::ParamNode& node);
621 
624  };
625 
627  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const Param& param);
628 
629 } // namespace OpenMS
630 
DataValue value
Value associated with the entry.
Definition: Param.h:100
A more convenient string class.
Definition: String.h:57
std::vector< ParamNode > nodes
Subnodes.
Definition: Param.h:175
std::vector< ParamEntry >::iterator EntryIterator
Iterator for entries.
Definition: Param.h:119
String description
Description of the node.
Definition: Param.h:171
const Param::ParamNode * root_
Pointer to the root node.
Definition: Param.h:228
Parameter entry used to store the actual information inside of a Param entry.
Definition: Param.h:79
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
std::vector< const Param::ParamNode * > stack_
Pointers to the ParamNodes we are in.
Definition: Param.h:232
Int current_
Index of the current ParamEntry (-1 means invalid)
Definition: Param.h:230
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
double max_float
Default: std::numeric_limits<double>::max()
Definition: Param.h:106
Log Stream Class.
Definition: LogStream.h:302
std::set< String > tags
Tags list, used e.g. for advanced parameter tag.
Definition: Param.h:102
Int min_int
Default: - std::numeric_limits<Int>::max()
Definition: Param.h:107
TraceInfo(const String &n, const String &d, bool o)
Constructor with name, description, and open flag.
Definition: Param.h:188
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
String name
Name of the node.
Definition: Param.h:169
String description
description of the node
Definition: Param.h:198
std::vector< ParamEntry >::const_iterator ConstEntryIterator
Iterator for entries.
Definition: Param.h:123
std::vector< ParamNode >::iterator NodeIterator
Iterator for child nodes.
Definition: Param.h:117
Int max_int
Default: std::numeric_limits<Int>::max()
Definition: Param.h:108
String name
Name of the entry.
Definition: Param.h:96
String name
name of the node
Definition: Param.h:196
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:420
Struct that captures information on entered / left nodes for ParamIterator.
Definition: Param.h:185
bool opened
If it was opened (true) or closed (false)
Definition: Param.h:200
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
String description
Description of the entry.
Definition: Param.h:98
Management and storage of parameters / INI files.
Definition: Param.h:74
std::vector< ParamNode >::const_iterator ConstNodeIterator
Iterator for child nodes.
Definition: Param.h:121
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
double min_float
Default: - std::numeric_limits<double>::max()
Definition: Param.h:105
std::vector< String > valid_strings
Default: empty.
Definition: Param.h:109
Forward const iterator for the Param class.
Definition: Param.h:181
std::vector< TraceInfo > trace_
Node traversal data during last ++ operation.
Definition: Param.h:234
Node inside a Param object which is used to build the internal tree.
Definition: Param.h:114
std::vector< ParamEntry > entries
Entries (leafs) in the node.
Definition: Param.h:173
int Int
Signed integer type.
Definition: Types.h:102
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:50
int verbose
Verbosity level ( "-v" is 1 and "-V" is 2 )
Param::ParamNode root_
Invisible root node that stores all the data.
Definition: Param.h:623