home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / function.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  18.6 KB  |  554 lines

  1. #ifndef __FUNCTION_H
  2. #define __FUNCTION_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __STD_FUNCTIONAL__
  6. #define __STD_FUNCTIONAL__
  7.  
  8. /***************************************************************************
  9.  *
  10.  * functional - global template functions
  11.  *
  12.  ***************************************************************************
  13.  *
  14.  * Copyright (c) 1994
  15.  * Hewlett-Packard Company
  16.  *
  17.  * Permission to use, copy, modify, distribute and sell this software
  18.  * and its documentation for any purpose is hereby granted without fee,
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both that copyright notice and this permission notice appear
  21.  * in supporting documentation.  Hewlett-Packard Company makes no
  22.  * representations about the suitability of this software for any
  23.  * purpose.  It is provided "as is" without express or implied warranty.
  24.  *
  25.  *
  26.  ***************************************************************************
  27.  *
  28.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  29.  *
  30.  * This computer software is owned by Rogue Wave Software, Inc. and is
  31.  * protected by U.S. copyright laws and other laws and by international
  32.  * treaties.  This computer software is furnished by Rogue Wave Software,
  33.  * Inc. pursuant to a written license agreement and may be used, copied,
  34.  * transmitted, and stored only in accordance with the terms of such
  35.  * license and with the inclusion of the above copyright notice.  This
  36.  * computer software or any other copies thereof may not be provided or
  37.  * otherwise made available to any other person.
  38.  *
  39.  * U.S. Government Restricted Rights.  This computer software is provided
  40.  * with Restricted Rights.  Use, duplication, or disclosure by the
  41.  * Government is subject to restrictions as set forth in subparagraph (c)
  42.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  43.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  44.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  45.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  46.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  47.  *
  48.  **************************************************************************/
  49.  
  50. #include <stdcomp.h>
  51.  
  52. #ifndef _RWSTD_NO_NAMESPACE
  53. namespace std {
  54. #endif
  55.  
  56. //
  57. // The bases of many of the function objects here.
  58. //
  59.  
  60.   template <class Arg, class Result>
  61.   struct unary_function
  62.   {
  63.     typedef Arg argument_type;
  64.     typedef Result result_type;
  65.   };
  66.  
  67.   template <class Arg1, class Arg2, class Result>
  68.   struct binary_function
  69.   {
  70.     typedef Arg1 first_argument_type;
  71.     typedef Arg2 second_argument_type;
  72.     typedef Result result_type;
  73.   };
  74.  
  75. //
  76. // Arithmetic operators.
  77. //
  78.  
  79.   template <class T>
  80.   struct plus : public binary_function<T, T, T>
  81.   {
  82.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  83.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  84.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  85.     T operator() (const T& x, const T& y) const { return x + y; }
  86.   };
  87.  
  88.   template <class T>
  89.   struct minus : public binary_function<T, T, T>
  90.   {
  91.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  92.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  93.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  94.     T operator() (const T& x, const T& y) const { return x - y; }
  95.   };
  96.  
  97.   template <class T>
  98.   struct multiplies : public binary_function<T, T, T>
  99.   {
  100.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  101.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  102.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  103.     T operator() (const T& x, const T& y) const { return x * y; }
  104.   };
  105.  
  106.   template <class T>
  107.   struct divides : public binary_function<T, T, T>
  108.   {
  109.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  110.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  111.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  112.     T operator() (const T& x, const T& y) const { return x / y; }
  113.   };
  114.  
  115.   template <class T>
  116.   struct modulus : public binary_function<T, T, T>
  117.   {
  118.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  119.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  120.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  121.     T operator() (const T& x, const T& y) const { return x % y; }
  122.   };
  123.  
  124.   template <class T>
  125.   struct negate : public unary_function<T, T>
  126.   {
  127.     typedef _TYPENAME unary_function<T,T>::argument_type argument_type;
  128.     typedef _TYPENAME unary_function<T,T>::result_type result_type;
  129.     T operator() (const T& x) const { return -x; }
  130.   };
  131.  
  132. //
  133. // Comparisons.
  134. //
  135.  
  136.   template <class T>
  137.   struct equal_to : public binary_function<T, T, bool>
  138.   {
  139.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  140.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  141.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  142.     bool operator() (const T& x, const T& y) const { return x == y; }
  143.   };
  144.  
  145.   template <class T>
  146.   struct not_equal_to : public binary_function<T, T, bool>
  147.   {
  148.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  149.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  150.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  151.     bool operator() (const T& x, const T& y) const { return x != y; }
  152.   };
  153.  
  154.   template <class T>
  155.   struct greater : public binary_function<T, T, bool>
  156.   {
  157.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  158.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  159.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  160.     bool operator() (const T& x, const T& y) const { return x > y; }
  161.   };
  162.  
  163.   template <class T>
  164.   struct less : public binary_function<T, T, bool>
  165.   {
  166.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  167.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  168.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  169.     bool operator() (const T& x, const T& y) const { return x < y; }
  170.   };
  171.  
  172.   template <class T>
  173.   struct greater_equal : public binary_function<T, T, bool>
  174.   {
  175.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  176.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  177.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  178.     bool operator() (const T& x, const T& y) const { return x >= y; }
  179.   };
  180.  
  181.   template <class T>
  182.   struct less_equal : public binary_function<T, T, bool>
  183.   {
  184.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  185.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  186.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  187.     bool operator() (const T& x, const T& y) const { return x <= y; }
  188.   };
  189.  
  190. //
  191. // Logical operations.
  192. //
  193.  
  194.   template <class T>
  195.   struct logical_and : public binary_function<T, T, bool>
  196.   {
  197.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  198.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  199.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  200.     bool operator() (const T& x, const T& y) const { return x && y; }
  201.   };
  202.  
  203.   template <class T>
  204.   struct logical_or : public binary_function<T, T, bool>
  205.   {
  206.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  207.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  208.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  209.     bool operator() (const T& x, const T& y) const { return x || y; }
  210.   };
  211.  
  212.   template <class T>
  213.   struct logical_not : public unary_function<T, bool>
  214.   {
  215.     typedef _TYPENAME unary_function<T,bool>::argument_type argument_type;
  216.     typedef _TYPENAME unary_function<T,bool>::result_type result_type;
  217.     bool operator() (const T& x) const { return !x; }
  218.   };
  219.  
  220. //
  221. // Negators.
  222. //
  223.  
  224.   template <class Predicate>
  225.   class unary_negate : public unary_function<_TYPENAME Predicate::argument_type,
  226.   bool>
  227.   {
  228.   protected:
  229.     Predicate pred;
  230.   public:
  231.     typedef _TYPENAME unary_function<_TYPENAME Predicate::argument_type,bool>::argument_type argument_type;
  232.     typedef _TYPENAME unary_function<_TYPENAME Predicate::argument_type,bool>::result_type result_type;
  233.     _EXPLICIT unary_negate (const Predicate& x) : pred(x) {}
  234.     bool operator() (const _TYPENAME unary_function<
  235.                      _TYPENAME Predicate::argument_type,bool>::argument_type& x) const
  236.     { return !pred(x); }
  237.   };
  238.  
  239.   template <class Predicate>
  240.   inline unary_negate<Predicate> not1(const Predicate& pred)
  241.   {
  242.     return unary_negate<Predicate>(pred);
  243.   }
  244.  
  245.   template <class Predicate> 
  246.   class binary_negate
  247.     : public binary_function<_TYPENAME Predicate::first_argument_type,
  248.   _TYPENAME Predicate::second_argument_type, bool>
  249.   {
  250.   protected:
  251.     Predicate pred;
  252.   public:
  253.     typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  254.     _TYPENAME Predicate::second_argument_type, bool>::second_argument_type second_argument_type;
  255.     typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  256.     _TYPENAME Predicate::second_argument_type, bool>::first_argument_type first_argument_type;
  257.     typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  258.     _TYPENAME Predicate::second_argument_type, bool>::result_type result_type;
  259.     _EXPLICIT binary_negate (const Predicate& x) : pred(x) {}
  260.     bool operator() (const _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  261.                      _TYPENAME Predicate::second_argument_type, bool>::first_argument_type& x, 
  262.                      const _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  263.                      _TYPENAME Predicate::second_argument_type, bool>::second_argument_type& y) const
  264.     {
  265.       return !pred(x, y); 
  266.     }
  267.   };
  268.  
  269.   template <class Predicate>
  270.   inline binary_negate<Predicate> not2(const Predicate& pred)
  271.   {
  272.     return binary_negate<Predicate>(pred);
  273.   }
  274.  
  275. //
  276. // Binders.
  277. //
  278.  
  279.   template <class Operation> 
  280.   class binder1st :public unary_function<_TYPENAME Operation::second_argument_type,
  281.   _TYPENAME Operation::result_type>
  282.   {
  283.   protected:
  284.     Operation op;
  285.     _TYPENAME Operation::first_argument_type value;
  286.   public:
  287.     typedef _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  288.     _TYPENAME Operation::result_type>::argument_type argument_type;
  289.     typedef _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  290.     _TYPENAME Operation::result_type>::result_type result_type;
  291.     binder1st (const Operation& x,
  292.                const _TYPENAME Operation::first_argument_type& y)
  293.       : op(x), value(y) {}
  294.     _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  295.       _TYPENAME Operation::result_type>::result_type
  296.     operator() (const _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  297.                 _TYPENAME Operation::result_type>::argument_type& x) const
  298.     {
  299.       return op(value, x); 
  300.     }
  301.   };
  302.  
  303.   template <class Operation, class T>
  304.   inline binder1st<Operation> bind1st (const Operation& op, const T& x)
  305.   {
  306.     typedef _TYPENAME Operation::first_argument_type the_argument_type;
  307.     return binder1st<Operation>(op, the_argument_type(x));
  308.   }
  309.  
  310.   template <class Operation> 
  311.   class binder2nd : public unary_function<_TYPENAME Operation::first_argument_type,
  312.   _TYPENAME Operation::result_type>
  313.   {
  314.   protected:
  315.     Operation op;
  316.     _TYPENAME Operation::second_argument_type value;
  317.   public:
  318.     typedef _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  319.     _TYPENAME Operation::result_type>::argument_type argument_type;
  320.     typedef _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  321.     _TYPENAME Operation::result_type>::result_type result_type;
  322.     binder2nd (const Operation& x,
  323.                const _TYPENAME Operation::second_argument_type& y) 
  324.       : op(x), value(y) {}
  325.     _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  326.       _TYPENAME Operation::result_type>::result_type
  327.     operator() (const _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  328.                 _TYPENAME Operation::result_type>::argument_type& x) const
  329.     {
  330.       return op(x, value); 
  331.     }
  332.   };
  333.  
  334.   template <class Operation, class T>
  335.   inline binder2nd<Operation> bind2nd (const Operation& op, const T& x)
  336.   {
  337.     typedef _TYPENAME Operation::second_argument_type the_argument_type;
  338.     return binder2nd<Operation>(op, the_argument_type(x));
  339.   }
  340. //
  341. // Adaptors.
  342. //
  343.  
  344.   template <class Arg, class Result>
  345.   class pointer_to_unary_function : public unary_function<Arg, Result>
  346.   {
  347.   protected:
  348.     Result (*ptr)(Arg);
  349.   public:
  350.     typedef _TYPENAME unary_function<Arg,Result>::argument_type argument_type;
  351.     typedef _TYPENAME unary_function<Arg,Result>::result_type result_type;
  352.     _EXPLICIT pointer_to_unary_function (Result (*x)(Arg)) : ptr(x) {}
  353.     Result operator() (Arg x) const { return ptr(x); }
  354.   };
  355.  
  356.   template <class Arg, class Result>
  357.   inline pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg))
  358.   {
  359.     return pointer_to_unary_function<Arg, Result>(x);
  360.   }
  361.  
  362.   template <class Arg1, class Arg2, class Result>
  363.   class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
  364.   {
  365.   protected:
  366.     Result (*ptr)(Arg1, Arg2);
  367.   public:
  368.     typedef _TYPENAME binary_function<Arg1, Arg2, Result>::second_argument_type second_argument_type;
  369.     typedef _TYPENAME binary_function<Arg1, Arg2, Result>::first_argument_type first_argument_type;
  370.     typedef _TYPENAME binary_function<Arg1, Arg2, Result>::result_type result_type;
  371.     _EXPLICIT pointer_to_binary_function (Result (*x)(Arg1, Arg2)) : ptr(x) {}
  372.     Result operator() (Arg1 x, Arg2 y) const
  373.     {
  374.       return ptr(x, y); 
  375.     }
  376.   };
  377.  
  378.   template <class Arg1, class Arg2, class Result>
  379.   inline pointer_to_binary_function<Arg1, Arg2, Result> 
  380.   ptr_fun(Result (*x)(Arg1, Arg2))
  381.   {
  382.     return pointer_to_binary_function<Arg1, Arg2, Result>(x);
  383.   }
  384.  
  385. //
  386. // Pointer to member function adaptors
  387. //
  388. // mem_fun_t, mem_fun1_t
  389. //
  390.  
  391.   template <class S, class T> 
  392.   class mem_fun_t  : public unary_function<T*,S>
  393.   {
  394.     S (T::*pmf)();
  395.  
  396.   public:
  397.     _EXPLICIT mem_fun_t(S (T::*p)()) : pmf(p)
  398.     { ; }
  399.     S operator()(T* p) const
  400.     { return (p->*pmf)(); }
  401.   };
  402.   template <class S, class T, class A> 
  403.   class mem_fun1_t : public binary_function<T*,A,S>
  404.   {
  405.     S (T::*pmf)(A);
  406.  
  407.   public:
  408.     _EXPLICIT mem_fun1_t(S (T::*p)(A)) : pmf(p)
  409.     { ; }
  410.     S operator()(T* p, A a) const
  411.     { return (p->*pmf)(a); }
  412.   };
  413.  
  414.   template <class S, class T> 
  415.   inline mem_fun_t<S,T> mem_fun(S (T::*f)())
  416.   {
  417.     return mem_fun_t<S,T>(f);
  418.   }
  419.  
  420.   template <class S, class T, class A> 
  421.   inline mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A))
  422.   {
  423.     return mem_fun1_t<S,T,A>(f);
  424.   }
  425.  
  426. //
  427. // mem_fun_ref_t, mem_fun1_ref_t
  428. //
  429.  
  430.   template <class S, class T> 
  431.   class mem_fun_ref_t  : public unary_function<T,S>
  432.   {
  433.     S (T::*pmf)();
  434.  
  435.   public:
  436.     _EXPLICIT mem_fun_ref_t(S (T::*p)()) : pmf(p)
  437.     { ; }
  438.     S operator()(T& p) const
  439.     { return (p.*pmf)(); }
  440.   };
  441.   template <class S, class T, class A> 
  442.   class mem_fun1_ref_t : public binary_function<T,A,S>
  443.   {
  444.     S (T::*pmf)(A);
  445.  
  446.   public:
  447.     _EXPLICIT mem_fun1_ref_t(S (T::*p)(A)) : pmf(p) 
  448.     { ; }
  449.     S operator()(T& p, A a) const
  450.     { return (p.*pmf)(a); }
  451.   };
  452.  
  453.   template <class S, class T> 
  454.   inline mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)())
  455.   {
  456.     return mem_fun_ref_t<S,T>(f);
  457.   }
  458.  
  459.   template <class S, class T, class A> 
  460.   inline mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A))
  461.   {
  462.     return mem_fun1_ref_t<S,T,A>(f);
  463.   }
  464.  
  465.   // 
  466.   // const_mem_fun_t and const_mem_fun1_t
  467.   //
  468.  
  469.   template <class S, class T> 
  470.   class const_mem_fun_t  : public unary_function<T*,S>
  471.   {
  472.     S (T::*pmf)() const;
  473.  
  474.   public:
  475.     _EXPLICIT const_mem_fun_t(S (T::*p)() const) : pmf(p)
  476.     { ; }
  477.     S operator()(const T* p) const
  478.     { return (p->*pmf)(); }
  479.   };
  480.   template <class S, class T, class A> 
  481.   class const_mem_fun1_t : public binary_function<T*,A,S>
  482.   {
  483.     S (T::*pmf)(A) const;
  484.  
  485.   public:
  486.     _EXPLICIT const_mem_fun1_t(S (T::*p)(A) const) : pmf(p)
  487.     { ; }
  488.     S operator()(const T* p, A a) const
  489.     { return (p->*pmf)(a); }
  490.   };
  491.  
  492.   template <class S, class T> 
  493.   inline const_mem_fun_t<S,T> mem_fun(S (T::*f)() const)
  494.   {
  495.     return const_mem_fun_t<S,T>(f);
  496.   }
  497.  
  498.   template <class S, class T, class A> 
  499.   inline const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const)
  500.   {
  501.     return const_mem_fun1_t<S,T,A>(f);
  502.   }
  503.  
  504. //
  505. // const_mem_fun_ref_t, const_mem_fun1_ref_t
  506. //
  507.  
  508.   template <class S, class T> 
  509.   class const_mem_fun_ref_t  : public unary_function<T,S>
  510.   {
  511.     S (T::*pmf)() const;
  512.  
  513.   public:
  514.     _EXPLICIT const_mem_fun_ref_t(S (T::*p)() const) : pmf(p)
  515.     { ; }
  516.     S operator()(const T& p) const
  517.     { return (p.*pmf)(); }
  518.   };
  519.   template <class S, class T, class A> 
  520.   class const_mem_fun1_ref_t : public binary_function<T,A,S>
  521.   {
  522.     S (T::*pmf)(A) const;
  523.  
  524.   public:
  525.     _EXPLICIT const_mem_fun1_ref_t(S (T::*p)(A) const) : pmf(p) 
  526.     { ; }
  527.     S operator()(const T& p, A a) const
  528.     { return (p.*pmf)(a); }
  529.   };
  530.  
  531.   template <class S, class T> 
  532.   inline const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const)
  533.   {
  534.     return const_mem_fun_ref_t<S,T>(f);
  535.   }
  536.  
  537.   template <class S, class T, class A> 
  538.   inline const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const)
  539.   {
  540.     return const_mem_fun1_ref_t<S,T,A>(f);
  541.   }
  542.  
  543. #ifndef _RWSTD_NO_NAMESPACE
  544. }
  545. #endif
  546.  
  547. #endif /*__STD_FUNCTIONAL__*/
  548. #ifndef __USING_STD_NAMES__
  549.   using namespace std;
  550. #endif
  551.  
  552. #pragma option pop
  553. #endif /* __FUNCTION_H */
  554.