Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*      Copyright (C) 2004 Garrett A. Kajmowicz
  2.         This file is part of the uClibc++ Library.
  3.         This library is free software; you can redistribute it and/or
  4.         modify it under the terms of the GNU Lesser General Public
  5.         License as published by the Free Software Foundation; either
  6.         version 2.1 of the License, or (at your option) any later version.
  7.  
  8.         This library is distributed in the hope that it will be useful,
  9.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.         Lesser General Public License for more details.
  12.  
  13.         You should have received a copy of the GNU Lesser General Public
  14.         License along with this library; if not, write to the Free Software
  15.         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  16. */
  17.  
  18.  
  19. #ifndef __STD_HEADER_FUNCTIONAL
  20. #define __STD_HEADER_FUNCTIONAL 1
  21.  
  22. #include <basic_definitions>
  23.  
  24. #pragma GCC visibility push(default)
  25.  
  26. namespace std{
  27.  
  28.         template <class Arg, class Result> struct unary_function;
  29.         template <class Arg1, class Arg2, class Result> struct binary_function;
  30.  
  31.         template <class T> struct plus;
  32.         template <class T> struct minus;
  33.         template <class T> struct multiplies;
  34.         template <class T> struct divides;
  35.         template <class T> struct modulus;
  36.         template <class T> struct negate;
  37.  
  38.         template <class T> struct equal_to;
  39.         template <class T> struct not_equal_to;
  40.         template <class T> struct greater;
  41.         template <class T> struct less;
  42.         template <class T> struct greater_equal;
  43.         template <class T> struct less_equal;
  44.  
  45.         template <class T> struct logical_and;
  46.         template <class T> struct logical_or;
  47.         template <class T> struct logical_not;
  48.  
  49.         template <class Predicate> struct unary_negate;
  50.         template <class Predicate> unary_negate<Predicate>  not1(const Predicate&);
  51.         template <class Predicate> struct binary_negate;
  52.         template <class Predicate> binary_negate<Predicate> not2(const Predicate&);
  53.  
  54.  
  55.         template <class Operation> class binder1st;
  56.         template <class Operation, class T> binder1st<Operation> bind1st(const Operation&, const T&);
  57.         template <class Operation> class binder2nd;
  58.         template <class Operation, class T> binder2nd<Operation> bind2nd(const Operation&, const T&);
  59.  
  60.         template <class Arg, class Result> class pointer_to_unary_function;
  61.         template <class Arg, class Result> pointer_to_unary_function<Arg,Result> ptr_fun(Result (*)(Arg));
  62.         template <class Arg1, class Arg2, class Result> class pointer_to_binary_function;
  63.         template <class Arg1, class Arg2, class Result>
  64.                 pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*)(Arg1,Arg2));
  65.  
  66.         template<class S, class T> class mem_fun_t;
  67.         template<class S, class T, class A> class mem_fun1_t;
  68.         template<class S, class T> class const_mem_fun_t;
  69.         template<class S, class T, class A> class const_mem_fun1_t;
  70.         template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)());
  71.         template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));
  72.         template<class S, class T> class mem_fun_ref_t;
  73.         template<class S, class T, class A> class mem_fun1_ref_t;
  74.         template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)());
  75.         template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun1_ref(S (T::*f)(A));
  76.  
  77.         //Implementation
  78.  
  79.         template <class Arg, class Result> struct _UCXXEXPORT unary_function{
  80.                 typedef Arg argument_type;
  81.                 typedef Result result_type;
  82.         };
  83.  
  84.  
  85.         template <class Arg1, class Arg2, class Result> struct _UCXXEXPORT binary_function{
  86.                 typedef Arg1   first_argument_type;
  87.                 typedef Arg2   second_argument_type;
  88.                 typedef Result result_type;
  89.         };
  90.  
  91.         template <class T> struct _UCXXEXPORT plus : binary_function<T,T,T>{
  92.                 T operator()(const T& x, const T& y) const{
  93.                         return x + y;
  94.                 }
  95.         };
  96.  
  97.         template <class T> struct _UCXXEXPORT minus : binary_function<T,T,T>{
  98.                 T operator()(const T& x, const T& y) const{
  99.                         return x - y;
  100.                 }
  101.         };
  102.  
  103.         template <class T> struct _UCXXEXPORT multiplies : binary_function<T,T,T>{
  104.                 T operator()(const T& x, const T& y) const{
  105.                         return x * y;
  106.                 }
  107.         };
  108.  
  109.         template <class T> struct _UCXXEXPORT divides : binary_function<T,T,T>{
  110.                 T operator()(const T& x, const T& y) const{
  111.                         return x / y;
  112.                 }
  113.         };
  114.  
  115.         template <class T> struct _UCXXEXPORT modulus : binary_function<T,T,T>{
  116.                 T operator()(const T& x, const T& y) const{
  117.                         return x % y;
  118.                 }
  119.         };
  120.  
  121.         template <class T> struct _UCXXEXPORT negate : unary_function<T,T>{
  122.                 T operator()(const T& x) const{
  123.                         return -x;
  124.                 }
  125.         };
  126.  
  127.         template <class T> struct _UCXXEXPORT equal_to : binary_function<T,T,bool>{
  128.                 bool operator()(const T& x, const T& y) const{
  129.                         return (x == y);
  130.                 }
  131.         };
  132.  
  133.         template <class T> struct _UCXXEXPORT not_equal_to : binary_function<T,T,bool>{
  134.                 bool operator()(const T& x, const T& y) const{
  135.                         return (x != y);
  136.                 }
  137.         };
  138.  
  139.         template <class T> struct _UCXXEXPORT greater : binary_function<T,T,bool>{
  140.                 bool operator()(const T& x, const T& y) const{
  141.                         return (x > y);
  142.                 }
  143.         };
  144.  
  145.         template <class T> struct _UCXXEXPORT less : binary_function<T,T,bool>{
  146.                 bool operator()(const T& x, const T& y) const{
  147.                         return (x < y);
  148.                 }
  149.         };
  150.  
  151.         template <class T> struct _UCXXEXPORT greater_equal : binary_function<T,T,bool>{
  152.                 bool operator()(const T& x, const T& y) const{
  153.                         return (x >= y);
  154.                 }
  155.         };
  156.  
  157.         template <class T> struct _UCXXEXPORT less_equal : binary_function<T,T,bool>{
  158.                 bool operator()(const T& x, const T& y) const{
  159.                         return (x <= y);
  160.                 }
  161.         };
  162.  
  163.         template <class T> struct _UCXXEXPORT logical_and : binary_function<T,T,bool> {
  164.                 bool operator()(const T& x, const T& y) const{
  165.                         return (x && y);
  166.                 }
  167.         };
  168.  
  169.         template <class T> struct _UCXXEXPORT logical_or : binary_function<T,T,bool> {
  170.                 bool operator()(const T& x, const T& y) const{
  171.                         return (x || y);
  172.                 }
  173.         };
  174.  
  175.         template <class T> struct _UCXXEXPORT logical_not : unary_function<T,bool> {
  176.                 bool operator()(const T& x) const{
  177.                         return !x;
  178.                 }
  179.         };
  180.  
  181.         template <class Predicate> class _UCXXEXPORT unary_negate
  182.                 : public unary_function<typename Predicate::argument_type,bool>
  183.         {
  184.         public:
  185.                 explicit unary_negate(const Predicate& pred) : p(pred) {  }
  186.                 bool operator()(const typename Predicate::argument_type& x) const{
  187.                         return !p(x);
  188.                 }
  189.         private:
  190.                 Predicate p;
  191.         };
  192.  
  193.  
  194.         template <class Predicate> _UCXXEXPORT unary_negate<Predicate> not1(const Predicate& pred){
  195.                 return unary_negate<Predicate>(pred);
  196.         }
  197.  
  198.  
  199.         template <class Predicate> class _UCXXEXPORT binary_negate : public
  200.                 binary_function<typename Predicate::first_argument_type,
  201.                         typename Predicate::second_argument_type, bool>
  202.         {
  203.         public:
  204.                 explicit binary_negate(const Predicate& pred) : p(pred) {  }
  205.                 bool operator()(const typename Predicate::first_argument_type& x,
  206.                         const typename Predicate::second_argument_type& y) const
  207.                 {
  208.                         return !p(x, y);
  209.                 }
  210.         private:
  211.                 Predicate p;
  212.         };
  213.  
  214.  
  215.         template <class Predicate> _UCXXEXPORT binary_negate<Predicate> not2(const Predicate& pred){
  216.                 return binary_negate<Predicate>(pred);
  217.         }
  218.  
  219.  
  220.         template <class Operation> class _UCXXEXPORT binder1st
  221.                 : public unary_function<typename Operation::second_argument_type,
  222.                         typename Operation::result_type>
  223.         {
  224.         protected:
  225.                 Operation                      op;
  226.                 typename Operation::first_argument_type value;
  227.         public:
  228.                 binder1st(const Operation& x, const typename Operation::first_argument_type& y) : op(x), value(y){  }
  229.                 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const{
  230.                         return op(value,x);
  231.                 }
  232.         };
  233.  
  234.  
  235.         template <class Operation, class T> _UCXXEXPORT binder1st<Operation> bind1st(const Operation& op, const T& x){
  236.                 return binder1st<Operation>(op, typename Operation::first_argument_type(x));
  237.         }
  238.  
  239.  
  240.         template <class Operation> class _UCXXEXPORT binder2nd
  241.                 : public unary_function<typename Operation::first_argument_type,
  242.                         typename Operation::result_type>
  243.         {
  244.         protected:
  245.                 Operation                       op;
  246.                 typename Operation::second_argument_type value;
  247.         public:
  248.                 binder2nd(const Operation& x, const typename Operation::second_argument_type& y) : op(x), value(y) {  }
  249.                 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const{
  250.                         return op(x,value);
  251.                 }
  252.         };
  253.  
  254.  
  255.         template <class Operation, class T> _UCXXEXPORT
  256.                 binder2nd<Operation> bind2nd(const Operation& op, const T& x)
  257.         {
  258.                 return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
  259.         }
  260.  
  261.  
  262.         template <class Arg, class Result> class _UCXXEXPORT
  263.                 pointer_to_unary_function : public unary_function<Arg, Result>
  264.         {
  265.         protected:
  266.                 Result (*func)(Arg);
  267.         public:
  268.                 explicit pointer_to_unary_function(Result (*f)(Arg)) : func(f) {  }
  269.                 Result operator()(Arg x) const{
  270.                         return func(x);
  271.                 }
  272.         };
  273.  
  274.  
  275.         template <class Arg, class Result> _UCXXEXPORT pointer_to_unary_function<Arg, Result> ptr_fun(Result (*f)(Arg)){
  276.                 return pointer_to_unary_function<Arg, Result>(f);
  277.         }
  278.  
  279.  
  280.         template <class Arg1, class Arg2, class Result> class _UCXXEXPORT
  281.                 pointer_to_binary_function : public binary_function<Arg1,Arg2,Result>
  282.         {
  283.         protected:
  284.                 Result (*func)(Arg1, Arg2);
  285.         public:
  286.                 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2)) : func(f) {  }
  287.                 Result operator()(Arg1 x, Arg2 y) const{
  288.                         return func(x, y);
  289.                 }
  290.         };
  291.  
  292.         template <class Arg1, class Arg2, class Result> _UCXXEXPORT
  293.                 pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1, Arg2))
  294.         {
  295.                 return pointer_to_binary_function<Arg1,Arg2,Result>(f);
  296.         }
  297.  
  298.  
  299.         template <class S, class T> class _UCXXEXPORT mem_fun_t
  300.                 : public unary_function<T*, S>
  301.         {
  302.         public:
  303.                 explicit mem_fun_t(S (T::*p)()) : m(p) {  }
  304.                 S operator()(T* p) const { return (p->*m)(); }
  305.         private:
  306.                 S (T::*m)();
  307.         };
  308.  
  309.  
  310.         template <class S, class T, class A> class _UCXXEXPORT mem_fun1_t
  311.                 : public binary_function<T*, A, S>
  312.         {
  313.         public:
  314.                 explicit mem_fun1_t(S (T::*p)(A)) : m(p) {  }
  315.                 S operator()(T* p, A x) const { return (p->*m)(x); }
  316.         private:
  317.                 S (T::*m)(A);
  318.         };
  319.  
  320.  
  321.         template <class S, class T> class _UCXXEXPORT const_mem_fun_t
  322.                 : public unary_function<const T*, S>
  323.         {
  324.         public:
  325.                 explicit const_mem_fun_t(S (T::*p)() const) : m(p) {  }
  326.                 S operator()(const T* p) const { return (p->*m)(); }
  327.         private:
  328.                 S (T::*m)() const;
  329.         };
  330.  
  331.  
  332.         template <class S, class T, class A> class _UCXXEXPORT const_mem_fun1_t
  333.                 : public binary_function<T*, A, S>
  334.         {
  335.         public:
  336.                 explicit const_mem_fun1_t(S (T::*p)(A) const) : m(p) {  }
  337.                 S operator()(const T* p, A x) const { return (p->*m)(x); }
  338.         private:
  339.                 S (T::*m)(A) const;
  340.         };
  341.  
  342.  
  343.         template<class S, class T> _UCXXEXPORT mem_fun_t<S,T> mem_fun(S (T::*f)()){
  344.                 return mem_fun_t<S, T>(f);
  345.         }
  346.  
  347.         template<class S, class T> _UCXXEXPORT const_mem_fun_t<S,T> mem_fun(S (T::*f)() const){
  348.                 return const_mem_fun_t<S, T>(f);
  349.         }
  350.  
  351.         template<class S, class T, class A> _UCXXEXPORT mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)){
  352.                 return mem_fun1_t<S, T, A>(f);
  353.         }
  354.  
  355.         template<class S, class T, class A> _UCXXEXPORT const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const){
  356.                 return const_mem_fun1_t<S, T, A>(f);
  357.         }
  358.  
  359.         template <class S, class T> class _UCXXEXPORT mem_fun_ref_t
  360.                 : public unary_function<T, S>
  361.         {
  362.         public:
  363.                 explicit mem_fun_ref_t(S (T::*p)()) : mf(p) {  }
  364.                 S operator()(T& p) { return (p.*mf)(); }
  365.         private:
  366.                 S (T::*mf)();
  367.         };
  368.  
  369.         template <class S, class T, class A> class _UCXXEXPORT mem_fun1_ref_t
  370.                 : public binary_function<T, A, S>
  371.         {
  372.         public:
  373.                 explicit mem_fun1_ref_t(S (T::*p)(A)) : mf(p) {  }
  374.                 S operator()(T& p, A x) { return (p.*mf)(x); }
  375.         private:
  376.                 S (T::*mf)(A);
  377.         };
  378.  
  379.         template<class S, class T> _UCXXEXPORT mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()){
  380.                 return mem_fun_ref_t<S,T>(f);
  381.         }
  382.  
  383.         template<class S, class T, class A> _UCXXEXPORT mem_fun1_ref_t<S,T,A> mem_fun1_ref(S (T::*f)(A)){
  384.                 return mem_fun1_ref_t<S,T,A>(f);
  385.         }
  386.  
  387.  
  388. }
  389.  
  390.  
  391. //These are SGI extensions which are checked for by some conformance checks.  They
  392. // are *NOT* part of the C++ standard, however
  393.  
  394. template <class Op1, class Op2> class _UCXXEXPORT unary_compose :
  395.         public std::unary_function<typename Op2::argument_type,
  396.                 typename Op1::result_type>
  397. {
  398. protected:
  399.         Op1 mf1;
  400.         Op2 mf2;
  401. public:
  402.         unary_compose(const Op1& x, const Op2& y) : mf1(x), mf2(y) {  }
  403.         typename Op1::result_type operator()(const typename Op2::argument_type& x) const {
  404.                 return mf1(mf2(x));
  405.         }
  406. };
  407.  
  408. template <class Op1, class Op2> _UCXXEXPORT
  409. inline unary_compose<Op1, Op2>
  410. compose1(const Op1& fn1, const Op2& fn2){
  411.         return unary_compose<Op1, Op2>(fn1, fn2);
  412. }
  413.  
  414. template <class Op1, class Op2, class Op3> class _UCXXEXPORT binary_compose :
  415.         public std::unary_function<typename Op2::argument_type, typename Op1::result_type>
  416. {
  417. protected:
  418.         Op1 mf1;
  419.         Op2 mf2;
  420.         Op3 mf3;
  421. public:
  422.         binary_compose(const Op1 & x, const Op2 & y, const Op3 & z)
  423.                 : mf1(x), mf2(y), mf3(z){  }
  424.         typename Op1::result_type operator()(const typename Op2::argument_type & x) const {
  425.                 return mf1(mf2(x), mf3(x));
  426.         }
  427. };
  428.  
  429. template <class Op1, class Op2, class Op3> inline _UCXXEXPORT binary_compose<Op1, Op2, Op3>
  430. compose2(const Op1 & fn1, const Op2 & fn2, const Op3 & fn3){
  431.         return binary_compose<Op1, Op2, Op3>(fn1, fn2, fn3);
  432. }
  433.  
  434. #pragma GCC visibility pop
  435.  
  436. #endif
  437.  
  438.  
  439.  
  440.