libboostasio.h does not compile with clang-3.9 due to following error when compile if cpp17 preprocessor check: embedding a directive within macro arguments has undefined behavior [-Werror,-Wembedded-directive]. Fix this by moving the bind away from preprocessor macro. Replace the STRAND_SOCKET_HANDLER, STRAND_TIMER_HANDLER macros with template functions.
This commit is contained in:
parent
9935f8414b
commit
4652a9cbdf
|
|
@ -1,5 +1,4 @@
|
||||||
/**
|
bBoostAsio.h
|
||||||
* LibBoostAsio.h
|
|
||||||
*
|
*
|
||||||
* Implementation for the AMQP::TcpHandler that is optimized for boost::asio. You can
|
* Implementation for the AMQP::TcpHandler that is optimized for boost::asio. You can
|
||||||
* use this class instead of a AMQP::TcpHandler class, just pass the boost asio service
|
* use this class instead of a AMQP::TcpHandler class, just pass the boost asio service
|
||||||
|
|
@ -27,33 +26,35 @@
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////
|
template<class Func>
|
||||||
#define STRAND_SOCKET_HANDLER(_fn) \
|
void strand_sock_dispatch_func(
|
||||||
[fn = _fn, strand = _strand](const boost::system::error_code &ec, \
|
Func fn,
|
||||||
const std::size_t bytes_transferred) \
|
std::weak_ptr<boost::asio::io_service::strand> strand,
|
||||||
{ \
|
const boost::system::error_code &ec,
|
||||||
const std::shared_ptr<boost::asio::io_service::strand> apStrand = strand.lock(); \
|
const std::size_t bytes_transferred
|
||||||
if (!apStrand) \
|
) {
|
||||||
{ \
|
const std::shared_ptr<boost::asio::io_service::strand> apStrand = strand.lock();
|
||||||
fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled),std::size_t{0}); \
|
if (!apStrand)
|
||||||
return; \
|
{
|
||||||
} \
|
fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled), std::size_t{0});
|
||||||
\
|
return;
|
||||||
apStrand->dispatch(boost::bind(fn,ec,bytes_transferred)); \
|
}
|
||||||
|
apStrand->dispatch(boost::bind(fn, ec, bytes_transferred));
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////
|
template<class Func>
|
||||||
#define STRAND_TIMER_HANDLER(_fn) \
|
void strand_timer_dispatch_func(
|
||||||
[fn = _fn, strand = _strand](const boost::system::error_code &ec) \
|
Func fn,
|
||||||
{ \
|
std::weak_ptr<boost::asio::io_service::strand> strand,
|
||||||
const std::shared_ptr<boost::asio::io_service::strand> apStrand = strand.lock(); \
|
const boost::system::error_code &ec
|
||||||
if (!apStrand) \
|
) {
|
||||||
{ \
|
const std::shared_ptr<boost::asio::io_service::strand> apStrand = strand.lock();
|
||||||
fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled)); \
|
if (!apStrand)
|
||||||
return; \
|
{
|
||||||
} \
|
fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled));
|
||||||
\
|
return;
|
||||||
apStrand->dispatch(boost::bind(fn,ec)); \
|
}
|
||||||
|
apStrand->dispatch(boost::bind(fn,ec));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -148,9 +149,7 @@ private:
|
||||||
|
|
||||||
_read_pending = true;
|
_read_pending = true;
|
||||||
|
|
||||||
_socket.async_read_some(boost::asio::null_buffers(),
|
auto func = boost::bind(&Watcher::read_handler,
|
||||||
STRAND_SOCKET_HANDLER(
|
|
||||||
boost::bind(&Watcher::read_handler,
|
|
||||||
this,
|
this,
|
||||||
boost::arg<1>(),
|
boost::arg<1>(),
|
||||||
boost::arg<2>(),
|
boost::arg<2>(),
|
||||||
|
|
@ -161,7 +160,13 @@ private:
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
#endif
|
#endif
|
||||||
connection,
|
connection,
|
||||||
fd)));
|
fd);
|
||||||
|
_socket.async_read_some(
|
||||||
|
boost::asio::null_buffers(),
|
||||||
|
[f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) {
|
||||||
|
strand_sock_dispatch_func(f, strand, ec, bytes_transferred);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -193,9 +198,7 @@ private:
|
||||||
|
|
||||||
_write_pending = true;
|
_write_pending = true;
|
||||||
|
|
||||||
_socket.async_write_some(boost::asio::null_buffers(),
|
auto func = boost::bind(&Watcher::write_handler,
|
||||||
STRAND_SOCKET_HANDLER(
|
|
||||||
boost::bind(&Watcher::write_handler,
|
|
||||||
this,
|
this,
|
||||||
boost::arg<1>(),
|
boost::arg<1>(),
|
||||||
boost::arg<2>(),
|
boost::arg<2>(),
|
||||||
|
|
@ -206,7 +209,13 @@ private:
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
#endif
|
#endif
|
||||||
connection,
|
connection,
|
||||||
fd)));
|
fd);
|
||||||
|
_socket.async_write_some(
|
||||||
|
boost::asio::null_buffers(),
|
||||||
|
[f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) {
|
||||||
|
strand_sock_dispatch_func(f, strand, ec, bytes_transferred);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -262,9 +271,7 @@ private:
|
||||||
{
|
{
|
||||||
_read_pending = true;
|
_read_pending = true;
|
||||||
|
|
||||||
_socket.async_read_some(boost::asio::null_buffers(),
|
auto func = boost::bind(&Watcher::read_handler,
|
||||||
STRAND_SOCKET_HANDLER(
|
|
||||||
boost::bind(&Watcher::read_handler,
|
|
||||||
this,
|
this,
|
||||||
boost::arg<1>(),
|
boost::arg<1>(),
|
||||||
boost::arg<2>(),
|
boost::arg<2>(),
|
||||||
|
|
@ -275,7 +282,13 @@ private:
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
#endif
|
#endif
|
||||||
connection,
|
connection,
|
||||||
fd)));
|
fd);
|
||||||
|
_socket.async_read_some(
|
||||||
|
boost::asio::null_buffers(),
|
||||||
|
[f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) {
|
||||||
|
strand_sock_dispatch_func(f, strand, ec, bytes_transferred);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Handle writes?
|
// 2. Handle writes?
|
||||||
|
|
@ -286,9 +299,7 @@ private:
|
||||||
{
|
{
|
||||||
_write_pending = true;
|
_write_pending = true;
|
||||||
|
|
||||||
_socket.async_write_some(boost::asio::null_buffers(),
|
auto func = boost::bind(&Watcher::write_handler,
|
||||||
STRAND_SOCKET_HANDLER(
|
|
||||||
boost::bind(&Watcher::write_handler,
|
|
||||||
this,
|
this,
|
||||||
boost::arg<1>(),
|
boost::arg<1>(),
|
||||||
boost::arg<2>(),
|
boost::arg<2>(),
|
||||||
|
|
@ -299,7 +310,13 @@ private:
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
#endif
|
#endif
|
||||||
connection,
|
connection,
|
||||||
fd)));
|
fd);
|
||||||
|
_socket.async_write_some(
|
||||||
|
boost::asio::null_buffers(),
|
||||||
|
[f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) {
|
||||||
|
strand_sock_dispatch_func(f, strand, ec, bytes_transferred);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -353,12 +370,7 @@ private:
|
||||||
connection->heartbeat();
|
connection->heartbeat();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reschedule the timer for the future:
|
auto func = boost::bind(&Timer::timeout,
|
||||||
_timer.expires_at(_timer.expires_at() + boost::posix_time::seconds(timeout));
|
|
||||||
|
|
||||||
// Posts the timer event
|
|
||||||
_timer.async_wait(STRAND_TIMER_HANDLER(
|
|
||||||
boost::bind(&Timer::timeout,
|
|
||||||
this,
|
this,
|
||||||
boost::arg<1>(),
|
boost::arg<1>(),
|
||||||
// C++17 has 'weak_from_this()' support.
|
// C++17 has 'weak_from_this()' support.
|
||||||
|
|
@ -368,7 +380,17 @@ private:
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
#endif
|
#endif
|
||||||
connection,
|
connection,
|
||||||
timeout)));
|
timeout);
|
||||||
|
|
||||||
|
// Reschedule the timer for the future:
|
||||||
|
_timer.expires_at(_timer.expires_at() + boost::posix_time::seconds(timeout));
|
||||||
|
|
||||||
|
// Posts the timer event
|
||||||
|
_timer.async_wait(
|
||||||
|
[f = func, strand = _strand](const boost::system::error_code &ec) {
|
||||||
|
strand_timer_dispatch_func(f, strand, ec);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -423,9 +445,7 @@ private:
|
||||||
// stop timer in case it was already set
|
// stop timer in case it was already set
|
||||||
stop();
|
stop();
|
||||||
|
|
||||||
_timer.expires_from_now(boost::posix_time::seconds(timeout));
|
auto func = boost::bind(&Timer::timeout,
|
||||||
_timer.async_wait(STRAND_TIMER_HANDLER(
|
|
||||||
boost::bind(&Timer::timeout,
|
|
||||||
this,
|
this,
|
||||||
boost::arg<1>(),
|
boost::arg<1>(),
|
||||||
// C++17 has 'weak_from_this()' support.
|
// C++17 has 'weak_from_this()' support.
|
||||||
|
|
@ -435,7 +455,13 @@ private:
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
#endif
|
#endif
|
||||||
connection,
|
connection,
|
||||||
timeout)));
|
timeout);
|
||||||
|
_timer.expires_from_now(boost::posix_time::seconds(timeout));
|
||||||
|
_timer.async_wait(
|
||||||
|
[f = func, strand = _strand](const boost::system::error_code &ec) {
|
||||||
|
strand_timer_dispatch_func(f, strand, ec);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue