Skip to content

Commit 571bff8

Browse files
committed
Use function-local classes for KeysView etc
This limits the scope of the classes.
1 parent a32db87 commit 571bff8

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

include/pybind11/stl_bind.h

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -595,34 +595,16 @@ template <typename Map, typename Class_> auto map_if_insertion_operator(Class_ &
595595
);
596596
}
597597

598-
template<typename Map>
599-
struct keys_view
600-
{
601-
Map &map;
602-
};
603-
604-
template<typename Map>
605-
struct values_view
606-
{
607-
Map &map;
608-
};
609-
610-
template<typename Map>
611-
struct items_view
612-
{
613-
Map &map;
614-
};
615-
616598
PYBIND11_NAMESPACE_END(detail)
617599

618600
template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args>
619601
class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&... args) {
620602
using KeyType = typename Map::key_type;
621603
using MappedType = typename Map::mapped_type;
622-
using KeysView = detail::keys_view<Map>;
623-
using ValuesView = detail::values_view<Map>;
624-
using ItemsView = detail::items_view<Map>;
625604
using Class_ = class_<Map, holder_type>;
605+
struct keys_view { Map &map; };
606+
struct values_view { Map &map; };
607+
struct items_view { Map &map; };
626608

627609
// If either type is a non-module-local bound type then make the map binding non-local as well;
628610
// otherwise (e.g. both types are either module-local or converting) the map will be
@@ -635,11 +617,11 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
635617
}
636618

637619
Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
638-
py::class_<KeysView> keys_view(
620+
py::class_<keys_view> KeysView(
639621
scope, ("KeysView[" + name + "]").c_str(), pybind11::module_local(local));
640-
py::class_<ValuesView> values_view(
622+
py::class_<values_view> ValuesView(
641623
scope, ("ValuesView[" + name + "]").c_str(), pybind11::module_local(local));
642-
py::class_<ItemsView> items_view(
624+
py::class_<items_view> ItemsView(
643625
scope, ("ItemsView[" + name + "]").c_str(), pybind11::module_local(local));
644626

645627
cl.def(init<>());
@@ -658,17 +640,17 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
658640
);
659641

660642
cl.def("keys",
661-
[](Map &m) { return KeysView{m}; },
643+
[](Map &m) { return keys_view{m}; },
662644
keep_alive<0, 1>() /* Essential: keep map alive while view exists */
663645
);
664646

665647
cl.def("values",
666-
[](Map &m) { return ValuesView{m}; },
648+
[](Map &m) { return values_view{m}; },
667649
keep_alive<0, 1>() /* Essential: keep map alive while view exists */
668650
);
669651

670652
cl.def("items",
671-
[](Map &m) { return ItemsView{m}; },
653+
[](Map &m) { return items_view{m}; },
672654
keep_alive<0, 1>() /* Essential: keep map alive while view exists */
673655
);
674656

@@ -705,33 +687,33 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
705687

706688
cl.def("__len__", &Map::size);
707689

708-
keys_view.def("__len__", [](KeysView &view) { return view.map.size(); });
709-
keys_view.def("__iter__",
710-
[](KeysView &view) {
690+
KeysView.def("__len__", [](keys_view &view) { return view.map.size(); });
691+
KeysView.def("__iter__",
692+
[](keys_view &view) {
711693
return make_key_iterator(view.map.begin(), view.map.end());
712694
},
713695
keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */
714696
);
715-
keys_view.def("__contains__",
716-
[](KeysView &view, const KeyType &k) -> bool {
697+
KeysView.def("__contains__",
698+
[](keys_view &view, const KeyType &k) -> bool {
717699
auto it = view.map.find(k);
718700
if (it == view.map.end())
719701
return false;
720702
return true;
721703
}
722704
);
723705

724-
values_view.def("__len__", [](ValuesView &view) { return view.map.size(); });
725-
values_view.def("__iter__",
726-
[](ValuesView &view) {
706+
ValuesView.def("__len__", [](values_view &view) { return view.map.size(); });
707+
ValuesView.def("__iter__",
708+
[](values_view &view) {
727709
return make_value_iterator(view.map.begin(), view.map.end());
728710
},
729711
keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */
730712
);
731713

732-
items_view.def("__len__", [](ItemsView &view) { return view.map.size(); });
733-
items_view.def("__iter__",
734-
[](ItemsView &view) {
714+
ItemsView.def("__len__", [](items_view &view) { return view.map.size(); });
715+
ItemsView.def("__iter__",
716+
[](items_view &view) {
735717
return make_iterator(view.map.begin(), view.map.end());
736718
},
737719
keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */

0 commit comments

Comments
 (0)