Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1c597a1 | 2017-10-06 15:34:24 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | b849f61 | 2018-04-01 23:52:54 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 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 | |
| 25 | #include "../common.hpp" |
| 26 | |
Davide Pesavento | 96b96af | 2015-09-19 23:00:40 +0200 | [diff] [blame] | 27 | #ifndef NDN_CXX_HAVE_STD_TO_STRING |
| 28 | #include <boost/lexical_cast.hpp> |
| 29 | #endif |
| 30 | |
Davide Pesavento | 1c597a1 | 2017-10-06 15:34:24 -0400 | [diff] [blame] | 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 | |
Davide Pesavento | a52eb1f | 2017-10-06 20:37:43 -0400 | [diff] [blame] | 43 | #if (__cplusplus >= 201402L) && NDN_CXX_HAS_CPP_ATTRIBUTE(deprecated) |
| 44 | # define NDN_CXX_DEPRECATED_MSG(msg) [[deprecated(msg)]] |
| 45 | #elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::deprecated) |
| 46 | # define NDN_CXX_DEPRECATED_MSG(msg) [[gnu::deprecated(msg)]] |
| 47 | #elif defined(__GNUC__) |
| 48 | # define NDN_CXX_DEPRECATED_MSG(msg) __attribute__((deprecated(msg))) |
| 49 | #else |
| 50 | # define NDN_CXX_DEPRECATED_MSG(msg) |
| 51 | #endif |
Junxiao Shi | b849f61 | 2018-04-01 23:52:54 +0000 | [diff] [blame^] | 52 | |
| 53 | /** \brief Mark a type, variable, or function as deprecated. |
| 54 | * |
| 55 | * To deprecate a type \c DeprecatedType: |
| 56 | * \code |
| 57 | * typedef ModernType DeprecatedType NDN_CXX_DEPRECATED; |
| 58 | * \endcode |
| 59 | * This macro can only be applied to a typedef, not directly on a class. |
| 60 | * |
| 61 | * To deprecate a variable or class member \c deprecatedVar: |
| 62 | * \code |
| 63 | * int deprecatedVar NDN_CXX_DEPRECATED; |
| 64 | * \endcode |
| 65 | * |
| 66 | * To deprecate a function \c deprecatedFunc: |
| 67 | * \code |
| 68 | * NDN_CXX_DEPRECATED |
| 69 | * void |
| 70 | * deprecatedFunc(int a, NamedEnum b) |
| 71 | * { |
| 72 | * } |
| 73 | * \endcode |
| 74 | */ |
Davide Pesavento | a52eb1f | 2017-10-06 20:37:43 -0400 | [diff] [blame] | 75 | #define NDN_CXX_DEPRECATED NDN_CXX_DEPRECATED_MSG("") |
| 76 | |
| 77 | #if (__cplusplus > 201402L) && NDN_CXX_HAS_CPP_ATTRIBUTE(fallthrough) |
Davide Pesavento | 1c597a1 | 2017-10-06 15:34:24 -0400 | [diff] [blame] | 78 | # define NDN_CXX_FALLTHROUGH [[fallthrough]] |
| 79 | #elif NDN_CXX_HAS_CPP_ATTRIBUTE(clang::fallthrough) |
| 80 | # define NDN_CXX_FALLTHROUGH [[clang::fallthrough]] |
| 81 | #elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::fallthrough) |
| 82 | # define NDN_CXX_FALLTHROUGH [[gnu::fallthrough]] |
| 83 | #elif defined(__GNUC__) && (__GNUC__ >= 7) |
| 84 | # define NDN_CXX_FALLTHROUGH __attribute__((fallthrough)) |
| 85 | #else |
| 86 | # define NDN_CXX_FALLTHROUGH ((void)0) |
| 87 | #endif |
| 88 | |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 89 | namespace ndn { |
| 90 | |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 91 | #if __cpp_lib_make_unique >= 201304 |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 92 | using std::make_unique; |
| 93 | #else |
Junxiao Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 94 | template<typename T, typename ...Args> |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 95 | inline unique_ptr<T> |
| 96 | make_unique(Args&&... args) |
| 97 | { |
| 98 | return unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 99 | } |
| 100 | #endif // __cpp_lib_make_unique |
| 101 | |
Davide Pesavento | 96b96af | 2015-09-19 23:00:40 +0200 | [diff] [blame] | 102 | #ifdef NDN_CXX_HAVE_STD_TO_STRING |
| 103 | using std::to_string; |
| 104 | #else |
| 105 | template<typename V> |
| 106 | inline std::string |
| 107 | to_string(const V& v) |
| 108 | { |
| 109 | return boost::lexical_cast<std::string>(v); |
| 110 | } |
| 111 | #endif // NDN_CXX_HAVE_STD_TO_STRING |
| 112 | |
Weiwei Liu | cfaa1ad | 2016-08-28 16:30:56 -0700 | [diff] [blame] | 113 | #if __cpp_lib_clamp >= 201603 |
| 114 | using std::clamp; |
| 115 | #else |
| 116 | template<typename T, typename Compare> |
| 117 | constexpr const T& |
| 118 | clamp(const T& v, const T& lo, const T& hi, Compare comp) |
| 119 | { |
| 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 | return (v < lo) ? lo : (hi < v) ? hi : v; |
| 128 | } |
| 129 | #endif // __cpp_lib_clamp |
| 130 | |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 131 | } // namespace ndn |
| 132 | |
Junxiao Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 133 | #include "backports-optional.hpp" |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 134 | #include "backports-ostream-joiner.hpp" |
Junxiao Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 135 | |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 136 | #endif // NDN_UTIL_BACKPORTS_HPP |