LeechCraft  0.6.70-9312-g4cc613a2df
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
typelist.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include <tuple>
33 
34 namespace LeechCraft
35 {
36 namespace Util
37 {
38  template<typename...>
39  struct Typelist
40  {
41  };
42 
43  template<template<typename...> class List, typename H, typename... T>
44  constexpr List<T...> Tail (List<H, T...>)
45  {
46  return {};
47  }
48 
49  namespace detail
50  {
51  template<int N, typename List>
52  struct DropImpl
53  {
54  using Result_t = typename DropImpl<N - 1, decltype (Tail (List {}))>::Result_t;
55  };
56 
57  template<typename List>
58  struct DropImpl<0, List>
59  {
60  using Result_t = List;
61  };
62  }
63 
64  template<int N, template<typename...> class List, typename... Args>
65  constexpr typename detail::DropImpl<N, List<Args...>>::Result_t Drop (List<Args...>)
66  {
67  return {};
68  }
69 
70  template<template<typename...> class List, typename... Args1, typename... Args2>
71  constexpr List<Args1..., Args2...> Concat (List<Args1...>, List<Args2...>)
72  {
73  return {};
74  }
75 
76  template<template<typename...> class List>
77  constexpr List<> Reverse (List<>)
78  {
79  return {};
80  }
81 
82  template<template<typename...> class List, typename Head, typename... Tail>
83  constexpr auto Reverse (List<Head, Tail...>) -> decltype (Concat (Reverse (List<Tail...> {}), List<Head> {}))
84  {
85  return {};
86  }
87 
88  namespace detail
89  {
90  template<template<typename...> class List, typename Tuple, size_t... Is>
91  constexpr auto InitImpl (std::integer_sequence<size_t, Is...>)
92  {
93  return List<std::tuple_element_t<Is, Tuple>...> {};
94  }
95  }
96 
97  template<template<typename...> class List, typename... Args>
98  constexpr auto Init (List<Args...>)
99  {
100  return detail::InitImpl<List, std::tuple<Args...>> (std::make_index_sequence<sizeof... (Args) - 1> {});
101  }
102 
103  namespace detail
104  {
105  template<typename Type, template<typename...> class List, typename... Tail>
106  constexpr bool HasTypeImpl (List<Type, Tail...>, int)
107  {
108  return true;
109  }
110 
111  template<typename, template<typename...> class List>
112  constexpr bool HasTypeImpl (List<>, float)
113  {
114  return false;
115  }
116 
117  template<typename Type, template<typename...> class List, typename Head, typename... Tail>
118  constexpr bool HasTypeImpl (List<Head, Tail...>, float)
119  {
120  return HasTypeImpl<Type> (List<Tail...> {}, 0);
121  }
122  }
123 
124  template<typename Type, template<typename...> class List, typename... Args>
125  constexpr bool HasType (List<Args...> list)
126  {
127  return detail::HasTypeImpl<Type> (list, 0);
128  }
129 
130  namespace detail
131  {
132  template<template<typename> class, typename, typename = void>
133  struct Filter;
134  }
135 
136  template<template<typename> class Pred, typename List>
137  using Filter_t = typename detail::Filter<Pred, List>::Result_t;
138 
139  namespace detail
140  {
141  template<template<typename> class Pred, template<typename...> class List, typename Head, typename... Tail>
142  struct Filter<Pred, List<Head, Tail...>, std::enable_if_t<Pred<Head>::value>>
143  {
144  using Result_t = decltype (Concat (List<Head> {}, Filter_t<Pred, List<Tail...>> {}));
145  };
146 
147  template<template<typename> class Pred, template<typename...> class List, typename Head, typename... Tail>
148  struct Filter<Pred, List<Head, Tail...>, std::enable_if_t<!Pred<Head>::value>>
149  {
150  using Result_t = Filter_t<Pred, List<Tail...>>;
151  };
152 
153  template<template<typename> class Pred, template<typename...> class List>
154  struct Filter<Pred, List<>>
155  {
156  using Result_t = List<>;
157  };
158  }
159 
160  template<typename T>
161  struct AsTypelist;
162 
163  template<template<typename...> class OtherList, typename... Args>
164  struct AsTypelist<OtherList<Args...>>
165  {
166  using Result_t = Typelist<Args...>;
167  };
168 
169  template<typename T>
171 }
172 }
constexpr List Reverse(List<>)
Definition: typelist.h:77
constexpr List< T...> Tail(List< H, T...>)
Definition: typelist.h:44
Container< T > Concat(const Container< Container< T >> &containers)
Definition: prelude.h:204
typename DropImpl< N-1, decltype(Tail(List{}))>::Result_t Result_t
Definition: typelist.h:54
Type
Describes the various types of XDG .desktop files.
Definition: itemtypes.h:48
constexpr detail::DropImpl< N, List< Args...> >::Result_t Drop(List< Args...>)
Definition: typelist.h:65
Container< T > Filter(const Container< T > &c, F f)
Definition: prelude.h:194
typename AsTypelist< T >::Result_t AsTypelist_t
Definition: typelist.h:170
decltype(Concat(List< Head >{}, Filter_t< Pred, List< Tail...>>{})) Result_t
Definition: typelist.h:144