lhapdf is hosted by Hepforge, IPPP Durham
LHAPDF  6.2.3
Info.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2019 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 
34 
36  Info() { }
37 
39  Info(const std::string& path) {
40  load(path);
41  }
42 
44  virtual ~Info() { }
45 
47 
48 
50 
51 
56  void load(const std::string& filepath);
57 
59 
60 
62 
63 
64  // /// Get all metadata as a map
65  // const std::map<std::string, std::string>& metadata() const {
66  // return _metadict;
67  // }
68 
69  // /// Get all metadata as a map (non-const)
70  // std::map<std::string, std::string>& metadata() {
71  // return _metadict;
72  // }
73 
74 
76  bool has_key_local(const std::string& key) const {
77  return _metadict.find(key) != _metadict.end();
78  }
79 
88  virtual bool has_key(const std::string& key) const {
89  return has_key_local(key);
90  }
91 
92 
94  const std::string& get_entry_local(const std::string& key) const {
95  if (has_key_local(key)) return _metadict.find(key)->second;
96  throw MetadataError("Metadata for key: " + key + " not found.");
97  }
98 
107  virtual const std::string& get_entry(const std::string& key) const {
108  return get_entry_local(key);
109  }
110 
111 
113  virtual const std::string& get_entry(const std::string& key, const std::string& fallback) const {
114  try {
115  return get_entry(key);
116  } catch (...) {
117  return fallback;
118  }
119  }
120 
121 
126  template <typename T>
127  T get_entry_as(const std::string& key) const {
128  const string& s = get_entry(key);
129  return lexical_cast<T>(s);
130  }
131 
132 
134  template <typename T>
135  T get_entry_as(const std::string& key, const T& fallback) const {
136  try {
137  return get_entry_as<T>(key);
138  } catch (...) {
139  return fallback;
140  }
141  }
142 
143 
145  template <typename T>
146  void set_entry(const std::string& key, const T& val) {
147  _metadict[key] = to_str(val);
148  }
149 
151 
152 
153  protected:
154 
156  std::map<std::string, std::string> _metadict;
157 
158  };
159 
160 
162 
163 
164  template <>
165  inline bool Info::get_entry_as(const std::string& key) const {
166  const string& s = get_entry(key);
167  try {
168  bool rtn = lexical_cast<bool>(s);
169  return rtn;
170  } catch (...) {
171  if (s == "true" || s == "on" || s == "yes") return true;
172  if (s == "false" || s == "off" || s == "no") return false;
173  }
174  throw MetadataError("'" + s + "' is not a valid string for conversion to bool type");
175  }
176 
177  template <>
178  inline std::vector<std::string> Info::get_entry_as(const std::string& key) const {
179  static const string delim = ",";
180  return split(get_entry(key), delim);
181  }
182 
183  template <>
184  inline std::vector<int> Info::get_entry_as(const std::string& key) const {
185  const vector<string> strs = get_entry_as< vector<string> >(key);
186  vector<int> rtn;
187  rtn.reserve(strs.size());
188  // for (const string& s : strs) rtn.push_back( lexical_cast<int>(s) ); //< @todo Restore when C++11 guaranteed
189  for (size_t i = 0; i < strs.size(); ++i) rtn.push_back( lexical_cast<int>(strs[i]) );
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) ); //< @todo Restore when C++11 guaranteed
200  for (size_t i = 0; i < strs.size(); ++i) rtn.push_back( lexical_cast<double>(strs[i]) );
201  assert(rtn.size() == strs.size());
202  return rtn;
203  }
204 
206 
207 
208 }
209 #endif
virtual ~Info()
Virtual destructor to allow inheritance.
Definition: Info.h:44
void set_entry(const std::string &key, const T &val)
Set a keyed value entry.
Definition: Info.h:146
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:57
Info(const std::string &path)
Constructor.
Definition: Info.h:39
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split a string by a given separator.
Definition: Utils.h:91
Info()
Default constructor.
Definition: Info.h:36
Error for unfound or broken metadata entries.
Definition: Exceptions.h:54
std::map< std::string, std::string > _metadict
The string -> string native metadata storage container.
Definition: Info.h:156
virtual bool has_key(const std::string &key) const
Definition: Info.h:88
T lexical_cast(const U &in)
Convert between any types via stringstream.
Definition: Utils.h:43
virtual const std::string & get_entry(const std::string &key) const
Definition: Info.h:107
void load(const std::string &filepath)
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:135
T get_entry_as(const std::string &key) const
Definition: Info.h:127
bool has_key_local(const std::string &key) const
Is a value defined for the given key on this specific object?
Definition: Info.h:76
Namespace for all LHAPDF functions and classes.
Definition: AlphaS.h:14
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:94
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:113
Metadata base class for PDFs, PDF sets, or global configuration.
Definition: Info.h:29