| OpenMS 2.5.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-2020.
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>
38 #include <OpenMS/DATASTRUCTURES/DataValue.h>
39 #include <OpenMS/DATASTRUCTURES/String.h>
40 #include <OpenMS/OpenMSConfig.h>
41
42 #include <iosfwd>
43 #include <set>
44
45 namespace OpenMS
46 {
47
48 template <class Key, class T>
49 class Map;
50
51 namespace Logger
52 {
53 class LogStream;
54 }
55
73 class OPENMS_DLLAPI Param
74 {
75 public:
76
78 struct OPENMS_DLLAPI ParamEntry
79 {
81 ParamEntry();
83 ParamEntry(const String& n, const DataValue& v, const String& d, const StringList& t = StringList());
85 ParamEntry(const ParamEntry&) = default;
87 ParamEntry(ParamEntry&&) = default;
89 ~ParamEntry();
90
92 ParamEntry& operator=(const ParamEntry&) = default;
94 ParamEntry& operator=(ParamEntry&&) & = default;
95
97 bool isValid(String& message) const;
99 bool operator==(const ParamEntry& rhs) const;
100
102 String name;
104 String description;
106 DataValue value;
108 std::set<String> tags;
110
111 double min_float;
112 double max_float;
113 Int min_int;
114 Int max_int;
115 std::vector<String> valid_strings;
116
117 };
118
120 struct OPENMS_DLLAPI ParamNode
121 {
123 typedef std::vector<ParamNode>::iterator NodeIterator;
125 typedef std::vector<ParamEntry>::iterator EntryIterator;
127 typedef std::vector<ParamNode>::const_iterator ConstNodeIterator;
129 typedef std::vector<ParamEntry>::const_iterator ConstEntryIterator;
130
132 ParamNode();
134 ParamNode(const String& n, const String& d);
136 ParamNode(const ParamNode&) = default;
138 ParamNode(ParamNode&&) = default;
140 ~ParamNode();
141
143 ParamNode& operator=(const ParamNode&) = default;
145 ParamNode& operator=(ParamNode&&) & = default;
146
148 bool operator==(const ParamNode& rhs) const;
149
155 EntryIterator findEntry(const String& name);
161 NodeIterator findNode(const String& name);
167 ParamNode* findParentOf(const String& name);
173 ParamEntry* findEntryRecursive(const String& name);
174
176 void insert(const ParamNode& node, const String& prefix = "");
178 void insert(const ParamEntry& entry, const String& prefix = "");
180 Size size() const;
182 String suffix(const String& key) const;
183
185 String name;
187 String description;
189 std::vector<ParamEntry> entries;
191 std::vector<ParamNode> nodes;
192 };
193
194 public:
195
197 class OPENMS_DLLAPI ParamIterator
198 {
199 public:
201 struct OPENMS_DLLAPI TraceInfo
202 {
204 inline TraceInfo(const String& n, const String& d, bool o) :
205 name(n),
206 description(d),
207 opened(o)
208 {
209 }
210
212 String name;
214 String description;
216 bool opened;
217 };
218
220 ParamIterator();
222 ParamIterator(const Param::ParamNode& root);
224 ~ParamIterator();
226 const Param::ParamEntry& operator*();
228 const Param::ParamEntry* operator->();
230 ParamIterator& operator++();
232 ParamIterator operator++(int);
234 bool operator==(const ParamIterator& rhs) const;
236 bool operator!=(const ParamIterator& rhs) const;
238 String getName() const;
240 const std::vector<TraceInfo>& getTrace() const;
241
242 protected:
244 const Param::ParamNode* root_;
246 Int current_;
248 std::vector<const Param::ParamNode*> stack_;
250 std::vector<TraceInfo> trace_;
251
252 };
253
255 Param();
256
258 Param(const Param&) = default;
259
261 Param(Param&&) = default;
262
264 ~Param();
265
267 Param& operator=(const Param&) = default;
268
270 Param& operator=(Param&&) & = default;
271
273 bool operator==(const Param& rhs) const;
274
276 ParamIterator begin() const;
277
279 ParamIterator end() const;
280
282
283
292 void setValue(const String& key, const DataValue& value, const String& description = "", const StringList& tags = StringList());
293
299 const DataValue& getValue(const String& key) const;
300
306 const ParamEntry& getEntry(const String& key) const;
307
314 bool exists(const String& key) const;
315
322 ParamIterator findFirst(const String& leaf) const;
323
331 ParamIterator findNext(const String& leaf, const ParamIterator& start_leaf) const;
333
335
336
345 void addTag(const String& key, const String& tag);
346
353 void addTags(const String& key, const StringList& tags);
354
363 bool hasTag(const String& key, const String& tag) const;
364
370 StringList getTags(const String& key) const;
371
377 void clearTags(const String& key);
379
380
382
383
389 const String& getDescription(const String& key) const;
390
399 void setSectionDescription(const String& key, const String& description);
400
406 const String& getSectionDescription(const String& key) const;
407
413 void addSection(const String& key, const String& description);
415
417
418
420 Size size() const;
421
423 bool empty() const;
424
426 void clear();
427
430 void insert(const String& prefix, const Param& param);
431
441 void remove(const String& key);
442
451 void removeAll(const String& prefix);
452
460 Param copy(const String& prefix, bool remove_prefix = false) const;
461
471 Param copySubset(const Param& subset) const;
472
478 bool update(const Param& p_outdated, const bool add_unknown = false);
479
485 bool update(const Param& p_outdated, const bool add_unknown, Logger::LogStream& stream);
486
487
508 bool update(const Param& p_outdated, bool verbose, bool add_unknown, bool fail_on_invalid_values, bool fail_on_unknown_parameters, Logger::LogStream& stream);
509
515 void merge(const Param& toMerge);
516
518
519
521
522
531 void setDefaults(const Param& defaults, const String& prefix = "", bool showMessage = false);
532
552 void checkDefaults(const String& name, const Param& defaults, const String& prefix = "") const;
554
556
557
565 void setValidStrings(const String& key, const std::vector<String>& strings);
566
574 void setMinInt(const String& key, Int min);
575
583 void setMaxInt(const String& key, Int max);
584
592 void setMinFloat(const String& key, double min);
593
601 void setMaxFloat(const String& key, double max);
603
605
606
624 void parseCommandLine(const int argc, const char** argv, const String& prefix = "");
625
639 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");
640
642
643 protected:
644
650 ParamEntry& getEntry_(const String& key) const;
651
653 Param(const Param::ParamNode& node);
654
656 mutable Param::ParamNode root_;
657 };
658
660 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const Param& param);
661
662 } // namespace OpenMS
663
OpenMS::Param::ParamEntry::max_int
Int max_int
Default: std::numeric_limits<Int>::max()
Definition: Param.h:114
OpenMS::Param::ParamIterator::TraceInfo::name
String name
name of the node
Definition: Param.h:212
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::DataValue
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
OpenMS::Param::ParamEntry::description
String description
Description of the entry.
Definition: Param.h:104
OpenMS::Param::ParamEntry::valid_strings
std::vector< String > valid_strings
Default: empty.
Definition: Param.h:115
OpenMS::Param::ParamNode::ConstEntryIterator
std::vector< ParamEntry >::const_iterator ConstEntryIterator
Iterator for entries.
Definition: Param.h:129
OpenMS::Param::ParamNode::NodeIterator
std::vector< ParamNode >::iterator NodeIterator
Iterator for child nodes.
Definition: Param.h:123
OpenMS::Param::ParamIterator::current_
Int current_
Index of the current ParamEntry (-1 means invalid)
Definition: Param.h:246
OpenMS::Param::ParamNode::EntryIterator
std::vector< ParamEntry >::iterator EntryIterator
Iterator for entries.
Definition: Param.h:125
OpenMS::Param::ParamIterator
Forward const iterator for the Param class.
Definition: Param.h:197
OpenMS::Internal::ClassTest::verbose
int verbose
Verbosity level ( "-v" is 1 and "-V" is 2 )
OpenMS::Param::ParamIterator::root_
const Param::ParamNode * root_
Pointer to the root node.
Definition: Param.h:244
KDTree::operator!=
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
Types.h
OpenMS::Param::ParamIterator::TraceInfo::description
String description
description of the node
Definition: Param.h:214
OpenMS::Param::ParamNode
Node inside a Param object which is used to build the internal tree.
Definition: Param.h:120
OpenMS::Param::ParamIterator::trace_
std::vector< TraceInfo > trace_
Node traversal data during last ++ operation.
Definition: Param.h:250
OpenMS::Param::ParamNode::description
String description
Description of the node.
Definition: Param.h:187
OpenMS::Param::ParamEntry::max_float
double max_float
Default: std::numeric_limits<double>::max()
Definition: Param.h:112
OpenMS::Param::ParamEntry::min_int
Int min_int
Default: - std::numeric_limits<Int>::max()
Definition: Param.h:113
OpenMS::Logger::LogStream
Log Stream Class.
Definition: LogStream.h:311
OpenMS::Param::ParamIterator::TraceInfo
Struct that captures information on entered / left nodes for ParamIterator.
Definition: Param.h:201
OpenMS::Param::ParamNode::name
String name
Name of the node.
Definition: Param.h:185
String.h
OpenMS::Map
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:50
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::Param::ParamEntry::name
String name
Name of the entry.
Definition: Param.h:102
OpenMS::Param::ParamEntry::value
DataValue value
Value associated with the entry.
Definition: Param.h:106
OpenMS::Param::ParamNode::entries
std::vector< ParamEntry > entries
Entries (leafs) in the node.
Definition: Param.h:189
OpenMS::Param::ParamIterator::stack_
std::vector< const Param::ParamNode * > stack_
Pointers to the ParamNodes we are in.
Definition: Param.h:248
OpenMS::Param::ParamEntry::tags
std::set< String > tags
Tags list, used e.g. for advanced parameter tag.
Definition: Param.h:108
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:73
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
int
OpenMS::Param::ParamNode::ConstNodeIterator
std::vector< ParamNode >::const_iterator ConstNodeIterator
Iterator for child nodes.
Definition: Param.h:127
DataValue.h
OpenMS::Param::ParamIterator::TraceInfo::opened
bool opened
If it was opened (true) or closed (false)
Definition: Param.h:216
OpenMS::Param::ParamIterator::TraceInfo::TraceInfo
TraceInfo(const String &n, const String &d, bool o)
Constructor with name, description, and open flag.
Definition: Param.h:204
OpenMS::Param::root_
Param::ParamNode root_
Invisible root node that stores all the data.
Definition: Param.h:656
OpenMS::Param::ParamEntry::min_float
double min_float
Default: - std::numeric_limits<double>::max()
Definition: Param.h:111
OpenMS::Param::ParamNode::nodes
std::vector< ParamNode > nodes
Subnodes.
Definition: Param.h:191
OpenMS::operator*
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:427
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Param::ParamEntry
Parameter entry used to store the actual information inside of a Param entry.
Definition: Param.h:78
KDTree::operator==
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806