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-2020 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_UTIL_BACKPORTS_HPP
23 #define NDN_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 // http://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 // http://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 NDEBUG
72 # define NDN_CXX_UNREACHABLE BOOST_ASSERT(false)
73 #elif BOOST_COMP_GNUC || BOOST_COMP_CLANG
74 # define NDN_CXX_UNREACHABLE __builtin_unreachable()
75 #elif BOOST_COMP_MSVC
76 # define NDN_CXX_UNREACHABLE __assume(0)
77 #else
78 # include <cstdlib>
79 # define NDN_CXX_UNREACHABLE std::abort()
80 #endif
81 
82 #include "ndn-cxx/util/nonstd/any.hpp"
83 #include "ndn-cxx/util/nonstd/optional.hpp"
84 #include "ndn-cxx/util/nonstd/variant.hpp"
85 
86 #ifndef NDN_CXX_HAVE_STD_TO_STRING
87 #include <boost/lexical_cast.hpp>
88 #endif
89 
90 namespace ndn {
91 
92 //
93 // https://redmine.named-data.net/issues/2743
94 // std::to_string() (C++11)
95 //
96 #ifdef NDN_CXX_HAVE_STD_TO_STRING
97 using std::to_string;
98 #else
99 template<typename T>
100 inline std::string
101 to_string(const T& val)
102 {
103  return boost::lexical_cast<std::string>(val);
104 }
105 #endif // NDN_CXX_HAVE_STD_TO_STRING
106 
107 //
108 // https://wg21.link/P0025
109 // std::clamp() (C++17)
110 //
111 #if __cpp_lib_clamp >= 201603L
112 using std::clamp;
113 #else
114 template<typename T, typename Compare>
115 constexpr const T&
116 clamp(const T& v, const T& lo, const T& hi, Compare comp)
117 {
118  BOOST_ASSERT(!comp(hi, lo));
119  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
120 }
121 
122 template<typename T>
123 constexpr const T&
124 clamp(const T& v, const T& lo, const T& hi)
125 {
126  BOOST_ASSERT(!(hi < lo));
127  return (v < lo) ? lo : (hi < v) ? hi : v;
128 }
129 #endif // __cpp_lib_clamp
130 
131 //
132 // https://wg21.link/P1682
133 // std::to_underlying() (approved for LWG as of July 2019)
134 //
135 #if __cpp_lib_to_underlying >= 202002L
136 using std::to_underlying;
137 #else
138 template<typename T>
139 constexpr std::underlying_type_t<T>
140 to_underlying(T val) noexcept
141 {
142  static_assert(std::is_enum<T>::value, "");
143  return static_cast<std::underlying_type_t<T>>(val);
144 }
145 #endif // __cpp_lib_to_underlying
146 
147 using ::nonstd::any;
148 using ::nonstd::any_cast;
149 using ::nonstd::bad_any_cast;
150 using ::nonstd::make_any;
151 
152 using ::nonstd::optional;
153 using ::nonstd::bad_optional_access;
154 using ::nonstd::nullopt;
155 using ::nonstd::nullopt_t;
156 using ::nonstd::in_place;
157 using ::nonstd::in_place_t;
158 using ::nonstd::make_optional;
159 
160 using ::nonstd::variant;
161 using ::nonstd::bad_variant_access;
162 using ::nonstd::monostate;
163 using ::nonstd::variant_npos;
164 using ::nonstd::get;
165 using ::nonstd::get_if;
166 using ::nonstd::holds_alternative;
167 using ::nonstd::visit;
168 
169 } // namespace ndn
170 
171 #endif // NDN_UTIL_BACKPORTS_HPP
Definition: data.cpp:26
std::string to_string(const T &val)
Definition: backports.hpp:101
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: backports.hpp:116
Common includes and macros used throughout the library.
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: backports.hpp:124
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:140