Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / sigc++ / tuple-utils / tuple_end.h
1 /* Copyright (C) 2016 Murray Cumming
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  *  (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/
15  */
16
17 #ifndef SIGC_TUPLE_UTILS_TUPLE_END_H
18 #define SIGC_TUPLE_UTILS_TUPLE_END_H
19
20 #include <sigc++/tuple-utils/tuple_cdr.h>
21
22 namespace sigc {
23
24 namespace internal {
25
26 namespace detail {
27
28 template <typename T, std::size_t remove_from_start>
29 struct tuple_end_impl {
30   constexpr
31   static
32   decltype(auto) // typename tuple_type_end<T, size - remove_from_start>::type
33   tuple_end(T&& t) {
34     static_assert(remove_from_start > 0, "remove_from_start must be more than zero.");
35
36     using cdr = typename tuple_type_cdr<std::decay_t<T>>::type;
37     return tuple_end_impl<cdr, remove_from_start - 1>::tuple_end(
38       tuple_cdr(std::forward<T>(t)));
39   }
40 };
41
42 template <typename T>
43 struct tuple_end_impl<T, 1> {
44   constexpr
45   static
46   decltype(auto)
47   tuple_end(T&& t) {
48     return tuple_cdr(std::forward<T>(t));
49   }
50 };
51
52 template <typename T>
53 struct tuple_end_impl<T, 0> {
54   constexpr
55   static
56   decltype(auto)
57   tuple_end(T&& t) {
58     return std::forward<T>(t);
59   }
60 };
61
62 } // detail namespace
63
64 /**
65  * Get the tuple with the last @a len items of the original.
66  */
67 template <std::size_t len, typename T>
68 constexpr
69 decltype(auto) // typename tuple_type_end<T, len>::type
70   tuple_end(T&& t) {
71   //We use std::decay_t<> because tuple_size is not defined for references.
72   constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
73   static_assert(len <= size, "The tuple size must be less than or equal to the length.");
74   constexpr auto size_start = size - len;
75   return detail::tuple_end_impl<T, size_start>::tuple_end(std::forward<T>(t));
76 }
77
78 } // namespace internal
79
80 } // namespace sigc
81
82 #endif //SIGC_TUPLE_UTILS_TUPLE_END_H