Skip to content

Commit 6676886

Browse files
Add C++ module support
1 parent 22a41e6 commit 6676886

11 files changed

Lines changed: 446 additions & 78 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ cpr_option(CPR_FORCE_DARWINSSL_BACKEND "Force to use the DarwinSSL backend. If C
6767
cpr_option(CPR_FORCE_MBEDTLS_BACKEND "Force to use the Mbed TLS backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
6868
cpr_option(CPR_ENABLE_LINTING "Set to ON to enable clang linting." OFF)
6969
cpr_option(CPR_ENABLE_CPPCHECK "Set to ON to enable Cppcheck static analysis. Requires CPR_BUILD_TESTS and CPR_BUILD_TESTS_SSL to be OFF to prevent checking google tests source code." OFF)
70+
cpr_option(CPR_BUILD_MODULES "Set to ON to build cpr as a C++ module." OFF)
7071
cpr_option(CPR_BUILD_TESTS "Set to ON to build cpr tests." OFF)
7172
cpr_option(CPR_BUILD_TESTS_SSL "Set to ON to build cpr ssl tests" ${CPR_BUILD_TESTS})
7273
cpr_option(CPR_BUILD_TESTS_PROXY "Set to ON to build proxy tests. They fail in case there is no valid proxy server available in proxy_tests.cpp" OFF)
@@ -322,6 +323,10 @@ else()
322323
set(CURL_LIB CURL::libcurl)
323324
endif()
324325

326+
if(CPR_BUILD_MODULES)
327+
add_subdirectory(modules)
328+
endif()
329+
325330
# GTest configuration
326331
if(CPR_BUILD_TESTS)
327332
if(CPR_USE_SYSTEM_GTEST)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ The only explicit requirements are:
221221

222222
* A `C++17` compatible compiler such as Clang or GCC. The minimum required version of GCC is unknown, so if anyone has trouble building this library with a specific version of GCC, do let us know.
223223
* In case you only have a `C++11` compatible compiler available, all versions below cpr 1.9.x are for you. The 1.10.0 release of cpr switches to `C++17` as a requirement.
224+
* If you would like to use cpr as a C++20 module, you must have CMake 3.28 enabled. Enable `CPR_BUILD_MODULES` to activate the feature.
224225
* If you would like to perform https requests `OpenSSL` and its development libraries are required.
225226
* If you do not use the built-in version of [curl](https://github.com/curl/curl) but instead use your systems version, make sure you use a version `>= 7.71.0`. Lower versions are not supported. This means you need Debian `>= 11` or Ubuntu `>= 22.04 LTS`.
226227
* [`The Meson Build System`](https://mesonbuild.com/) is required build PSL from source ([PSL support for curl](https://everything.curl.dev/build/deps.html#libpsl)). For more information take a look at the `CPR_CURL_USE_LIBPSL` and `CPR_USE_SYSTEM_LIB_PSL` CMake options.

include/cpr/accept_encoding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum class AcceptEncodingMethods {
1818
};
1919

2020
// NOLINTNEXTLINE(cert-err58-cpp)
21-
static const std::map<AcceptEncodingMethods, std::string> AcceptEncodingMethodsStringMap{{AcceptEncodingMethods::identity, "identity"}, {AcceptEncodingMethods::deflate, "deflate"}, {AcceptEncodingMethods::zlib, "zlib"}, {AcceptEncodingMethods::gzip, "gzip"}, {AcceptEncodingMethods::disabled, "disabled"}};
21+
inline const std::map<AcceptEncodingMethods, std::string> AcceptEncodingMethodsStringMap{{AcceptEncodingMethods::identity, "identity"}, {AcceptEncodingMethods::deflate, "deflate"}, {AcceptEncodingMethods::zlib, "zlib"}, {AcceptEncodingMethods::gzip, "gzip"}, {AcceptEncodingMethods::disabled, "disabled"}};
2222

2323
class AcceptEncoding {
2424
public:

include/cpr/cookies.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace cpr {
1212
* EXPIRES_STRING_SIZE is an explicitly static and const variable that could be only accessed within the same namespace and is immutable.
1313
* To be used for "std::array", the expression must have a constant value, so EXPIRES_STRING_SIZE must be a const value.
1414
**/
15-
static const std::size_t EXPIRES_STRING_SIZE = 100;
15+
inline const std::size_t EXPIRES_STRING_SIZE = 100;
1616

1717
class Cookie {
1818
public:

include/cpr/cpr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "cpr/connection_pool.h"
1111
#include "cpr/cookies.h"
1212
#include "cpr/cprtypes.h"
13+
#ifndef CPR_AS_MODULE
1314
#include "cpr/cprver.h"
15+
#endif
1416
#include "cpr/curl_container.h"
1517
#include "cpr/curlholder.h"
1618
#include "cpr/error.h"

include/cpr/status_codes.h

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,81 @@
11
#ifndef CPR_STATUS_CODES
22
#define CPR_STATUS_CODES
3-
namespace cpr {
4-
namespace status {
3+
namespace cpr::status {
54
// Information responses
6-
constexpr long HTTP_CONTINUE = 100;
7-
constexpr long HTTP_SWITCHING_PROTOCOL = 101;
8-
constexpr long HTTP_PROCESSING = 102;
9-
constexpr long HTTP_EARLY_HINTS = 103;
5+
inline constexpr long HTTP_CONTINUE = 100;
6+
inline constexpr long HTTP_SWITCHING_PROTOCOL = 101;
7+
inline constexpr long HTTP_PROCESSING = 102;
8+
inline constexpr long HTTP_EARLY_HINTS = 103;
109
// Successful responses
11-
constexpr long HTTP_OK = 200;
12-
constexpr long HTTP_CREATED = 201;
13-
constexpr long HTTP_ACCEPTED = 202;
14-
constexpr long HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
15-
constexpr long HTTP_NO_CONTENT = 204;
16-
constexpr long HTTP_RESET_CONTENT = 205;
17-
constexpr long HTTP_PARTIAL_CONTENT = 206;
18-
constexpr long HTTP_MULTI_STATUS = 207;
19-
constexpr long HTTP_ALREADY_REPORTED = 208;
20-
constexpr long HTTP_IM_USED = 226;
10+
inline constexpr long HTTP_OK = 200;
11+
inline constexpr long HTTP_CREATED = 201;
12+
inline constexpr long HTTP_ACCEPTED = 202;
13+
inline constexpr long HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
14+
inline constexpr long HTTP_NO_CONTENT = 204;
15+
inline constexpr long HTTP_RESET_CONTENT = 205;
16+
inline constexpr long HTTP_PARTIAL_CONTENT = 206;
17+
inline constexpr long HTTP_MULTI_STATUS = 207;
18+
inline constexpr long HTTP_ALREADY_REPORTED = 208;
19+
inline constexpr long HTTP_IM_USED = 226;
2120
// Redirection messages
22-
constexpr long HTTP_MULTIPLE_CHOICE = 300;
23-
constexpr long HTTP_MOVED_PERMANENTLY = 301;
24-
constexpr long HTTP_FOUND = 302;
25-
constexpr long HTTP_SEE_OTHER = 303;
26-
constexpr long HTTP_NOT_MODIFIED = 304;
27-
constexpr long HTTP_USE_PROXY = 305;
28-
constexpr long HTTP_UNUSED = 306;
29-
constexpr long HTTP_TEMPORARY_REDIRECT = 307;
30-
constexpr long HTTP_PERMANENT_REDIRECT = 308;
21+
inline constexpr long HTTP_MULTIPLE_CHOICE = 300;
22+
inline constexpr long HTTP_MOVED_PERMANENTLY = 301;
23+
inline constexpr long HTTP_FOUND = 302;
24+
inline constexpr long HTTP_SEE_OTHER = 303;
25+
inline constexpr long HTTP_NOT_MODIFIED = 304;
26+
inline constexpr long HTTP_USE_PROXY = 305;
27+
inline constexpr long HTTP_UNUSED = 306;
28+
inline constexpr long HTTP_TEMPORARY_REDIRECT = 307;
29+
inline constexpr long HTTP_PERMANENT_REDIRECT = 308;
3130
// Client error responses
32-
constexpr long HTTP_BAD_REQUEST = 400;
33-
constexpr long HTTP_UNAUTHORIZED = 401;
34-
constexpr long HTTP_PAYMENT_REQUIRED = 402;
35-
constexpr long HTTP_FORBIDDEN = 403;
36-
constexpr long HTTP_NOT_FOUND = 404;
37-
constexpr long HTTP_METHOD_NOT_ALLOWED = 405;
38-
constexpr long HTTP_NOT_ACCEPTABLE = 406;
39-
constexpr long HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
40-
constexpr long HTTP_REQUEST_TIMEOUT = 408;
41-
constexpr long HTTP_CONFLICT = 409;
42-
constexpr long HTTP_GONE = 410;
43-
constexpr long HTTP_LENGTH_REQUIRED = 411;
44-
constexpr long HTTP_PRECONDITION_FAILED = 412;
45-
constexpr long HTTP_PAYLOAD_TOO_LARGE = 413;
46-
constexpr long HTTP_URI_TOO_LONG = 414;
47-
constexpr long HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
48-
constexpr long HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
49-
constexpr long HTTP_EXPECTATION_FAILED = 417;
50-
constexpr long HTTP_IM_A_TEAPOT = 418;
51-
constexpr long HTTP_MISDIRECTED_REQUEST = 421;
52-
constexpr long HTTP_UNPROCESSABLE_ENTITY = 422;
53-
constexpr long HTTP_LOCKED = 423;
54-
constexpr long HTTP_FAILED_DEPENDENCY = 424;
55-
constexpr long HTTP_TOO_EARLY = 425;
56-
constexpr long HTTP_UPGRADE_REQUIRED = 426;
57-
constexpr long HTTP_PRECONDITION_REQUIRED = 428;
58-
constexpr long HTTP_TOO_MANY_REQUESTS = 429;
59-
constexpr long HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
60-
constexpr long HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
31+
inline constexpr long HTTP_BAD_REQUEST = 400;
32+
inline constexpr long HTTP_UNAUTHORIZED = 401;
33+
inline constexpr long HTTP_PAYMENT_REQUIRED = 402;
34+
inline constexpr long HTTP_FORBIDDEN = 403;
35+
inline constexpr long HTTP_NOT_FOUND = 404;
36+
inline constexpr long HTTP_METHOD_NOT_ALLOWED = 405;
37+
inline constexpr long HTTP_NOT_ACCEPTABLE = 406;
38+
inline constexpr long HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
39+
inline constexpr long HTTP_REQUEST_TIMEOUT = 408;
40+
inline constexpr long HTTP_CONFLICT = 409;
41+
inline constexpr long HTTP_GONE = 410;
42+
inline constexpr long HTTP_LENGTH_REQUIRED = 411;
43+
inline constexpr long HTTP_PRECONDITION_FAILED = 412;
44+
inline constexpr long HTTP_PAYLOAD_TOO_LARGE = 413;
45+
inline constexpr long HTTP_URI_TOO_LONG = 414;
46+
inline constexpr long HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
47+
inline constexpr long HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
48+
inline constexpr long HTTP_EXPECTATION_FAILED = 417;
49+
inline constexpr long HTTP_IM_A_TEAPOT = 418;
50+
inline constexpr long HTTP_MISDIRECTED_REQUEST = 421;
51+
inline constexpr long HTTP_UNPROCESSABLE_ENTITY = 422;
52+
inline constexpr long HTTP_LOCKED = 423;
53+
inline constexpr long HTTP_FAILED_DEPENDENCY = 424;
54+
inline constexpr long HTTP_TOO_EARLY = 425;
55+
inline constexpr long HTTP_UPGRADE_REQUIRED = 426;
56+
inline constexpr long HTTP_PRECONDITION_REQUIRED = 428;
57+
inline constexpr long HTTP_TOO_MANY_REQUESTS = 429;
58+
inline constexpr long HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
59+
inline constexpr long HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
6160
// Server response errors
62-
constexpr long HTTP_INTERNAL_SERVER_ERROR = 500;
63-
constexpr long HTTP_NOT_IMPLEMENTED = 501;
64-
constexpr long HTTP_BAD_GATEWAY = 502;
65-
constexpr long HTTP_SERVICE_UNAVAILABLE = 503;
66-
constexpr long HTTP_GATEWAY_TIMEOUT = 504;
67-
constexpr long HTTP_HTTP_VERSION_NOT_SUPPORTED = 505;
68-
constexpr long HTTP_VARIANT_ALSO_NEGOTIATES = 506;
69-
constexpr long HTTP_INSUFFICIENT_STORAGE = 507;
70-
constexpr long HTTP_LOOP_DETECTED = 508;
71-
constexpr long HTTP_NOT_EXTENDED = 510;
72-
constexpr long HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;
61+
inline constexpr long HTTP_INTERNAL_SERVER_ERROR = 500;
62+
inline constexpr long HTTP_NOT_IMPLEMENTED = 501;
63+
inline constexpr long HTTP_BAD_GATEWAY = 502;
64+
inline constexpr long HTTP_SERVICE_UNAVAILABLE = 503;
65+
inline constexpr long HTTP_GATEWAY_TIMEOUT = 504;
66+
inline constexpr long HTTP_HTTP_VERSION_NOT_SUPPORTED = 505;
67+
inline constexpr long HTTP_VARIANT_ALSO_NEGOTIATES = 506;
68+
inline constexpr long HTTP_INSUFFICIENT_STORAGE = 507;
69+
inline constexpr long HTTP_LOOP_DETECTED = 508;
70+
inline constexpr long HTTP_NOT_EXTENDED = 510;
71+
inline constexpr long HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;
7372

74-
constexpr long INFO_CODE_OFFSET = 100;
75-
constexpr long SUCCESS_CODE_OFFSET = 200;
76-
constexpr long REDIRECT_CODE_OFFSET = 300;
77-
constexpr long CLIENT_ERROR_CODE_OFFSET = 400;
78-
constexpr long SERVER_ERROR_CODE_OFFSET = 500;
79-
constexpr long MISC_CODE_OFFSET = 600;
73+
inline constexpr long INFO_CODE_OFFSET = 100;
74+
inline constexpr long SUCCESS_CODE_OFFSET = 200;
75+
inline constexpr long REDIRECT_CODE_OFFSET = 300;
76+
inline constexpr long CLIENT_ERROR_CODE_OFFSET = 400;
77+
inline constexpr long SERVER_ERROR_CODE_OFFSET = 500;
78+
inline constexpr long MISC_CODE_OFFSET = 600;
8079

8180
constexpr bool is_informational(const long code) {
8281
return (code >= INFO_CODE_OFFSET && code < SUCCESS_CODE_OFFSET);
@@ -93,7 +92,5 @@ constexpr bool is_client_error(const long code) {
9392
constexpr bool is_server_error(const long code) {
9493
return (code >= SERVER_ERROR_CODE_OFFSET && code < MISC_CODE_OFFSET);
9594
}
96-
97-
} // namespace status
98-
} // namespace cpr
95+
} // namespace cpr::status
9996
#endif

include/cpr/threadpool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
#define CPR_DEFAULT_THREAD_POOL_MAX_THREAD_NUM std::thread::hardware_concurrency()
1717

18-
constexpr size_t CPR_DEFAULT_THREAD_POOL_MIN_THREAD_NUM = 1;
19-
constexpr std::chrono::milliseconds CPR_DEFAULT_THREAD_POOL_MAX_IDLE_TIME{250};
18+
inline constexpr size_t CPR_DEFAULT_THREAD_POOL_MIN_THREAD_NUM = 1;
19+
inline constexpr std::chrono::milliseconds CPR_DEFAULT_THREAD_POOL_MAX_IDLE_TIME{250};
2020

2121
namespace cpr {
2222

include/cpr/timeout.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Timeout {
1818

1919
// No way around since curl uses a long here.
2020
// NOLINTNEXTLINE(google-runtime-int)
21+
[[nodiscard]]
2122
long Milliseconds() const;
2223

2324
std::chrono::milliseconds ms;

include/cpr/unix_socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class UnixSocket {
1010
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
1111
UnixSocket(std::string unix_socket) : unix_socket_(std::move(unix_socket)) {}
1212

13+
[[nodiscard]]
1314
const char* GetUnixSocketString() const noexcept;
1415

1516
private:

modules/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
add_library(cpr_module)
4+
5+
target_sources(cpr_module
6+
PUBLIC
7+
FILE_SET CXX_MODULES FILES
8+
cpr.cppm
9+
)
10+
11+
target_compile_features(cpr_module PUBLIC cxx_std_20)
12+
13+
target_include_directories(cpr_module PUBLIC
14+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
15+
$<INSTALL_INTERFACE:include>
16+
)
17+
18+
add_library(cpr::module ALIAS cpr_module)
19+
20+
# Installation
21+
install(TARGETS cpr_module
22+
EXPORT ${PROJECT_NAME}Targets
23+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
24+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
25+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
26+
FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/module
27+
)

0 commit comments

Comments
 (0)