Skip to content

Commit e0570c4

Browse files
committed
Untested code that compiles
1 parent 5470ee3 commit e0570c4

30 files changed

+264
-133
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ set(ACTUAL_SOURCES
218218
src/RequestBodyStringImpl.cpp
219219
src/RequestBodyFileImpl.cpp
220220
src/url_encode.cpp
221+
src/boost_compitability.cpp
221222
${LOGGING_SRC}
222223
)
223224

examples/logip/logip.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <boost/log/expressions.hpp>
2121
#endif
2222

23+
#include <boost/exception/all.hpp>
24+
#include <boost/exception/diagnostic_information.hpp>
25+
2326
#include "restc-cpp/restc-cpp.h"
2427
#include "restc-cpp/RequestBuilder.h"
2528
#include "restc-cpp/SerializeJson.h"

include/restc-cpp/DataReader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DataReader {
4444
virtual ~DataReader() = default;
4545

4646
virtual bool IsEof() const = 0;
47-
virtual boost::asio::const_buffers_1 ReadSome() = 0;
47+
virtual boost_const_buffer ReadSome() = 0;
4848
virtual void Finish() = 0; // Make sure there are no pending data for the current request
4949

5050
static ptr_t CreateIoReader(const Connection::ptr_t& conn,

include/restc-cpp/DataReaderStream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class DataReaderStream : public DataReader {
3434
}
3535

3636
/*! Read whatever we have buffered or can get downstream */
37-
boost::asio::const_buffers_1 ReadSome() override;
37+
boost_const_buffer ReadSome() override;
3838

3939
/*! Read up to maxBytes from whatever we have buffered or can get downstream.*/
40-
boost::asio::const_buffers_1 GetData(size_t maxBytes);
40+
boost_const_buffer GetData(size_t maxBytes);
4141

4242
/*! Get one char
4343
*

include/restc-cpp/DataWriter.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class DataWriter {
4444
virtual ~DataWriter() = default;
4545

4646
/*! Write some data */
47-
virtual void Write(boost::asio::const_buffers_1 buffers) = 0;
47+
virtual void Write(boost_const_buffer buffers) = 0;
4848

4949
/*! Write without altering the data (headers) */
50-
virtual void WriteDirect(boost::asio::const_buffers_1 buffers) = 0;
50+
virtual void WriteDirect(boost_const_buffer buffers) = 0;
5151

5252
/*! Write some data */
5353
virtual void Write(const write_buffers_t& buffers) = 0;

include/restc-cpp/RapidJsonReader.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <assert.h>
88

9+
#include "restc-cpp/boost_compatibility.h"
910
#include "rapidjson/reader.h"
1011
#include "restc-cpp/restc-cpp.h"
1112

@@ -96,7 +97,7 @@ class RapidJsonReader {
9697
const auto len = boost::asio::buffer_size(buffer);
9798

9899
if (len) {
99-
ch_ = boost::asio::buffer_cast<const char*>(buffer);
100+
ch_ = boost_buffer_cast(buffer);
100101
end_ = ch_ + len;
101102
} else {
102103
ch_ = end_ = nullptr;

include/restc-cpp/Socket.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <boost/system/error_code.hpp>
1313

14+
#include "restc-cpp/boost_compatibility.h"
1415
#include "restc-cpp/typename.h"
1516
#include "restc-cpp/logging.h"
1617

@@ -34,21 +35,21 @@ class Socket
3435

3536
virtual const boost::asio::ip::tcp::socket& GetSocket() const = 0;
3637

37-
virtual std::size_t AsyncReadSome(boost::asio::mutable_buffers_1 buffers,
38+
virtual std::size_t AsyncReadSome(boost_mutable_buffer buffers,
3839
boost::asio::yield_context& yield) = 0;
3940

40-
virtual std::size_t AsyncRead(boost::asio::mutable_buffers_1 buffers,
41+
virtual std::size_t AsyncRead(boost_mutable_buffer buffers,
4142
boost::asio::yield_context& yield) = 0;
4243

43-
virtual void AsyncWrite(const boost::asio::const_buffers_1& buffers,
44+
virtual void AsyncWrite(const boost_const_buffer& buffers,
4445
boost::asio::yield_context& yield) = 0;
4546

4647
virtual void AsyncWrite(const write_buffers_t& buffers,
4748
boost::asio::yield_context& yield) = 0;
4849

4950
template <typename T>
5051
void AsyncWriteT(const T& buffer, boost::asio::yield_context& yield) {
51-
boost::asio::const_buffers_1 b{buffer.data(), buffer.size()};
52+
boost_const_buffer b{buffer.data(), buffer.size()};
5253
AsyncWrite(b, yield);
5354
}
5455

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#pragma once
2+
3+
#include <boost/asio.hpp>
4+
#include <boost/version.hpp>
5+
#include <boost/asio/ip/address_v4.hpp>
6+
7+
/* Boost keeps introducing breaking changes in the asio library. It seems like
8+
* their window of backwards compatibility is about 5 years.
9+
*
10+
* This is a nightmare for library developers, if we want to maintain support
11+
* for older versions of boost. I don't want the users of restc-cpp to be forced to
12+
* refactor their code just because the boost library has been updated. Refactoring
13+
* for this reason alone is just a waste of time and a huge cost for no gain.
14+
*
15+
* Here I try to handle the differences between the different versions of boost::asio
16+
* in order to make it easier to maintain support for older versions of boost.
17+
*
18+
* So if restc-spp is the only library that you are using that requires broken parts
19+
* of boost, then you should be fine.
20+
*
21+
* I take full credits for whatever works well here. All blame goes to ChatGPT! ;)
22+
*/
23+
24+
#if BOOST_VERSION >= 107000
25+
#include <boost/coroutine/exceptions.hpp>
26+
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
27+
catch (boost::coroutines::detail::forced_unwind const&) { \
28+
throw; /* required for Boost Coroutine! */ \
29+
} catch (...)
30+
#elif BOOST_VERSION >= 106000
31+
#include <boost/coroutine/detail/forced_unwind.hpp>
32+
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
33+
catch (boost::coroutines::detail::forced_unwind const&) { \
34+
throw; /* required for Boost Coroutine! */ \
35+
} catch (...)
36+
#else
37+
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
38+
catch (...)
39+
#endif
40+
41+
#if BOOST_VERSION >= 108100
42+
// They changed the function signature. In boost 1.86 it broke the build.
43+
#define RESTC_CPP_SPAWN_TRAILER \
44+
, boost::asio::detached
45+
#else
46+
#define RESTC_CPP_SPAWN_TRAILER
47+
#endif
48+
49+
50+
namespace restc_cpp {
51+
52+
#if BOOST_VERSION >= 107000
53+
using boost_const_buffer = boost::asio::const_buffer;
54+
using boost_mutable_buffer = boost::asio::mutable_buffer;
55+
#else
56+
using boost_const_buffer = boost::asio::const_buffers_1;
57+
sing boost_mutable_buffer = boost::asio::mutable_buffers_1;
58+
#endif
59+
60+
61+
#if BOOST_VERSION >= 106600
62+
using boost_io_service = boost::asio::io_context;
63+
using boost_work = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;
64+
65+
#else
66+
using boost_io_service = boost::asio::io_service;
67+
using boost_work = boost::asio::io_service::work;
68+
#endif
69+
70+
template <typename Buffer>
71+
const char* boost_buffer_cast(const Buffer& buffer) {
72+
#if BOOST_VERSION >= 107000
73+
return static_cast<const char*>(buffer.data());
74+
#else
75+
return boost::asio::buffer_cast<const char*>(buffer);
76+
#endif
77+
}
78+
79+
template <typename Buffer>
80+
std::size_t boost_buffer_size(const Buffer& buffer) {
81+
#if BOOST_VERSION >= 107000
82+
return buffer.size();
83+
#else
84+
return boost::asio::buffer_size(buffer);
85+
#endif
86+
}
87+
88+
template <typename IOService, typename Handler>
89+
void boost_dispatch(IOService& io_service, Handler&& handler) {
90+
#if BOOST_VERSION >= 106600
91+
// Determine if IOService is a pointer
92+
if constexpr (std::is_pointer_v<IOService>) {
93+
io_service->get_executor().dispatch(
94+
std::forward<Handler>(handler),
95+
std::allocator<void>() // Default allocator
96+
);
97+
} else {
98+
io_service.get_executor().dispatch(
99+
std::forward<Handler>(handler),
100+
std::allocator<void>() // Default allocator
101+
);
102+
}
103+
#else
104+
io_service.dispatch(std::forward<Handler>(handler));
105+
#endif
106+
}
107+
108+
boost::asio::ip::tcp::endpoint boost_create_endpoint(const std::string& ip_address, unsigned short port);
109+
uint32_t boost_convert_ipv4_to_uint(const std::string& ip_address);
110+
std::unique_ptr<boost_work> boost_make_work(boost_io_service& ioservice);
111+
112+
} // ns
113+
114+

include/restc-cpp/helper.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ class ToBuffer
5959
{
6060
}
6161

62-
operator boost::asio::const_buffers_1 () const {
62+
#if BOOST_VERSION >= 107000
63+
// For Boost 1.70 and newer
64+
operator boost::asio::const_buffer() const {
65+
return {buf_.data(), buf_.size()};
66+
}
67+
#else
68+
// For Boost versions older than 1.70
69+
operator boost::asio::const_buffers_1() const {
6370
return {buf_.c_str(), buf_.size()};
6471
}
72+
#endif
6573

6674
operator const boost::string_ref() const {
6775
return buf_;

include/restc-cpp/restc-cpp.h

+17-29
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <boost/uuid/uuid.hpp>
2626
#include <boost/uuid/uuid_io.hpp>
2727

28+
#include "restc-cpp/boost_compatibility.h"
2829
#include "restc-cpp/helper.h"
2930
#include "restc-cpp/Connection.h"
3031

@@ -52,19 +53,6 @@
5253
# define RESTC_CPP_IO_BUFFER_SIZE (1024 * 16)
5354
#endif
5455

55-
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
56-
catch (boost::coroutines::detail::forced_unwind const&) { \
57-
throw; /* required for Boost Coroutine! */ \
58-
} catch (...)
59-
60-
#if BOOST_VERSION >= 108100
61-
// They changed the function signature. In boost 1.86 it broke the build.
62-
#define RESTC_CPP_SPAWN_TRAILER \
63-
, boost::asio::detached
64-
#else
65-
#define RESTC_CPP_SPAWN_TRAILER
66-
#endif
67-
6856
namespace restc_cpp {
6957

7058
class RestClient;
@@ -285,7 +273,7 @@ class Reply {
285273
* be fetched from the server. The data is safe to use until
286274
* the method is called again.
287275
*/
288-
virtual boost::asio::const_buffers_1 GetSomeData() = 0;
276+
virtual boost_const_buffer GetSomeData() = 0;
289277

290278
/*! Returns true as long as you have not yet pulled all
291279
* the data from the response.
@@ -350,15 +338,15 @@ class Context {
350338
const auto microseconds =
351339
std::chrono::duration_cast<std::chrono::microseconds>(
352340
duration).count();
353-
boost::posix_time::microseconds ms(microseconds);
341+
::boost::posix_time::microseconds ms(microseconds);
354342
Sleep(ms);
355343
}
356344

357345
/*! Asynchronously sleep for a period */
358-
virtual void Sleep(const boost::posix_time::microseconds& ms) = 0;
346+
virtual void Sleep(const ::boost::posix_time::microseconds& ms) = 0;
359347

360348
static std::unique_ptr<Context>
361-
Create(boost::asio::yield_context& yield,
349+
Create(::boost::asio::yield_context& yield,
362350
RestClient& rc);
363351
};
364352

@@ -442,7 +430,7 @@ class RestClient {
442430
}
443431

444432
virtual std::shared_ptr<ConnectionPool> GetConnectionPool() = 0;
445-
virtual boost::asio::io_service& GetIoService() = 0;
433+
virtual boost_io_service& GetIoService() = 0;
446434

447435
#ifdef RESTC_CPP_WITH_TLS
448436
virtual std::shared_ptr<boost::asio::ssl::context> GetTLSContext() = 0;
@@ -476,30 +464,30 @@ class RestClient {
476464

477465
#ifdef RESTC_CPP_WITH_TLS
478466
static std::unique_ptr<RestClient> Create(
479-
std::shared_ptr<boost::asio::ssl::context> ctx);
467+
std::shared_ptr<::boost::asio::ssl::context> ctx);
480468
static std::unique_ptr<RestClient> Create(
481-
std::shared_ptr<boost::asio::ssl::context> ctx,
482-
const boost::optional<Request::Properties>& properties);
469+
std::shared_ptr<::boost::asio::ssl::context> ctx,
470+
const ::boost::optional<Request::Properties>& properties);
483471
static std::unique_ptr<RestClient> Create(
484-
std::shared_ptr<boost::asio::ssl::context> ctx,
485-
const boost::optional<Request::Properties>& properties,
486-
boost::asio::io_service& ioservice);
472+
std::shared_ptr<::boost::asio::ssl::context> ctx,
473+
const ::boost::optional<Request::Properties>& properties,
474+
boost_io_service& ioservice);
487475
#endif
488476

489477
static std::unique_ptr<RestClient>
490-
Create(const boost::optional<Request::Properties>& properties);
478+
Create(const ::boost::optional<Request::Properties>& properties);
491479

492480
static std::unique_ptr<RestClient> CreateUseOwnThread();
493481

494482
static std::unique_ptr<RestClient>
495-
CreateUseOwnThread(const boost::optional<Request::Properties>& properties);
483+
CreateUseOwnThread(const ::boost::optional<Request::Properties>& properties);
496484

497485
static std::unique_ptr<RestClient>
498-
Create(const boost::optional<Request::Properties>& properties,
499-
boost::asio::io_service& ioservice);
486+
Create(const ::boost::optional<Request::Properties>& properties,
487+
boost_io_service& ioservice);
500488

501489
static std::unique_ptr<RestClient>
502-
Create(boost::asio::io_service& ioservice);
490+
Create(boost_io_service& ioservice);
503491

504492

505493
protected:

src/ChunkedReaderImpl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class ChunkedReaderImpl : public DataReader {
5555
return out.str();
5656
}
5757

58-
static void Log(const boost::asio::const_buffers_1 buffers, const char * /*tag*/)
58+
static void Log(const ::restc_cpp::boost_const_buffer buffers, const char * /*tag*/)
5959
{
60-
const auto buf_len = boost::asio::buffer_size(*buffers.begin());
60+
const auto buf_len = boost_buffer_size(buffers);
6161

6262
// At the time of the implementation, there are never multiple buffers.
6363
RESTC_CPP_LOG_TRACE_(tag << ' ' << "# " << buf_len
@@ -67,7 +67,7 @@ class ChunkedReaderImpl : public DataReader {
6767
buf_len}));
6868
}
6969

70-
boost::asio::const_buffers_1 ReadSome() override {
70+
::restc_cpp::boost_const_buffer ReadSome() override {
7171

7272
EatPadding();
7373

@@ -113,7 +113,7 @@ class ChunkedReaderImpl : public DataReader {
113113
}
114114
}
115115

116-
boost::asio::const_buffers_1 GetData() {
116+
::restc_cpp::boost_const_buffer GetData() {
117117

118118
auto rval = stream_->GetData(chunk_len_);
119119
const auto seg_len = boost::asio::buffer_size(rval);

src/ChunkedWriterImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class ChunkedWriterImpl : public DataWriter {
2020
{
2121
}
2222

23-
void WriteDirect(boost::asio::const_buffers_1 buffers) override {
23+
void WriteDirect(::restc_cpp::boost_const_buffer buffers) override {
2424
next_->WriteDirect(buffers);
2525
}
2626

27-
void Write(boost::asio::const_buffers_1 buffers) override {
27+
void Write(::restc_cpp::boost_const_buffer buffers) override {
2828
const auto len = boost::asio::buffer_size(buffers);
2929
buffers_.resize(2);
3030
buffers_[1] = buffers;

0 commit comments

Comments
 (0)