Claw 1.7.3
functional.hpp
Go to the documentation of this file.
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
30#ifndef __CLAW_FUNCTIONAL_HPP__
31#define __CLAW_FUNCTIONAL_HPP__
32
33#include <utility>
34
35namespace claw
36{
37 /*-------------------------------------------------------------------------*/
42 template <class T1, class T2>
43 class first
44 {
45 public:
46 using argument_type = std::pair<T1, T2>&;
47 using result_type = T1&;
48
49 public:
50 T1& operator()( std::pair<T1, T2>& p ) const
51 {
52 return p.first;
53 } // operator() const
54 }; // class first
55
56 /*-------------------------------------------------------------------------*/
61 template <class T1, class T2>
63 {
64 public:
65 using argument_type = const std::pair<T1, T2>&;
66 using result_type = const T1&;
67
68 public:
69 const T1& operator()( const std::pair<T1, T2>& p ) const
70 {
71 return p.first;
72 } // operator()
73
74 }; // class const_first
75
76 /*-------------------------------------------------------------------------*/
83 template<class Pair>
85 public first<typename Pair::first_type, typename Pair::second_type>
86 {
87 // nothing
88 }; // class pair_first
89
90 /*-------------------------------------------------------------------------*/
97 template<class Pair>
99 public const_first<typename Pair::first_type, typename Pair::second_type>
100 {
101 // nothing
102 }; // class const_pair_first
103
104 /*-------------------------------------------------------------------------*/
109 template <class T1, class T2>
110 class second
111 {
112 public:
113 using argument_type = std::pair<T1, T2>&;
114 using result_type = T2&;
115
116 public:
117 T2& operator()( std::pair<T1, T2>& p ) const
118 {
119 return p.second;
120 } // operator() const
121 }; // class second
122
123 /*-------------------------------------------------------------------------*/
128 template <class T1, class T2>
130 {
131 public:
132 using argument_type = const std::pair<T1, T2>&;
133 using result_type = const T2&;
134
135 public:
136 const T2& operator()( const std::pair<T1, T2>& p ) const
137 {
138 return p.second;
139 } // operator()
140
141 }; // class const_second
142
143 /*-------------------------------------------------------------------------*/
150 template< class Pair >
151 class pair_second: public second< typename Pair::first_type,
152 typename Pair::second_type >
153 {
154 // nothing
155 }; // class pair_second
156
157 /*-------------------------------------------------------------------------*/
164 template<class Pair>
165 class const_pair_second:
166 public const_second<typename Pair::first_type, typename Pair::second_type>
167 {
168 public:
169 const_pair_second() {}
170
171 template<typename F, typename S>
172 const_pair_second( const second<F, S>& )
173 {}
174
175 }; // class const_pair_second
176
177 /*-------------------------------------------------------------------------*/
188 template<class T>
190 {
191 public:
192 using argument_type = const T&;
193 using result_type = bool;
194
195 public:
196 bool operator()( const T& t ) const { return true; }
197 }; // class unary_true
198
199 /*-------------------------------------------------------------------------*/
211 template<class T, class U>
213 {
214 public:
215 using first_argument_type = const T&;
216 using second_argument_type = const U&;
217 using result_type = bool;
218
219 public:
220 bool operator()( const T& t, const U& u ) const
221 {
222 return true;
223 } // operator()
224 }; // class binary_true
225
226 /*-------------------------------------------------------------------------*/
238 template<typename F1, typename F2>
239 class unary_compose
240 {
241 public:
242 using argument_type = typename F2::argument_type;
243 using result_type = typename F1::result_type;
244
245 public:
246 unary_compose() {}
247
255 template<typename G1, typename G2>
256 unary_compose( const unary_compose<G1, G2>& that ) { }
257
261 typename F1::result_type
262 operator()( typename F2::argument_type& a ) const
263 {
264 return F1()( F2()(a) );
265 }
266 }; // class unary_compose
267
268 /*-------------------------------------------------------------------------*/
278 template<typename T>
280 {
281 public:
282 using argument_type = const T&;
283 using result_type = void;
284
285 public:
286 void operator()( const T& a ) const
287 {
288 delete a;
289 }
290 }; // class delete_function
291
292 /*-------------------------------------------------------------------------*/
302 template<typename T>
303 class clone
304 {
305 public:
306 using argument_type = const T*;
307 using result_type = T*;
308
309 public:
310 T* operator()( const T* a ) const
311 {
312 return new T(*a);
313 }
314 }; // class clone
315
316 /*-------------------------------------------------------------------------*/
325 template<typename T>
327 {
328 public:
329 using argument_type = T*;
330 using result_type = T&;
331
332 public:
333 T& operator()( T* a ) const
334 {
335 return *a;
336 }
337
338 }; // class dereference
339
340 /*-------------------------------------------------------------------------*/
349 template<typename T>
350 class const_dereference
351 {
352 public:
353 using argument_type = const T*;
354 using result_type = const T&;
355
356 public:
357 const_dereference() { }
358 const_dereference( const dereference<T>& ) { }
359 const_dereference( const const_dereference<T>& ) { }
360
361 const T& operator()( const T* a ) const
362 {
363 return *a;
364 }
365
366 }; // class const_dereference
367} // namespace claw
368
369#endif // __CLAW_FUNCTIONAL_HPP__
Always true binary predicate.
Function object that clones a pointer.
Fuction object to get the first element of a std::pair.
Fuction object to get the first element of a std::pair.
Fuction object to get the second element of a std::pair.
Function object that deletes a pointer.
Function object that dereferences a pointer.
Fuction object to get the first element of a std::pair.
Fuction object to get the first element of a std::pair.
Fuction object to get the second element of a std::pair.
Fuction object to get the second element of a std::pair.
unary_compose(const unary_compose< G1, G2 > &that)
Copy constructor.
F1::result_type operator()(typename F2::argument_type &a) const
Return (F1 o F2)(a).
Always true unary predicate.
This is the main namespace.
Definition algorithm.hpp:34