Add map .at method from c++11

closes #184
This commit is contained in:
Jason Turner 2015-06-10 18:41:50 -06:00
parent ba492308f4
commit f9e0193353
5 changed files with 34 additions and 2 deletions

View File

@ -408,6 +408,12 @@ namespace chaiscript
m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions"); m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions");
m->add(user_type<std::out_of_range>(), "out_of_range");
m->add(user_type<std::logic_error>(), "logic_error");
m->add(chaiscript::base_class<std::exception, std::logic_error>());
m->add(chaiscript::base_class<std::logic_error, std::out_of_range>());
m->add(chaiscript::base_class<std::exception, std::out_of_range>());
m->add(user_type<std::runtime_error>(), "runtime_error"); m->add(user_type<std::runtime_error>(), "runtime_error");
m->add(chaiscript::base_class<std::exception, std::runtime_error>()); m->add(chaiscript::base_class<std::exception, std::runtime_error>());

View File

@ -438,9 +438,13 @@ namespace chaiscript
m->add(user_type<MapType>(), type); m->add(user_type<MapType>(), type);
typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &); typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &);
typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const;
m->add(fun(static_cast<elem_access>(&MapType::operator[])), "[]"); m->add(fun(static_cast<elem_access>(&MapType::operator[])), "[]");
m->add(fun(static_cast<elem_access>(&MapType::at)), "at");
m->add(fun(static_cast<const_elem_access>(&MapType::at)), "at");
container_type<MapType>(type, m); container_type<MapType>(type, m);
default_constructible_type<MapType>(type, m); default_constructible_type<MapType>(type, m);
assignable_type<MapType>(type, m); assignable_type<MapType>(type, m);

View File

@ -878,7 +878,7 @@ namespace chaiscript
} }
std::stable_sort(ordered_funcs.begin(), ordered_funcs.end(), std::stable_sort(ordered_funcs.begin(), ordered_funcs.end(),
[](const decltype(ordered_funcs)::const_reference &t_lhs, const decltype(ordered_funcs)::const_reference &t_rhs) [](decltype(ordered_funcs)::const_reference t_lhs, decltype(ordered_funcs)::const_reference t_rhs)
{ {
return t_lhs.first < t_rhs.first; return t_lhs.first < t_rhs.first;
} }

View File

@ -1340,9 +1340,14 @@ namespace chaiscript
} }
throw; throw;
} }
catch (const std::runtime_error &e) {
retval = handle_exception(t_ss, Boxed_Value(std::ref(e)));
}
catch (const std::out_of_range &e) {
retval = handle_exception(t_ss, Boxed_Value(std::ref(e)));
}
catch (const std::exception &e) { catch (const std::exception &e) {
retval = handle_exception(t_ss, Boxed_Value(std::ref(e))); retval = handle_exception(t_ss, Boxed_Value(std::ref(e)));
} }
catch (Boxed_Value &e) { catch (Boxed_Value &e) {
retval = handle_exception(t_ss, e); retval = handle_exception(t_ss, e);

View File

@ -1,2 +1,19 @@
auto x = ["bob":2, "fred":3] auto x = ["bob":2, "fred":3]
assert_equal(3, x["fred"]) assert_equal(3, x["fred"])
try {
auto m = ["bob":2, "fred":3];
m.at("tom");
assert_true(false);
} catch (out_of_range e) {
print("out_of_range")
assert_true(true);
} catch (e) {
print("other")
dump_object(e);
assert_true(false);
}
assert_equal(["bob":2, "fred":3].at("fred"), 3);