home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / KPlib 1.2.1 / KPbasic.h < prev    next >
Encoding:
Text File  |  1994-11-07  |  2.1 KB  |  103 lines  |  [TEXT/R*ch]

  1. // A module of KPlib v1.2.1.
  2. // Written by Keith Pomakis during the summer of 1994.
  3. // Released to the public domain on October 10, 1994.
  4.  
  5. #ifndef KP_BASIC_DEFINED
  6. #define KP_BASIC_DEFINED
  7.  
  8. #include <string.h>
  9. #include <iostream.h>
  10. #include <stdlib.h>
  11.  
  12. /****************************************************************************/
  13.  
  14. #ifndef NULL
  15. #define NULL 0
  16. #endif
  17.  
  18. #ifndef EXIT_FAILURE
  19. #define EXIT_FAILURE 1
  20. #endif
  21.  
  22. #ifndef EXIT_SUCCESS
  23. #define EXIT_SUCCESS 0
  24. #endif
  25.  
  26. /****************************************************************************/
  27.  
  28. // Since the bool type has only recently been added to the C++ standard,
  29. // many compilers may not implement it yet.  Uncomment the following lines
  30. // to simulate the bool type.
  31.  
  32. /*
  33. typedef int bool;
  34. const int false = 0;
  35. const int true = 1;
  36. */
  37.  
  38. /****************************************************************************/
  39.  
  40. #ifndef min         // Hopefully this isn't already defined as a macro
  41.  
  42. template <class T>
  43. inline T
  44. min(T arg1, T arg2)
  45. {
  46.     return (arg1 < arg2)? arg1 : arg2;
  47. }
  48.  
  49. inline char *
  50. min(char *arg1, char *arg2)
  51. {
  52.     return (strcmp(arg1, arg2) < 0)? arg1 : arg2;
  53. }
  54.  
  55. #endif
  56.  
  57. /****************************************************************************/
  58.  
  59. #ifndef max         // Hopefully this isn't already defined as a macro
  60.  
  61. template <class T>
  62. inline T
  63. max(T arg1, T arg2)
  64. {
  65.     return (arg2 < arg1)? arg1 : arg2;
  66. }
  67.  
  68. inline char *
  69. max(char *arg1, char *arg2)
  70. {
  71.     return (strcmp(arg2, arg1) < 0)? arg1 : arg2;
  72. }
  73.  
  74. #endif
  75.  
  76. /****************************************************************************/
  77.  
  78. template <class T>
  79. inline void
  80. swap(T &arg1, T &arg2)
  81. {
  82.     T tmp = arg1;
  83.     arg1 = arg2;
  84.     arg2 = tmp;
  85. }
  86.  
  87. /****************************************************************************/
  88.  
  89. inline void
  90. check_mem(void *ptr)
  91. {
  92.     // Declare string as static so that it isn't defined per instantiation.
  93.     static const char *const mem_err = "Error allocating memory.\n";
  94.     if (ptr == NULL) {
  95.         cerr << mem_err;
  96.         exit(EXIT_FAILURE);
  97.     }
  98. }
  99.  
  100. /****************************************************************************/
  101.  
  102. #endif /* KP_BASIC_DEFINED */
  103.