_function_base.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996-1998
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  29. #define _STLP_INTERNAL_FUNCTION_BASE_H
  30. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_TYPE_TRAITS_H)
  31. # include <stl/type_traits.h>
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. template <class _Arg, class _Result>
  35. struct unary_function {
  36. typedef _Arg argument_type;
  37. typedef _Result result_type;
  38. #if !defined (__BORLANDC__) || (__BORLANDC__ < 0x580)
  39. protected:
  40. /* This class purpose is to be derived but it is not polymorphic so users should never try
  41. * to destroy an instance of it directly. The protected non-virtual destructor make this
  42. * fact obvious at compilation time. */
  43. ~unary_function() {}
  44. #endif
  45. };
  46. template <class _Arg1, class _Arg2, class _Result>
  47. struct binary_function {
  48. typedef _Arg1 first_argument_type;
  49. typedef _Arg2 second_argument_type;
  50. typedef _Result result_type;
  51. #if !defined (__BORLANDC__) || (__BORLANDC__ < 0x580)
  52. protected:
  53. /* See unary_function comment. */
  54. ~binary_function() {}
  55. #endif
  56. };
  57. template <class _Tp>
  58. struct equal_to : public binary_function<_Tp, _Tp, bool> {
  59. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
  60. };
  61. template <class _Tp>
  62. struct less : public binary_function<_Tp,_Tp,bool>
  63. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  64. /* less is the default template parameter for many STL containers, to fully use
  65. * the move constructor feature we need to know that the default less is just a
  66. * functor.
  67. */
  68. , public __stlport_class<less<_Tp> >
  69. #endif
  70. {
  71. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
  72. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  73. void _M_swap_workaround(less<_Tp>& __x) {}
  74. #endif
  75. };
  76. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  77. template <class _Tp>
  78. struct __type_traits<less<_Tp> > {
  79. #if !defined (__BORLANDC__)
  80. typedef typename _IsSTLportClass<less<_Tp> >::_Ret _STLportLess;
  81. #else
  82. enum { _Is = _IsSTLportClass<less<_Tp> >::_Is };
  83. typedef typename __bool2type<_Is>::_Ret _STLportLess;
  84. #endif
  85. typedef _STLportLess has_trivial_default_constructor;
  86. typedef _STLportLess has_trivial_copy_constructor;
  87. typedef _STLportLess has_trivial_assignment_operator;
  88. typedef _STLportLess has_trivial_destructor;
  89. typedef _STLportLess is_POD_type;
  90. };
  91. #endif
  92. _STLP_MOVE_TO_PRIV_NAMESPACE
  93. template <class _Tp>
  94. less<_Tp> __less(_Tp* ) { return less<_Tp>(); }
  95. template <class _Tp>
  96. equal_to<_Tp> __equal_to(_Tp* ) { return equal_to<_Tp>(); }
  97. _STLP_MOVE_TO_STD_NAMESPACE
  98. template <class _Tp>
  99. struct plus : public binary_function<_Tp, _Tp, _Tp> {
  100. _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
  101. };
  102. template <class _Tp>
  103. struct minus : public binary_function<_Tp, _Tp, _Tp> {
  104. _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
  105. };
  106. _STLP_MOVE_TO_PRIV_NAMESPACE
  107. template <class _Tp>
  108. plus<_Tp> __plus(_Tp* ) { return plus<_Tp>(); }
  109. template <class _Tp>
  110. minus<_Tp> __minus(_Tp* ) { return minus<_Tp>(); }
  111. _STLP_MOVE_TO_STD_NAMESPACE
  112. template <class _Tp>
  113. struct multiplies : public binary_function<_Tp, _Tp, _Tp> {
  114. _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
  115. };
  116. _STLP_MOVE_TO_PRIV_NAMESPACE
  117. template <class _Pair>
  118. struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
  119. const typename _Pair::first_type& operator()(const _Pair& __x) const {
  120. return __x.first;
  121. }
  122. };
  123. template <class _Pair>
  124. struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type> {
  125. const typename _Pair::second_type& operator()(const _Pair& __x) const {
  126. return __x.second;
  127. }
  128. };
  129. // project1st and project2nd are extensions: they are not part of the standard
  130. template <class _Arg1, class _Arg2>
  131. struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
  132. _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
  133. };
  134. template <class _Arg1, class _Arg2>
  135. struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
  136. _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
  137. };
  138. #if defined (_STLP_MULTI_CONST_TEMPLATE_ARG_BUG)
  139. // fbp : sort of select1st just for maps
  140. template <class _Pair, class _Whatever>
  141. // JDJ (CW Pro1 doesn't like const when first_type is also const)
  142. struct __Select1st_hint : public unary_function<_Pair, _Whatever> {
  143. const _Whatever& operator () (const _Pair& __x) const { return __x.first; }
  144. };
  145. # define _STLP_SELECT1ST(__x,__y) _STLP_PRIV __Select1st_hint< __x, __y >
  146. #else
  147. # define _STLP_SELECT1ST(__x, __y) _STLP_PRIV _Select1st< __x >
  148. #endif
  149. template <class _Tp>
  150. struct _Identity : public unary_function<_Tp,_Tp> {
  151. const _Tp& operator()(const _Tp& __x) const { return __x; }
  152. };
  153. template <class _Result, class _Argument>
  154. struct _Constant_unary_fun {
  155. typedef _Argument argument_type;
  156. typedef _Result result_type;
  157. result_type _M_val;
  158. _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
  159. const result_type& operator()(const _Argument&) const { return _M_val; }
  160. };
  161. template <class _Result, class _Arg1, class _Arg2>
  162. struct _Constant_binary_fun {
  163. typedef _Arg1 first_argument_type;
  164. typedef _Arg2 second_argument_type;
  165. typedef _Result result_type;
  166. _Result _M_val;
  167. _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
  168. const result_type& operator()(const _Arg1&, const _Arg2&) const {
  169. return _M_val;
  170. }
  171. };
  172. // identity_element (not part of the C++ standard).
  173. template <class _Tp> inline _Tp __identity_element(plus<_Tp>) { return _Tp(0); }
  174. template <class _Tp> inline _Tp __identity_element(multiplies<_Tp>) { return _Tp(1); }
  175. _STLP_MOVE_TO_STD_NAMESPACE
  176. _STLP_END_NAMESPACE
  177. #endif /* _STLP_INTERNAL_FUNCTION_BASE_H */
  178. // Local Variables:
  179. // mode:C++
  180. // End: