blob: 072d1c764c6c449e68962c5bde74417918891be5 [file] [log] [blame]
Davide Pesavento409cc202015-09-19 14:13:16 +02001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Weiwei Liucfaa1ad2016-08-28 16:30:56 -07003 * Copyright (c) 2015-2016 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
Weiwei Liucfaa1ad2016-08-28 16:30:56 -070031#include <algorithm>
32
Davide Pesavento409cc202015-09-19 14:13:16 +020033namespace ndn {
34
35#if __cpp_lib_make_unique
36using std::make_unique;
37#else
38template<typename T, typename... Args>
39inline unique_ptr<T>
40make_unique(Args&&... args)
41{
42 return unique_ptr<T>(new T(std::forward<Args>(args)...));
43}
44#endif // __cpp_lib_make_unique
45
Davide Pesavento96b96af2015-09-19 23:00:40 +020046#ifdef NDN_CXX_HAVE_STD_TO_STRING
47using std::to_string;
48#else
49template<typename V>
50inline std::string
51to_string(const V& v)
52{
53 return boost::lexical_cast<std::string>(v);
54}
55#endif // NDN_CXX_HAVE_STD_TO_STRING
56
Weiwei Liucfaa1ad2016-08-28 16:30:56 -070057#if __cpp_lib_clamp >= 201603
58using std::clamp;
59#else
60template<typename T, typename Compare>
61constexpr const T&
62clamp(const T& v, const T& lo, const T& hi, Compare comp)
63{
64 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
65}
66
67template<typename T>
68constexpr const T&
69clamp(const T& v, const T& lo, const T& hi)
70{
71 return (v < lo) ? lo : (hi < v) ? hi : v;
72}
73#endif // __cpp_lib_clamp
74
Davide Pesavento409cc202015-09-19 14:13:16 +020075} // namespace ndn
76
77#endif // NDN_UTIL_BACKPORTS_HPP