diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 0a53201..88fed35 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -224,6 +224,20 @@ namespace chaiscript return boost::bind(&return_int_impl, boost::function(boost::mem_fn(t_func)), _1, _2, _3); } + template + void insert(T &t_target, const T &t_other) + { + t_target.insert(t_other.begin(), t_other.end()); + } + + template + void insert_ref(T &t_target, const typename T::value_type &t_val) + { + t_target.insert(t_val); + } + + + /** * Add Bidir_Range support for the given ContainerType @@ -477,6 +491,18 @@ namespace chaiscript erase eraseptr(&ContainerType::erase); m->add(fun(boost::function(detail::return_int(eraseptr))), "erase"); + + m->add(fun(&detail::insert), "insert"); + + std::string insert_name; + if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) + { + insert_name = "insert_ref"; + } else { + insert_name = "insert"; + } + + m->add(fun(&detail::insert_ref), insert_name); return m; } diff --git a/unittests/map.chai b/unittests/map.chai index 3fe753a..b2901ce 100644 --- a/unittests/map.chai +++ b/unittests/map.chai @@ -10,4 +10,20 @@ assert_equal(1, m.erase("a")) assert_equal(1, m.size()) assert_equal(0, m.erase("a")) +assert_equal(1, m.size()); +var m2 = ["c":3, "b":4] +m.insert(m2); + +assert_equal(3, m["c"]) +// The inserted values do not overwrite the existing ones +assert_equal(2, m["b"]) +assert_equal(2, m.size()) + +var v = "bob"; + +m.insert_ref(Map_Pair("d", v)) + +assert_equal("bob", m["d"]) +v = "bob2" +assert_equal("bob2", m["d"])