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

C++20 compatibility #1226

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

radistmorse
Copy link
Contributor

While the project is aiming at C++17, it should be possible to use it with newer versions of the standard. There are two problems preventing it.

First is the hinnant-date library. It was included in the C++20 standard, but with quirks, so using it as-is is problematic. On some compilers it just fails. This PR introduces the wrapper, which replaces the lib with the standard found in std::chrono if the language version is high enough.

Second is the implicit lambda capture of this. It is deprecated since C++20, you are supposed to use [=, this] instead. The problem is, this syntax is only introduced in C++20, and it doesn't work in the previous versions. The additional problem is that in pistache the [=] capture is used everywhere, even when it doesn't make any sense. For example in the Transport::asyncWrite function we capture buffer by value (copy the content once), and then pass it to the BufferHolder constructor which copies it again. I tried to understand the logic behind BufferHolder but I couldn't. Why does BufferHolder::detach copies the content of the buffer before passing it to the constructor that copies it again? And same with the Connection, why do you use refs in a lambda that captures everything by-value? Argh! My head hurts! But anyway, this is outside the scope of this PR. I replaced the implicit captures with explicit ones in places where it caused problems and tried to keep everything exactly as-is. Fixing the memory management should be a task for somebody more qualified than me.

It was standardized in C++20 and thus not needed.
Also it causes compilation issues with newer stdlib.
This behaviour is deprecated since C++20
Capture by-reference is probably what was intended
from the start, seeing how the buffer is passed.
Copy link

codecov bot commented Aug 6, 2024

Codecov Report

Attention: Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.

Project coverage is 76.49%. Comparing base (3e580e4) to head (364c722).
Report is 86 commits behind head on master.

Files Patch % Lines
include/pistache/transport.h 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1226      +/-   ##
==========================================
- Coverage   78.49%   76.49%   -2.00%     
==========================================
  Files          52       58       +6     
  Lines        6282     9668    +3386     
==========================================
+ Hits         4931     7396    +2465     
- Misses       1351     2272     +921     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

#include <chrono>
int main() {
std::stringstream ss;
ss << std::format("%FT%T%z", std::chrono::system_clock::now());
Copy link
Member

Choose a reason for hiding this comment

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

I would avoid using std::format, since it isn't well supported everywhere and we're just checking for C++20 chrono anyway. Also, the code isn't ran, so you can simply call the functions you want to check for, without actually writing something that "makes sense".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we want to replace date::to_stream from hinnant-date we kinda have no other option. This is the way to turn dates into strings in C++20, no way around it. The fmt lib is also available as a sort of preview of C++20 std::format like hinnant-date is a preview of the date changes. So a fallback could be introduced, but it would be a rare cornercase for something that is already a rare cornercase (building pistache for C++20).

@Tachi107
Copy link
Member

Tachi107 commented Aug 6, 2024 via email

@@ -186,7 +186,7 @@ namespace Pistache::Http::Experimental
void registerPoller(Polling::Epoll& poller) override;
void unregisterPoller(Polling::Epoll& poller) override;

Async::Promise<void> asyncConnect(std::shared_ptr<Connection> connection,
Async::Promise<void> asyncConnect(const std::shared_ptr<Connection>& connection,
Copy link
Member

Choose a reason for hiding this comment

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

Is this related to the modified lambda captures? If not, please either split this in a separate commit explaining why this is needed or drop this change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, no, it's just an error. When I zoned out for a little and just started pressing "yes" to the CLion suggestions to fix things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants