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
72 # define NDN_CXX_UNREACHABLE BOOST_ASSERT(false)
73 #elif BOOST_COMP_GNUC || BOOST_COMP_CLANG
74 # define NDN_CXX_UNREACHABLE __builtin_unreachable()
76 # define NDN_CXX_UNREACHABLE __assume(0)
79 # define NDN_CXX_UNREACHABLE std::abort()
82 #ifndef NDN_CXX_HAVE_STD_TO_STRING
83 #include <boost/lexical_cast.hpp>
92 #ifdef NDN_CXX_HAVE_STD_TO_STRING
99 return boost::lexical_cast<std::string>(val);
107 #if __cpp_lib_clamp >= 201603L
110 template<
typename T,
typename Compare>
112 clamp(
const T& v,
const T& lo,
const T& hi, Compare comp)
114 BOOST_ASSERT(!comp(hi, lo));
115 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
120 clamp(
const T& v,
const T& lo,
const T& hi)
122 BOOST_ASSERT(!(hi < lo));
123 return (v < lo) ? lo : (hi < v) ? hi : v;
131 #if __cpp_lib_to_underlying >= 202102L
139 static_assert(std::is_enum<T>::value,
"");
140 return static_cast<std::underlying_type_t<T>
>(val);
#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)