Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
30c03523
Commit
30c03523
authored
Jun 10, 2019
by
Blake Thompson
Committed by
Wenzel Jakob
Jun 10, 2019
Browse files
Added __contains__ to stl bindings for maps (#1767)
* Added __contains__ to stl bindings for maps
parent
c2514340
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
0 deletions
+11
-0
include/pybind11/stl_bind.h
include/pybind11/stl_bind.h
+9
-0
tests/test_stl.py
tests/test_stl.py
+2
-0
No files found.
include/pybind11/stl_bind.h
View file @
30c03523
...
@@ -579,6 +579,15 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
...
@@ -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
);
...
...
tests/test_stl.py
View file @
30c03523
...
@@ -56,7 +56,9 @@ def test_map(doc):
...
@@ -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]"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment