home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classinc.pak / STDTEMPL.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  3KB  |  74 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  STDTEMPL.H                                                            */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*                                                                        */
  9. /*  Implements commonly used template functions min(), max(), range()     */
  10. /*                                                                        */
  11. /*      T min( T, T ) returns the lesser of its arguments                 */
  12. /*                                                                        */
  13. /*      T min( T, T, T ) returns the least of its arguments               */
  14. /*                                                                        */
  15. /*      T max( T, T ) returns the greater of its arguments                */
  16. /*                                                                        */
  17. /*      T max( T, T, T ) returns the greatest of its arguments            */
  18. /*                                                                        */
  19. /*      T range( T minVal, T maxVal, T val ) returns val if val is        */
  20. /*          between minVal and maxVal.  If val is greater than maxVal,    */
  21. /*          returns maxVal.  If val is less than minVal, returns minVal.  */
  22. /*                                                                        */
  23. /*------------------------------------------------------------------------*/
  24.  
  25. #if !defined( CLASSLIB_STDTEMPL_H )
  26. #define CLASSLIB_STDTEMPL_H
  27.  
  28. #if !defined( CLASSLIB_DEFS_H )
  29. #include <classlib/defs.h>
  30. #endif
  31.  
  32. #pragma option -Vo-
  33. #if defined( BI_CLASSLIB_NO_po )
  34. #pragma option -po-
  35. #endif
  36.  
  37. #if !defined( __MINMAX_DEFINED )    // avoid conflict with RTL
  38. #define __MINMAX_DEFINED
  39. template <class T> inline const T& min( const T& t1, const T& t2 )
  40. {
  41.     return t1>t2 ? t2 : t1;
  42. }
  43.  
  44. template <class T> inline const T& max( const T& t1, const T& t2 )
  45. {
  46.     return t1>t2 ? t1 : t2;
  47. }
  48. #endif  // __MINMAX_DEFINED
  49.  
  50. template <class T> inline const T& min( const T& t1, const T& t2, const T& t3 )
  51. {
  52.     return t1>t2 ? (t2>t3 ? t3 : t2) : (t1>t3 ? t3 : t1 );
  53. }
  54.  
  55. template <class T> inline T max( const T& t1, const T& t2, const T& t3 )
  56. {
  57.     return t1>t2 ? (t1>t3 ? t1 : t3) : (t2>t3 ? t2 : t3);
  58. }
  59.  
  60. template <class T> inline const T& range( const T& minVal, const T& maxVal,
  61.                                           const T& val )
  62. {
  63.     return min( maxVal, max( minVal, val ) );
  64. }
  65.  
  66. #if defined( BI_CLASSLIB_NO_po )
  67. #pragma option -po.
  68. #endif
  69.  
  70. #pragma option -Vo.
  71.  
  72. #endif  // CLASSLIB_STDTEMPL_H
  73.  
  74.