home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CLASSINC.PAK / STDTEMPL.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.6 KB  |  108 lines

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.10  $
  6. //
  7. // Implements commonly used template functions min(), max(), range()
  8. //
  9. //   T min( T, T ) returns the lesser of its arguments
  10. //
  11. //   T min( T, T, T ) returns the least of its arguments
  12. //
  13. //   T max( T, T ) returns the greater of its arguments
  14. //
  15. //   T max( T, T, T ) returns the greatest of its arguments
  16. //
  17. //   T range( T minVal, T maxVal, T val ) returns val if val is
  18. //       between minVal and maxVal.  If val is greater than maxVal,
  19. //       returns maxVal.  If val is less than minVal, returns minVal.
  20. //
  21. //   void Swap( T, T ) swaps the contents of the two given references
  22. //
  23. //----------------------------------------------------------------------------
  24. #if !defined(CLASSLIB_STDTEMPL_H)
  25. #define CLASSLIB_STDTEMPL_H
  26.  
  27. #if !defined(CLASSLIB_DEFS_H)
  28. # include <classlib/defs.h>
  29. #endif
  30.  
  31. //#pragma option -Vo-
  32. #if defined( BI_CLASSLIB_NO_po )
  33. # pragma option -po-
  34. #endif
  35.  
  36. //
  37. // Make sure that there are no min() or max() macros defined
  38. //
  39. #undef min
  40. #undef max
  41.  
  42. //
  43. // Check macro to avoid conflict with RTL's template functions
  44. //
  45. #if !defined( __MINMAX_DEFINED )
  46. #define __MINMAX_DEFINED
  47. template <class T> inline const T& min( const T& t1, const T& t2 )
  48. {
  49.     return t1>t2 ? t2 : t1;
  50. }
  51.  
  52. template <class T> inline const T& max( const T& t1, const T& t2 )
  53. {
  54.     return t1>t2 ? t1 : t2;
  55. }
  56. #endif  // __MINMAX_DEFINED
  57.  
  58. //
  59. // The following Min() and Max() are for compatibility
  60. //
  61. template <class T> inline T Min(const T& a, const T& b)
  62. {
  63.   return a<b ? a : b;
  64. }
  65.  
  66. template <class T> inline T Max(const T& a, const T& b)
  67. {
  68.   return a>b ? a : b;
  69. }
  70.  
  71. template <class T> inline const T& min( const T& t1, const T& t2, const T& t3 )
  72. {
  73.     return t1>t2 ? (t2>t3 ? t3 : t2) : (t1>t3 ? t3 : t1 );
  74. }
  75.  
  76. template <class T> inline T max( const T& t1, const T& t2, const T& t3 )
  77. {
  78.     return t1>t2 ? (t1>t3 ? t1 : t3) : (t2>t3 ? t2 : t3);
  79. }
  80.  
  81. #if defined(BI_NAMESPACE)
  82. namespace ClassLib {
  83. #endif
  84.  
  85. template <class T> inline const T& range( const T& minVal, const T& maxVal,
  86.                                           const T& val )
  87. {
  88.     return min( maxVal, max( minVal, val ) );
  89. }
  90.  
  91. template <class T> inline void Swap(T _BIDSFARDATA& a, T _BIDSFARDATA& b)
  92. {
  93.   T t = a;
  94.   a = b;
  95.   b = t;
  96. }
  97.  
  98. #if defined(BI_NAMESPACE)
  99. }   // namespace ClassLib
  100. #endif
  101.  
  102. #if defined( BI_CLASSLIB_NO_po )
  103. # pragma option -po.
  104. #endif
  105. //#pragma option -Vo.
  106.  
  107. #endif  // CLASSLIB_STDTEMPL_H
  108.