C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
chapter-7-zip_view.hpp
Go to the documentation of this file.
1// ===-- chapter-7/chapter-7-zip_view.hpp ----------------- -*- C++ -*- --=== //
10#ifndef CHAPTER_7_ZIP_VIEW
11#define CHAPTER_7_ZIP_VIEW
12
13#include <boost/mpl/begin_end.hpp>
14#include <boost/mpl/deref.hpp>
15#include <boost/mpl/iterator_category.hpp>
16#include <boost/mpl/iterator_range.hpp>
17#include <boost/mpl/placeholders.hpp>
18#include <boost/mpl/transform.hpp>
19using namespace boost::mpl::placeholders;
20
21// This is work from exercise 7-2...
22#ifndef FORWARD_ONLY_ZIP_VIEW
23
24#include <boost/mpl/empty.hpp>
25#include <boost/mpl/find_if.hpp>
26#include <boost/mpl/filter_view.hpp>
27
28#include <boost/type_traits/is_same.hpp>
29
30
31namespace chapter7 {
32
41template <typename S>
43{
44 typedef typename boost::mpl::begin<S>::type::category type;
45};
46
62template <typename S>
64{
66 typedef typename boost::mpl::filter_view<
67 S,
68 boost::mpl::not_<
69 boost::is_same<
71 boost::mpl::random_access_iterator_tag>
72 >
74
77 typedef typename boost::mpl::filter_view<
79 boost::mpl::not_<
80 boost::is_same<
82 boost::mpl::bidirectional_iterator_tag>
83 >
85
86 typedef typename boost::mpl::if_<
87 boost::mpl::empty<non_random_access_iterators>,
88 boost::mpl::random_access_iterator_tag,
89 typename boost::mpl::if_<
90 boost::mpl::empty<non_bidirectional_iterators>,
91 boost::mpl::bidirectional_iterator_tag,
92 boost::mpl::forward_iterator_tag>::type
94};
95
96#else
97
98// This is a place-holder until exercise 7-2
99template <typename S>
101{
102 typedef boost::mpl::forward_iterator_tag type;
103};
104
105#endif // !FORWARD_ONLY_ZIP_VIEW
106
108template <typename IteratorSeq, typename Category>
110 : boost::mpl::transform< IteratorSeq, boost::mpl::deref<_1> >
111{
112 typedef IteratorSeq base;
113 typedef Category category;
114};
115
117struct zip_view_tag { };
118
124template <typename Sequences>
126 : boost::mpl::iterator_range<
127 zip_iterator<
128 typename boost::mpl::transform<
129 Sequences, boost::mpl::begin<_1>
130 >::type,
131 typename least_refined_iterator_category<Sequences>::type
132 >,
133 zip_iterator<
134 typename boost::mpl::transform<
135 Sequences, boost::mpl::end<_1>
136 >::type,
137 typename least_refined_iterator_category<Sequences>::type
138 >
139 >
140{
142};
143
144} // namespace chapter7
145
146
147#include <boost/static_assert.hpp>
148#include <boost/mpl/or.hpp>
149
150#include <boost/mpl/at.hpp>
151
152
153namespace boost {
154namespace mpl {
155
158
160template <typename IteratorSeq, typename Category>
161struct next< chapter7::zip_iterator<IteratorSeq, Category> >
162{
164 typename transform< IteratorSeq, next<_1> >::type,
165 Category
167};
168
169// Begin metafunctions for exercise 7-2...
170
177template <typename IteratorSeq, typename N>
178struct advance<
179 chapter7::zip_iterator<IteratorSeq, random_access_iterator_tag>, N >
180{
182 typename transform< IteratorSeq, advance<_1, N> >::type,
183 random_access_iterator_tag
185};
186
193template <>
194struct at_impl<chapter7::zip_view_tag>
195{
196 template <typename S, typename Category, typename N> struct apply_impl { };
197
198 template <typename S, typename N>
199 struct apply_impl<S, random_access_iterator_tag, N>
200 {
202 typename transform< S, at<_1, N> >::type,
203 random_access_iterator_tag> type;
204 };
205
206 template <typename S, typename N> struct apply { };
207 template <typename S, typename N>
208 struct apply< chapter7::zip_view<S>, N >
209 : apply_impl<
210 S,
211 typename chapter7::least_refined_iterator_category<S>::type,
212 N>
213 { };
214};
215
217template <typename IteratorSeq, typename Category>
218struct prior< chapter7::zip_iterator<IteratorSeq, Category> >
219{
221 boost::mpl::or_<
222 boost::is_same<
223 Category,
224 boost::mpl::bidirectional_iterator_tag>,
225 boost::is_same<
226 Category,
227 boost::mpl::random_access_iterator_tag>
228 >::type::value));
229
231 typename transform< IteratorSeq, prior<_1> >::type,
232 Category
234};
235
237
238} // namespace mpl
239} // namespace boost
240
241#endif // CHAPTER_7_ZIP_VIEW
Exists to inject functionality into the Boost MPL namespace.
Exists to inject functionality into the Boost namespace.
Provide utilities availble to all Chapter 7 solutions.
chapter7::zip_iterator< typename transform< IteratorSeq, advance< _1, N > >::type, random_access_iterator_tag > type
chapter7::zip_iterator< typename transform< S, at< _1, N > >::type, random_access_iterator_tag > type
chapter7::zip_iterator< typename transform< IteratorSeq, next< _1 > >::type, Category > type
chapter7::zip_iterator< typename transform< IteratorSeq, prior< _1 > >::type, Category > type
BOOST_STATIC_ASSERT((boost::mpl::or_< boost::is_same< Category, boost::mpl::bidirectional_iterator_tag >, boost::is_same< Category, boost::mpl::random_access_iterator_tag > >::type::value))
A simple metafunction to get the iterator category from the first iterator in a sequence.
boost::mpl::begin< S >::type::category type
Assuming a sequence of sequences, find the least refined category of all subsequences.
boost::mpl::if_< boost::mpl::empty< non_random_access_iterators >, boost::mpl::random_access_iterator_tag, typenameboost::mpl::if_< boost::mpl::empty< non_bidirectional_iterators >, boost::mpl::bidirectional_iterator_tag, boost::mpl::forward_iterator_tag >::type >::type type
boost::mpl::filter_view< non_random_access_iterators, boost::mpl::not_< boost::is_same< extract_iterator_category< _1 >, boost::mpl::bidirectional_iterator_tag > > >::type non_bidirectional_iterators
Show us everything that is not categorized as either "bidirectional" OR "random access....
boost::mpl::filter_view< S, boost::mpl::not_< boost::is_same< extract_iterator_category< _1 >, boost::mpl::random_access_iterator_tag > > >::type non_random_access_iterators
Show us everything that is not categorized as "random access.".
An iterator into a sequence of sequences.
Type for tag-based implementation of algorithms below.
Wraps corresponding elements across a sequence of sequences.