DP3
ParameterSetImpl.h
Go to the documentation of this file.
1 // ParameterSetImpl.h: Implements a map of Key-Value pairs.
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #ifndef LOFAR_COMMON_PARAMETERSETIMPL_H
7 #define LOFAR_COMMON_PARAMETERSETIMPL_H
8 
9 // Never #include <config.h> or #include <lofar_config.h> in a header file!
10 #include "ParameterValue.h"
11 #include "StringTools.h"
12 
13 #include <map>
14 #include <memory>
15 #include <mutex>
16 #include <set>
17 
18 namespace dp3 {
19 namespace common {
20 
23 
25 
26 const char PC_QUAL_STABLE[] = {"stable"};
27 const char PC_QUAL_TEST[] = {"test"};
28 const char PC_QUAL_DEVELOP[] = {"development"};
29 const char PC_KEY_VERSIONNR[] = {"versionnr"};
30 const char PC_KEY_QUAL[] = {"qualification"};
31 
33 
37 typedef std::map<std::string, ParameterValue, KeyCompare> KVMap;
38 
51 class ParameterSetImpl : public KVMap {
52  public:
53  typedef KVMap::iterator iterator;
54  typedef KVMap::const_iterator const_iterator;
55 
60 
64 
67 
70  explicit ParameterSetImpl(const std::string& theFilename,
71  KeyCompare::Mode mode);
73 
76  const ParameterValue& get(const std::string& aKey) const;
77 
79  KeyCompare::Mode keyCompareMode() const { return itsMode; }
80 
84 
88  void adoptFile(const std::string& theFilename,
89  const std::string& thePrefix = "");
90 
94  void adoptBuffer(const std::string& theBuffer,
95  const std::string& thePrefix = "");
96 
100  void adoptCollection(const ParameterSetImpl& theCollection,
101  const std::string& thePrefix = "");
102 
105  void adoptArguments(const std::vector<std::string>& arguments);
107 
111 
113  void writeFile(const std::string& theFilename, bool append = false) const;
114 
117  void writeBuffer(std::string& theBuffer) const;
118 
121  void writeStream(std::ostream& os) const;
123 
128 
133  std::shared_ptr<ParameterSetImpl> makeSubset(
134  const std::string& baseKey, const std::string& prefix = "") const;
135 
139  void subtractSubset(const std::string& fullPrefix);
141 
146 
149  void add(const std::string& aKey, const ParameterValue& aValue);
150 
153  void replace(const std::string& aKey, const ParameterValue& aValue);
154 
156  void remove(const std::string& aKey);
158 
165 
167  bool isDefined(const std::string& searchKey) const {
168  return (find(searchKey) != end());
169  };
170 
173  std::string locateModule(const std::string& shortName) const;
174 
178  std::string fullModuleName(const std::string& shortName) const;
179 
182  bool getBool(const std::string& aKey) const;
183  bool getBool(const std::string& aKey, bool aValue) const;
184  int getInt(const std::string& aKey) const;
185  int getInt(const std::string& aKey, int aValue) const;
186  unsigned int getUint(const std::string& aKey) const;
187  unsigned int getUint(const std::string& aKey, unsigned int aValue) const;
188  int16_t getInt16(const std::string& aKey) const;
189  int16_t getInt16(const std::string& aKey, int16_t aValue) const;
190  uint16_t getUint16(const std::string& aKey) const;
191  uint16_t getUint16(const std::string& aKey, uint16_t aValue) const;
192  int32_t getInt32(const std::string& aKey) const;
193  int32_t getInt32(const std::string& aKey, int32_t aValue) const;
194  uint32_t getUint32(const std::string& aKey) const;
195  uint32_t getUint32(const std::string& aKey, uint32_t aValue) const;
196  int64_t getInt64(const std::string& aKey) const;
197  int64_t getInt64(const std::string& aKey, int64_t aValue) const;
198  uint64_t getUint64(const std::string& aKey) const;
199  uint64_t getUint64(const std::string& aKey, uint64_t aValue) const;
200  float getFloat(const std::string& aKey) const;
201  float getFloat(const std::string& aKey, float aValue) const;
202  double getDouble(const std::string& aKey) const;
203  double getDouble(const std::string& aKey, double aValue) const;
204  std::string getString(const std::string& aKey) const;
205  std::string getString(const std::string& aKey,
206  const std::string& aValue) const;
209  time_t getTime(const std::string& aKey) const;
210  time_t getTime(const std::string& aKey, const time_t& aValue) const;
213 
216  std::vector<bool> getBoolVector(const std::string& aKey,
217  bool expandable) const;
218  std::vector<bool> getBoolVector(const std::string& aKey,
219  const std::vector<bool>& aValue,
220  bool expandable) const;
221  std::vector<int> getIntVector(const std::string& aKey, bool expandable) const;
222  std::vector<int> getIntVector(const std::string& aKey,
223  const std::vector<int>& aValue,
224  bool expandable) const;
225  std::vector<unsigned int> getUintVector(const std::string& aKey,
226  bool expandable) const;
227  std::vector<unsigned int> getUintVector(
228  const std::string& aKey, const std::vector<unsigned int>& aValue,
229  bool expandable) const;
230  std::vector<int16_t> getInt16Vector(const std::string& aKey,
231  bool expandable) const;
232  std::vector<int16_t> getInt16Vector(const std::string& aKey,
233  const std::vector<int16_t>& aValue,
234  bool expandable) const;
235  std::vector<uint16_t> getUint16Vector(const std::string& aKey,
236  bool expandable) const;
237  std::vector<uint16_t> getUint16Vector(const std::string& aKey,
238  const std::vector<uint16_t>& aValue,
239  bool expandable) const;
240  std::vector<int32_t> getInt32Vector(const std::string& aKey,
241  bool expandable) const;
242  std::vector<int32_t> getInt32Vector(const std::string& aKey,
243  const std::vector<int32_t>& aValue,
244  bool expandable) const;
245  std::vector<uint32_t> getUint32Vector(const std::string& aKey,
246  bool expandable) const;
247  std::vector<uint32_t> getUint32Vector(const std::string& aKey,
248  const std::vector<uint32_t>& aValue,
249  bool expandable) const;
250  std::vector<int64_t> getInt64Vector(const std::string& aKey,
251  bool expandable) const;
252  std::vector<int64_t> getInt64Vector(const std::string& aKey,
253  const std::vector<int64_t>& aValue,
254  bool expandable) const;
255  std::vector<uint64_t> getUint64Vector(const std::string& aKey,
256  bool expandable) const;
257  std::vector<uint64_t> getUint64Vector(const std::string& aKey,
258  const std::vector<uint64_t>& aValue,
259  bool expandable) const;
260  std::vector<float> getFloatVector(const std::string& aKey,
261  bool expandable) const;
262  std::vector<float> getFloatVector(const std::string& aKey,
263  const std::vector<float>& aValue,
264  bool expandable) const;
265  std::vector<double> getDoubleVector(const std::string& aKey,
266  bool expandable) const;
267  std::vector<double> getDoubleVector(const std::string& aKey,
268  const std::vector<double>& aValue,
269  bool expandable) const;
270  std::vector<std::string> getStringVector(const std::string& aKey,
271  bool expandable) const;
272  std::vector<std::string> getStringVector(
273  const std::string& aKey, const std::vector<std::string>& aValue,
274  bool expandable) const;
275  std::vector<time_t> getTimeVector(const std::string& aKey,
276  bool expandable) const;
277  std::vector<time_t> getTimeVector(const std::string& aKey,
278  const std::vector<time_t>& aValue,
279  bool expandable) const;
281 
283 
287 
288  // Allow printing the whole parameter collection.
289  friend std::ostream& operator<<(std::ostream& os,
290  const ParameterSetImpl& thePS);
292 
294  std::vector<std::string> unusedKeys() const;
295 
296  private:
299  ParameterSetImpl(const ParameterSetImpl& that);
300  ParameterSetImpl& operator=(const ParameterSetImpl& that);
302 
308  void readFile(const std::string& theFile, const std::string& prefix,
309  const bool merge);
310  void readBuffer(const std::string& theFile, const std::string& prefix,
311  const bool merge);
312  void readStream(std::istream& inputStream, const std::string& prefix,
313  const bool merge);
315 
319  const_iterator findKV(const std::string& aKey, bool doThrow = true) const;
320 
323  void addMerge(const std::string& key, const std::string& value, bool merge);
324 
326  void addUnlocked(const std::string& aKey, const ParameterValue& aValue);
327 
329  void replaceUnlocked(const std::string& aKey, const ParameterValue& aValue);
330 
332  const KeyCompare::Mode itsMode;
334  mutable std::set<std::string> itsAskedParms;
336  mutable std::mutex itsMutex;
337 };
338 
339 // -------------------- Global functions --------------------
341 bool isValidVersionNr(const std::string& versionNr);
342 
346 bool isValidVersionNrRef(const std::string& versionNr);
347 
348 // Returns the value of the given string or 0 if it is not a valid seqnr
349 // uint32 seqNr(const std::string& aString);
350 
351 // When a hierarchical keyname is passed to \c fullKeyName the methods returns
359 std::string keyName(const std::string& fullKeyName);
360 
361 // When a hierarchical keyname is passed to \c moduleName the methods returns
369 std::string moduleName(const std::string& fullKeyName);
370 
373 std::string keyPart(const std::string& parameterLine);
374 
379 std::string valuePart(const std::string& parameterLine);
380 
389 int32_t indexValue(const std::string& label, const char indexMarker[2]);
390 
392 
394  const std::string& aKey) const {
395  return findKV(aKey)->second;
396 }
397 
398 inline bool ParameterSetImpl::getBool(const std::string& aKey) const {
399  return findKV(aKey)->second.getBool();
400 }
401 
402 inline bool ParameterSetImpl::getBool(const std::string& aKey,
403  bool aValue) const {
404  const_iterator it = findKV(aKey, false);
405  if (it == end()) return aValue;
406  return it->second.getBool();
407 }
408 
409 inline int ParameterSetImpl::getInt(const std::string& aKey) const {
410  return findKV(aKey)->second.getInt();
411 }
412 
413 inline int ParameterSetImpl::getInt(const std::string& aKey, int aValue) const {
414  const_iterator it = findKV(aKey, false);
415  if (it == end()) return aValue;
416  return it->second.getInt();
417 }
418 
419 inline unsigned int ParameterSetImpl::getUint(const std::string& aKey) const {
420  return findKV(aKey)->second.getUint();
421 }
422 
423 inline unsigned int ParameterSetImpl::getUint(const std::string& aKey,
424  unsigned int aValue) const {
425  const_iterator it = findKV(aKey, false);
426  if (it == end()) return aValue;
427  return it->second.getUint();
428 }
429 
430 inline int16_t ParameterSetImpl::getInt16(const std::string& aKey) const {
431  return findKV(aKey)->second.getInt16();
432 }
433 
434 inline int16_t ParameterSetImpl::getInt16(const std::string& aKey,
435  int16_t aValue) const {
436  const_iterator it = findKV(aKey, false);
437  if (it == end()) return aValue;
438  return it->second.getInt16();
439 }
440 
441 inline uint16_t ParameterSetImpl::getUint16(const std::string& aKey) const {
442  return findKV(aKey)->second.getUint16();
443 }
444 
445 inline uint16_t ParameterSetImpl::getUint16(const std::string& aKey,
446  uint16_t aValue) const {
447  const_iterator it = findKV(aKey, false);
448  if (it == end()) return aValue;
449  return it->second.getUint16();
450 }
451 
452 inline int32_t ParameterSetImpl::getInt32(const std::string& aKey) const {
453  return findKV(aKey)->second.getInt32();
454 }
455 
456 inline int32_t ParameterSetImpl::getInt32(const std::string& aKey,
457  int32_t aValue) const {
458  const_iterator it = findKV(aKey, false);
459  if (it == end()) return aValue;
460  return it->second.getInt32();
461 }
462 
463 inline uint32_t ParameterSetImpl::getUint32(const std::string& aKey) const {
464  return findKV(aKey)->second.getUint32();
465 }
466 
467 inline uint32_t ParameterSetImpl::getUint32(const std::string& aKey,
468  uint32_t aValue) const {
469  const_iterator it = findKV(aKey, false);
470  if (it == end()) return aValue;
471  return it->second.getUint32();
472 }
473 
474 inline int64_t ParameterSetImpl::getInt64(const std::string& aKey) const {
475  return findKV(aKey)->second.getInt64();
476 }
477 
478 inline int64_t ParameterSetImpl::getInt64(const std::string& aKey,
479  int64_t aValue) const {
480  const_iterator it = findKV(aKey, false);
481  if (it == end()) return aValue;
482  return it->second.getInt64();
483 }
484 
485 inline uint64_t ParameterSetImpl::getUint64(const std::string& aKey) const {
486  return findKV(aKey)->second.getUint64();
487 }
488 
489 inline uint64_t ParameterSetImpl::getUint64(const std::string& aKey,
490  uint64_t aValue) const {
491  const_iterator it = findKV(aKey, false);
492  if (it == end()) return aValue;
493  return it->second.getUint64();
494 }
495 
496 inline float ParameterSetImpl::getFloat(const std::string& aKey) const {
497  return findKV(aKey)->second.getFloat();
498 }
499 
500 inline float ParameterSetImpl::getFloat(const std::string& aKey,
501  float aValue) const {
502  const_iterator it = findKV(aKey, false);
503  if (it == end()) return aValue;
504  return it->second.getFloat();
505 }
506 
507 inline double ParameterSetImpl::getDouble(const std::string& aKey) const {
508  return findKV(aKey)->second.getDouble();
509 }
510 
511 inline double ParameterSetImpl::getDouble(const std::string& aKey,
512  double aValue) const {
513  const_iterator it = findKV(aKey, false);
514  if (it == end()) return aValue;
515  return it->second.getDouble();
516 }
517 
518 inline std::string ParameterSetImpl::getString(const std::string& aKey) const {
519  return findKV(aKey)->second.getString();
520 }
521 
522 inline std::string ParameterSetImpl::getString(
523  const std::string& aKey, const std::string& aValue) const {
524  const_iterator it = findKV(aKey, false);
525  if (it == end()) return aValue;
526  return it->second.getString();
527 }
528 
529 inline time_t ParameterSetImpl::getTime(const std::string& aKey) const {
530  return findKV(aKey)->second.getTime();
531 }
532 
533 inline time_t ParameterSetImpl::getTime(const std::string& aKey,
534  const time_t& aValue) const {
535  const_iterator it = findKV(aKey, false);
536  if (it == end()) return aValue;
537  return it->second.getTime();
538 }
539 
540 } // namespace common
541 } // namespace dp3
542 
543 #endif
Implements a map of Key-Value pairs. Description of class. The ParameterSetImpl class is a key-value ...
Definition: ParameterSetImpl.h:51
std::vector< int > getIntVector(const std::string &aKey, bool expandable) const
std::shared_ptr< ParameterSetImpl > makeSubset(const std::string &baseKey, const std::string &prefix="") const
std::vector< time_t > getTimeVector(const std::string &aKey, bool expandable) const
int32_t getInt32(const std::string &aKey) const
Definition: ParameterSetImpl.h:452
unsigned int getUint(const std::string &aKey) const
Definition: ParameterSetImpl.h:419
void add(const std::string &aKey, const ParameterValue &aValue)
bool isDefined(const std::string &searchKey) const
Checks if the given Key is defined in the ParameterSetImpl.
Definition: ParameterSetImpl.h:167
std::vector< float > getFloatVector(const std::string &aKey, const std::vector< float > &aValue, bool expandable) const
const ParameterValue & get(const std::string &aKey) const
Definition: ParameterSetImpl.h:393
void adoptCollection(const ParameterSetImpl &theCollection, const std::string &thePrefix="")
std::vector< uint32_t > getUint32Vector(const std::string &aKey, const std::vector< uint32_t > &aValue, bool expandable) const
~ParameterSetImpl()
Destroy the contents.
std::vector< uint32_t > getUint32Vector(const std::string &aKey, bool expandable) const
std::vector< int32_t > getInt32Vector(const std::string &aKey, bool expandable) const
std::vector< bool > getBoolVector(const std::string &aKey, bool expandable) const
std::vector< int16_t > getInt16Vector(const std::string &aKey, bool expandable) const
std::string fullModuleName(const std::string &shortName) const
std::vector< double > getDoubleVector(const std::string &aKey, const std::vector< double > &aValue, bool expandable) const
void writeBuffer(std::string &theBuffer) const
int16_t getInt16(const std::string &aKey) const
Definition: ParameterSetImpl.h:430
std::vector< std::string > unusedKeys() const
Get all unused parameter names.
uint16_t getUint16(const std::string &aKey) const
Definition: ParameterSetImpl.h:441
std::vector< time_t > getTimeVector(const std::string &aKey, const std::vector< time_t > &aValue, bool expandable) const
std::vector< int > getIntVector(const std::string &aKey, const std::vector< int > &aValue, bool expandable) const
std::vector< uint16_t > getUint16Vector(const std::string &aKey, const std::vector< uint16_t > &aValue, bool expandable) const
time_t getTime(const std::string &aKey) const
Definition: ParameterSetImpl.h:529
void replace(const std::string &aKey, const ParameterValue &aValue)
void writeStream(std::ostream &os) const
std::vector< uint16_t > getUint16Vector(const std::string &aKey, bool expandable) const
std::string getString(const std::string &aKey) const
Definition: ParameterSetImpl.h:518
std::vector< std::string > getStringVector(const std::string &aKey, bool expandable) const
void remove(const std::string &aKey)
Removes the pair with the given key. Removing a non-existing key is ok.
std::vector< unsigned int > getUintVector(const std::string &aKey, const std::vector< unsigned int > &aValue, bool expandable) const
std::vector< int64_t > getInt64Vector(const std::string &aKey, const std::vector< int64_t > &aValue, bool expandable) const
std::vector< bool > getBoolVector(const std::string &aKey, const std::vector< bool > &aValue, bool expandable) const
ParameterSetImpl(KeyCompare::Mode mode)
int getInt(const std::string &aKey) const
Definition: ParameterSetImpl.h:409
uint32_t getUint32(const std::string &aKey) const
Definition: ParameterSetImpl.h:463
void adoptBuffer(const std::string &theBuffer, const std::string &thePrefix="")
uint64_t getUint64(const std::string &aKey) const
Definition: ParameterSetImpl.h:485
void adoptArguments(const std::vector< std::string > &arguments)
int64_t getInt64(const std::string &aKey) const
Definition: ParameterSetImpl.h:474
void subtractSubset(const std::string &fullPrefix)
ParameterSetImpl(const std::string &theFilename, KeyCompare::Mode mode)
float getFloat(const std::string &aKey) const
Definition: ParameterSetImpl.h:496
std::vector< std::string > getStringVector(const std::string &aKey, const std::vector< std::string > &aValue, bool expandable) const
KVMap::iterator iterator
Definition: ParameterSetImpl.h:53
std::vector< double > getDoubleVector(const std::string &aKey, bool expandable) const
std::vector< uint64_t > getUint64Vector(const std::string &aKey, const std::vector< uint64_t > &aValue, bool expandable) const
std::vector< unsigned int > getUintVector(const std::string &aKey, bool expandable) const
std::vector< float > getFloatVector(const std::string &aKey, bool expandable) const
std::vector< int16_t > getInt16Vector(const std::string &aKey, const std::vector< int16_t > &aValue, bool expandable) const
void adoptFile(const std::string &theFilename, const std::string &thePrefix="")
std::vector< int64_t > getInt64Vector(const std::string &aKey, bool expandable) const
KeyCompare::Mode keyCompareMode() const
Return the key comparison mode.
Definition: ParameterSetImpl.h:79
KVMap::const_iterator const_iterator
Definition: ParameterSetImpl.h:54
friend std::ostream & operator<<(std::ostream &os, const ParameterSetImpl &thePS)
void writeFile(const std::string &theFilename, bool append=false) const
Writes the Key-Values pair from the current ParCollection to the file.
std::vector< uint64_t > getUint64Vector(const std::string &aKey, bool expandable) const
std::vector< int32_t > getInt32Vector(const std::string &aKey, const std::vector< int32_t > &aValue, bool expandable) const
bool getBool(const std::string &aKey) const
Definition: ParameterSetImpl.h:398
std::string locateModule(const std::string &shortName) const
double getDouble(const std::string &aKey) const
Definition: ParameterSetImpl.h:507
The value of a parameter.
Definition: ParameterValue.h:24
Functor to compare two strings. Strings can be compared case sensitive (NORMAL) and case insensitive ...
Definition: StringTools.h:43
Mode
String comparison mode.
Definition: StringTools.h:46
std::string keyName(const std::string &fullKeyName)
common::stringtools::Compare KeyCompare
Definition: ParameterSetImpl.h:32
const char PC_QUAL_DEVELOP[]
Definition: ParameterSetImpl.h:28
std::string moduleName(const std::string &fullKeyName)
const char PC_KEY_VERSIONNR[]
Definition: ParameterSetImpl.h:29
std::map< std::string, ParameterValue, KeyCompare > KVMap
Definition: ParameterSetImpl.h:37
bool isValidVersionNr(const std::string &versionNr)
Checks if the given string is a valid versionnumber (x.y.z)
bool isValidVersionNrRef(const std::string &versionNr)
const char PC_QUAL_TEST[]
Definition: ParameterSetImpl.h:27
const char PC_KEY_QUAL[]
Definition: ParameterSetImpl.h:30
std::string keyPart(const std::string &parameterLine)
const char PC_QUAL_STABLE[]
Implements a map of Key-Value pairs.
Definition: ParameterSetImpl.h:26
int32_t indexValue(const std::string &label, const char indexMarker[2])
std::string valuePart(const std::string &parameterLine)
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53