LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
prelude.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 <functional>
33 #include <type_traits>
34 #include <iterator>
35 #include <QPair>
36 #include <QStringList>
37 
38 namespace boost
39 {
40  template<typename>
42 }
43 
44 namespace LC
45 {
46 namespace Util
47 {
48  template<typename T>
49  struct WrapType
50  {
51  using type = T;
52  };
53 
54  template<typename T>
55  using WrapType_t = typename WrapType<T>::type;
56 
57  template<>
58  struct WrapType<QList<QString>>
59  {
60  using type = QStringList;
61  };
62 
63  template<template<typename U> class Container, typename T1, typename T2, typename F>
64  auto ZipWith (const Container<T1>& c1, const Container<T2>& c2, F f) -> WrapType_t<Container<std::decay_t<std::result_of_t<F (T1, T2)>>>>
65  {
67 
68  using std::begin;
69  using std::end;
70 
71  auto i1 = begin (c1), e1 = end (c1);
72  auto i2 = begin (c2), e2 = end (c2);
73  for ( ; i1 != e1 && i2 != e2; ++i1, ++i2)
74  result.push_back (f (*i1, *i2));
75  return result;
76  }
77 
78  template<typename T1, typename T2,
79  template<typename U> class Container,
80  template<typename U1, typename U2> class Pair = QPair>
81  auto Zip (const Container<T1>& c1, const Container<T2>& c2) -> Container<Pair<T1, T2>>
82  {
83  return ZipWith (c1, c2,
84  [] (const T1& t1, const T2& t2) -> Pair<T1, T2>
85  { return { t1, t2}; });
86  }
87 
88  namespace detail
89  {
90  template<typename Res, typename T>
91  void Append (Res& result, T&& val, decltype (result.push_back (std::forward<T> (val)))* = nullptr)
92  {
93  result.push_back (std::forward<T> (val));
94  }
95 
96  template<typename Res, typename T>
97  void Append (Res& result, T&& val, decltype (result.insert (std::forward<T> (val)))* = nullptr)
98  {
99  result.insert (std::forward<T> (val));
100  }
101 
102  template<typename>
103  struct CountArgs
104  {
105  static const size_t ArgsCount = 0;
106  };
107 
108  template<template<typename...> class Container, typename... Args>
109  struct CountArgs<Container<Args...>>
110  {
111  static const size_t ArgsCount = sizeof... (Args);
112  };
113 
114  template<typename C>
115  constexpr bool IsSimpleContainer ()
116  {
117  return CountArgs<std::decay_t<C>>::ArgsCount == 1;
118  }
119 
120  template<typename Container, typename T>
121  struct Replace
122  {
123  using Type = struct Fail;
124  };
125 
126  template<template<typename> class Container, typename U, typename T>
127  struct Replace<Container<U>, T>
128  {
129  using Type = Container<T>;
130  };
131 
132  template<typename>
133  struct IsNotBrokenSFINAE : std::false_type {};
134 
135  template<typename T>
136  struct IsNotBrokenSFINAE<boost::iterator_range<T>> : std::true_type {};
137 
138  template<typename T>
140 
141  template<template<typename...> class Fallback, bool ForceFallback, typename Container, typename F>
142  auto MapImpl (Container&& c, F f)
143  {
144  using FRet_t = std::decay_t<decltype (std::invoke (f, *c.begin ()))>;
145  static_assert (!std::is_same<void, FRet_t> {}, "The function shall not return void.");
146 
147  using DecayContainer_t = std::decay_t<Container>;
148 
149  using ResultCont_t = std::conditional_t<
150  !ForceFallback &&
151  detail::IsSimpleContainer<DecayContainer_t> () &&
152  !detail::IsNotBrokenSFINAE_v<DecayContainer_t>,
154  Fallback<FRet_t>>;
155 
157  for (auto&& t : c)
158  detail::Append (cont, std::invoke (f, t));
159  return cont;
160  }
161  }
162 
163  template<typename Container, typename F>
164  auto Map (Container&& c, F f)
165  {
166  return detail::MapImpl<QList, false> (std::forward<Container> (c), std::forward<F> (f));
167  }
168 
169  template<template<typename...> class Fallback, typename Container, typename F>
170  auto MapAs (Container&& c, F&& f)
171  {
172  return detail::MapImpl<Fallback, true> (std::forward<Container> (c), std::forward<F> (f));
173  }
174 
175  template<typename T, template<typename U> class Container, typename F>
176  Container<T> Filter (const Container<T>& c, F f)
177  {
178  Container<T> result;
179  for (const auto& item : c)
180  if (std::invoke (f, item))
181  detail::Append (result, item);
182  return result;
183  }
184 
185  template<template<typename> class Container, typename T>
186  Container<T> Concat (const Container<Container<T>>& containers)
187  {
188  Container<T> result;
189  for (const auto& cont : containers)
190  std::copy (cont.begin (), cont.end (), std::back_inserter (result));
191  return result;
192  }
193 
194  template<template<typename...> class Container, typename... ContArgs>
195  auto Concat (const Container<ContArgs...>& containers) -> std::decay_t<decltype (*containers.begin ())>
196  {
197  std::decay_t<decltype (*containers.begin ())> result;
198  for (const auto& cont : containers)
199  for (const auto& item : cont)
200  detail::Append (result, item);
201  return result;
202  }
203 
204  template<typename Cont, typename F>
205  auto ConcatMap (Cont&& c, F&& f)
206  {
207  return Concat (Map (std::forward<Cont> (c), std::forward<F> (f)));
208  }
209 
210  template<template<typename> class Container, typename T>
211  Container<Container<T>> SplitInto (size_t numChunks, const Container<T>& container)
212  {
213  Container<Container<T>> result;
214 
215  const size_t chunkSize = container.size () / numChunks;
216  for (size_t i = 0; i < numChunks; ++i)
217  {
218  Container<T> subcont;
219  const auto start = container.begin () + chunkSize * i;
220  const auto end = start + chunkSize;
221  std::copy (start, end, std::back_inserter (subcont));
222  result.push_back (subcont);
223  }
224 
225  const auto lastStart = container.begin () + chunkSize * numChunks;
226  const auto lastEnd = container.end ();
227  std::copy (lastStart, lastEnd, std::back_inserter (result.front ()));
228 
229  return result;
230  }
231 
232  template<typename Cont>
233  decltype (auto) Sorted (Cont&& cont)
234  {
235  std::sort (cont.begin (), cont.end ());
236  return std::forward<Cont> (cont);
237  }
238 
239  constexpr auto Id = [] (auto&& t) ->decltype (auto) { return std::forward<decltype (t)> (t); };
240 
241  template<typename R>
242  auto ComparingBy (R r)
243  {
244  return [r] (const auto& left, const auto& right) { return std::invoke (r, left) < std::invoke (r, right); };
245  }
246 
247  template<typename R>
248  auto EqualityBy (R r)
249  {
250  return [r] (const auto& left, const auto& right) { return std::invoke (r, left) == std::invoke (r, right); };
251  }
252 
253  constexpr auto Apply = [] (const auto& t) { return t (); };
254 
255  constexpr auto Fst = [] (const auto& pair) { return pair.first; };
256 
257  constexpr auto Snd = [] (const auto& pair) { return pair.second; };
258 
259  template<typename F>
260  auto First (F&& f)
261  {
262  return [f = std::forward<F> (f)] (const auto& pair) { return std::invoke (f, pair.first); };
263  }
264 
265  template<typename F>
266  auto Second (F&& f)
267  {
268  return [f = std::forward<F> (f)] (const auto& pair) { return std::invoke (f, pair.second); };
269  }
270 
271  template<typename F>
272  auto Flip (F&& f)
273  {
274  return [f = std::forward<F> (f)] (auto&& left, auto&& right)
275  {
276  return f (std::forward<decltype (right)> (right),
277  std::forward<decltype (left)> (left));
278  };
279  }
280 }
281 }
constexpr bool IsSimpleContainer()
Definition: prelude.h:115
Definition: prelude.h:38
Container< T > Concat(const Container< Container< T >> &containers)
Definition: prelude.h:186
auto Flip(F &&f)
Definition: prelude.h:272
constexpr auto Apply
Definition: prelude.h:253
auto ComparingBy(R r)
Definition: prelude.h:242
auto MapImpl(Container &&c, F f)
Definition: prelude.h:142
constexpr bool IsNotBrokenSFINAE_v
Definition: prelude.h:139
auto Second(F &&f)
Definition: prelude.h:266
auto MapAs(Container &&c, F &&f)
Definition: prelude.h:170
static const size_t ArgsCount
Definition: prelude.h:105
Container< T > Filter(const Container< T > &c, F f)
Definition: prelude.h:176
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:955
Container< Container< T > > SplitInto(size_t numChunks, const Container< T > &container)
Definition: prelude.h:211
auto First(F &&f)
Definition: prelude.h:260
auto Map(Container &&c, F f)
Definition: prelude.h:164
constexpr auto Id
Definition: prelude.h:239
decltype(auto) Sorted(Cont &&cont)
Definition: prelude.h:233
auto EqualityBy(R r)
Definition: prelude.h:248
constexpr auto Fst
Definition: prelude.h:255
auto ZipWith(const Container< T1 > &c1, const Container< T2 > &c2, F f) -> WrapType_t< Container< std::decay_t< std::result_of_t< F(T1, T2)>>>>
Definition: prelude.h:64
auto Zip(const Container< T1 > &c1, const Container< T2 > &c2) -> Container< Pair< T1, T2 >>
Definition: prelude.h:81
Definition: constants.h:35
auto ConcatMap(Cont &&c, F &&f)
Definition: prelude.h:205
constexpr auto Snd
Definition: prelude.h:257
typename WrapType< T >::type WrapType_t
Definition: prelude.h:55
void Append(Res &result, T &&val, decltype(result.push_back(std::forward< T >(val))) *=nullptr)
Definition: prelude.h:91