Tuple.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_TUPLE_HPP
30 #define AURORA_TUPLE_HPP
31 
32 #include <Aurora/Config.hpp>
34 #include <Aurora/Meta/Variadic.hpp>
35 
36 #include <tuple>
37 
38 
39 namespace aurora
40 {
41 namespace detail
42 {
43 
44  // Metafunction to access tuple elements with dynamic index
45  template <typename Tuple, typename Result, std::size_t N>
46  struct TupleDynamic
47  {
48  static Result* get(Tuple& t, std::size_t i)
49  {
50  if (N-1 == i)
51  return &std::get<N-1>(t);
52  else
53  return TupleDynamic<Tuple, Result, N-1>::get(t, i);
54  }
55  };
56 
57  template <typename Tuple, typename Result>
58  struct TupleDynamic<Tuple, Result, 0u>
59  {
60  static Result* get(Tuple&, std::size_t)
61  {
62  return nullptr;
63  }
64  };
65 
66 
67  // Metafunction to transform each element of a tuple and infer the return type
68  template <typename SrcTuple, typename Transformer, std::size_t N>
69  struct TupleTransform
70  {
71  static auto apply(const SrcTuple& src) -> AURORA_AUTO_RETURN
72  (
73  std::tuple_cat(
74  TupleTransform<SrcTuple, Transformer, N-1>::apply(src),
75  std::make_tuple(
76  Transformer::transform(std::get<N-1>(src))
77  ))
78  )
79  };
80 
81  template <typename SrcTuple, typename Transformer>
82  struct TupleTransform<SrcTuple, Transformer, 0u>
83  {
84  static std::tuple<> apply(const SrcTuple&)
85  {
86  return std::make_tuple();
87  }
88  };
89 
90 
91 } // namespace detail
92 
95 
102 template <typename Result, typename Tuple>
103 Result* dynamicGet(Tuple& t, std::size_t i)
104 {
105  const std::size_t N = std::tuple_size<Tuple>::value;
106  return detail::TupleDynamic<Tuple, Result, N>::get(t, i);
107 }
108 
128 template <typename Transformer, typename SrcTuple>
129 auto tupleTransform(const SrcTuple& src) -> AURORA_AUTO_RETURN
130 (
131  detail::TupleTransform<SrcTuple, Transformer, std::tuple_size<SrcTuple>::value>::apply(src)
132 )
133 
134 template <typename Tuple>
137 auto tupleFront(Tuple&& t) -> AURORA_AUTO_RETURN
138 (
139  std::get<0>(t)
140 )
141 
144 template <typename Tuple>
145 auto tupleBack(Tuple&& t) -> AURORA_AUTO_RETURN
146 (
147  std::get<std::tuple_size<typename std::remove_reference<Tuple>::type>::value - 1>(t)
148 )
149 
152 template <typename T, typename... Us>
153 T& tupleGet(std::tuple<Us...>& tuple)
154 {
155  return std::get<detail::IndexOfType<T, Us...>::value>(tuple);
156 }
157 
159 
160 } // namespace aurora
161 
162 #endif // AURORA_TEMPLATES_HPP
Utilities for template metaprogramming.
Metaprogramming helpers for variadic templates.
#define AURORA_AUTO_RETURN(...)
Function declaration with inferred return type.
Definition: Templates.hpp:212
auto tupleTransform(const SrcTuple &src) -> InferredReturnType Transformer, std::tuple_size< SrcTuple >::value >::apply(src)) template< typename Tuple > auto tupleFront(Tuple &&t) -> InferredReturnType template< typename Tuple > auto tupleBack(Tuple &&t) -> InferredReturnType template< typename T, typename...Us > T &tupleGet(std::tuple< Us... > &tuple)
Transform one tuple into another.
Definition: Tuple.hpp:129
Configuration header of the library.
Result * dynamicGet(Tuple &t, std::size_t i)
Access tuple with dynamic index.
Definition: Tuple.hpp:103
Definition: DispatchTraits.hpp:39