diff --git a/include/bal.hh b/include/bal.hh index 63989d0..5d31c3b 100644 --- a/include/bal.hh +++ b/include/bal.hh @@ -26,6 +26,12 @@ #ifndef _BAL_HH_INCLUDED # define _BAL_HH_INCLUDED +# if defined(__has_include) +# define __HAS_INCLUDE(hdr) __has_include(hdr) +# else +# define __HAS_INCLUDE(hdr) false +# endif + # include "bal.h" # include # include @@ -35,7 +41,12 @@ # include # include # include -# include +# if __HAS_INCLUDE() +# include +# define bit_cast std::bit_cast +# else +# define bit_cast reinterpret_cast +# endif /** The one and only namespace for libbal. */ namespace bal @@ -653,12 +664,12 @@ namespace bal static socket_base* from_user_data(bal_socket* s) { - return std::bit_cast(s->user_data); + return bit_cast(s->user_data); } uintptr_t to_user_data() const { - return std::bit_cast(this); + return bit_cast(this); } async_io_cb on_read; @@ -706,7 +717,7 @@ namespace bal if (self == nullptr) { _bal_dbglog("no user_data for socket " BAL_SOCKET_SPEC " (0x%" PRIxPTR ", mask = %08" PRIx32 ")", s->sd, - std::bit_cast(s), s->state.mask); + bit_cast(s), s->state.mask); return; } @@ -715,8 +726,7 @@ namespace bal # if defined(BAL_DBGLOG) _bal_dbglog("early return for socket " BAL_SOCKET_SPEC " (0x%" PRIxPTR ", evt = %08" PRIx32 ", self = 0x%" PRIxPTR ")", - s->sd, std::bit_cast(s), evt, - std::bit_cast(self)); + s->sd, bit_cast(s), evt, bit_cast(self)); # else BAL_UNUSED(s); BAL_UNUSED(self); diff --git a/include/bal/errors.h b/include/bal/errors.h index 868b8de..ba9e557 100644 --- a/include/bal/errors.h +++ b/include/bal/errors.h @@ -29,9 +29,6 @@ # include "types.h" # if defined(__cplusplus) -# if _has_include() -# include -# endif extern "C" { # endif @@ -164,7 +161,8 @@ bool __bal_validate(bool expr, int err, const char* func, const char* file, # if defined(BAL_DBGLOG) void __bal_dbglog(const char* func, const char* file, uint32_t line, const char* format, ...); -# if defined(__cplusplus) && _has_include() +# if defined(__cplusplus) && __HAS_INCLUDE() +# include # define _bal_dbglog(...) \ do { \ std::source_location loc = std::source_location::current(); \ diff --git a/sample/balserver.cc b/sample/balserver.cc index 4afb60e..82acdc2 100644 --- a/sample/balserver.cc +++ b/sample/balserver.cc @@ -119,7 +119,7 @@ int main(int argc, char** argv) PRINT("got connection from %s %s:%s on " BAL_SOCKET_SPEC " (0x%" PRIxPTR ");" " now have %zu client(s)", addrinfo.get_type().c_str(), addrinfo.get_addr().c_str(), addrinfo.get_port().c_str(), - client_sock.get_descriptor(), std::bit_cast(client_sock.get()), + client_sock.get_descriptor(), bit_cast(client_sock.get()), _clients.size() + 1); _clients[client_sock.get_descriptor()] = std::move(client_sock);