-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,7 +263,7 @@ 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; | ||
|
@@ -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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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();