New PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT option (current on by default).

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-06-22 09:27:30 -07:00
parent 21fc6b31c7
commit d2bafaeba7
2 changed files with 28 additions and 3 deletions

View File

@ -112,6 +112,28 @@ struct tu_local_no_data_always_false {
} // namespace
# ifndef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT
# define PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT
# endif
template <typename TypeCasterType, typename SFINAE = void>
struct get_type_caster_source_file_line {
# ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT
static_assert(TypeCasterType::source_file_line,
"PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE is MISSING: Please add that macro to the "
"TypeCasterType, or undefine PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT");
# else
static constexpr auto source_file_line = tu_local_const_name("UNAVAILABLE");
# endif
};
template <typename TypeCasterType>
struct get_type_caster_source_file_line<
TypeCasterType,
enable_if_t<std::is_class<decltype(TypeCasterType::source_file_line)>::value>> {
static constexpr auto source_file_line = TypeCasterType::source_file_line;
};
template <typename IntrinsicType, typename TypeCasterType>
struct type_caster_odr_guard : TypeCasterType {
static tu_local_no_data_always_false translation_unit_local;
@ -121,9 +143,10 @@ template <typename IntrinsicType, typename TypeCasterType>
tu_local_no_data_always_false
type_caster_odr_guard<IntrinsicType, TypeCasterType>::translation_unit_local
= []() {
type_caster_odr_guard_impl(typeid(IntrinsicType),
TypeCasterType::source_file_line.text,
PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_THROW_DISABLED);
type_caster_odr_guard_impl(
typeid(IntrinsicType),
get_type_caster_source_file_line<TypeCasterType>::source_file_line.text,
PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_THROW_DISABLED);
return tu_local_no_data_always_false();
}();

View File

@ -10,7 +10,9 @@ struct type_mrc {
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
#endif
static py::handle
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {