backports.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_UTIL_BACKPORTS_HPP
23 #define NDN_CXX_UTIL_BACKPORTS_HPP
24 
26 
27 #include <boost/predef/compiler/clang.h>
28 #include <boost/predef/compiler/gcc.h>
29 #include <boost/predef/compiler/visualc.h>
30 
31 #ifdef __has_cpp_attribute
32 # define NDN_CXX_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
33 #else
34 # define NDN_CXX_HAS_CPP_ATTRIBUTE(x) 0
35 #endif
36 
37 #ifdef __has_include
38 # define NDN_CXX_HAS_INCLUDE(x) __has_include(x)
39 #else
40 # define NDN_CXX_HAS_INCLUDE(x) 0
41 #endif
42 
43 //
44 // https://wg21.link/P0188
45 // [[fallthrough]] attribute (C++17)
46 //
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))
55 #else
56 # define NDN_CXX_FALLTHROUGH ((void)0)
57 #endif
58 
59 //
60 // https://wg21.link/P0189
61 // [[nodiscard]] attribute (C++17)
62 //
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]]
67 #else
68 # define NDN_CXX_NODISCARD
69 #endif
70 
71 #ifndef NDN_CXX_HAVE_STD_TO_STRING
72 #include <boost/lexical_cast.hpp>
73 #endif
74 
75 namespace ndn {
76 
77 //
78 // https://redmine.named-data.net/issues/2743
79 // std::to_string() (C++11)
80 //
81 #ifdef NDN_CXX_HAVE_STD_TO_STRING
82 using std::to_string;
83 #else
84 template<typename T>
85 std::string
86 to_string(const T& val)
87 {
88  return boost::lexical_cast<std::string>(val);
89 }
90 #endif // NDN_CXX_HAVE_STD_TO_STRING
91 
92 //
93 // https://wg21.link/P0025
94 // std::clamp() (C++17)
95 //
96 #if __cpp_lib_clamp >= 201603L
97 using std::clamp;
98 #else
99 template<typename T, typename Compare>
100 constexpr const T&
101 clamp(const T& v, const T& lo, const T& hi, Compare comp)
102 {
103  BOOST_ASSERT(!comp(hi, lo));
104  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
105 }
106 
107 template<typename T>
108 constexpr const T&
109 clamp(const T& v, const T& lo, const T& hi)
110 {
111  BOOST_ASSERT(!(hi < lo));
112  return (v < lo) ? lo : (hi < v) ? hi : v;
113 }
114 #endif // __cpp_lib_clamp
115 
116 //
117 // https://wg21.link/P1682
118 // std::to_underlying() (C++23)
119 //
120 #if __cpp_lib_to_underlying >= 202102L
121 using std::to_underlying;
122 #else
123 template<typename T>
124 NDN_CXX_NODISCARD constexpr std::underlying_type_t<T>
125 to_underlying(T val) noexcept
126 {
127  // instantiating underlying_type with a non-enum type is UB before C++20
128  static_assert(std::is_enum<T>::value, "");
129  return static_cast<std::underlying_type_t<T>>(val);
130 }
131 #endif // __cpp_lib_to_underlying
132 
133 //
134 // https://wg21.link/P0627
135 // std::unreachable() (C++23)
136 //
137 #ifndef NDEBUG
138 # define NDN_CXX_UNREACHABLE BOOST_ASSERT(false)
139 #elif __cpp_lib_unreachable >= 202202L
140 # define NDN_CXX_UNREACHABLE std::unreachable()
141 #else
142 # define NDN_CXX_UNREACHABLE ::ndn::detail::unreachable()
143 namespace detail {
144 [[noreturn]] inline void
145 unreachable()
146 {
147 #if BOOST_COMP_GNUC || BOOST_COMP_CLANG
148  __builtin_unreachable();
149 #elif BOOST_COMP_MSVC
150  __assume(0);
151 #endif
152 } // unreachable()
153 } // namespace detail
154 #endif
155 
156 } // namespace ndn
157 
158 #endif // NDN_CXX_UTIL_BACKPORTS_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Common includes and macros used throughout the library.
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:31
Definition: data.cpp:25
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: backports.hpp:109
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:125
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: backports.hpp:101