]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/backport14.h
d56e24c3e512f821f32f3fda7edaf2210371a0dd
[ceph.git] / ceph / src / common / backport14.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15 #include <memory>
16 #include <type_traits>
17
18 #ifndef CEPH_COMMON_BACKPORT14_H
19 #define CEPH_COMMON_BACKPORT14_H
20
21 // Library code from C++14 that can be implemented in C++11.
22
23 namespace ceph {
24 template<typename T>
25 using remove_extent_t = typename std::remove_extent<T>::type;
26 template<typename T>
27 using remove_reference_t = typename std::remove_reference<T>::type;
28 template<typename T>
29 using result_of_t = typename std::result_of<T>::type;
30
31 namespace _backport14 {
32 template<typename T>
33 struct uniquity {
34 using datum = std::unique_ptr<T>;
35 };
36
37 template<typename T>
38 struct uniquity<T[]> {
39 using array = std::unique_ptr<T[]>;
40 };
41
42 template<typename T, std::size_t N>
43 struct uniquity<T[N]> {
44 using verboten = void;
45 };
46
47 template<typename T, typename... Args>
48 inline typename uniquity<T>::datum make_unique(Args&&... args) {
49 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
50 }
51
52 template<typename T>
53 inline typename uniquity<T>::array make_unique(std::size_t n) {
54 return std::unique_ptr<T>(new remove_extent_t<T>[n]());
55 }
56
57 template<typename T, class... Args>
58 typename uniquity<T>::verboten
59 make_unique(Args&&...) = delete;
60
61 // The constexpr variant of std::max().
62 template<class T>
63 constexpr const T& max(const T& a, const T& b) {
64 return a < b ? b : a;
65 }
66 } // namespace _backport14
67
68 namespace _backport17 {
69 template <class C>
70 constexpr auto size(const C& c) -> decltype(c.size()) {
71 return c.size();
72 }
73
74 template <typename T, std::size_t N>
75 constexpr std::size_t size(const T (&array)[N]) noexcept {
76 return N;
77 }
78 } // namespace _backport17
79 using _backport14::make_unique;
80 using _backport17::size;
81 using _backport14::max;
82 } // namespace ceph
83
84 #endif // CEPH_COMMON_BACKPORT14_H