Skip to content

Commit

Permalink
Merge pull request #1226 from radistmorse/master
Browse files Browse the repository at this point in the history
C++20 compatibility
  • Loading branch information
kiplingw authored Dec 17, 2024
2 parents a474d71 + a96d40e commit 9f4a8b3
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 39 deletions.
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([=](PST_SSIZE_T) {}, PrintException());
.then([](PST_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 @@ -750,7 +750,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 PISTACHE_USE_STD_CHRONO
#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 @@ -302,7 +302,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 @@ -322,13 +322,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',
'em_socket_t.h',
'emosandlibevdefs.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 @@ -69,7 +69,7 @@ namespace Pistache::Tcp
//
// Note: fd could be PS_FD_EMPTY
return Async::Promise<PST_SSIZE_T>(
[=](Async::Deferred<PST_SSIZE_T> deferred) mutable {
[&, this](Async::Deferred<PST_SSIZE_T> deferred) mutable {
BufferHolder holder { buffer };
WriteEntry write(std::move(deferred), std::move(holder),
fd, flags
Expand All @@ -84,7 +84,7 @@ namespace Pistache::Tcp

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

loadRequest_ = std::move(deferred);
Expand Down
50 changes: 34 additions & 16 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,44 @@ 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')
]
public_deps = []

#Check if compiler supports C++20 date

cxx_chrono_date_check_code = '''
#include <chrono>
#include <format>
#if defined(__cpp_lib_format) && (__cpp_lib_format >= 201907L) && \
defined(__cpp_lib_chrono) && (__cpp_lib_chrono >= 201907L)
// OK
#else
#error "no"
#endif
'''
has_working_cxx_chrono_date = compiler.compiles(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
else
add_project_arguments('-DPISTACHE_USE_STD_CHRONO', language: 'cpp')
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 @@ -356,7 +356,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 @@ -793,7 +793,7 @@ namespace Pistache::Http::Experimental
// lengths in Pistache (e.g. in struct ifaddr), hence why we
// cast here
.then(
[=]() {
[sfd, this]() {
socklen_t len = sizeof(saddr);
PST_SOCK_GETSOCKNAME(sfd, reinterpret_cast<struct sockaddr*>(&saddr), &len);
connectionState_.store(Connected);
Expand Down Expand Up @@ -998,7 +998,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 @@ -1011,7 +1011,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 @@ -1099,11 +1099,11 @@ namespace Pistache::Http
return transport_->asyncWrite(fd, buffer)
.then<std::function<Async::Promise<PST_SSIZE_T>(PST_SSIZE_T)>,
std::function<void(std::exception_ptr&)>>(
[=](PST_SSIZE_T data) {
[](PST_SSIZE_T data) {
return Async::Promise<PST_SSIZE_T>::resolved(data);
},

[=](std::exception_ptr& eptr) {
[](std::exception_ptr& eptr) {
return Async::Promise<PST_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 @@ -711,7 +711,7 @@ namespace Pistache::Aio
{
PS_TIMEDBG_START;

thread = std::thread([=]() {
thread = std::thread([this]() {
PS_TIMEDBG_START;

if (!threadsName_.empty())
Expand Down
2 changes: 1 addition & 1 deletion src/server/endpoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,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([=](PST_SSIZE_T) { removePeer(peer); }, [=](std::exception_ptr) { removePeer(peer); });
response.send(Http::Code::Request_Timeout).then([peer, this](PST_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 @@ -597,7 +597,7 @@ namespace Pistache::Tcp
shutdownFd.bind(poller);
PS_LOG_DEBUG("shutdownFd.bind done");

acceptThread = std::thread([=]() {
acceptThread = std::thread([this]() {
PS_TIMEDBG_START;
this->run();
});
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.4.24.20241213
0.4.25.20241217

0 comments on commit 9f4a8b3

Please sign in to comment.