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&) = default;
88  ParamEntry(ParamEntry&&) = default;
90  ~ParamEntry();
91 
93  ParamEntry& operator=(const ParamEntry&) = default;
95  ParamEntry& operator=(ParamEntry&&) & = default;
96 
98  bool isValid(String& message) const;
100  bool operator==(const ParamEntry& rhs) const;
101 
109  std::set<String> tags;
111 
112  double min_float;
113  double max_float;
116  std::vector<String> valid_strings;
117 
118  };
119 
121  struct OPENMS_DLLAPI ParamNode
122  {
124  typedef std::vector<ParamNode>::iterator NodeIterator;
126  typedef std::vector<ParamEntry>::iterator EntryIterator;
128  typedef std::vector<ParamNode>::const_iterator ConstNodeIterator;
130  typedef std::vector<ParamEntry>::const_iterator ConstEntryIterator;
131 
133  ParamNode();
135  ParamNode(const String& n, const String& d);
137  ParamNode(const ParamNode&) = default;
139  ParamNode(ParamNode&&) = default;
141  ~ParamNode();
142 
144  ParamNode& operator=(const ParamNode&) = default;
146  ParamNode& operator=(ParamNode&&) & = default;
147 
149  bool operator==(const ParamNode& rhs) const;
150 
156  EntryIterator findEntry(const String& name);
162  NodeIterator findNode(const String& name);
168  ParamNode* findParentOf(const String& name);
174  ParamEntry* findEntryRecursive(const String& name);
175 
177  void insert(const ParamNode& node, const String& prefix = "");
179  void insert(const ParamEntry& entry, const String& prefix = "");
181  Size size() const;
183  String suffix(const String& key) const;
184 
190  std::vector<ParamEntry> entries;
192  std::vector<ParamNode> nodes;
193  };
194 
195 public:
196 
198  class OPENMS_DLLAPI ParamIterator
199  {
200 public:
202  struct OPENMS_DLLAPI TraceInfo
203  {
205  inline TraceInfo(const String& n, const String& d, bool o) :
206  name(n),
207  description(d),
208  opened(o)
209  {
210  }
211 
217  bool opened;
218  };
219 
221  ParamIterator();
223  ParamIterator(const Param::ParamNode& root);
225  ~ParamIterator();
227  const Param::ParamEntry& operator*();
229  const Param::ParamEntry* operator->();
231  ParamIterator& operator++();
233  ParamIterator operator++(int);
235  bool operator==(const ParamIterator& rhs) const;
237  bool operator!=(const ParamIterator& rhs) const;
239  String getName() const;
241  const std::vector<TraceInfo>& getTrace() const;
242 
243 protected:
249  std::vector<const Param::ParamNode*> stack_;
251  std::vector<TraceInfo> trace_;
252 
253  };
254 
256  Param();
257 
259  Param(const Param&) = default;
260 
262  Param(Param&&) = default;
263 
265  ~Param();
266 
268  Param& operator=(const Param&) = default;
269 
271  Param& operator=(Param&&) & = default;
272 
274  bool operator==(const Param& rhs) const;
275 
277  ParamIterator begin() const;
278 
280  ParamIterator end() const;
281 
283 
284 
293  void setValue(const String& key, const DataValue& value, const String& description = "", const StringList& tags = StringList());
294 
300  const DataValue& getValue(const String& key) const;
301 
307  const ParamEntry& getEntry(const String& key) const;
308 
315  bool exists(const String& key) const;
316 
323  ParamIterator findFirst(const String& leaf) const;
324 
332  ParamIterator findNext(const String& leaf, const ParamIterator& start_leaf) const;
334 
336 
337 
346  void addTag(const String& key, const String& tag);
347 
354  void addTags(const String& key, const StringList& tags);
355 
364  bool hasTag(const String& key, const String& tag) const;
365 
371  StringList getTags(const String& key) const;
372 
378  void clearTags(const String& key);
380 
381 
383 
384 
390  const String& getDescription(const String& key) const;
391 
400  void setSectionDescription(const String& key, const String& description);
401 
407  const String& getSectionDescription(const String& key) const;
408 
414  void addSection(const String& key, const String& description);
416 
418 
419 
421  Size size() const;
422 
424  bool empty() const;
425 
427  void clear();
428 
431  void insert(const String& prefix, const Param& param);
432 
442  void remove(const String& key);
443 
452  void removeAll(const String& prefix);
453 
461  Param copy(const String& prefix, bool remove_prefix = false) const;
462 
468  bool update(const Param& p_outdated, const bool add_unknown = false);
469 
475  bool update(const Param& p_outdated, const bool add_unknown, Logger::LogStream& stream);
476 
477 
498  bool update(const Param& p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream& stream);
499 
505  void merge(const Param& toMerge);
506 
508 
509 
511 
512 
521  void setDefaults(const Param& defaults, const String& prefix = "", bool showMessage = false);
522 
542  void checkDefaults(const String& name, const Param& defaults, const String& prefix = "") const;
544 
546 
547 
555  void setValidStrings(const String& key, const std::vector<String>& strings);
556 
564  void setMinInt(const String& key, Int min);
565 
573  void setMaxInt(const String& key, Int max);
574 
582  void setMinFloat(const String& key, double min);
583 
591  void setMaxFloat(const String& key, double max);
593 
595 
596 
614  void parseCommandLine(const int argc, const char** argv, const String& prefix = "");
615 
629  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");
630 
632 
633 protected:
634 
640  ParamEntry& getEntry_(const String& key) const;
641 
643  Param(const Param::ParamNode& node);
644 
647  };
648 
650  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const Param& param);
651 
652 } // namespace OpenMS
653 
DataValue value
Value associated with the entry.
Definition: Param.h:107
A more convenient string class.
Definition: String.h:58
std::vector< ParamNode > nodes
Subnodes.
Definition: Param.h:192
std::vector< ParamEntry >::iterator EntryIterator
Iterator for entries.
Definition: Param.h:126
String description
Description of the node.
Definition: Param.h:188
const Param::ParamNode * root_
Pointer to the root node.
Definition: Param.h:245
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:249
Int current_
Index of the current ParamEntry (-1 means invalid)
Definition: Param.h:247
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
double max_float
Default: std::numeric_limits<double>::max()
Definition: Param.h:113
Log Stream Class.
Definition: LogStream.h:304
std::set< String > tags
Tags list, used e.g. for advanced parameter tag.
Definition: Param.h:109
Int min_int
Default: - std::numeric_limits<Int>::max()
Definition: Param.h:114
TraceInfo(const String &n, const String &d, bool o)
Constructor with name, description, and open flag.
Definition: Param.h:205
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:186
String description
description of the node
Definition: Param.h:215
std::vector< ParamEntry >::const_iterator ConstEntryIterator
Iterator for entries.
Definition: Param.h:130
std::vector< ParamNode >::iterator NodeIterator
Iterator for child nodes.
Definition: Param.h:124
Int max_int
Default: std::numeric_limits<Int>::max()
Definition: Param.h:115
String name
Name of the entry.
Definition: Param.h:103
String name
name of the node
Definition: Param.h:213
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:202
bool opened
If it was opened (true) or closed (false)
Definition: Param.h:217
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
String description
Description of the entry.
Definition: Param.h:105
Management and storage of parameters / INI files.
Definition: Param.h:74
std::vector< ParamNode >::const_iterator ConstNodeIterator
Iterator for child nodes.
Definition: Param.h:128
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:112
std::vector< String > valid_strings
Default: empty.
Definition: Param.h:116
Forward const iterator for the Param class.
Definition: Param.h:198
std::vector< TraceInfo > trace_
Node traversal data during last ++ operation.
Definition: Param.h:251
Node inside a Param object which is used to build the internal tree.
Definition: Param.h:121
std::vector< ParamEntry > entries
Entries (leafs) in the node.
Definition: Param.h:190
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:646