NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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) 2015-2017 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 
25 #include "../common.hpp"
26 
27 #ifndef NDN_CXX_HAVE_STD_TO_STRING
28 #include <boost/lexical_cast.hpp>
29 #endif
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 #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
52 #define NDN_CXX_DEPRECATED NDN_CXX_DEPRECATED_MSG("")
53 
54 #if (__cplusplus > 201402L) && NDN_CXX_HAS_CPP_ATTRIBUTE(fallthrough)
55 # define NDN_CXX_FALLTHROUGH [[fallthrough]]
56 #elif NDN_CXX_HAS_CPP_ATTRIBUTE(clang::fallthrough)
57 # define NDN_CXX_FALLTHROUGH [[clang::fallthrough]]
58 #elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
59 # define NDN_CXX_FALLTHROUGH [[gnu::fallthrough]]
60 #elif defined(__GNUC__) && (__GNUC__ >= 7)
61 # define NDN_CXX_FALLTHROUGH __attribute__((fallthrough))
62 #else
63 # define NDN_CXX_FALLTHROUGH ((void)0)
64 #endif
65 
66 namespace ndn {
67 
68 #if __cpp_lib_make_unique >= 201304
69 using std::make_unique;
70 #else
71 template<typename T, typename ...Args>
72 inline unique_ptr<T>
73 make_unique(Args&&... args)
74 {
75  return unique_ptr<T>(new T(std::forward<Args>(args)...));
76 }
77 #endif // __cpp_lib_make_unique
78 
79 #ifdef NDN_CXX_HAVE_STD_TO_STRING
80 using std::to_string;
81 #else
82 template<typename V>
83 inline std::string
84 to_string(const V& v)
85 {
86  return boost::lexical_cast<std::string>(v);
87 }
88 #endif // NDN_CXX_HAVE_STD_TO_STRING
89 
90 #if __cpp_lib_clamp >= 201603
91 using std::clamp;
92 #else
93 template<typename T, typename Compare>
94 constexpr const T&
95 clamp(const T& v, const T& lo, const T& hi, Compare comp)
96 {
97  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
98 }
99 
100 template<typename T>
101 constexpr const T&
102 clamp(const T& v, const T& lo, const T& hi)
103 {
104  return (v < lo) ? lo : (hi < v) ? hi : v;
105 }
106 #endif // __cpp_lib_clamp
107 
108 } // namespace ndn
109 
110 #include "backports-optional.hpp"
112 
113 #endif // NDN_UTIL_BACKPORTS_HPP
Copyright (c) 2011-2015 Regents of the University of California.
unique_ptr< T > make_unique(Args &&...args)
Definition: backports.hpp:73
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: backports.hpp:95
Backport of ostream_joiner from the Library Fundamentals v2 TS.
C++17 std::optional backport implemented using boost::optional.
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: backports.hpp:102
std::string to_string(const V &v)
Definition: backports.hpp:84