LHAPDF  6.5.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
Info.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2022 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 
69 
71  bool has_key_local(const std::string& key) const {
72  return _metadict.find(key) != _metadict.end();
73  }
74 
83  virtual bool has_key(const std::string& key) const {
84  return has_key_local(key);
85  }
86 
87 
89  const std::string& get_entry_local(const std::string& key) const {
90  if (has_key_local(key)) return _metadict.find(key)->second;
91  throw MetadataError("Metadata for key: " + key + " not found.");
92  }
93 
102  virtual const std::string& get_entry(const std::string& key) const {
103  return get_entry_local(key);
104  }
105 
106 
108  virtual const std::string& get_entry(const std::string& key, const std::string& fallback) const {
109  try {
110  return get_entry(key);
111  } catch (...) {
112  return fallback;
113  }
114  }
115 
116 
121  template <typename T>
122  T get_entry_as(const std::string& key) const {
123  const string& s = get_entry(key);
124  return lexical_cast<T>(s);
125  }
126 
127 
129  template <typename T>
130  T get_entry_as(const std::string& key, const T& fallback) const {
131  try {
132  return get_entry_as<T>(key);
133  } catch (...) {
134  return fallback;
135  }
136  }
137 
138 
140  template <typename T>
141  void set_entry(const std::string& key, const T& val) {
142  _metadict[key] = to_str(val);
143  }
144 
146 
147 
148  protected:
149 
151  std::map<std::string, std::string> _metadict;
152 
153  };
154 
155 
156 
159 
160  template <>
161  inline bool Info::get_entry_as(const std::string& key) const {
162  const string& s = get_entry(key);
163  try {
164  bool rtn = lexical_cast<bool>(s);
165  return rtn;
166  } catch (...) {
167  if (s == "true" || s == "on" || s == "yes") return true;
168  if (s == "false" || s == "off" || s == "no") return false;
169  }
170  throw MetadataError("'" + s + "' is not a valid string for conversion to bool type");
171  }
172 
173  template <>
174  inline std::vector<std::string> Info::get_entry_as(const std::string& key) const {
175  static const string delim = ",";
176  string strval = trim(get_entry(key));
177  // cout << "@@ " << strval << endl;
178  if (startswith(strval, "[")) strval = strval.substr(1, strval.size()-1);
179  if (endswith(strval, "]")) strval = strval.substr(0, strval.size()-1);
180  // cout << "## " << strval << endl;
181  return split(strval, delim);
182  }
183 
184  template <>
185  inline std::vector<int> Info::get_entry_as(const std::string& key) const {
186  const vector<string> strs = get_entry_as< vector<string> >(key);
187  vector<int> rtn;
188  rtn.reserve(strs.size());
189  for (const string& s : strs) rtn.push_back( lexical_cast<int>(s) );
190  assert(rtn.size() == strs.size());
191  return rtn;
192  }
193 
194  template <>
195  inline std::vector<double> Info::get_entry_as(const std::string& key) const {
196  const vector<string> strs = get_entry_as< vector<string> >(key);
197  vector<double> rtn;
198  rtn.reserve(strs.size());
199  for (const string& s : strs) rtn.push_back( lexical_cast<double>(s) );
200  assert(rtn.size() == strs.size());
201  return rtn;
202  }
203 
205 
206 
207 }
208 #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:141
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:130
Info(const std::string &path)
Constructor.
Definition: Info.h:36
virtual const std::string & get_entry(const std::string &key) const
Definition: Info.h:102
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:89
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:108
std::map< std::string, std::string > _metadict
The string -&gt; string native metadata storage container.
Definition: Info.h:151
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:122
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:83
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:61
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:71