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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class MyHandler : public Http::Handler
{
response
.send(Http::Code::Request_Timeout, "Timeout")
.then([=](ssize_t) {}, PrintException());
.then([](ssize_t) {}, PrintException());
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/pistache/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ namespace Pistache::Async
void finishResolve(P& promise)
{
auto chainer = makeChainer(promise);
promise.then(std::move(chainer), [=](std::exception_ptr exc) {
promise.then(std::move(chainer), [this](std::exception_ptr exc) {
auto core = this->chain_;
core->exc = std::move(exc);
core->state = State::Rejected;
Expand Down
30 changes: 30 additions & 0 deletions include/pistache/date_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2024 George Sedov
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#if __cplusplus >= 202002L
#include <chrono>
#include <format>
namespace date
{
using namespace std::chrono;

/**
* special case for the to_stream function, which was not introduced
* in the standard, as the functionality was already a part of std::format
*/
template <class CharT, class Traits, typename... Args>
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const std::format_string<Args...>& fmt, Args&&... args)
{
os << std::format(fmt, std::forward<Args>(args)...);
return os;
}
}
#else
#include <date/date.h>
#endif
6 changes: 3 additions & 3 deletions include/pistache/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ namespace Pistache
template <typename Duration>
void arm(Duration duration)
{
Async::Promise<uint64_t> p([=](Async::Deferred<uint64_t> deferred) {
Async::Promise<uint64_t> p([duration, this](Async::Deferred<uint64_t> deferred) {
#ifdef _USE_LIBEVENT
std::shared_ptr<EventMethEpollEquiv>
event_meth_epoll_equiv(
Expand All @@ -318,13 +318,13 @@ namespace Pistache
});

p.then(
[=](uint64_t numWakeup) {
[this](uint64_t numWakeup) {
this->armed = false;
this->onTimeout(numWakeup);
CLOSE_FD(timerFd);
timerFd = PS_FD_EMPTY;
},
[=](std::exception_ptr exc) { std::rethrow_exception(exc); });
[](std::exception_ptr exc) { std::rethrow_exception(exc); });

armed = true;
}
Expand Down
1 change: 1 addition & 0 deletions include/pistache/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ install_headers(
'common.h',
'config.h',
'cookie.h',
'date_wrapper.h',
'description.h',
'endpoint.h',
'eventmeth.h',
Expand Down
4 changes: 2 additions & 2 deletions include/pistache/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Pistache::Tcp
// Always enqueue reponses for sending. Giving preference to consumer
// context means chunked responses could be sent out of order.
return Async::Promise<ssize_t>(
[=](Async::Deferred<ssize_t> deferred) mutable {
[&, this](Async::Deferred<ssize_t> deferred) mutable {
BufferHolder holder { buffer };
WriteEntry write(std::move(deferred), std::move(holder),
fd, flags
Expand All @@ -77,7 +77,7 @@ namespace Pistache::Tcp

Async::Promise<rusage> load()
{
return Async::Promise<rusage>([=](Async::Deferred<rusage> deferred) {
return Async::Promise<rusage>([this](Async::Deferred<rusage> deferred) {
PS_TIMEDBG_START_CURLY;

loadRequest_ = std::move(deferred);
Expand Down
49 changes: 33 additions & 16 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,42 @@ if get_option('b_coverage')
add_project_arguments(compiler.get_supported_arguments(['-fstack-protector-all', '--param=ssp-buffer-size=4']), language: 'cpp')
endif

# howardhinnant/date has several names - look for them, from the most
# to the least explicit name.
# In Meson 0.60.0, this can be replaced with a simpler:
#
# dependency('howardhinnant-date', 'hinnant-date', 'date')
#
date_dep = dependency('howardhinnant-date', required: false)
if not date_dep.found()
date_dep = dependency('hinnant-date', required: false)
endif
if not date_dep.found()
date_dep = dependency('date', fallback: ['hinnant-date', 'date_dep'])
endif

deps_libpistache = [
dependency('threads'),
date_dep
dependency('threads')
]

#Check if compiler supports C++20 date

cxx_chrono_date_check_code = '''
#include <sstream>
#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).

std::chrono::system_clock::time_point tp;
ss >> std::chrono::parse("%FT%T%z", tp);
return 0;
}
'''
has_working_cxx_chrono_date = compiler.links(cxx_chrono_date_check_code, name: 'C++20 std::chrono')

if (not has_working_cxx_chrono_date)
# howardhinnant/date has several names - look for them, from the most
# to the least explicit name.
# In Meson 0.60.0, this can be replaced with a simpler:
#
# dependency('howardhinnant-date', 'hinnant-date', 'date')
#
date_dep = dependency('howardhinnant-date', required: false)
if not date_dep.found()
date_dep = dependency('hinnant-date', required: false)
endif
if not date_dep.found()
date_dep = dependency('date', fallback: ['hinnant-date', 'date_dep'])
endif
deps_libpistache += date_dep
endif

if get_option('PISTACHE_USE_RAPIDJSON')
rapidjson_dep = dependency('RapidJSON', fallback: ['rapidjson', 'rapidjson_dep'])
deps_libpistache += rapidjson_dep
Expand Down
8 changes: 4 additions & 4 deletions src/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ namespace Pistache::Http::Experimental
PS_TIMEDBG_START_THIS;

return Async::Promise<void>(
[=](Async::Resolver& resolve, Async::Rejection& reject) {
[connection, address, addr_len, this](Async::Resolver& resolve, Async::Rejection& reject) {
PS_TIMEDBG_START;

ConnectionEntry entry(std::move(resolve), std::move(reject), connection,
Expand Down Expand Up @@ -728,7 +728,7 @@ namespace Pistache::Http::Experimental
transport_
->asyncConnect(shared_from_this(), addr->ai_addr, addr->ai_addrlen)
.then(
[=]() {
[sfd, this]() {
socklen_t len = sizeof(saddr);
getsockname(sfd, reinterpret_cast<struct sockaddr*>(&saddr), &len);
connectionState_.store(Connected);
Expand Down Expand Up @@ -931,7 +931,7 @@ namespace Pistache::Http::Experimental
PS_TIMEDBG_START_THIS;

return Async::Promise<Response>(
[=](Async::Resolver& resolve, Async::Rejection& reject) {
[&, this](Async::Resolver& resolve, Async::Rejection& reject) {
PS_TIMEDBG_START;
performImpl(request, std::move(resolve), std::move(reject),
std::move(onDone));
Expand All @@ -944,7 +944,7 @@ namespace Pistache::Http::Experimental
PS_TIMEDBG_START_THIS;

return Async::Promise<Response>(
[=](Async::Resolver& resolve, Async::Rejection& reject) {
[&, this](Async::Resolver& resolve, Async::Rejection& reject) {
PS_TIMEDBG_START;

requestsQueue.push(RequestData(std::move(resolve), std::move(reject),
Expand Down
4 changes: 2 additions & 2 deletions src/common/description.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ namespace Pistache::Rest
void Swagger::install(Rest::Router& router)
{

Route::Handler uiHandler = [=](const Rest::Request& req,
Route::Handler uiHandler = [this](const Rest::Request& req,
Http::ResponseWriter response) {
const auto& res = req.resource();

Expand Down Expand Up @@ -529,7 +529,7 @@ namespace Pistache::Rest
return Route::Result::Failure;
};

router.addCustomHandler(uiHandler);
router.addCustomHandler(std::move(uiHandler));
}

} // namespace Pistache::Rest
4 changes: 2 additions & 2 deletions src/common/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1055,11 +1055,11 @@ namespace Pistache::Http
return transport_->asyncWrite(fd, buffer)
.then<std::function<Async::Promise<ssize_t>(ssize_t)>,
std::function<void(std::exception_ptr&)>>(
[=](ssize_t data) {
[](ssize_t data) {
return Async::Promise<ssize_t>::resolved(data);
},

[=](std::exception_ptr& eptr) {
[](std::exception_ptr& eptr) {
return Async::Promise<ssize_t>::rejected(eptr);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/http_defs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
#include <date/date.h>
#include <pistache/date_wrapper.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/common/reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ namespace Pistache::Aio

void run()
{
thread = std::thread([=]() {
thread = std::thread([this]() {
if (!threadsName_.empty())
{
pthread_setname_np(
Expand Down
2 changes: 1 addition & 1 deletion src/server/endpoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ namespace Pistache::Http
else
{
ResponseWriter response(Http::Version::Http11, this, static_cast<Http::Handler*>(handler_.get()), peer);
response.send(Http::Code::Request_Timeout).then([=](ssize_t) { removePeer(peer); }, [=](std::exception_ptr) { removePeer(peer); });
response.send(Http::Code::Request_Timeout).then([peer, this](ssize_t) { removePeer(peer); }, [peer, this](std::exception_ptr) { removePeer(peer); });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ namespace Pistache::Tcp
void Listener::runThreaded()
{
shutdownFd.bind(poller);
acceptThread = std::thread([=]() { this->run(); });
acceptThread = std::thread([this]() { this->run(); });
}

void Listener::shutdown()
Expand Down
2 changes: 1 addition & 1 deletion tests/cookie_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <gtest/gtest.h>

#include <date/date.h>
#include <pistache/date_wrapper.h>
#include <pistache/cookie.h>

using namespace Pistache;
Expand Down
2 changes: 1 addition & 1 deletion tests/cookie_test_2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <gtest/gtest.h>

#include <date/date.h>
#include <pistache/date_wrapper.h>
#include <pistache/cookie.h>

using namespace Pistache;
Expand Down
2 changes: 1 addition & 1 deletion tests/headers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <date/date.h>
#include <pistache/date_wrapper.h>
#include <pistache/http.h>

#include <gmock/gmock-matchers.h>
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.1.20240626
0.3.1.20240806
Loading