]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | /* Copyright 2016-2017 Joaquin M Lopez Munoz. |
2 | * Distributed under the Boost Software License, Version 1.0. | |
3 | * (See accompanying file LICENSE_1_0.txt or copy at | |
4 | * http://www.boost.org/LICENSE_1_0.txt) | |
5 | * | |
6 | * See http://www.boost.org/libs/poly_collection for library home page. | |
7 | */ | |
8 | ||
9 | #ifndef BOOST_POLY_COLLECTION_DETAIL_TYPE_RESTITUTION_HPP | |
10 | #define BOOST_POLY_COLLECTION_DETAIL_TYPE_RESTITUTION_HPP | |
11 | ||
12 | #if defined(_MSC_VER) | |
13 | #pragma once | |
14 | #endif | |
15 | ||
16 | #include <boost/poly_collection/detail/functional.hpp> | |
17 | #include <boost/poly_collection/detail/iterator_traits.hpp> | |
18 | #include <typeinfo> | |
19 | #include <utility> | |
20 | ||
21 | namespace boost{ | |
22 | ||
23 | namespace poly_collection{ | |
24 | ||
25 | namespace detail{ | |
26 | ||
27 | /* Given types Ts..., a const std::type_info& info and a local_base_iterator | |
28 | * it, we denote by restitute<Ts...>(info,it): | |
29 | * - a local_iterator<Ti> from it, if info==typeid(Ti) for some Ti in Ts... | |
30 | * - it otherwise. | |
31 | * | |
32 | * Using this notation, restitute_range<Ts...>(f,args...)(s) resolves to | |
33 | * f(restitute<Ts...>(info,begin),restitute<Ts...>(info,end),args...) where | |
34 | * info=s.type_info(), begin=s.begin(), end=s.end(). | |
35 | */ | |
36 | ||
37 | template<typename F,typename... Ts> | |
38 | struct restitute_range_class; | |
39 | ||
40 | template<typename F,typename T,typename... Ts> | |
41 | struct restitute_range_class<F,T,Ts...>: | |
42 | restitute_range_class<F,Ts...> | |
43 | { | |
44 | using super=restitute_range_class<F,Ts...>; | |
45 | using super::super; | |
46 | ||
47 | template<typename SegmentInfo> | |
48 | auto operator()(SegmentInfo&& s) | |
49 | ->decltype(std::declval<F>()(s.begin(),s.end())) | |
50 | { | |
51 | using traits=iterator_traits<decltype(s.begin())>; | |
52 | using local_iterator=typename traits::template local_iterator<T>; | |
53 | ||
54 | if(s.type_info()==typeid(T)) | |
55 | return (this->f)( | |
56 | local_iterator{s.begin()},local_iterator{s.end()}); | |
57 | else | |
58 | return super::operator()(std::forward<SegmentInfo>(s)); | |
59 | } | |
60 | }; | |
61 | ||
62 | template<typename F> | |
63 | struct restitute_range_class<F> | |
64 | { | |
65 | restitute_range_class(const F& f):f(f){} | |
66 | ||
67 | template<typename SegmentInfo> | |
68 | auto operator()(SegmentInfo&& s) | |
69 | ->decltype(std::declval<F>()(s.begin(),s.end())) | |
70 | { | |
71 | return f(s.begin(),s.end()); | |
72 | } | |
73 | ||
74 | F f; | |
75 | }; | |
76 | ||
77 | template<typename... Ts,typename F,typename... Args> | |
78 | auto restitute_range(const F& f,Args&&... args) | |
79 | ->restitute_range_class< | |
80 | decltype(tail_closure(f,std::forward<Args>(args)...)), | |
81 | Ts... | |
82 | > | |
83 | { | |
84 | return tail_closure(f,std::forward<Args>(args)...); | |
85 | } | |
86 | ||
87 | /* restitute_iterator<Ts...>(f,args2...)(index,it,args1...) resolves to | |
88 | * f(restitute<Ts...>(index,it),args1...,args2...). | |
89 | */ | |
90 | ||
91 | template<typename F,typename... Ts> | |
92 | struct restitute_iterator_class; | |
93 | ||
94 | template<typename F,typename T,typename... Ts> | |
95 | struct restitute_iterator_class<F,T,Ts...>: | |
96 | restitute_iterator_class<F,Ts...> | |
97 | { | |
98 | using super=restitute_iterator_class<F,Ts...>; | |
99 | using super::super; | |
100 | ||
101 | template<typename Iterator,typename... Args> | |
102 | auto operator()( | |
103 | const std::type_info& info,Iterator&& it,Args&&... args) | |
104 | ->decltype( | |
105 | std::declval<F>() | |
106 | (std::forward<Iterator>(it),std::forward<Args>(args)...)) | |
107 | { | |
108 | using traits=iterator_traits<typename std::decay<Iterator>::type>; | |
109 | using local_iterator=typename traits::template local_iterator<T>; | |
110 | ||
111 | if(info==typeid(T)) | |
112 | return (this->f)( | |
113 | local_iterator{it},std::forward<Args>(args)...); | |
114 | else | |
115 | return super::operator()( | |
116 | info,std::forward<Iterator>(it),std::forward<Args>(args)...); | |
117 | } | |
118 | }; | |
119 | ||
120 | template<typename F> | |
121 | struct restitute_iterator_class<F> | |
122 | { | |
123 | restitute_iterator_class(const F& f):f(f){} | |
124 | ||
125 | template<typename Iterator,typename... Args> | |
126 | auto operator()( | |
127 | const std::type_info&,Iterator&& it,Args&&... args) | |
128 | ->decltype( | |
129 | std::declval<F>() | |
130 | (std::forward<Iterator>(it),std::forward<Args>(args)...)) | |
131 | { | |
132 | return f(std::forward<Iterator>(it),std::forward<Args>(args)...); | |
133 | } | |
134 | ||
135 | F f; | |
136 | }; | |
137 | ||
138 | template<typename... Ts,typename F,typename... Args> | |
139 | auto restitute_iterator(const F& f,Args&&... args) | |
140 | ->restitute_iterator_class< | |
141 | decltype(tail_closure(f,std::forward<Args>(args)...)), | |
142 | Ts... | |
143 | > | |
144 | { | |
145 | return tail_closure(f,std::forward<Args>(args)...); | |
146 | } | |
147 | ||
148 | /* binary_restitute_iterator<Ts...>(f,args...)(index1,it1,index2,it2) resolves | |
149 | * to f(restitute<Ts...>(index1,it1),restitute<Ts...>(index2,it2),args...). | |
150 | */ | |
151 | ||
152 | template<typename F,typename... Ts> | |
153 | struct binary_restitute_iterator_class | |
154 | { | |
155 | binary_restitute_iterator_class(const F& f):f(f){} | |
156 | ||
157 | template<typename Iterator1,typename Iterator2> | |
158 | auto operator()( | |
159 | const std::type_info& info1,Iterator1&& it1, | |
160 | const std::type_info& info2,Iterator2&& it2) | |
161 | ->decltype( | |
162 | std::declval<F>() | |
163 | (std::forward<Iterator1>(it1),std::forward<Iterator2>(it2))) | |
164 | { | |
165 | return restitute_iterator<Ts...>(*this)( | |
166 | info2,std::forward<Iterator2>(it2),info1,std::forward<Iterator1>(it1)); | |
167 | } | |
168 | ||
169 | template<typename Iterator2,typename Iterator1> | |
170 | auto operator()( | |
171 | Iterator2&& it2,const std::type_info& info1,Iterator1&& it1) | |
172 | ->decltype( | |
173 | std::declval<F>() | |
174 | (std::forward<Iterator1>(it1),std::forward<Iterator2>(it2))) | |
175 | { | |
176 | return restitute_iterator<Ts...>(f)( | |
177 | info1,std::forward<Iterator1>(it1),std::forward<Iterator2>(it2)); | |
178 | } | |
179 | ||
180 | F f; | |
181 | }; | |
182 | ||
183 | template<typename... Ts,typename F,typename... Args> | |
184 | auto binary_restitute_iterator(const F& f,Args&&... args) | |
185 | ->binary_restitute_iterator_class< | |
186 | decltype(tail_closure(f,std::forward<Args>(args)...)), | |
187 | Ts... | |
188 | > | |
189 | { | |
190 | return tail_closure(f,std::forward<Args>(args)...); | |
191 | } | |
192 | ||
193 | } /* namespace poly_collection::detail */ | |
194 | ||
195 | } /* namespace poly_collection */ | |
196 | ||
197 | } /* namespace boost */ | |
198 | ||
199 | #endif |