1 | #ifndef _theplu_yat_utility_concept_check_ |
---|
2 | #define _theplu_yat_utility_concept_check_ |
---|
3 | |
---|
4 | // $Id: concept_check.h 3393 2015-03-17 03:38:36Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2010, 2011, 2013 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | // always include config first |
---|
26 | #include "config_public.h" |
---|
27 | |
---|
28 | #include "iterator_traits.h" |
---|
29 | |
---|
30 | #include <boost/concept_archetype.hpp> |
---|
31 | #include <boost/concept_check.hpp> |
---|
32 | #include <boost/iterator/iterator_concepts.hpp> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace utility { |
---|
37 | |
---|
38 | /** |
---|
39 | \brief Concept check for \ref concept_container_2d |
---|
40 | |
---|
41 | This class is intended to be used in a <a |
---|
42 | href="\boost_url/concept_check/using_concept_check.htm"> |
---|
43 | BOOST_CONCEPT_ASSERT </a> |
---|
44 | |
---|
45 | \code |
---|
46 | template<class T> |
---|
47 | void some_function(const T& t) |
---|
48 | { |
---|
49 | BOOST_CONCEPT_ASSERT((Container2D<T>)); |
---|
50 | ... |
---|
51 | } |
---|
52 | \endcode |
---|
53 | |
---|
54 | \since New in yat 0.7 |
---|
55 | */ |
---|
56 | template <class T> |
---|
57 | class Container2D |
---|
58 | { |
---|
59 | public: |
---|
60 | #ifdef YAT_HAVE_BOOST_CONCEPT_WITH_CONSTRUCTOR |
---|
61 | /// Default constructor |
---|
62 | Container2D(void) {} |
---|
63 | #endif |
---|
64 | |
---|
65 | /// value_type |
---|
66 | typedef typename T::value_type value_type; |
---|
67 | /// const_reference |
---|
68 | typedef typename T::const_reference const_reference; |
---|
69 | /// const_iterator |
---|
70 | typedef typename T::const_iterator const_iterator; |
---|
71 | /// const_row_iterator |
---|
72 | typedef typename T::const_row_iterator const_row_iterator; |
---|
73 | /// const_column_iterator |
---|
74 | typedef typename T::const_column_iterator const_column_iterator; |
---|
75 | |
---|
76 | /** |
---|
77 | \brief function doing the concept test |
---|
78 | */ |
---|
79 | BOOST_CONCEPT_USAGE(Container2D) |
---|
80 | { |
---|
81 | const_iterator iter_ = t_.begin(); |
---|
82 | iter_ = t_.end(); |
---|
83 | const_row_iterator row_iter_ = t_.begin_row(0); |
---|
84 | row_iter_ = t_.end_row(0); |
---|
85 | const_column_iterator col_iter_ = t_.begin_column(0); |
---|
86 | col_iter_ = t_.end_column(0); |
---|
87 | const_reference r = t_(0,0); |
---|
88 | value_ = r; |
---|
89 | size_t n = t_.rows(); |
---|
90 | n = t_.columns(); |
---|
91 | some_func(n); |
---|
92 | using boost_concepts::ReadableIterator; |
---|
93 | BOOST_CONCEPT_ASSERT((ReadableIterator<const_iterator>)); |
---|
94 | BOOST_CONCEPT_ASSERT((ReadableIterator<const_row_iterator>)); |
---|
95 | BOOST_CONCEPT_ASSERT((ReadableIterator<const_column_iterator>)); |
---|
96 | } |
---|
97 | |
---|
98 | private: |
---|
99 | void some_func(size_t x) const {} |
---|
100 | T t_; |
---|
101 | value_type value_; |
---|
102 | }; |
---|
103 | |
---|
104 | /** |
---|
105 | \brief Concept check for \ref concept_mutable_container_2d |
---|
106 | |
---|
107 | This class is intended to be used in a <a |
---|
108 | href="\boost_url/concept_check/using_concept_check.htm"> |
---|
109 | BOOST_CONCEPT_ASSERT </a> |
---|
110 | |
---|
111 | \code |
---|
112 | template<class T> |
---|
113 | void some_function(const T& t) |
---|
114 | { |
---|
115 | BOOST_CONCEPT_ASSERT((Mutable_Container2D<T>)); |
---|
116 | ... |
---|
117 | } |
---|
118 | \endcode |
---|
119 | |
---|
120 | \since New in yat 0.7 |
---|
121 | */ |
---|
122 | template <class T> |
---|
123 | class Mutable_Container2D : public Container2D<T> |
---|
124 | { |
---|
125 | public: |
---|
126 | /// reference |
---|
127 | typedef typename T::reference reference; |
---|
128 | /// iterator |
---|
129 | typedef typename T::iterator iterator; |
---|
130 | /// row_iterator |
---|
131 | typedef typename T::row_iterator row_iterator; |
---|
132 | /// column_iterator |
---|
133 | typedef typename T::column_iterator column_iterator; |
---|
134 | |
---|
135 | /** |
---|
136 | \brief function doing the concept test |
---|
137 | */ |
---|
138 | BOOST_CONCEPT_USAGE(Mutable_Container2D) |
---|
139 | { |
---|
140 | iterator iter_ = t_.begin(); |
---|
141 | iter_ = t_.end(); |
---|
142 | row_iterator row_iter_ = t_.begin_row(0); |
---|
143 | row_iter_ = t_.end_row(0); |
---|
144 | column_iterator col_iter_ = t_.begin_column(0); |
---|
145 | col_iter_ = t_.end_column(0); |
---|
146 | reference r = t_(0,0); |
---|
147 | t_(0,0) = r; |
---|
148 | using boost::Mutable_RandomAccessIterator; // just to avoid long lines |
---|
149 | BOOST_CONCEPT_ASSERT((Mutable_RandomAccessIterator<iterator>)); |
---|
150 | BOOST_CONCEPT_ASSERT((Mutable_RandomAccessIterator<row_iterator>)); |
---|
151 | BOOST_CONCEPT_ASSERT((Mutable_RandomAccessIterator<column_iterator>)); |
---|
152 | } |
---|
153 | private: |
---|
154 | T t_; |
---|
155 | }; |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | \brief Concept check for \trivial_iterator |
---|
160 | |
---|
161 | This class is intended to be used in a <a |
---|
162 | href="\boost_url/concept_check/using_concept_check.htm"> |
---|
163 | BOOST_CONCEPT_ASSERT </a> |
---|
164 | |
---|
165 | \code |
---|
166 | template<class Iterator> |
---|
167 | void some_function(const Iterator& it) |
---|
168 | { |
---|
169 | BOOST_CONCEPT_ASSERT((TrivialIterator<Iterator>)); |
---|
170 | ... |
---|
171 | } |
---|
172 | \endcode |
---|
173 | |
---|
174 | \since New in yat 0.7 |
---|
175 | */ |
---|
176 | template <class T> |
---|
177 | class TrivialIterator |
---|
178 | : public boost::Assignable<T> |
---|
179 | , public boost::EqualityComparable<T> |
---|
180 | , public boost::DefaultConstructible<T> |
---|
181 | |
---|
182 | { |
---|
183 | public: |
---|
184 | /// iterator_category |
---|
185 | typedef typename std::iterator_traits<T>::iterator_category iterator_category; |
---|
186 | /// value_type |
---|
187 | typedef typename std::iterator_traits<T>::value_type value_type; |
---|
188 | /// difference_type |
---|
189 | typedef typename std::iterator_traits<T>::difference_type difference_type; |
---|
190 | /// pointer |
---|
191 | typedef typename std::iterator_traits<T>::pointer pointer; |
---|
192 | /// reference |
---|
193 | typedef typename std::iterator_traits<T>::reference reference; |
---|
194 | |
---|
195 | /** |
---|
196 | \brief function doing the concept test |
---|
197 | */ |
---|
198 | BOOST_CONCEPT_USAGE(TrivialIterator) |
---|
199 | { |
---|
200 | T t; |
---|
201 | value_ = *t; |
---|
202 | } |
---|
203 | private: |
---|
204 | value_type value_; |
---|
205 | }; |
---|
206 | |
---|
207 | /** |
---|
208 | \brief Concept check for \ref concept_data_iterator |
---|
209 | |
---|
210 | This class is intended to be used in a <a |
---|
211 | href="\boost_url/concept_check/using_concept_check.htm"> |
---|
212 | BOOST_CONCEPT_ASSERT </a> |
---|
213 | |
---|
214 | \code |
---|
215 | template<class Iterator> |
---|
216 | void some_function(const Iterator& it) |
---|
217 | { |
---|
218 | BOOST_CONCEPT_ASSERT((DataIteratorConcept<Iterator>)); |
---|
219 | ... |
---|
220 | } |
---|
221 | \endcode |
---|
222 | |
---|
223 | \since New in yat 0.7 |
---|
224 | */ |
---|
225 | template <class T> |
---|
226 | class DataIteratorConcept |
---|
227 | { |
---|
228 | public: |
---|
229 | /// tag |
---|
230 | typedef typename weighted_iterator_traits<T>::type tag; |
---|
231 | /// value_type |
---|
232 | typedef typename std::iterator_traits<T>::value_type value_type; |
---|
233 | |
---|
234 | /** |
---|
235 | \brief function doing the concept test |
---|
236 | */ |
---|
237 | BOOST_CONCEPT_USAGE(DataIteratorConcept) |
---|
238 | { |
---|
239 | BOOST_CONCEPT_ASSERT((TrivialIterator<T>)); |
---|
240 | tag t; |
---|
241 | constraints(t); |
---|
242 | } |
---|
243 | private: |
---|
244 | void constraints(yat::utility::unweighted_iterator_tag t) const |
---|
245 | { |
---|
246 | BOOST_CONCEPT_ASSERT((boost::Convertible<value_type, double>)); |
---|
247 | } |
---|
248 | |
---|
249 | void constraints(yat::utility::weighted_iterator_tag t) const |
---|
250 | { |
---|
251 | BOOST_CONCEPT_ASSERT((boost::Convertible<value_type, DataWeight>)); |
---|
252 | } |
---|
253 | }; |
---|
254 | |
---|
255 | |
---|
256 | /** |
---|
257 | \brief Concept check for a \ref concept_distance |
---|
258 | |
---|
259 | This class is intended to be used in a <a |
---|
260 | href="\boost_url/concept_check/using_concept_check.htm"> |
---|
261 | BOOST_CONCEPT_ASSERT </a> |
---|
262 | |
---|
263 | \code |
---|
264 | template<class Distance> |
---|
265 | void some_function(double x) |
---|
266 | { |
---|
267 | BOOST_CONCEPT_ASSERT((DistanceConcept<Distance>)); |
---|
268 | ... |
---|
269 | } |
---|
270 | \endcode |
---|
271 | |
---|
272 | \since New in yat 0.7 |
---|
273 | */ |
---|
274 | template <class T> |
---|
275 | class DistanceConcept : public boost::CopyConstructible<T> |
---|
276 | { |
---|
277 | public: |
---|
278 | /** |
---|
279 | \brief function doing the concept test |
---|
280 | */ |
---|
281 | BOOST_CONCEPT_USAGE(DistanceConcept) |
---|
282 | { |
---|
283 | boost::random_access_iterator_archetype<double> unweighted; |
---|
284 | boost::random_access_iterator_archetype<DataWeight> weighted; |
---|
285 | double d=0; |
---|
286 | d += distance_(unweighted, unweighted, unweighted); |
---|
287 | d += distance_(unweighted, unweighted, weighted); |
---|
288 | d += distance_(weighted, weighted, unweighted); |
---|
289 | d += distance_(weighted, weighted, weighted); |
---|
290 | // avoid compiler warning |
---|
291 | func(d); |
---|
292 | } |
---|
293 | private: |
---|
294 | void func(double x) {} |
---|
295 | T distance_; |
---|
296 | }; |
---|
297 | |
---|
298 | }}} // of namespace utility, yat, and theplu |
---|
299 | #endif |
---|