XTL  0.1
eXtended Template Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tuple.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #include <xtd/xtd.hpp>
8 
9 namespace xtd {
10 
11  /*
12  @defgroup Tuple
13  @{
14  Generic tuple declaration
15  */
16  template <typename...> class tuple;
18 
19 #if (!DOXY_INVOKED)
20  namespace _ {
21 //tuple_item
22  template <typename _Ty, typename ...> class tuple_item;
23  template <typename _Ty> class tuple_item<_Ty>{};
24 
25  template <typename _Ty, typename ... _Ts> class tuple_item<_Ty, _Ty, _Ts...> {
26  public:
27  static _Ty& get(tuple<_Ty, _Ts...>& oTuple) { return oTuple.Value; }
28  };
29  template <typename _Ty, typename _HeadT, typename ... _TailT> class tuple_item<_Ty, _HeadT, _TailT...> {
30  public:
31  static _Ty& get(tuple<_HeadT, _TailT...>& oTuple) { return tuple_item<_Ty, _TailT...>::get(static_cast<tuple<_TailT...>&>(oTuple)); }
32  };
33 
34 //tuple_index
35  template <int, typename ...> class tuple_index;
36  template <typename _HeadT, typename..._TailT> class tuple_index<0, _HeadT, _TailT...> {
37  public:
38  using value_type = _HeadT;
39  static value_type& get(tuple<_HeadT, _TailT...>& oTuple) { return oTuple.Value; }
40  };
41  template <int index, typename _HeadT, typename..._TailT> class tuple_index<index, _HeadT, _TailT...> {
42  public:
43  using value_type = typename tuple_index<index-1, _TailT...>::value_type;
44  static value_type& get(tuple<_HeadT, _TailT...>& oTuple) { return tuple_index<index - 1, _TailT...>::get(static_cast<tuple<_TailT...>&>(oTuple)); }
45  };
46 
47 //tuple_append
48  template <typename, typename> class tuple_append;
49  template <typename _Ty, typename ... _TupleTs> class tuple_append<_Ty, xtd::tuple<_TupleTs...>> {
50  public:
51  using tuple_type = xtd::tuple<_TupleTs..., _Ty>;
52  };
53  template <typename ... _AppendTs, typename ... _TupleTs> class tuple_append<xtd::tuple<_AppendTs...>, xtd::tuple<_TupleTs...>> {
54  public:
55  using tuple_type = xtd::tuple<_TupleTs..., _AppendTs...>;
56  };
57 
58 //tuple_prepend
59  template <typename, typename> class tuple_prepend;
60  template <typename _Ty, typename ... _TupleTs> class tuple_prepend<_Ty, xtd::tuple<_TupleTs...>> {
61  public:
62  using tuple_type = xtd::tuple<_Ty, _TupleTs...>;
63  };
64  template <typename ... _PrependTs, typename ... _TupleTs> class tuple_prepend<xtd::tuple<_PrependTs...>, xtd::tuple<_TupleTs...>> {
65  public:
66  using tuple_type = xtd::tuple<_PrependTs..., _TupleTs...>;
67  };
68 
69 //tuple_index_of
70  template <typename, typename...> class tuple_index_of;
71  template <typename _Ty, typename..._TailT> class tuple_index_of<_Ty, _Ty, _TailT...> {
72  public:
73  static constexpr int value = 0;
74  };
75  template <typename _Ty, typename _HeadT, typename..._TailT> class tuple_index_of<_Ty, _HeadT, _TailT...> {
76  public:
77  static constexpr int value = 1 + tuple_index_of<_Ty, _TailT...>::value;
78  };
79  }
80 #endif
81 
82  /*
83  @addtogroup Tuple
84  @{
85  Generic tuple declaration
86  */
88  template <> class tuple<> {
89  public:
90  static constexpr size_t Length = 0;
91  tuple(){}
92  tuple(const tuple&){}
93  tuple(tuple&&){}
94  };
95 
97  template <typename _HeadT, typename ... _TailT> class tuple<_HeadT, _TailT...> : public tuple<_TailT...>{
98  public:
99  using _self_t = tuple<_HeadT, _TailT...>;
100  using _super_t = tuple<_TailT...>;
101  using value_type = _HeadT;
102  static constexpr size_t Length = 1 + _super_t::Length;
103 
108  template <typename _Ty> _Ty& Item() { return _::tuple_item<_Ty, _HeadT, _TailT...>::get(*this); }
110  template <int index> typename _::tuple_index<index, _HeadT, _TailT...>::value_type& Index() { return _::tuple_index<index, _HeadT, _TailT...>::get(*this); }
112  template <typename _Ty> using Append = typename _::tuple_append<_Ty, tuple<_HeadT, _TailT...>>::tuple_type;
114  template <typename _Ty> using Prepend = typename _::tuple_prepend <_Ty, tuple<_HeadT, _TailT...>>::tuple_type;
116  template <typename _Ty> using index_of = _::tuple_index_of<_Ty, _HeadT, _TailT...>;
117 
118  tuple() : _super_t() {}
119  tuple(tuple&& src) : _super_t(std::move(src)), Value(std::move(src.value)) {}
120  tuple(const tuple& src) : _super_t(src), Value(src.Value) {}
121 
122  value_type Value;
123  };
125 }
126 
127 
_Ty & Item()
Gets an item from the tuple by type.
Definition: tuple.hpp:108
Tuple specialization with one or more elements.
Definition: tuple.hpp:97
host, target and build configurations and settings Various components are purpose built for specific ...
_::tuple_index_of< _Ty, _HeadT, _TailT...> index_of
Gets the index of a specified type.
Definition: tuple.hpp:116
_::tuple_index< index, _HeadT, _TailT...>::value_type & Index()
Gets an item from the tuple by index.
Definition: tuple.hpp:110
typename _::tuple_append< _Ty, tuple< _HeadT, _TailT...>>::tuple_type Append
Appends a type to the tuple.
Definition: tuple.hpp:112
typename _::tuple_prepend< _Ty, tuple< _HeadT, _TailT...>>::tuple_type Prepend
Prepends a type to the tuple.
Definition: tuple.hpp:114