allow command to accept "--" separator
[folly.git] / folly / experimental / TupleOps.h
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <limits>
20 #include <tuple>
21 #include <type_traits>
22
23 // tupleRange<start, n>(tuple): select n elements starting at index start
24 //    in the given tuple
25 // tupleRange<start>(tuple): select all elements starting at index start
26 //    until the end of the given tuple
27 // tuplePrepend(x, tuple): return a tuple obtained by prepending x to the
28 //    given tuple.
29 //
30 // In Lisp lingo, std::get<0> is car, tupleRange<1> is cdr, and tuplePrepend
31 // is cons.
32
33 namespace folly {
34
35 // TemplateSeq<T, ...> is a type parametrized by sizeof...(Xs) values of type
36 // T. Used to destructure the values into a template parameter pack;
37 // see the example in TupleSelect, below.
38 template <class T, T... xs>
39 struct TemplateSeq {
40   template <T x>
41   using Prepend = TemplateSeq<T, x, xs...>;
42 };
43
44 // TemplateRange<T, start, n>::type is
45 // TemplateSeq<T, start+1, start+2, ..., start+n-1>
46 template <class T, T start, T n, class Enable=void> struct TemplateRange;
47
48 template <class T, T start, T n>
49 struct TemplateRange<
50   T, start, n,
51   typename std::enable_if<(n > 0)>::type> {
52   using type =
53     typename TemplateRange<T, start+1, n-1>::type::template Prepend<start>;
54 };
55
56 template <class T, T start, T n>
57 struct TemplateRange<
58   T, start, n,
59   typename std::enable_if<(n <= 0)>::type> {
60   using type = TemplateSeq<T>;
61 };
62
63 // Similar to TemplateRange, given a tuple T,
64 // TemplateTupleRange<T, start, n>::type is
65 // TemplateSeq<size_t, start, start+1, ..., start+k-1>
66 // where k = min(tuple_size<T>::value - start, n)
67 // (that is, it's a TemplateSeq of at most n elements, but won't extend
68 // past the end of the given tuple)
69 template <
70     class T,
71     std::size_t start = 0,
72     std::size_t n = std::numeric_limits<std::size_t>::max(),
73     std::size_t size =
74         std::tuple_size<typename std::remove_reference<T>::type>::value,
75     class Enable = typename std::enable_if<(start <= size)>::type>
76 struct TemplateTupleRange {
77   using type = typename TemplateRange<
78       std::size_t,
79       start,
80       (n <= size - start ? n : size - start)>::type;
81 };
82
83 namespace detail {
84
85 // Helper class to select a subset of a tuple
86 template <class S> struct TupleSelect;
87 template <std::size_t... Ns>
88 struct TupleSelect<TemplateSeq<std::size_t, Ns...>> {
89   template <class T>
90   static auto select(T&& v)
91   -> decltype(std::make_tuple(std::get<Ns>(std::forward<T>(v))...)) {
92     return std::make_tuple(std::get<Ns>(std::forward<T>(v))...);
93   }
94 };
95
96 } // namespace detail
97
98 // Return a tuple consisting of the elements at a range of indices.
99 //
100 // Use as tupleRange<start, n>(t) to return a tuple of (at most) n
101 // elements starting at index start in tuple t.
102 // If only start is specified (tupleRange<start>(t)), returns all elements
103 // starting at index start until the end of the tuple t.
104 // Won't compile if start > size of t.
105 // Will return fewer elements (size - start) if start + n > size of t.
106 template <
107   std::size_t start = 0,
108   std::size_t n = std::numeric_limits<std::size_t>::max(),
109   class T,
110   class Seq = typename TemplateTupleRange<T, start, n>::type>
111 auto tupleRange(T&& v)
112 -> decltype(detail::TupleSelect<Seq>::select(std::forward<T>(v))) {
113   return detail::TupleSelect<Seq>::select(std::forward<T>(v));
114 }
115
116 // Return a tuple obtained by prepending car to the tuple cdr.
117 template <class T, class U>
118 auto tuplePrepend(T&& car, U&& cdr)
119 -> decltype(std::tuple_cat(std::make_tuple(std::forward<T>(car)),
120                            std::forward<U>(cdr))) {
121   return std::tuple_cat(std::make_tuple(std::forward<T>(car)),
122                         std::forward<U>(cdr));
123 }
124
125 } // namespace folly