1 #ifndef ARGUMENTSDICT_H
2 #define ARGUMENTSDICT_H
7 #include "xtensor-python/pyarray.hpp"
8 #include "xtensor/xio.hpp"
25 template <
class K,
class T>
36 using pointer =
typename base_type::pointer;
43 using base_type::empty;
44 using base_type::find;
45 using base_type::emplace;
47 using base_type::begin;
49 using base_type::cbegin;
50 using base_type::cend;
63 template <
class K,
class T>
66 template <
class K,
class T>
77 xt::pyarray<T>&
array(
const std::string& key);
84 template <
class D1,
class D2>
85 typename D1::mapped_type& find_element(
const std::string& key,
88 const std::string& expected_type,
89 const std::string& other_type);
92 std::string get_additional_error_msg(
const std::string& key,
94 const std::string& asked_type,
95 const std::string& tried_type)
const;
112 return std::string(
"key ") + key + std::string(
" not found");
116 template <
class K,
class T>
119 auto it = this->find(k);
127 template <
class K,
class T>
130 return base_type::insert(std::move(
v));
133 template <
class K,
class T>
136 auto it = base_type::find(k);
137 if(it == base_type::end())
139 return base_type::emplace(k, std::move(
v));
143 it->second = std::move(
v);
144 return std::make_pair(it,
false);
153 inline xt::pyarray<double>& arguments_dict::array<double>(
const std::string& key)
155 return find_element(key,
m_darray,
m_iarray,
"pyarray<double>",
"pyarray<int>");
159 inline xt::pyarray<int>& arguments_dict::array<int>(
const std::string& key)
161 return find_element(key,
m_iarray,
m_darray,
"pyarray<int>",
"pyarray<double>");
165 inline double& arguments_dict::scalar<double>(
const std::string& key)
171 inline int& arguments_dict::scalar<int>(
const std::string& key)
176 template <
class D1,
class D2>
177 typename D1::mapped_type& arguments_dict::find_element(
const std::string& key,
179 const D2& other_dict,
180 const std::string& expected_type,
181 const std::string& other_type)
185 return expected_dict[key];
187 catch(std::runtime_error& e)
189 throw std::runtime_error(e.what()
190 + get_additional_error_msg(key, other_dict, expected_type, other_type));
195 std::string arguments_dict::get_additional_error_msg(
const std::string& key,
197 const std::string& asked_type,
198 const std::string& tried_type)
const
200 auto it = d.find(key);
203 return " in any of the internal dicts";
207 return " in dict of " + asked_type +
" but found in dict of " + tried_type;