blob: df1d77580a51e7986357e3d45e8539f677261ef6 [file] [log] [blame]
Davide Pesavento409cc202015-09-19 14:13:16 +02001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento1c597a12017-10-06 15:34:24 -04002/*
Davide Pesaventocf415762017-02-25 23:46:47 -05003 * Copyright (c) 2015-2017 Regents of the University of California.
Davide Pesavento409cc202015-09-19 14:13:16 +02004 *
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 Pesavento96b96af2015-09-19 23:00:40 +020027#ifndef NDN_CXX_HAVE_STD_TO_STRING
28#include <boost/lexical_cast.hpp>
29#endif
30
Davide Pesavento1c597a12017-10-06 15:34:24 -040031#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#if NDN_CXX_HAS_CPP_ATTRIBUTE(fallthrough)
44# define NDN_CXX_FALLTHROUGH [[fallthrough]]
45#elif NDN_CXX_HAS_CPP_ATTRIBUTE(clang::fallthrough)
46# define NDN_CXX_FALLTHROUGH [[clang::fallthrough]]
47#elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
48# define NDN_CXX_FALLTHROUGH [[gnu::fallthrough]]
49#elif defined(__GNUC__) && (__GNUC__ >= 7)
50# define NDN_CXX_FALLTHROUGH __attribute__((fallthrough))
51#else
52# define NDN_CXX_FALLTHROUGH ((void)0)
53#endif
54
Davide Pesavento409cc202015-09-19 14:13:16 +020055namespace ndn {
56
Davide Pesaventocf415762017-02-25 23:46:47 -050057#if __cpp_lib_make_unique >= 201304
Davide Pesavento409cc202015-09-19 14:13:16 +020058using std::make_unique;
59#else
Junxiao Shi1aecae22016-08-30 11:23:59 +000060template<typename T, typename ...Args>
Davide Pesavento409cc202015-09-19 14:13:16 +020061inline unique_ptr<T>
62make_unique(Args&&... args)
63{
64 return unique_ptr<T>(new T(std::forward<Args>(args)...));
65}
66#endif // __cpp_lib_make_unique
67
Davide Pesavento96b96af2015-09-19 23:00:40 +020068#ifdef NDN_CXX_HAVE_STD_TO_STRING
69using std::to_string;
70#else
71template<typename V>
72inline std::string
73to_string(const V& v)
74{
75 return boost::lexical_cast<std::string>(v);
76}
77#endif // NDN_CXX_HAVE_STD_TO_STRING
78
Weiwei Liucfaa1ad2016-08-28 16:30:56 -070079#if __cpp_lib_clamp >= 201603
80using std::clamp;
81#else
82template<typename T, typename Compare>
83constexpr const T&
84clamp(const T& v, const T& lo, const T& hi, Compare comp)
85{
86 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
87}
88
89template<typename T>
90constexpr const T&
91clamp(const T& v, const T& lo, const T& hi)
92{
93 return (v < lo) ? lo : (hi < v) ? hi : v;
94}
95#endif // __cpp_lib_clamp
96
Davide Pesavento409cc202015-09-19 14:13:16 +020097} // namespace ndn
98
Junxiao Shi1aecae22016-08-30 11:23:59 +000099#include "backports-optional.hpp"
Davide Pesaventocf415762017-02-25 23:46:47 -0500100#include "backports-ostream-joiner.hpp"
Junxiao Shi1aecae22016-08-30 11:23:59 +0000101
Davide Pesavento409cc202015-09-19 14:13:16 +0200102#endif // NDN_UTIL_BACKPORTS_HPP