1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
16 #include <type_traits>
18 #ifndef CEPH_COMMON_BACKPORT14_H
19 #define CEPH_COMMON_BACKPORT14_H
21 // Library code from C++14 that can be implemented in C++11.
25 using remove_extent_t = typename std::remove_extent<T>::type;
27 using remove_reference_t = typename std::remove_reference<T>::type;
29 using result_of_t = typename std::result_of<T>::type;
31 using decay_t = typename std::decay<T>::type;
33 namespace _backport14 {
36 using datum = std::unique_ptr<T>;
40 struct uniquity<T[]> {
41 using array = std::unique_ptr<T[]>;
44 template<typename T, std::size_t N>
45 struct uniquity<T[N]> {
46 using verboten = void;
49 template<typename T, typename... Args>
50 inline typename uniquity<T>::datum make_unique(Args&&... args) {
51 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
55 inline typename uniquity<T>::array make_unique(std::size_t n) {
56 return std::unique_ptr<T>(new remove_extent_t<T>[n]());
59 template<typename T, class... Args>
60 typename uniquity<T>::verboten
61 make_unique(Args&&...) = delete;
63 // The constexpr variant of std::max().
65 constexpr const T& max(const T& a, const T& b) {
68 } // namespace _backport14
70 namespace _backport17 {
72 constexpr auto size(const C& c) -> decltype(c.size()) {
76 template <typename T, std::size_t N>
77 constexpr std::size_t size(const T (&array)[N]) noexcept {
81 /// http://en.cppreference.com/w/cpp/utility/functional/not_fn
82 // this implementation uses c++14's result_of_t (above) instead of the c++17
83 // invoke_result_t, and so may not behave correctly when SFINAE is required
86 using DecayF = decay_t<F>;
89 explicit not_fn_result(F&& f) : fn(std::forward<F>(f)) {}
90 not_fn_result(not_fn_result&& f) = default;
91 not_fn_result(const not_fn_result& f) = default;
93 template<class... Args>
94 auto operator()(Args&&... args) &
95 -> decltype(!std::declval<result_of_t<DecayF&(Args...)>>()) {
96 return !fn(std::forward<Args>(args)...);
98 template<class... Args>
99 auto operator()(Args&&... args) const&
100 -> decltype(!std::declval<result_of_t<DecayF const&(Args...)>>()) {
101 return !fn(std::forward<Args>(args)...);
104 template<class... Args>
105 auto operator()(Args&&... args) &&
106 -> decltype(!std::declval<result_of_t<DecayF(Args...)>>()) {
107 return !std::move(fn)(std::forward<Args>(args)...);
109 template<class... Args>
110 auto operator()(Args&&... args) const&&
111 -> decltype(!std::declval<result_of_t<DecayF const(Args...)>>()) {
112 return !std::move(fn)(std::forward<Args>(args)...);
116 template <typename F>
117 not_fn_result<F> not_fn(F&& fn) {
118 return not_fn_result<F>(std::forward<F>(fn));
121 } // namespace _backport17
122 using _backport14::make_unique;
123 using _backport17::size;
124 using _backport14::max;
125 using _backport17::not_fn;
128 #endif // CEPH_COMMON_BACKPORT14_H