26 #include <momemta/any.h> 27 #include <momemta/InputTag.h> 28 #include <momemta/impl/traits.h> 29 #include <momemta/Logging.h> 30 #include <momemta/Utils.h> 39 void setInputTagsForInput(
const ArgDef&,
ParameterSet&,
const std::vector<InputTag>&);
40 class ComputationGraph;
86 template<
typename T>
const T&
get(
const std::string& name)
const {
87 auto value = m_set.find(name);
88 if (value == m_set.end())
89 throw not_found_error(
"Parameter '" + name +
"' not found.");
92 return momemta::any_cast<
const T&>(value->second.value);
93 }
catch (
const momemta::bad_any_cast& e) {
94 LOG(fatal) <<
"Exception while trying to get parameter '" << name <<
"'. Requested a '" 95 << demangle(
typeid(T).name())
96 <<
"' while parameter is a '" 97 << demangle(value->second.value.type().name())
103 template<
typename T> T&
get(
const std::string& name) {
104 auto value = m_set.find(name);
105 if (value == m_set.end())
106 throw not_found_error(
"Parameter '" + name +
"' not found.");
109 return momemta::any_cast<T&>(value->second.value);
110 }
catch (
const momemta::bad_any_cast& e) {
111 LOG(fatal) <<
"Exception while trying to get parameter '" << name <<
"'. Requested a '" 112 << demangle(
typeid(T).name())
113 <<
"' while parameter is a '" 114 << demangle(value->second.value.type().name())
120 template<
typename T>
const T&
get(
const std::string& name,
const T& defaultValue)
const {
121 auto value = m_set.find(name);
122 if (value == m_set.end())
126 return momemta::any_cast<
const T&>(value->second.value);
127 }
catch (
const momemta::bad_any_cast &e) {
128 LOG(fatal) <<
"Exception while trying to get parameter '" << name <<
"'. Requested a '" 129 << demangle(
typeid(T).name())
130 <<
"' while parameter is a '" 131 << demangle(value->second.value.type().name())
144 const momemta::any& rawGet(
const std::string& name)
const;
146 bool exists(
const std::string& name)
const;
147 template<
typename T>
bool existsAs(
const std::string& name)
const {
148 auto value = m_set.find(name);
149 return (value != m_set.end() && value->second.value.type() ==
typeid(T));
159 typename std::enable_if<std::is_same<T, bool>::value ||
160 std::is_same<T, InputTag>::value>::type
set(
const std::string& name,
const T& value) {
161 set_helper(name, value);
173 void set(
const std::string& name,
const std::vector<T>& value) {
175 std::is_same<T, int64_t>::value ||
176 std::is_same<T, double>::value ||
177 std::is_same<T, bool>::value ||
178 std::is_same<T, std::string>::value ||
179 std::is_same<T, InputTag>::value,
182 set_helper(name, value);
194 typename std::enable_if<is_string<T>::value>::type
set(
const std::string& name,
const T& value) {
195 set_helper(name, std::string(value));
207 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value>::type
set(
const std::string& name,
const T& value) {
208 set_helper(name, static_cast<int64_t>(value));
220 typename std::enable_if<std::is_floating_point<T>::value>::type
set(
const std::string& name,
const T& value) {
221 set_helper(name, static_cast<double>(value));
224 std::string getModuleName()
const {
225 return get<std::string>(
"@name",
"");
228 std::string getModuleType()
const {
229 return get<std::string>(
"@type",
"");
233 auto it = m_set.find(
"@global_parameters");
234 if (it == m_set.end())
237 return momemta::any_cast<
const ParameterSet&>(it->second.value);
249 std::vector<std::string> getNames()
const;
263 template <
typename T>
268 template <
typename T>
275 ParameterSet(
const std::string& module_type,
const std::string& module_name);
282 virtual bool lazy()
const;
290 virtual void create(
const std::string& name,
const momemta::any& value);
291 virtual void setInternal(
const std::string& name,
Element& element,
const momemta::any& value);
293 virtual void freeze();
295 std::map<std::string, Element> m_set;
299 class not_found_error:
public std::runtime_error {
300 using std::runtime_error::runtime_error;
303 class frozen_error:
public std::runtime_error {
304 using std::runtime_error::runtime_error;
318 void set_helper(
const std::string& name,
const T& value) {
320 std::is_same<T, int64_t>::value ||
321 std::is_same<T, double>::value ||
322 std::is_same<T, bool>::value ||
323 std::is_same<T, std::string>::value ||
324 std::is_same<T, InputTag>::value ||
325 std::is_same<T, std::vector<int64_t>>::value ||
326 std::is_same<T, std::vector<double>>::value ||
327 std::is_same<T, std::vector<bool>>::value ||
328 std::is_same<T, std::vector<std::string>>::value ||
329 std::is_same<T, std::vector<InputTag>>::value,
334 LOG(fatal) <<
"You are not allowed to edit a set once frozen.";
335 throw frozen_error(
"This ParameterSet is frozen");
338 raw_set(name, value);
341 template <
typename T>
342 void raw_set(
const std::string& name,
const T& value) {
344 auto it = m_set.find(name);
347 if (it == m_set.end()) {
350 setInternal(name, it->second, value);
354 void remove(
const std::string& name);
A lua configuration file parser.
A small wrapper around a momemta::any value.
Defines an input / output.
A class encapsulating a lua table.
A frozen snapshot of the configuration file.
Element(const T &v)
If true, it means we hold a lazy value which should be evaluated.