/* Copyright (C) 2004 Garrett A. Kajmowicz This file is part of the uClibc++ Library. 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __STD_HEADER_FUNCTIONAL #define __STD_HEADER_FUNCTIONAL 1 #include namespace std{ template struct unary_function; template struct binary_function; template struct plus; template struct minus; template struct multiplies; template struct divides; template struct modulus; template struct negate; template struct equal_to; template struct not_equal_to; template struct greater; template struct less; template struct greater_equal; template struct less_equal; template struct logical_and; template struct logical_or; template struct logical_not; template class unary_negate; template unary_negate not1(const Predicate&); template class binary_negate; template binary_negate not2(const Predicate&); template class binder1st; template binder1st bind1st(const Operation&, const T&); template class binder2nd; template binder2nd bind2nd(const Operation&, const T&); template class pointer_to_unary_function; template pointer_to_unary_function ptr_fun(Result (*)(Arg)); template class pointer_to_binary_function; template pointer_to_binary_function ptr_fun(Result (*)(Arg1,Arg2)); template class mem_fun_t; template class mem_fun1_t; template class const_mem_fun_t; template class const_mem_fun1_t; template mem_fun_t mem_fun(S (T::*f)()); template mem_fun1_t mem_fun(S (T::*f)(A)); template class mem_fun_ref_t; template class mem_fun1_ref_t; template mem_fun_ref_t mem_fun_ref(S (T::*f)()); template mem_fun1_ref_t mem_fun1_ref(S (T::*f)(A)); //Implementation template struct _UCXXEXPORT unary_function{ typedef Arg argument_type; typedef Result result_type; }; template struct _UCXXEXPORT binary_function{ typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; }; template struct _UCXXEXPORT plus : binary_function{ T operator()(const T& x, const T& y) const{ return x + y; } }; template struct _UCXXEXPORT minus : binary_function{ T operator()(const T& x, const T& y) const{ return x - y; } }; template struct _UCXXEXPORT multiplies : binary_function{ T operator()(const T& x, const T& y) const{ return x * y; } }; template struct _UCXXEXPORT divides : binary_function{ T operator()(const T& x, const T& y) const{ return x / y; } }; template struct _UCXXEXPORT modulus : binary_function{ T operator()(const T& x, const T& y) const{ return x % y; } }; template struct _UCXXEXPORT negate : unary_function{ T operator()(const T& x) const{ return -x; } }; template struct _UCXXEXPORT equal_to : binary_function{ bool operator()(const T& x, const T& y) const{ return (x == y); } }; template struct _UCXXEXPORT not_equal_to : binary_function{ bool operator()(const T& x, const T& y) const{ return (x != y); } }; template struct _UCXXEXPORT greater : binary_function{ bool operator()(const T& x, const T& y) const{ return (x > y); } }; template struct _UCXXEXPORT less : binary_function{ bool operator()(const T& x, const T& y) const{ return (x < y); } }; template struct _UCXXEXPORT greater_equal : binary_function{ bool operator()(const T& x, const T& y) const{ return (x >= y); } }; template struct _UCXXEXPORT less_equal : binary_function{ bool operator()(const T& x, const T& y) const{ return (x <= y); } }; template struct _UCXXEXPORT logical_and : binary_function { bool operator()(const T& x, const T& y) const{ return (x && y); } }; template struct _UCXXEXPORT logical_or : binary_function { bool operator()(const T& x, const T& y) const{ return (x || y); } }; template struct _UCXXEXPORT logical_not : unary_function { bool operator()(const T& x) const{ return !x; } }; template class _UCXXEXPORT unary_negate : public unary_function { public: explicit unary_negate(const Predicate& pred) : p(pred) { } bool operator()(const typename Predicate::argument_type& x) const{ return !p(x); } private: Predicate p; }; template _UCXXEXPORT unary_negate not1(const Predicate& pred){ return unary_negate(pred); } template class _UCXXEXPORT binary_negate : public binary_function { public: explicit binary_negate(const Predicate& pred) : p(pred) { } bool operator()(const typename Predicate::first_argument_type& x, const typename Predicate::second_argument_type& y) const { return !p(x, y); } private: Predicate p; }; template _UCXXEXPORT binary_negate not2(const Predicate& pred){ return binary_negate(pred); } template class _UCXXEXPORT binder1st : public unary_function { protected: Operation op; typename Operation::first_argument_type value; public: binder1st(const Operation& x, const typename Operation::first_argument_type& y) : op(x), value(y){ } typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const{ return op(value,x); } }; template _UCXXEXPORT binder1st bind1st(const Operation& op, const T& x){ return binder1st(op, typename Operation::first_argument_type(x)); } template class _UCXXEXPORT binder2nd : public unary_function { protected: Operation op; typename Operation::second_argument_type value; public: binder2nd(const Operation& x, const typename Operation::second_argument_type& y) : op(x), value(y) { } typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const{ return op(x,value); } }; template _UCXXEXPORT binder2nd bind2nd(const Operation& op, const T& x) { return binder2nd(op, typename Operation::second_argument_type(x)); } template class _UCXXEXPORT pointer_to_unary_function : public unary_function { protected: Result (*func)(Arg); public: explicit pointer_to_unary_function(Result (*f)(Arg)) : func(f) { } Result operator()(Arg x) const{ return func(x); } }; template _UCXXEXPORT pointer_to_unary_function ptr_fun(Result (*f)(Arg)){ return pointer_to_unary_function(f); } template class _UCXXEXPORT pointer_to_binary_function : public binary_function { protected: Result (*func)(Arg1, Arg2); public: explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2)) : func(f) { } Result operator()(Arg1 x, Arg2 y) const{ return func(x, y); } }; template _UCXXEXPORT pointer_to_binary_function ptr_fun(Result (*f)(Arg1, Arg2)) { return pointer_to_binary_function(f); } template class _UCXXEXPORT mem_fun_t : public unary_function { public: explicit mem_fun_t(S (T::*p)()) : m(p) { } S operator()(T* p) const { return (p->*m)(); } private: S (T::*m)(); }; template class _UCXXEXPORT mem_fun1_t : public binary_function { public: explicit mem_fun1_t(S (T::*p)(A)) : m(p) { } S operator()(T* p, A x) const { return (p->*m)(x); } private: S (T::*m)(A); }; template class _UCXXEXPORT const_mem_fun_t : public unary_function { public: explicit const_mem_fun_t(S (T::*p)() const) : m(p) { } S operator()(const T* p) const { return (p->*m)(); } private: S (T::*m)() const; }; template class _UCXXEXPORT const_mem_fun1_t : public binary_function { public: explicit const_mem_fun1_t(S (T::*p)(A) const) : m(p) { } S operator()(const T* p, A x) const { return (p->*m)(x); } private: S (T::*m)(A) const; }; template _UCXXEXPORT mem_fun_t mem_fun(S (T::*f)()){ return mem_fun_t(f); } template _UCXXEXPORT const_mem_fun_t mem_fun(S (T::*f)() const){ return const_mem_fun_t(f); } template _UCXXEXPORT mem_fun1_t mem_fun(S (T::*f)(A)){ return mem_fun1_t(f); } template _UCXXEXPORT const_mem_fun1_t mem_fun(S (T::*f)(A) const){ return const_mem_fun1_t(f); } template class _UCXXEXPORT mem_fun_ref_t : public unary_function { public: explicit mem_fun_ref_t(S (T::*p)()) : mf(p) { } S operator()(T& p) { return (p.*mf)(); } private: S (T::*mf)(); }; template class _UCXXEXPORT mem_fun1_ref_t : public binary_function { public: explicit mem_fun1_ref_t(S (T::*p)(A)) : mf(p) { } S operator()(T& p, A x) { return (p.*mf)(x); } private: S (T::*mf)(A); }; template _UCXXEXPORT mem_fun_ref_t mem_fun_ref(S (T::*f)()){ return mem_fun_ref_t(f); } template _UCXXEXPORT mem_fun1_ref_t mem_fun1_ref(S (T::*f)(A)){ return mem_fun1_ref_t(f); } } //These are SGI extensions which are checked for by some conformance checks. They // are *NOT* part of the C++ standard, however template class _UCXXEXPORT unary_compose : public std::unary_function { protected: Op1 mf1; Op2 mf2; public: unary_compose(const Op1& x, const Op2& y) : mf1(x), mf2(y) { } typename Op1::result_type operator()(const typename Op2::argument_type& x) const { return mf1(mf2(x)); } }; template _UCXXEXPORT inline unary_compose compose1(const Op1& fn1, const Op2& fn2){ return unary_compose(fn1, fn2); } template class _UCXXEXPORT binary_compose : public std::unary_function { protected: Op1 mf1; Op2 mf2; Op3 mf3; public: binary_compose(const Op1 & x, const Op2 & y, const Op3 & z) : mf1(x), mf2(y), mf3(z){ } typename Op1::result_type operator()(const typename Op2::argument_type & x) const { return mf1(mf2(x), mf3(x)); } }; template inline _UCXXEXPORT binary_compose compose2(const Op1 & fn1, const Op2 & fn2, const Op3 & fn3){ return binary_compose(fn1, fn2, fn3); } #endif