Skip to content
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

[FEATURE] Add seqan3::copyable_wrapper #2963

Merged
merged 3 commits into from
May 17, 2022
Merged

Conversation

eseiler
Copy link
Member

@eseiler eseiler commented Apr 12, 2022

This replaces seqan3::semiregular_box (implemented via range-v3) with seqan3::copyable_wrapper (independent of range-v3).

In early C++20 days, views were required to be default_initialisable (i.e., view{}).
For views that store a lambda, like filter or transform, this meant that the lambda would need to be default_constructible. To do this, semiregular_box provided a wrapper that made any type semiregular (copyable + default_constructible).

After some amendments to C++20, views are conditionally default_constructible (it is not a requirement anymore). Hence, we only need to make a lambda copyable -> copyable_wrapper.
However, this change only applies starting with GCC12, for earlier versions, we still need semiregular (SEQAN3_WORKAROUND_DEFAULT_CONSTRUCTIBLE_VIEW takes care of this).

There are two specialisations:

Note:

  • SEQAN3_WORKAROUND_DEFAULT_CONSTRUCTIBLE_VIEW enables another check for 2. We can only use 2 with gcc<12 iff the underlying type is default_constructible. Otherwise, we need to fall back to 1 - std::optional offers default_constructible.

References:

@vercel
Copy link

vercel bot commented Apr 12, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/seqan/seqan3/47y3BFmLSXaH92nNpcnVYwxE7eon
✅ Preview: https://seqan3-git-fork-eseiler-misc-range-seqan.vercel.app

@codecov
Copy link

codecov bot commented Apr 14, 2022

Codecov Report

Merging #2963 (60789bd) into master (f10e557) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2963   +/-   ##
=======================================
  Coverage   98.19%   98.19%           
=======================================
  Files         267      268    +1     
  Lines       11521    11525    +4     
=======================================
+ Hits        11313    11317    +4     
  Misses        208      208           
Impacted Files Coverage Δ
...alignment/configuration/align_config_on_result.hpp 100.00% <100.00%> (ø)
include/seqan3/core/detail/copyable_wrapper.hpp 100.00% <100.00%> (ø)
include/seqan3/io/views/detail/take_until_view.hpp 96.42% <100.00%> (-0.13%) ⬇️
include/seqan3/search/configuration/on_result.hpp 100.00% <100.00%> (ø)
...qan3/alignment/pairwise/alignment_configurator.hpp 97.91% <0.00%> (-0.05%) ⬇️
...nclude/seqan3/core/configuration/configuration.hpp 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f10e557...60789bd. Read the comment docs.

@eseiler eseiler marked this pull request as ready for review April 22, 2022 11:24
@vercel
Copy link

vercel bot commented Apr 22, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
seqan3 ✅ Ready (Inspect) Visit Preview May 17, 2022 at 11:12AM (UTC)

@eseiler eseiler requested review from a team and SGSSGene and removed request for a team April 25, 2022 11:45
@SGSSGene SGSSGene requested review from a team and smehringer and removed request for a team May 2, 2022 08:42
Copy link
Member

@smehringer smehringer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only questions. I don't know if I fully understood every change you made.

/*!\brief Helper concept for seqan3::copyable_wrapper.
* \ingroup core
* \see https://en.cppreference.com/w/cpp/ranges/copyable_wrapper
* \noapi{Exposition only.}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't this be in seqan3::detail?

Copy link
Member Author

@eseiler eseiler May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not because semiregular_box wasn't...but because it's core, we can just move it to detail, right?

Exposition only. only refers to the standard, where it is considered as such. Won't matter if we move it to detail

Shall I move it into detail?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that in my former re-review.

IMHO, I would avoid "exposition only" mentions since we already have the detail namespace and the \noapi tag for stuff that we don't give guarantees for. Because this doesn't seem to be something we will expose to the user at any given point, I see no reason to NOT make it detail?

Comment on lines 86 to 88
* This is a SeqAn-specific extension that allows easy invocation of the wrapped object.
* `t` needs to be callable with the passed arguments.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give a short example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seqan3::copyable_wrapper wrapper{[] (int const x) { return x + 1; }};
auto wrapper_2 = wrapper; // A lambda function wouldn't be copyable.
wrapper(2); // Returns 3
wrapper_2(2); // Returns 3

It's a functionality that semiregular_box had, but is not part of the Standard.
And we use it quite heavily, the alternative would be wrapper->operator()(2).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This is a SeqAn-specific extension that allows easy invocation of the wrapped object.
* `t` needs to be callable with the passed arguments.
*
* This is a SeqAn-specific extension that allows easy invocation of the wrapped object.
* `t` needs to be callable with the passed arguments.
* e.g.:
* ```
* seqan3::copyable_wrapper wrapper{[] (int const x) { return x + 1; }};
* auto wrapper_2 = wrapper; // A lambda function wouldn't be copyable.
* wrapper(2); // Returns 3
* wrapper_2(2); // Returns 3
* ```
*
since this is deep detail I'd be fine with no snippet but hard coded code

* \see https://en.cppreference.com/w/cpp/ranges/copyable_wrapper
* \details
*
* If `t` is `std::copyable`, the STL allows to only store `t`, i.e. no `std::optional` is needed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* If `t` is `std::copyable`, the STL allows to only store `t`, i.e. no `std::optional` is needed.
* If `t` is `std::copyable`, the STL allows to store `t` directly, i.e. no `std::optional` is needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will apply when applying other changes.

Copy link
Member

@smehringer smehringer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two doc issues but than it LGTM

Comment on lines 86 to 88
* This is a SeqAn-specific extension that allows easy invocation of the wrapped object.
* `t` needs to be callable with the passed arguments.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This is a SeqAn-specific extension that allows easy invocation of the wrapped object.
* `t` needs to be callable with the passed arguments.
*
* This is a SeqAn-specific extension that allows easy invocation of the wrapped object.
* `t` needs to be callable with the passed arguments.
* e.g.:
* ```
* seqan3::copyable_wrapper wrapper{[] (int const x) { return x + 1; }};
* auto wrapper_2 = wrapper; // A lambda function wouldn't be copyable.
* wrapper(2); // Returns 3
* wrapper_2(2); // Returns 3
* ```
*
since this is deep detail I'd be fine with no snippet but hard coded code

/*!\brief Helper concept for seqan3::copyable_wrapper.
* \ingroup core
* \see https://en.cppreference.com/w/cpp/ranges/copyable_wrapper
* \noapi{Exposition only.}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that in my former re-review.

IMHO, I would avoid "exposition only" mentions since we already have the detail namespace and the \noapi tag for stuff that we don't give guarantees for. Because this doesn't seem to be something we will expose to the user at any given point, I see no reason to NOT make it detail?

@eseiler eseiler force-pushed the misc/range branch 2 times, most recently from f09c6b7 to 6ccd4cf Compare May 17, 2022 10:45
@eseiler eseiler requested a review from smehringer May 17, 2022 10:46
@eseiler
Copy link
Member Author

eseiler commented May 17, 2022

@smehringer I did add a snippet. As it turns out, it's really, really hard to deliberately make a lambda non-copyable in cpp20. So, the example shows how to use operator(), but the lambda itself would already be copyable by default.
Non-copyable lambdas happen in the wild (e.g., alignment-module) though, just seems way more involved and hard to show in a small snippet.

@eseiler eseiler merged commit 369ae97 into seqan:master May 17, 2022
@eseiler eseiler deleted the misc/range branch May 17, 2022 11:41
@eseiler eseiler restored the misc/range branch May 17, 2022 13:05
@eseiler eseiler deleted the misc/range branch May 17, 2022 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants