lhapdf is hosted by Hepforge, IPPP Durham
LHAPDF  6.5.4
Info.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2023 The LHAPDF collaboration (see AUTHORS for details)
5 //
6 #pragma once
7 #ifndef LHAPDF_Info_H
8 #define LHAPDF_Info_H
9 
10 #include "LHAPDF/Utils.h"
11 #include "LHAPDF/Paths.h"
12 #include "LHAPDF/Exceptions.h"
13 
14 namespace LHAPDF {
15 
16 
23  // class Info;
24  // Info& config();
25 
26 
27 
29  class Info {
30  public:
31 
33  Info() { }
34 
36  Info(const std::string& path) {
37  load(path);
38  }
39 
41  virtual ~Info() { }
42 
43 
46 
51  void load(const std::string& filepath);
52 
54 
55 
58 
59  // /// Get all metadata as a map
60  // const std::map<std::string, std::string>& metadata() const {
61  // return _metadict;
62  // }
63 
64  // /// Get all metadata as a map (non-const)
65  // std::map<std::string, std::string>& metadata() {
66  // return _metadict;
67  // }
68 
70  std::vector<std::string> keys_local() const {
71  std::vector<std::string> rtn;
72  rtn.reserve(_metadict.size());
73  for (const auto& kv : _metadict) rtn.push_back(kv.first);
74  return rtn;
75  }
76 
82  std::vector<std::string> keys() const {
83  return keys_local();
84  }
85 
87  bool has_key_local(const std::string& key) const {
88  return _metadict.find(key) != _metadict.end();
89  }
90 
100  virtual bool has_key(const std::string& key) const {
101  return has_key_local(key);
102  }
103 
104 
106  const std::string& get_entry_local(const std::string& key) const {
107  if (has_key_local(key)) return _metadict.find(key)->second;
108  throw MetadataError("Metadata for key: " + key + " not found.");
109  }
110 
119  virtual const std::string& get_entry(const std::string& key) const {
120  return get_entry_local(key);
121  }
122 
123 
125  virtual const std::string& get_entry(const std::string& key, const std::string& fallback) const {
126  try {
127  return get_entry(key);
128  } catch (...) {
129  return fallback;
130  }
131  }
132 
133 
138  template <typename T>
139  T get_entry_as(const std::string& key) const {
140  const string& s = get_entry(key);
141  return lexical_cast<T>(s);
142  }
143 
144 
146  template <typename T>
147  T get_entry_as(const std::string& key, const T& fallback) const {
148  try {
149  return get_entry_as<T>(key);
150  } catch (...) {
151  return fallback;
152  }
153  }
154 
155 
157  template <typename T>
158  void set_entry(const std::string& key, const T& val) {
159  _metadict[key] = to_str(val);
160  }
161 
163 
164 
165  protected:
166 
168  std::map<std::string, std::string> _metadict;
169 
170  };
171 
172 
173 
176 
177  template <>
178  inline bool Info::get_entry_as(const std::string& key) const {
179  const string& s = get_entry(key);
180  try {
181  bool rtn = lexical_cast<bool>(s);
182  return rtn;
183  } catch (...) {
184  if (s == "true" || s == "on" || s == "yes") return true;
185  if (s == "false" || s == "off" || s == "no") return false;
186  }
187  throw MetadataError("'" + s + "' is not a valid string for conversion to bool type");
188  }
189 
190  template <>
191  inline std::vector<std::string> Info::get_entry_as(const std::string& key) const {
192  static const string delim = ",";
193  string strval = trim(get_entry(key));
194  // cout << "@@ " << strval << endl;
195  if (startswith(strval, "[")) strval = strval.substr(1, strval.size()-1);
196  if (endswith(strval, "]")) strval = strval.substr(0, strval.size()-1);
197  // cout << "## " << strval << endl;
198  return split(strval, delim);
199  }
200 
201  template <>
202  inline std::vector<int> Info::get_entry_as(const std::string& key) const {
203  const vector<string> strs = get_entry_as< vector<string> >(key);
204  vector<int> rtn;
205  rtn.reserve(strs.size());
206  for (const string& s : strs) rtn.push_back( lexical_cast<int>(s) );
207  assert(rtn.size() == strs.size());
208  return rtn;
209  }
210 
211  template <>
212  inline std::vector<double> Info::get_entry_as(const std::string& key) const {
213  const vector<string> strs = get_entry_as< vector<string> >(key);
214  vector<double> rtn;
215  rtn.reserve(strs.size());
216  for (const string& s : strs) rtn.push_back( lexical_cast<double>(s) );
217  assert(rtn.size() == strs.size());
218  return rtn;
219  }
220 
222 
223 
224 }
225 #endif
bool endswith(const std::string &s, const std::string &sub)
Does a string s end with the sub substring?
Definition: Utils.h:120
virtual ~Info()
Virtual destructor to allow inheritance.
Definition: Info.h:41
void set_entry(const std::string &key, const T &val)
Set a keyed value entry.
Definition: Info.h:158
T get_entry_as(const std::string &key, const T &fallback) const
Retrieve a metadata entry by key name, with an inline type cast and default fallback.
Definition: Info.h:147
Info(const std::string &path)
Constructor.
Definition: Info.h:36
virtual const std::string & get_entry(const std::string &key) const
Definition: Info.h:119
const std::string & get_entry_local(const std::string &key) const
Retrieve a metadata string by key name, as defined on this specific object.
Definition: Info.h:106
Info()
Default constructor.
Definition: Info.h:33
Error for unfound or broken metadata entries.
Definition: Exceptions.h:54
virtual const std::string & get_entry(const std::string &key, const std::string &fallback) const
Retrieve a metadata string by key name, with a default fallback.
Definition: Info.h:125
std::map< std::string, std::string > _metadict
The string -&gt; string native metadata storage container.
Definition: Info.h:168
std::string trim(const std::string &s)
Strip leading and trailing spaces (not in-place)
Definition: Utils.h:130
void load(const std::string &filepath)
T get_entry_as(const std::string &key) const
Definition: Info.h:139
bool startswith(const std::string &s, const std::string &sub)
Does a string s start with the sub substring?
Definition: Utils.h:115
virtual bool has_key(const std::string &key) const
Definition: Info.h:100
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:61
std::vector< std::string > keys() const
Definition: Info.h:82
std::vector< std::string > keys_local() const
Get the keys defined on this specific object.
Definition: Info.h:70
T lexical_cast(const U &in)
Convert between types via stringstream.
Definition: Utils.h:47
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split a string by a given separator.
Definition: Utils.h:95
Metadata base class for PDFs, PDF sets, or global configuration.
Definition: Info.h:29
bool has_key_local(const std::string &key) const
Is a value defined for the given key on this specific object?
Definition: Info.h:87