functional.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_FUNCTIONAL_HPP__
#define __CLAW_FUNCTIONAL_HPP__
#include <utility>
namespace claw
{
/*-------------------------------------------------------------------------*/
template <class T1, class T2>
class first
{
public:
using argument_type = std::pair<T1, T2>&;
using result_type = T1&;
public:
T1& operator()( std::pair<T1, T2>& p ) const
{
return p.first;
} // operator() const
}; // class first
/*-------------------------------------------------------------------------*/
template <class T1, class T2>
class const_first
{
public:
using argument_type = const std::pair<T1, T2>&;
using result_type = const T1&;
public:
const T1& operator()( const std::pair<T1, T2>& p ) const
{
return p.first;
} // operator()
}; // class const_first
/*-------------------------------------------------------------------------*/
template<class Pair>
class pair_first:
public first<typename Pair::first_type, typename Pair::second_type>
{
// nothing
}; // class pair_first
/*-------------------------------------------------------------------------*/
template<class Pair>
class const_pair_first:
public const_first<typename Pair::first_type, typename Pair::second_type>
{
// nothing
}; // class const_pair_first
/*-------------------------------------------------------------------------*/
template <class T1, class T2>
class second
{
public:
using argument_type = std::pair<T1, T2>&;
using result_type = T2&;
public:
T2& operator()( std::pair<T1, T2>& p ) const
{
return p.second;
} // operator() const
}; // class second
/*-------------------------------------------------------------------------*/
template <class T1, class T2>
class const_second
{
public:
using argument_type = const std::pair<T1, T2>&;
using result_type = const T2&;
public:
const T2& operator()( const std::pair<T1, T2>& p ) const
{
return p.second;
} // operator()
}; // class const_second
/*-------------------------------------------------------------------------*/
template< class Pair >
class pair_second: public second< typename Pair::first_type,
typename Pair::second_type >
{
// nothing
}; // class pair_second
/*-------------------------------------------------------------------------*/
template<class Pair>
class const_pair_second:
public const_second<typename Pair::first_type, typename Pair::second_type>
{
public:
const_pair_second() {}
template<typename F, typename S>
const_pair_second( const second<F, S>& )
{}
}; // class const_pair_second
/*-------------------------------------------------------------------------*/
template<class T>
class unary_true
{
public:
using argument_type = const T&;
using result_type = bool;
public:
bool operator()( const T& t ) const { return true; }
}; // class unary_true
/*-------------------------------------------------------------------------*/
template<class T, class U>
class binary_true
{
public:
using first_argument_type = const T&;
using second_argument_type = const U&;
using result_type = bool;
public:
bool operator()( const T& t, const U& u ) const
{
return true;
} // operator()
}; // class binary_true
/*-------------------------------------------------------------------------*/
template<typename F1, typename F2>
class unary_compose
{
public:
using argument_type = typename F2::argument_type;
using result_type = typename F1::result_type;
public:
unary_compose() {}
template<typename G1, typename G2>
unary_compose( const unary_compose<G1, G2>& that ) { }
typename F1::result_type
operator()( typename F2::argument_type& a ) const
{
return F1()( F2()(a) );
}
}; // class unary_compose
/*-------------------------------------------------------------------------*/
template<typename T>
class delete_function
{
public:
using argument_type = const T&;
using result_type = void;
public:
void operator()( const T& a ) const
{
delete a;
}
}; // class delete_function
/*-------------------------------------------------------------------------*/
template<typename T>
class clone
{
public:
using argument_type = const T*;
using result_type = T*;
public:
T* operator()( const T* a ) const
{
return new T(*a);
}
}; // class clone
/*-------------------------------------------------------------------------*/
template<typename T>
class dereference
{
public:
using argument_type = T*;
using result_type = T&;
public:
T& operator()( T* a ) const
{
return *a;
}
}; // class dereference
/*-------------------------------------------------------------------------*/
template<typename T>
class const_dereference
{
public:
using argument_type = const T*;
using result_type = const T&;
public:
const_dereference() { }
const_dereference( const dereference<T>& ) { }
const_dereference( const const_dereference<T>& ) { }
const T& operator()( const T* a ) const
{
return *a;
}
}; // class const_dereference
} // namespace claw
#endif // __CLAW_FUNCTIONAL_HPP__
claw::binary_true
Always true binary predicate.
Definition functional.hpp:213
claw::clone
Function object that clones a pointer.
Definition functional.hpp:304
claw::const_first
Fuction object to get the first element of a std::pair.
Definition functional.hpp:63
claw::const_pair_first
Fuction object to get the first element of a std::pair.
Definition functional.hpp:100
claw::const_second
Fuction object to get the second element of a std::pair.
Definition functional.hpp:130
claw::delete_function
Function object that deletes a pointer.
Definition functional.hpp:280
claw::dereference
Function object that dereferences a pointer.
Definition functional.hpp:327
claw::first
Fuction object to get the first element of a std::pair.
Definition functional.hpp:44
claw::pair_first
Fuction object to get the first element of a std::pair.
Definition functional.hpp:86
claw::pair_second
Fuction object to get the second element of a std::pair.
Definition functional.hpp:153
claw::second
Fuction object to get the second element of a std::pair.
Definition functional.hpp:111
claw::unary_compose::unary_compose
unary_compose(const unary_compose< G1, G2 > &that)
Copy constructor.
Definition functional.hpp:256
claw::unary_compose::operator()
F1::result_type operator()(typename F2::argument_type &a) const
Return (F1 o F2)(a).
Definition functional.hpp:262
claw::unary_true
Always true unary predicate.
Definition functional.hpp:190
claw
This is the main namespace.
Definition algorithm.hpp:34