-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Improve support for std::optional-like classes #850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
#include <iostream> | ||
#include <list> | ||
#include <valarray> | ||
#include <utility> | ||
|
||
#if defined(_MSC_VER) | ||
#pragma warning(push) | ||
|
@@ -228,6 +229,26 @@ template <typename Key, typename Value, typename Compare, typename Alloc> struct | |
template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>> | ||
: map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> { }; | ||
|
||
/// Helper class to reset an std::optional-like class in the best way. | ||
template<typename T> struct optional_reset { | ||
private: | ||
// Either or both of these overloads may exist (SFINAE decides), and if | ||
// both exist then the .reset version is preferred due to a better match | ||
// in the second argument. | ||
template<typename T2 = T> | ||
static decltype(std::declval<T2>().reset()) reset_impl(T &value, int) { | ||
return value.reset(); | ||
} | ||
|
||
template<typename T2 = T> | ||
static decltype(std::declval<T2>() = {}) reset_impl(T &value, long) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and here |
||
return value = {}; | ||
} | ||
|
||
public: | ||
static void reset(T &value) { reset_impl(value, 0); } | ||
}; | ||
|
||
// This type caster is intended to be used for std::optional and std::experimental::optional | ||
template<typename T> struct optional_caster { | ||
using value_conv = make_caster<typename T::value_type>; | ||
|
@@ -242,14 +263,14 @@ template<typename T> struct optional_caster { | |
if (!src) { | ||
return false; | ||
} else if (src.is_none()) { | ||
value = {}; // nullopt | ||
optional_reset<T>::reset(value); | ||
return true; | ||
} | ||
value_conv inner_caster; | ||
if (!inner_caster.load(src, convert)) | ||
return false; | ||
|
||
value.emplace(cast_op<typename T::value_type>(inner_caster)); | ||
value.emplace(std::move(cast_op<typename T::value_type>(inner_caster))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can someone sanity-check me that this is safe? I don't have an understanding of how all the casting magic works yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not safe: type casters can return a lvalue reference to a variable they don't own. We might, however, be able to coax type casters cast ops in rvalue context into giving up an rvalue reference. |
||
return true; | ||
} | ||
|
||
|
@@ -265,11 +286,20 @@ template<> struct type_caster<std::nullopt_t> | |
#endif | ||
|
||
#if PYBIND11_HAS_EXP_OPTIONAL | ||
// std::experimental::optional doesn't have a reset method, and the value = {} | ||
// strategy can fail if the underlying type isn't move-assignable. | ||
template<typename T> struct optional_reset<std::experimental::optional<T>> { | ||
static void reset(std::experimental::optional<T> &value) { | ||
value = std::experimental::nullopt; | ||
} | ||
}; | ||
|
||
template<typename T> struct type_caster<std::experimental::optional<T>> | ||
: public optional_caster<std::experimental::optional<T>> {}; | ||
|
||
template<> struct type_caster<std::experimental::nullopt_t> | ||
: public void_caster<std::experimental::nullopt_t> {}; | ||
|
||
#endif | ||
|
||
/// Visit a variant and cast any found type to Python | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static void_t<decltype(std::declval<T2>().reset())> reset_impl...
would be better here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And
return value.reset();
->value.reset();