mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 21:25:13 +00:00
Re-add (but deprecated) bool operator for attr/items
PR #425 removed the bool operator from attribute accessors. This is likely in use by existing code as it was the only way before #425 added the `hasattr` function to check for the existence of an attribute, via: if (obj.attr("foo")) { ... } This commit adds it back in for attr and item accessors, but with a deprecation warning to use `hasattr(obj, ...)` or `obj.contains(...)` instead.
This commit is contained in:
parent
103d78d368
commit
7b8e3f9ec8
@ -206,6 +206,18 @@ public:
|
||||
void operator=(handle value) && { Policy::set(obj, key, value); }
|
||||
void operator=(handle value) & { get_cache() = object(value, true); }
|
||||
|
||||
template <typename T = Policy>
|
||||
PYBIND11_DEPRECATED("Use of obj.attr(...) as bool is deprecated in favor of pybind11::hasattr(obj, ...)")
|
||||
operator enable_if_t<std::is_same<T, accessor_policies::str_attr>::value ||
|
||||
std::is_same<T, accessor_policies::obj_attr>::value, bool>() const {
|
||||
return hasattr(obj, key);
|
||||
}
|
||||
template <typename T = Policy>
|
||||
PYBIND11_DEPRECATED("Use of obj[key] as bool is deprecated in favor of obj.contains(key)")
|
||||
operator enable_if_t<std::is_same<T, accessor_policies::generic_item>::value, bool>() const {
|
||||
return obj.contains(key);
|
||||
}
|
||||
|
||||
operator object() const { return get_cache(); }
|
||||
PyObject *ptr() const { return get_cache().ptr(); }
|
||||
template <typename T> T cast() const { return get_cache().template cast<T>(); }
|
||||
|
Loading…
Reference in New Issue
Block a user