NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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 
86 
87 #ifndef NDN_CXX_HAVE_STD_TO_STRING
88 #include <boost/lexical_cast.hpp>
89 #endif
90 
91 namespace ndn {
92 
93 //
94 // https://redmine.named-data.net/issues/2743
95 // std::to_string() (C++11)
96 //
97 #ifdef NDN_CXX_HAVE_STD_TO_STRING
98 using std::to_string;
99 #else
100 template<typename T>
101 inline std::string
102 to_string(const T& val)
103 {
104  return boost::lexical_cast<std::string>(val);
105 }
106 #endif // NDN_CXX_HAVE_STD_TO_STRING
107 
108 //
109 // https://wg21.link/P0025
110 // std::clamp() (C++17)
111 //
112 #if __cpp_lib_clamp >= 201603L
113 using std::clamp;
114 #else
115 template<typename T, typename Compare>
116 constexpr const T&
117 clamp(const T& v, const T& lo, const T& hi, Compare comp)
118 {
119  BOOST_ASSERT(!comp(hi, lo));
120  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
121 }
122 
123 template<typename T>
124 constexpr const T&
125 clamp(const T& v, const T& lo, const T& hi)
126 {
127  BOOST_ASSERT(!(hi < lo));
128  return (v < lo) ? lo : (hi < v) ? hi : v;
129 }
130 #endif // __cpp_lib_clamp
131 
132 //
133 // https://wg21.link/P1682
134 // std::to_underlying() (approved for LWG as of July 2019)
135 //
136 #if __cpp_lib_to_underlying >= 202002L
137 using std::to_underlying;
138 #else
139 template<typename T>
140 constexpr std::underlying_type_t<T>
141 to_underlying(T val) noexcept
142 {
143  static_assert(std::is_enum<T>::value, "");
144  return static_cast<std::underlying_type_t<T>>(val);
145 }
146 #endif // __cpp_lib_to_underlying
147 
148 using ::nonstd::any;
150 using ::nonstd::bad_any_cast;
151 using ::nonstd::make_any;
152 
153 using ::nonstd::optional;
154 using ::nonstd::bad_optional_access;
156 using ::nonstd::nullopt_t;
158 using ::nonstd::in_place_t;
160 
161 using ::nonstd::variant;
162 using ::nonstd::bad_variant_access;
163 using ::nonstd::monostate;
169 
170 } // namespace ndn
171 
172 #endif // NDN_UTIL_BACKPORTS_HPP
common.hpp
Common includes and macros used throughout the library.
nonstd::any_lite::any_cast
any_nodiscard ValueType any_cast(any const &operand)
Definition: any.hpp:617
nonstd::variants::get
R & get(variant< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > &v, nonstd_lite_in_place_type_t(R)=nonstd_lite_in_place_type(R))
Definition: variant.hpp:1753
ostream-joiner.hpp
Backport of ostream_joiner from the Library Fundamentals v2 TS.
nonstd::variants::visit
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2194
nonstd::variants::variant_npos
static const std::size_t variant_npos
Definition: variant.hpp:1166
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
ndn::clamp
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: backports.hpp:117
nonstd::variants::holds_alternative
bool holds_alternative(variant< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > const &v) variant_noexcept
Definition: variant.hpp:1747
nonstd::variants::get_if
std11::add_pointer< T >::type get_if(variant< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > *pv, nonstd_lite_in_place_type_t(T)=nonstd_lite_in_place_type(T))
Definition: variant.hpp:1842
ndn::clamp
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: backports.hpp:125
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
variant.hpp
nonstd::optional_lite::make_optional
optional< T > make_optional(T const &value)
Definition: optional.hpp:1650
any.hpp
ndn::to_underlying
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:141
nonstd::in_place
in_place_t in_place(detail::in_place_type_tag< T >=detail::in_place_type_tag< T >())
Definition: any.hpp:124
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
optional.hpp