mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-29 08:32:02 +00:00
Added __contains__ to stl bindings for maps (#1767)
* Added __contains__ to stl bindings for maps
This commit is contained in:
parent
c251434011
commit
30c0352348
@ -579,6 +579,15 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
|
|||||||
return_value_policy::reference_internal // ref + keepalive
|
return_value_policy::reference_internal // ref + keepalive
|
||||||
);
|
);
|
||||||
|
|
||||||
|
cl.def("__contains__",
|
||||||
|
[](Map &m, const KeyType &k) -> bool {
|
||||||
|
auto it = m.find(k);
|
||||||
|
if (it == m.end())
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// Assignment provided only if the type is copyable
|
// Assignment provided only if the type is copyable
|
||||||
detail::map_assignment<Map, Class_>(cl);
|
detail::map_assignment<Map, Class_>(cl);
|
||||||
|
|
||||||
|
@ -56,7 +56,9 @@ def test_map(doc):
|
|||||||
"""std::map <-> dict"""
|
"""std::map <-> dict"""
|
||||||
d = m.cast_map()
|
d = m.cast_map()
|
||||||
assert d == {"key": "value"}
|
assert d == {"key": "value"}
|
||||||
|
assert "key" in d
|
||||||
d["key2"] = "value2"
|
d["key2"] = "value2"
|
||||||
|
assert "key2" in d
|
||||||
assert m.load_map(d)
|
assert m.load_map(d)
|
||||||
|
|
||||||
assert doc(m.cast_map) == "cast_map() -> Dict[str, str]"
|
assert doc(m.cast_map) == "cast_map() -> Dict[str, str]"
|
||||||
|
Loading…
Reference in New Issue
Block a user