home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / fctional < prev    next >
Text File  |  1998-06-16  |  9KB  |  314 lines

  1. // functional standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _FUNCTIONAL_
  8. #define _FUNCTIONAL_
  9. #include <xstddef>
  10.  
  11. #ifdef  _MSC_VER
  12. #pragma pack(push,8)
  13. #endif  /* _MSC_VER */
  14. _STD_BEGIN
  15.         // TEMPLATE STRUCT unary_function
  16. template<class _A, class _R>
  17.     struct unary_function {
  18.     typedef _A argument_type;
  19.     typedef _R result_type;
  20.     };
  21.         // TEMPLATE STRUCT binary_function
  22. template<class _A1, class _A2, class _R>
  23.     struct binary_function {
  24.     typedef _A1 first_argument_type;
  25.     typedef _A2 second_argument_type;
  26.     typedef _R result_type;
  27.     };
  28.         // TEMPLATE STRUCT plus
  29. template<class _Ty>
  30.     struct plus : binary_function<_Ty, _Ty, _Ty> {
  31.     _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  32.         {return (_X + _Y); }
  33.     };
  34.         // TEMPLATE STRUCT minus
  35. template<class _Ty>
  36.     struct minus : binary_function<_Ty, _Ty, _Ty> {
  37.     _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  38.         {return (_X - _Y); }
  39.     };
  40.         // TEMPLATE STRUCT multiplies
  41. template<class _Ty>
  42.     struct multiplies : binary_function<_Ty, _Ty, _Ty> {
  43.     _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  44.         {return (_X * _Y); }
  45.     };
  46.         // TEMPLATE STRUCT divides
  47. template<class _Ty>
  48.     struct divides : binary_function<_Ty, _Ty, _Ty> {
  49.     _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  50.         {return (_X / _Y); }
  51.     };
  52.         // TEMPLATE STRUCT modulus
  53. template<class _Ty>
  54.     struct modulus : binary_function<_Ty, _Ty, _Ty> {
  55.     _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  56.         {return (_X % _Y); }
  57.     };
  58.         // TEMPLATE STRUCT negate
  59. template<class _Ty>
  60.     struct negate : unary_function<_Ty, _Ty> {
  61.     _Ty operator()(const _Ty& _X) const
  62.         {return (-_X); }
  63.     };
  64.         // TEMPLATE STRUCT equal_to
  65. template<class _Ty>
  66.     struct equal_to : binary_function<_Ty, _Ty, bool> {
  67.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  68.         {return (_X == _Y); }
  69.     };
  70.         // TEMPLATE STRUCT not_equal_to
  71. template<class _Ty>
  72.     struct not_equal_to : binary_function<_Ty, _Ty, bool> {
  73.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  74.         {return (_X != _Y); }
  75.     };
  76.         // TEMPLATE STRUCT greater
  77. template<class _Ty>
  78.     struct greater : binary_function<_Ty, _Ty, bool> {
  79.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  80.         {return (_X > _Y); }
  81.     };
  82.         // TEMPLATE STRUCT less
  83. template<class _Ty>
  84.     struct less : binary_function<_Ty, _Ty, bool> {
  85.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  86.         {return (_X < _Y); }
  87.     };
  88.         // TEMPLATE STRUCT greater_equal
  89. template<class _Ty>
  90.     struct greater_equal : binary_function<_Ty, _Ty, bool> {
  91.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  92.         {return (_X >= _Y); }
  93.     };
  94.         // TEMPLATE STRUCT less_equal
  95. template<class _Ty>
  96.     struct less_equal : binary_function<_Ty, _Ty, bool> {
  97.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  98.         {return (_X <= _Y); }
  99.     };
  100.         // TEMPLATE STRUCT logical_and
  101. template<class _Ty>
  102.     struct logical_and : binary_function<_Ty, _Ty, bool> {
  103.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  104.         {return (_X && _Y); }
  105.     };
  106.         // TEMPLATE STRUCT logical_or
  107. template<class _Ty>
  108.     struct logical_or : binary_function<_Ty, _Ty, bool> {
  109.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  110.         {return (_X || _Y); }
  111.     };
  112.         // TEMPLATE STRUCT logical_not
  113. template<class _Ty>
  114.     struct logical_not : unary_function<_Ty, bool> {
  115.     bool operator()(const _Ty& _X) const
  116.         {return (!_X); }
  117.     };
  118.         // TEMPLATE CLASS unary_negate
  119. template<class _Ufn>
  120.     class unary_negate
  121.     : public unary_function<_Ufn::argument_type, bool> {
  122. public:
  123.     explicit unary_negate(const _Ufn& _X)
  124.         : _Fn(_X) {}
  125.     bool operator()(const _Ufn::argument_type& _X) const
  126.         {return (!_Fn(_X)); }
  127. protected:
  128.     _Ufn _Fn;
  129.     };
  130.         // TEMPLATE FUNCTION not1
  131. template<class _Ufn> inline
  132.     unary_negate<_Ufn> not1(const _Ufn& _X)
  133.         {return (unary_negate<_Ufn>(_X)); }
  134.         // TEMPLATE CLASS binary_negate
  135. template<class _Bfn>
  136.     class binary_negate
  137.     : public binary_function<_Bfn::first_argument_type,
  138.         _Bfn::second_argument_type, bool> {
  139. public:
  140.     explicit binary_negate(const _Bfn& _X)
  141.         : _Fn(_X) {}
  142.     bool operator()(const _Bfn::first_argument_type& _X,
  143.         const _Bfn::second_argument_type& _Y) const
  144.         {return (!_Fn(_X, _Y)); }
  145. protected:
  146.     _Bfn _Fn;
  147.     };
  148.         // TEMPLATE FUNCTION not2
  149. template<class _Bfn> inline
  150.     binary_negate<_Bfn> not2(const _Bfn& _X)
  151.         {return (binary_negate<_Bfn>(_X)); }
  152.         // TEMPLATE CLASS binder1st
  153. template<class _Bfn>
  154.     class binder1st
  155.     : public unary_function<_Bfn::second_argument_type,
  156.         _Bfn::result_type> {
  157. public:
  158.     binder1st(const _Bfn& _X,
  159.         const _Bfn::first_argument_type& _Y)
  160.         : op(_X), value(_Y) {}
  161.     result_type operator()(const argument_type& _X) const
  162.         {return (op(value, _X)); }
  163. protected:
  164.     _Bfn op;
  165.     _Bfn::first_argument_type value;
  166.     };
  167.         // TEMPLATE FUNCTION bind1st
  168. template<class _Bfn, class _Ty> inline
  169.     binder1st<_Bfn> bind1st(const _Bfn& _X, const _Ty& _Y)
  170.         {return (binder1st<_Bfn>(_X,
  171.             _Bfn::first_argument_type(_Y))); }
  172.         // TEMPLATE CLASS binder2nd
  173. template<class _Bfn>
  174.     class binder2nd
  175.     : public unary_function<_Bfn::first_argument_type,
  176.         _Bfn::result_type> {
  177. public:
  178.     binder2nd(const _Bfn& _X,
  179.         const _Bfn::second_argument_type& _Y)
  180.         : op(_X), value(_Y) {}
  181.     result_type operator()(const argument_type& _X) const
  182.         {return (op(_X, value)); }
  183. protected:
  184.     _Bfn op;
  185.     _Bfn::second_argument_type value;
  186.     };
  187.         // TEMPLATE FUNCTION bind2nd
  188. template<class _Bfn, class _Ty> inline
  189.     binder2nd<_Bfn> bind2nd(const _Bfn& _X, const _Ty& _Y)
  190.         {return (binder2nd<_Bfn>(_X,
  191.             _Bfn::second_argument_type(_Y))); }
  192.         // TEMPLATE CLASS pointer_to_unary_function
  193. template<class _A, class _R>
  194.     class pointer_to_unary_function
  195.         : public unary_function<_A, _R> {
  196. public:
  197.     explicit pointer_to_unary_function(_R (__cdecl *_X)(_A))
  198.         : _Fn(_X) {}
  199.     _R operator()(_A _X) const
  200.         {return (_Fn(_X)); }
  201. protected:
  202.     _R (__cdecl *_Fn)(_A);
  203.     };
  204.         // TEMPLATE CLASS pointer_to_binary_function
  205. template<class _A1, class _A2, class _R>
  206.     class pointer_to_binary_function
  207.         : public binary_function<_A1, _A2, _R> {
  208. public:
  209.     explicit pointer_to_binary_function(
  210.         _R (__cdecl *_X)(_A1, _A2))
  211.         : _Fn(_X) {}
  212.     _R operator()(_A1 _X, _A2 _Y) const
  213.         {return (_Fn(_X, _Y)); }
  214. protected:
  215.     _R (__cdecl *_Fn)(_A1, _A2);
  216.     };
  217.         // TEMPLATE FUNCTION ptr_fun
  218. template<class _A, class _R> inline
  219.     pointer_to_unary_function<_A, _R>
  220.         ptr_fun(_R (__cdecl *_X)(_A))
  221.         {return (pointer_to_unary_function<_A, _R>(_X)); }
  222. template<class _A1, class _A2, class _R> inline
  223.     pointer_to_binary_function<_A1, _A2, _R>
  224.         ptr_fun(_R (__cdecl *_X)(_A1, _A2))
  225.         {return (pointer_to_binary_function<_A1, _A2, _R>(_X)); }
  226.         // TEMPLATE CLASS mem_fun_t
  227. template<class _R, class _Ty>
  228.     class mem_fun_t : public unary_function<_Ty *, _R> {
  229. public:
  230.     explicit mem_fun_t(_R (_Ty::*_Pm)())
  231.         : _Ptr(_Pm) {}
  232.     _R operator()(_Ty *_P) const
  233.         {return ((_P->*_Ptr)()); }
  234. private:
  235.     _R (_Ty::*_Ptr)();
  236.     };
  237.         // TEMPLATE FUNCTION mem_fun
  238. template<class _R, class _Ty> inline
  239.     mem_fun_t<_R, _Ty> mem_fun(_R (_Ty::*_Pm)())
  240.     {return (mem_fun_t<_R, _Ty>(_Pm)); }
  241.         // TEMPLATE CLASS mem_fun1_t
  242. template<class _R, class _Ty, class _A>
  243.     class mem_fun1_t : public binary_function<_Ty *, _A, _R> {
  244. public:
  245.     explicit mem_fun1_t(_R (_Ty::*_Pm)(_A))
  246.         : _Ptr(_Pm) {}
  247.     _R operator()(_Ty *_P, _A _Arg) const
  248.         {return ((_P->*_Ptr)(_Arg)); }
  249. private:
  250.     _R (_Ty::*_Ptr)(_A);
  251.     };
  252.         // TEMPLATE FUNCTION mem_fun1
  253. template<class _R, class _Ty, class _A> inline
  254.     mem_fun1_t<_R, _Ty, _A> mem_fun1(_R (_Ty::*_Pm)(_A))
  255.     {return (mem_fun1_t<_R, _Ty, _A>(_Pm)); }
  256.         // TEMPLATE CLASS mem_fun_ref_t
  257. template<class _R, class _Ty>
  258.     class mem_fun_ref_t : public unary_function<_Ty, _R> {
  259. public:
  260.     explicit mem_fun_ref_t(_R (_Ty::*_Pm)())
  261.         : _Ptr(_Pm) {}
  262.     _R operator()(_Ty& _X) const
  263.         {return ((_X.*_Ptr)()); }
  264. private:
  265.     _R (_Ty::*_Ptr)();
  266.     };
  267.         // TEMPLATE FUNCTION mem_fun_ref
  268. template<class _R, class _Ty> inline
  269.     mem_fun_ref_t<_R, _Ty> mem_fun_ref(_R (_Ty::*_Pm)())
  270.     {return (mem_fun_ref_t<_R, _Ty>(_Pm)); }
  271.         // TEMPLATE CLASS mem_fun1_ref_t
  272. template<class _R, class _Ty, class _A>
  273.     class mem_fun1_ref_t : public binary_function<_Ty *, _A, _R> {
  274. public:
  275.     explicit mem_fun1_ref_t(_R (_Ty::*_Pm)(_A))
  276.         : _Ptr(_Pm) {}
  277.     _R operator()(_Ty& _X, _A _Arg) const
  278.         {return ((_X.*_Ptr)(_Arg)); }
  279. private:
  280.     _R (_Ty::*_Ptr)(_A);
  281.     };
  282.         // TEMPLATE FUNCTION mem_fun1_ref
  283. template<class _R, class _Ty, class _A> inline
  284.     mem_fun1_ref_t<_R, _Ty, _A> mem_fun1_ref(_R (_Ty::*_Pm)(_A))
  285.     {return (mem_fun1_ref_t<_R, _Ty, _A>(_Pm)); }
  286. _STD_END
  287. #ifdef  _MSC_VER
  288. #pragma pack(pop)
  289. #endif  /* _MSC_VER */
  290.  
  291. #endif /* _FUNCTIONAL_ */
  292.  
  293. /*
  294.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  295.  * Consult your license regarding permissions and restrictions.
  296.  */
  297.  
  298. /*
  299.  * This file is derived from software bearing the following
  300.  * restrictions:
  301.  *
  302.  * Copyright (c) 1994
  303.  * Hewlett-Packard Company
  304.  *
  305.  * Permission to use, copy, modify, distribute and sell this
  306.  * software and its documentation for any purpose is hereby
  307.  * granted without fee, provided that the above copyright notice
  308.  * appear in all copies and that both that copyright notice and
  309.  * this permission notice appear in supporting documentation.
  310.  * Hewlett-Packard Company makes no representations about the
  311.  * suitability of this software for any purpose. It is provided
  312.  * "as is" without express or implied warranty.
  313.  */
  314.