LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
qtutil.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 #include <boost/range.hpp>
34 #include "sllconfig.h"
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
40  namespace detail
41  {
42  template<template<typename> class F, typename V>
43  using MF = typename F<V>::type;
44 
45  template<typename T>
46  struct Identity
47  {
48  using type = T;
49  };
50 
51  template<
52  template<template<typename> class, template<typename> class, typename, template<typename, typename> class> class This,
53  template<typename> class KeyMF,
54  template<typename> class ValueMF,
55  typename Iter,
56  template<typename, typename> class PairType,
57  typename KeyType = MF<KeyMF, decltype (Iter {}.key ())>,
58  typename ValueType = MF<ValueMF, decltype (Iter {}.value ())>
59  >
60  using IteratorAdaptorBase = boost::iterator_adaptor<
61  This<KeyMF, ValueMF, Iter, PairType>,
62  Iter,
63  PairType<KeyType, ValueType>,
64  boost::use_default,
65  PairType<KeyType, ValueType>
66  >;
67 
68  template<
69  template<typename> class KeyMF,
70  template<typename> class ValueMF,
71  typename Iter,
72  template<typename, typename> class PairType
73  >
74  class StlAssocIteratorAdaptor : public IteratorAdaptorBase<StlAssocIteratorAdaptor, KeyMF, ValueMF, Iter, PairType>
75  {
77 
79  public:
80  StlAssocIteratorAdaptor () = default;
81 
83  : Super_t { it }
84  {
85  }
86  private:
87  typename Super_t::reference dereference () const
88  {
89  return { this->base ().key (), this->base ().value () };
90  }
91  };
92 
93  template<
94  template<typename> class KeyMF,
95  template<typename> class ValueMF,
96  typename Iter,
97  typename Assoc,
98  template<typename K, typename V> class PairType
99  >
100  struct StlAssocRange : private std::tuple<Assoc>
101  , public boost::iterator_range<StlAssocIteratorAdaptor<KeyMF, ValueMF, Iter, PairType>>
102  {
103  public:
104  StlAssocRange (Assoc&& assoc)
105  : std::tuple<Assoc> { std::move (assoc) }
106  , boost::iterator_range<StlAssocIteratorAdaptor<KeyMF, ValueMF, Iter, PairType>> { std::get<0> (*this).begin (), std::get<0> (*this).end () }
107  {
108  }
109  };
110 
111  template<
112  template<typename> class KeyMF,
113  template<typename> class ValueMF,
114  typename Iter,
115  typename Assoc,
116  template<typename K, typename V> class PairType
117  >
119  {
120  public:
121  StlAssocRange (Assoc& assoc)
122  : boost::iterator_range<StlAssocIteratorAdaptor<KeyMF, ValueMF, Iter, PairType>> { assoc.begin (), assoc.end () }
123  {
124  }
125 
126  auto AsList () const
127  {
129  result.reserve (std::distance (this->begin (), this->end ()));
130  std::copy (this->begin (), this->end (), std::back_inserter (result));
131  return result;
132  }
133  };
134  }
135 
165  template<template<typename K, typename V> class PairType = std::pair, typename Assoc>
166  auto Stlize (Assoc&& assoc) -> detail::StlAssocRange<detail::Identity, detail::Identity, decltype (assoc.begin ()), Assoc, PairType>
167  {
168  return { std::forward<Assoc> (assoc) };
169  }
170 
171  template<template<typename K, typename V> class PairType = std::pair, typename Assoc>
172  auto StlizeCopy (Assoc&& assoc) -> detail::StlAssocRange<std::decay, std::decay, decltype (assoc.begin ()), Assoc, PairType>
173  {
174  return { std::forward<Assoc> (assoc) };
175  }
176 }
177 }
auto Stlize(Assoc &&assoc) -> detail::StlAssocRange< detail::Identity, detail::Identity, decltype(assoc.begin()), Assoc, PairType >
Converts an Qt&#39;s associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:166
Definition: prelude.h:39
STL namespace.
typename F< V >::type MF
Definition: qtutil.h:43
boost::iterator_adaptor< This< KeyMF, ValueMF, Iter, PairType >, Iter, PairType< KeyType, ValueType >, boost::use_default, PairType< KeyType, ValueType > > IteratorAdaptorBase
Definition: qtutil.h:66
auto StlizeCopy(Assoc &&assoc) -> detail::StlAssocRange< std::decay, std::decay, decltype(assoc.begin()), Assoc, PairType >
Definition: qtutil.h:172