Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,37 @@ class type_caster_enum_type {
if (!underlying_caster.load(src.attr("value"), convert)) {
pybind11_fail("native_enum internal consistency failure.");
}
value = static_cast<EnumType>(static_cast<Underlying>(underlying_caster));
native_value = static_cast<EnumType>(static_cast<Underlying>(underlying_caster));
native_loaded = true;
return true;
}
if (!pybind11_enum_) {
pybind11_enum_.reset(new type_caster_base<EnumType>());

type_caster_base<EnumType> legacy_caster;
if (legacy_caster.load(src, convert)) {
legacy_ptr = static_cast<EnumType *>(legacy_caster);
return true;
}
return pybind11_enum_->load(src, convert);
return false;
}

template <typename T>
using cast_op_type = detail::cast_op_type<T>;

// NOLINTNEXTLINE(google-explicit-constructor)
operator EnumType *() {
if (!pybind11_enum_) {
return &value;
}
return pybind11_enum_->operator EnumType *();
}
operator EnumType *() { return native_loaded ? &native_value : legacy_ptr; }

// NOLINTNEXTLINE(google-explicit-constructor)
operator EnumType &() {
if (!pybind11_enum_) {
return value;
if (!native_loaded && !legacy_ptr) {
throw reference_cast_error();
}
return pybind11_enum_->operator EnumType &();
return native_loaded ? native_value : *legacy_ptr;
}

private:
std::unique_ptr<type_caster_base<EnumType>> pybind11_enum_;
EnumType value;
EnumType native_value; // if loading a py::native_enum
bool native_loaded = false;
EnumType *legacy_ptr = nullptr; // if loading a py::enum_
};

template <typename EnumType, typename SFINAE = void>
Expand Down
Loading