LHAPDF is hosted by Hepforge, IPPP Durham
LHAPDF  6.2.1
Info.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2016 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 #include <fstream>
14 
15 namespace LHAPDF {
16 
17 
24  // class Info;
25  // Info& config();
26 
27 
28 
30  class Info {
31  public:
32 
34 
35 
37  Info() { }
38 
40  Info(const std::string& path) {
41  load(path);
42  }
43 
45  virtual ~Info() { }
46 
48 
49 
51 
52 
57  void load(const std::string& filepath);
58 
60 
61 
63 
64 
65  // /// Get all metadata as a map
66  // const std::map<std::string, std::string>& metadata() const {
67  // return _metadict;
68  // }
69 
70  // /// Get all metadata as a map (non-const)
71  // std::map<std::string, std::string>& metadata() {
72  // return _metadict;
73  // }
74 
75 
77  bool has_key_local(const std::string& key) const {
78  return _metadict.find(key) != _metadict.end();
79  }
80 
89  virtual bool has_key(const std::string& key) const {
90  return has_key_local(key);
91  }
92 
93 
95  const std::string& get_entry_local(const std::string& key) const {
96  if (has_key_local(key)) return _metadict.find(key)->second;
97  throw MetadataError("Metadata for key: " + key + " not found.");
98  }
99 
108  virtual const std::string& get_entry(const std::string& key) const {
109  return get_entry_local(key);
110  }
111 
112 
114  virtual const std::string& get_entry(const std::string& key, const std::string& fallback) const {
115  try {
116  return get_entry(key);
117  } catch (...) {
118  return fallback;
119  }
120  }
121 
122 
127  template <typename T>
128  T get_entry_as(const std::string& key) const {
129  const string& s = get_entry(key);
130  return lexical_cast<T>(s);
131  }
132 
133 
135  template <typename T>
136  T get_entry_as(const std::string& key, const T& fallback) const {
137  try {
138  return get_entry_as<T>(key);
139  } catch (...) {
140  return fallback;
141  }
142  }
143 
144 
146  template <typename T>
147  void set_entry(const std::string& key, const T& val) {
148  _metadict[key] = to_str(val);
149  }
150 
152 
153 
154  protected:
155 
157  std::map<std::string, std::string> _metadict;
158 
159  };
160 
161 
163 
164 
165  template <>
166  inline bool Info::get_entry_as(const std::string& key) const {
167  const string& s = get_entry(key);
168  try {
169  bool rtn = lexical_cast<bool>(s);
170  return rtn;
171  } catch (...) {
172  if (s == "true" || s == "on" || s == "yes") return true;
173  if (s == "false" || s == "off" || s == "no") return false;
174  }
175  throw MetadataError("'" + s + "' is not a valid string for conversion to bool type");
176  }
177 
178  template <>
179  inline std::vector<std::string> Info::get_entry_as(const std::string& key) const {
180  static const string delim = ",";
181  return split(get_entry(key), 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) ); //< @todo Restore when C++11 guaranteed
190  for (size_t i = 0; i < strs.size(); ++i) rtn.push_back( lexical_cast<int>(strs[i]) );
191  assert(rtn.size() == strs.size());
192  return rtn;
193  }
194 
195  template <>
196  inline std::vector<double> Info::get_entry_as(const std::string& key) const {
197  const vector<string> strs = get_entry_as< vector<string> >(key);
198  vector<double> rtn;
199  rtn.reserve(strs.size());
200  //for (const string& s : strs) rtn.push_back( lexical_cast<double>(s) ); //< @todo Restore when C++11 guaranteed
201  for (size_t i = 0; i < strs.size(); ++i) rtn.push_back( lexical_cast<double>(strs[i]) );
202  assert(rtn.size() == strs.size());
203  return rtn;
204  }
205 
207 
208 
209 }
210 #endif
virtual ~Info()
Virtual destructor to allow inheritance.
Definition: Info.h:45
void set_entry(const std::string &key, const T &val)
Set a keyed value entry.
Definition: Info.h:147
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:60
Info(const std::string &path)
Constructor.
Definition: Info.h:40
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split a string by a given separator.
Definition: Utils.h:94
Info()
Default constructor.
Definition: Info.h:37
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:157
virtual bool has_key(const std::string &key) const
Definition: Info.h:89
T lexical_cast(const U &in)
Convert between any types via stringstream.
Definition: Utils.h:46
virtual const std::string & get_entry(const std::string &key) const
Definition: Info.h:108
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:136
T get_entry_as(const std::string &key) const
Definition: Info.h:128
bool has_key_local(const std::string &key) const
Is a value defined for the given key on this specific object?
Definition: Info.h:77
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:95
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:114
Metadata base class for PDFs, PDF sets, or global configuration.
Definition: Info.h:30