22 #ifndef NDN_CXX_UTIL_BACKPORTS_HPP
23 #define NDN_CXX_UTIL_BACKPORTS_HPP
27 #include <boost/predef/compiler/clang.h>
28 #include <boost/predef/compiler/gcc.h>
29 #include <boost/predef/compiler/visualc.h>
31 #ifdef __has_cpp_attribute
32 # define NDN_CXX_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
34 # define NDN_CXX_HAS_CPP_ATTRIBUTE(x) 0
38 # define NDN_CXX_HAS_INCLUDE(x) __has_include(x)
40 # define NDN_CXX_HAS_INCLUDE(x) 0
47 #if (__cplusplus > 201402L) && NDN_CXX_HAS_CPP_ATTRIBUTE(fallthrough)
48 # define NDN_CXX_FALLTHROUGH [[fallthrough]]
49 #elif NDN_CXX_HAS_CPP_ATTRIBUTE(clang::fallthrough)
50 # define NDN_CXX_FALLTHROUGH [[clang::fallthrough]]
51 #elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
52 # define NDN_CXX_FALLTHROUGH [[gnu::fallthrough]]
53 #elif BOOST_COMP_GNUC >= BOOST_VERSION_NUMBER(7,0,0)
54 # define NDN_CXX_FALLTHROUGH __attribute__((fallthrough))
56 # define NDN_CXX_FALLTHROUGH ((void)0)
63 #if (__cplusplus > 201402L) && NDN_CXX_HAS_CPP_ATTRIBUTE(nodiscard)
64 # define NDN_CXX_NODISCARD [[nodiscard]]
65 #elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::warn_unused_result)
66 # define NDN_CXX_NODISCARD [[gnu::warn_unused_result]]
68 # define NDN_CXX_NODISCARD
71 #ifndef NDN_CXX_HAVE_STD_TO_STRING
72 #include <boost/lexical_cast.hpp>
81 #ifdef NDN_CXX_HAVE_STD_TO_STRING
88 return boost::lexical_cast<std::string>(val);
96 #if __cpp_lib_clamp >= 201603L
99 template<
typename T,
typename Compare>
101 clamp(
const T& v,
const T& lo,
const T& hi, Compare comp)
103 BOOST_ASSERT(!comp(hi, lo));
104 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
109 clamp(
const T& v,
const T& lo,
const T& hi)
111 BOOST_ASSERT(!(hi < lo));
112 return (v < lo) ? lo : (hi < v) ? hi : v;
120 #if __cpp_lib_to_underlying >= 202102L
128 static_assert(std::is_enum<T>::value,
"");
129 return static_cast<std::underlying_type_t<T>
>(val);
138 # define NDN_CXX_UNREACHABLE BOOST_ASSERT(false)
139 #elif __cpp_lib_unreachable >= 202202L
140 # define NDN_CXX_UNREACHABLE std::unreachable()
142 # define NDN_CXX_UNREACHABLE ::ndn::detail::unreachable()
144 [[noreturn]]
inline void
147 #if BOOST_COMP_GNUC || BOOST_COMP_CLANG
148 __builtin_unreachable();
149 #elif BOOST_COMP_MSVC
#define NDN_CXX_NODISCARD
Common includes and macros used throughout the library.
std::string to_string(const errinfo_stacktrace &x)
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)