2023-11-14 21:01:42 +00:00
|
|
|
#include "pybind11/stl_bind.h"
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace test_cases_for_stubgen {
|
|
|
|
|
2023-11-14 22:08:59 +00:00
|
|
|
struct UserType {
|
|
|
|
bool operator<(const UserType &) const { return false; }
|
2023-11-14 21:01:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct minimal_caster {
|
2023-11-14 22:08:59 +00:00
|
|
|
static constexpr auto name = py::detail::const_name<UserType>();
|
2023-11-14 21:01:42 +00:00
|
|
|
|
|
|
|
static py::handle
|
2023-11-14 22:08:59 +00:00
|
|
|
cast(UserType const & /*src*/, py::return_value_policy /*policy*/, py::handle /*parent*/) {
|
2023-11-14 21:01:42 +00:00
|
|
|
return py::none().release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximizing simplicity. This will go terribly wrong for other arg types.
|
|
|
|
template <typename>
|
2023-11-14 22:08:59 +00:00
|
|
|
using cast_op_type = const UserType &;
|
2023-11-14 21:01:42 +00:00
|
|
|
|
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2023-11-14 22:08:59 +00:00
|
|
|
operator UserType const &() {
|
|
|
|
static UserType obj;
|
2023-11-14 21:01:42 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load(py::handle /*src*/, bool /*convert*/) { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace test_cases_for_stubgen
|
|
|
|
|
|
|
|
namespace pybind11 {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <>
|
2023-11-14 22:08:59 +00:00
|
|
|
struct type_caster<test_cases_for_stubgen::UserType> : test_cases_for_stubgen::minimal_caster {};
|
2023-11-14 21:01:42 +00:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace pybind11
|
|
|
|
|
|
|
|
TEST_SUBMODULE(cases_for_stubgen, m) {
|
2023-11-14 22:08:59 +00:00
|
|
|
using UserType = test_cases_for_stubgen::UserType;
|
2023-11-14 21:01:42 +00:00
|
|
|
|
2023-11-14 22:08:59 +00:00
|
|
|
m.def("pass_user_type", [](const UserType &) {});
|
|
|
|
m.def("return_user_type", []() { return UserType(); });
|
2023-11-14 21:01:42 +00:00
|
|
|
|
2023-11-14 22:08:59 +00:00
|
|
|
py::bind_map<std::map<int, UserType>>(m, "MapIntUserType");
|
|
|
|
py::bind_map<std::map<UserType, int>>(m, "MapUserTypeInt");
|
2023-11-14 21:01:42 +00:00
|
|
|
|
|
|
|
#define MAP_TYPE(MapTypePythonName, ...) \
|
|
|
|
py::class_<__VA_ARGS__>(m, MapTypePythonName) \
|
|
|
|
.def( \
|
|
|
|
"keys", \
|
|
|
|
[](const __VA_ARGS__ &v) { return py::make_key_iterator(v); }, \
|
|
|
|
py::keep_alive<0, 1>()) \
|
|
|
|
.def( \
|
|
|
|
"values", \
|
|
|
|
[](const __VA_ARGS__ &v) { return py::make_value_iterator(v); }, \
|
|
|
|
py::keep_alive<0, 1>()) \
|
|
|
|
.def( \
|
|
|
|
"__iter__", \
|
|
|
|
[](const __VA_ARGS__ &v) { return py::make_iterator(v.begin(), v.end()); }, \
|
|
|
|
py::keep_alive<0, 1>())
|
|
|
|
|
2023-11-14 22:08:59 +00:00
|
|
|
MAP_TYPE("MapFloatUserType", std::map<float, UserType>);
|
|
|
|
MAP_TYPE("MapUserTypeFloat", std::map<UserType, float>);
|
2023-11-14 21:01:42 +00:00
|
|
|
|
|
|
|
#undef MAP_TYPE
|
|
|
|
}
|