Cantera  2.4.0
AnyMap.inl.h
Go to the documentation of this file.
1 //! @file AnyMap.inl.h
2 
3 #ifndef CT_ANYMAP_INL_H
4 #define CT_ANYMAP_INL_H
5 
6 #include "cantera/base/AnyMap.h"
7 
8 #include <boost/any.hpp>
9 #include <boost/algorithm/string.hpp>
10 
11 namespace Cantera
12 {
13 
14 // Definitions for AnyValue templated functions
15 
16 template<class T>
17 const T &AnyValue::as() const {
18  try {
19  return boost::any_cast<const T&>(*m_value);
20  } catch (boost::bad_any_cast&) {
21  if (m_value->type() == typeid(void)) {
22  // Values that have not been set are of type 'void'
23  throw CanteraError("AnyValue::as", "Key '{}' not found", m_key);
24  } else {
25  throw CanteraError("AnyValue::as",
26  "Key '{}' contains a '{}',\nnot a '{}'.",
27  m_key, demangle(m_value->type()), demangle(typeid(T)));
28  }
29  }
30 }
31 
32 template<class T>
33 T &AnyValue::as() {
34  try {
35  return boost::any_cast<T&>(*m_value);
36  } catch (boost::bad_any_cast&) {
37  if (m_value->type() == typeid(void)) {
38  // Values that have not been set are of type 'void'
39  throw CanteraError("AnyValue::as", "Key '{}' not found", m_key);
40  } else {
41  throw CanteraError("AnyValue::as",
42  "Key '{}' contains a '{}',\nnot a '{}'.",
43  m_key, demangle(m_value->type()), demangle(typeid(T)));
44  }
45  }
46 }
47 
48 template<class T>
49 bool AnyValue::is() const {
50  return m_value->type() == typeid(T);
51 }
52 
53 template<class T>
54 AnyValue &AnyValue::operator=(const std::vector<T> &value) {
55  *m_value = value;
56  return *this;
57 }
58 
59 template<class T>
60 const std::vector<T> &AnyValue::asVector() const {
61  return as<std::vector<T>>();
62 }
63 
64 template<class T>
65 std::vector<T> &AnyValue::asVector() {
66  return as<std::vector<T>>();
67 }
68 
69 template<class T>
70 AnyValue& AnyValue::operator=(const std::unordered_map<std::string, T> items) {
71  *m_value = AnyMap();
72  AnyMap& dest = as<AnyMap>();
73  for (const auto& item : items) {
74  dest[item.first] = item.second;
75  }
76  return *this;
77 }
78 
79 template<class T>
80 AnyValue& AnyValue::operator=(const std::map<std::string, T> items) {
81  *m_value = AnyMap();
82  AnyMap& dest = as<AnyMap>();
83  for (const auto& item : items) {
84  dest[item.first] = item.second;
85  }
86  return *this;
87 }
88 
89 template<>
90 inline AnyMap& AnyValue::as<AnyMap>() {
91  try {
92  // This is where nested AnyMaps are created when the syntax
93  // m[key1][key2] is used.
94  if (m_value->type() == typeid(void)) {
95  *m_value = AnyMap();
96  }
97  return boost::any_cast<AnyMap&>(*m_value);
98  } catch (boost::bad_any_cast&) {
99  throw CanteraError("AnyValue::as",
100  "value of key '{}' is a '{}',\nnot an 'AnyMap'.",
101  m_key, demangle(m_value->type()));
102  }
103 }
104 
105 template<class T>
106 std::map<std::string, T> AnyValue::asMap()
107 {
108  std::map<std::string, T> dest;
109  for (const auto& item : as<AnyMap>().m_data) {
110  try {
111  dest[item.first] = boost::any_cast<T>(*item.second.m_value);
112  } catch (boost::bad_any_cast&) {
113  throw CanteraError("AnyValue::asMap",
114  "Value of key '{}' is not a '{}'",
115  item.first, demangle(typeid(T)));
116  }
117  }
118  return dest;
119 }
120 
121 }
122 #endif
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8