blob: bb0c46201d230685dde1fb7cc443e92bd2f4262d [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/*
Junxiao Shib849f612018-04-01 23:52:54 +00003 * Copyright (c) 2013-2018 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 Pesavento1c597a12017-10-06 15:34:24 -040027#ifdef __has_cpp_attribute
28# define NDN_CXX_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
29#else
30# define NDN_CXX_HAS_CPP_ATTRIBUTE(x) 0
31#endif
32
33#ifdef __has_include
34# define NDN_CXX_HAS_INCLUDE(x) __has_include(x)
35#else
36# define NDN_CXX_HAS_INCLUDE(x) 0
37#endif
38
Davide Pesaventoa52eb1f2017-10-06 20:37:43 -040039#if (__cplusplus > 201402L) && NDN_CXX_HAS_CPP_ATTRIBUTE(fallthrough)
Davide Pesavento1c597a12017-10-06 15:34:24 -040040# define NDN_CXX_FALLTHROUGH [[fallthrough]]
41#elif NDN_CXX_HAS_CPP_ATTRIBUTE(clang::fallthrough)
42# define NDN_CXX_FALLTHROUGH [[clang::fallthrough]]
43#elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
44# define NDN_CXX_FALLTHROUGH [[gnu::fallthrough]]
45#elif defined(__GNUC__) && (__GNUC__ >= 7)
46# define NDN_CXX_FALLTHROUGH __attribute__((fallthrough))
47#else
48# define NDN_CXX_FALLTHROUGH ((void)0)
49#endif
50
Davide Pesavento1fd00242018-05-20 00:11:01 -040051#include "backports-ostream-joiner.hpp"
Davide Pesavento17b61b42018-11-21 23:10:48 -050052#include "nonstd/any.hpp"
Davide Pesavento1fd00242018-05-20 00:11:01 -040053#include "nonstd/optional.hpp"
Davide Pesavento17b61b42018-11-21 23:10:48 -050054#include "nonstd/variant.hpp"
Davide Pesavento409cc202015-09-19 14:13:16 +020055
Davide Pesavento1fd00242018-05-20 00:11:01 -040056#ifndef NDN_CXX_HAVE_STD_TO_STRING
57#include <boost/lexical_cast.hpp>
58#endif
59
60namespace ndn {
Davide Pesavento409cc202015-09-19 14:13:16 +020061
Davide Pesavento96b96af2015-09-19 23:00:40 +020062#ifdef NDN_CXX_HAVE_STD_TO_STRING
63using std::to_string;
64#else
65template<typename V>
66inline std::string
67to_string(const V& v)
68{
69 return boost::lexical_cast<std::string>(v);
70}
71#endif // NDN_CXX_HAVE_STD_TO_STRING
72
Weiwei Liucfaa1ad2016-08-28 16:30:56 -070073#if __cpp_lib_clamp >= 201603
74using std::clamp;
75#else
76template<typename T, typename Compare>
77constexpr const T&
78clamp(const T& v, const T& lo, const T& hi, Compare comp)
79{
80 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
81}
82
83template<typename T>
84constexpr const T&
85clamp(const T& v, const T& lo, const T& hi)
86{
87 return (v < lo) ? lo : (hi < v) ? hi : v;
88}
89#endif // __cpp_lib_clamp
90
Davide Pesavento17b61b42018-11-21 23:10:48 -050091using ::nonstd::any;
92using ::nonstd::any_cast;
93using ::nonstd::bad_any_cast;
94using ::nonstd::make_any;
95
Davide Pesavento1fd00242018-05-20 00:11:01 -040096using ::nonstd::optional;
97using ::nonstd::bad_optional_access;
98using ::nonstd::nullopt;
99using ::nonstd::nullopt_t;
100using ::nonstd::in_place;
101using ::nonstd::in_place_t;
102using ::nonstd::make_optional;
Davide Pesavento409cc202015-09-19 14:13:16 +0200103
Davide Pesavento17b61b42018-11-21 23:10:48 -0500104using ::nonstd::variant;
105using ::nonstd::bad_variant_access;
106using ::nonstd::monostate;
107using ::nonstd::visit;
108using ::nonstd::holds_alternative;
109using ::nonstd::get_if;
110using ::nonstd::variant_npos;
111
Davide Pesavento1fd00242018-05-20 00:11:01 -0400112} // namespace ndn
Junxiao Shi1aecae22016-08-30 11:23:59 +0000113
Davide Pesavento409cc202015-09-19 14:13:16 +0200114#endif // NDN_UTIL_BACKPORTS_HPP