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 #include <functional>
35 
36 namespace claw
37 {
38  /*-------------------------------------------------------------------------*/
43  template <class T1, class T2>
44  class first: public std::unary_function< std::pair<T1, T2>, T1& >
45  {
46  public:
47  T1& operator()( std::pair<T1, T2>& p ) const
48  {
49  return p.first;
50  } // operator() const
51  }; // class first
52 
53  /*-------------------------------------------------------------------------*/
58  template <class T1, class T2>
59  class const_first:
60  public std::unary_function<const std::pair<T1, T2>, const T1&>
61  {
62  public:
63  const T1& operator()( const std::pair<T1, T2>& p ) const
64  {
65  return p.first;
66  } // operator()
67 
68  }; // class const_first
69 
70  /*-------------------------------------------------------------------------*/
77  template<class Pair>
78  class pair_first:
79  public first<typename Pair::first_type, typename Pair::second_type>
80  {
81  // nothing
82  }; // class pair_first
83 
84  /*-------------------------------------------------------------------------*/
91  template<class Pair>
93  public const_first<typename Pair::first_type, typename Pair::second_type>
94  {
95  // nothing
96  }; // class const_pair_first
97 
98  /*-------------------------------------------------------------------------*/
103  template <class T1, class T2>
104  class second: public std::unary_function< std::pair<T1, T2>, T2& >
105  {
106  public:
107  T2& operator()( std::pair<T1, T2>& p ) const
108  {
109  return p.second;
110  } // operator() const
111  }; // class second
112 
113  /*-------------------------------------------------------------------------*/
118  template <class T1, class T2>
120  public std::unary_function< const std::pair<T1, T2>, const T2& >
121  {
122  public:
123  const T2& operator()( const std::pair<T1, T2>& p ) const
124  {
125  return p.second;
126  } // operator()
127 
128  }; // class const_second
129 
130  /*-------------------------------------------------------------------------*/
137  template< class Pair >
138  class pair_second: public second< typename Pair::first_type,
139  typename Pair::second_type >
140  {
141  // nothing
142  }; // class pair_second
143 
144  /*-------------------------------------------------------------------------*/
151  template<class Pair>
153  public const_second<typename Pair::first_type, typename Pair::second_type>
154  {
155  public:
156  const_pair_second() {}
157 
158  template<typename F, typename S>
160  {}
161 
162  }; // class const_pair_second
163 
164  /*-------------------------------------------------------------------------*/
175  template<class T>
176  class unary_true: public std::unary_function<T, bool>
177  {
178  public:
179  bool operator()( const T& t ) const { return true; }
180  }; // class unary_true
181 
182  /*-------------------------------------------------------------------------*/
194  template<class T, class U>
195  class binary_true: public std::binary_function<T, U, bool>
196  {
197  public:
198  bool operator()( const T& t, const U& u ) const
199  {
200  return true;
201  } // operator()
202  }; // class binary_true
203 
204  /*-------------------------------------------------------------------------*/
216  template<typename F1, typename F2>
218  : public std::unary_function< typename F2::argument_type,
219  typename F1::result_type >
220  {
221  public:
222  unary_compose() {}
223 
231  template<typename G1, typename G2>
233 
237  typename F1::result_type
238  operator()( typename F2::argument_type& a ) const
239  {
240  return F1()( F2()(a) );
241  }
242  }; // class unary_compose
243 
244  /*-------------------------------------------------------------------------*/
254  template<typename T>
255  class delete_function: public std::unary_function<T, void>
256  {
257  public:
258  void operator()( const T& a ) const
259  {
260  delete a;
261  }
262  }; // class delete_function
263 
264  /*-------------------------------------------------------------------------*/
274  template<typename T>
275  class clone: public std::unary_function<T*, T*>
276  {
277  public:
278  T* operator()( const T* a ) const
279  {
280  return new T(*a);
281  }
282  }; // class clone
283 
284  /*-------------------------------------------------------------------------*/
293  template<typename T>
294  class dereference:
295  public std::unary_function<T*, T&>
296  {
297  public:
298  T& operator()( T* a ) const
299  {
300  return *a;
301  }
302 
303  }; // class dereference
304 
305  /*-------------------------------------------------------------------------*/
314  template<typename T>
316  public std::unary_function<const T*, const T&>
317  {
318  public:
319  const_dereference() { }
320  const_dereference( const dereference<T>& ) { }
322 
323  const T& operator()( const T* a ) const
324  {
325  return *a;
326  }
327 
328  }; // class const_dereference
329 } // namespace claw
330 
331 #endif // __CLAW_FUNCTIONAL_HPP__