home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / reject10.exe / SOURCE.ZIP / BASE.HPP next >
Encoding:
C/C++ Source or Header  |  1994-05-25  |  3.0 KB  |  85 lines

  1. #ifndef __INC_BASE_HPP__
  2. #define __INC_BASE_HPP__
  3.  
  4. #include "constant.hpp"
  5.  
  6. /*********************************************************************************
  7. **
  8. **  CLASS: CBase. This is the base class from which *ALL* other classes should be 
  9. **         derived. This class is used to provide consistent error handling for all
  10. **         classes. If the constructor for a class fails in some way it should 
  11. **         call one of the Fail () funtions below so that the validity of the class 
  12. **         can be tested using the ValidObject function below.
  13. **
  14. **  PRIVATE MEMBERS: 
  15. **        fValid      :  Flag to indicate the validity of the Object.
  16. **
  17. **  PUBLIC MEMBERS:
  18. **        Valid ()    :  Function to test the validity of the object. Returns
  19. **                       bTRUE if the object is valid or bFALSE if the object
  20. **                       is invalid.
  21. **  PROTECTED MEMBERS:
  22. **        Fail ()     :  Function to invalidate the object. Can only be called from
  23. **                       the derived classes and is typicaly called from the constructor
  24. **                       if it fails in some way.
  25. **
  26. **        Fail (int)  :  Similar to the the above function but takes an int. If the 
  27. **                       int is >0 then the class will be invalidated. eg: 
  28. **
  29. **                            ptr = new char[200];
  30. **                            Fail (ptr == NULL);
  31. **
  32. **                       will invalidate the object if the new fails.
  33. **
  34. *********************************************************************************/
  35.  
  36. class CBase
  37. {
  38.      private:
  39.           BOOLEAN fValid;
  40.  
  41.      public:
  42.           CBase () 
  43.           { 
  44.                fValid = bTRUE; 
  45.           }
  46.  
  47.           virtual ~CBase () {;}
  48.           BOOLEAN  Valid () {return fValid;}
  49.  
  50.      protected:
  51.           void Fail () {fValid = bFALSE;}
  52.           void Fail (int exp) {if (exp) fValid = bFALSE;}
  53. };
  54.  
  55. /*********************************************************************************
  56. **
  57. **  FUNCTION: This function Validates an object which is derived from the 
  58. **            class CBase. A Class can fail to initialise in 2 places. The 
  59. **            memory for the class itself can fail to be allocated or something
  60. **            the constructor does can fail. This function will test for both
  61. **            cases. eg:
  62. **
  63. **                  ObjPrt = new TestObject;
  64. **                  if (!ValidObject (ObjPtr)) 
  65. **                  {
  66. **                       -=-=- Handle error -=-=- 
  67. **                  }
  68. **
  69. **  ENTRY:    A pointer to the object.
  70. **  RETURNS:  bTRUE  if the object is valid 
  71. **            bFALSE if the object is invalid and should not be used (and  
  72. **                      should be deleted.)
  73. **
  74. *********************************************************************************/
  75.  
  76. inline BOOLEAN ValidObject (CBase *base)
  77. {
  78.      if ((base) && (base->Valid() == bTRUE))
  79.           return (bTRUE);
  80.      else
  81.           return (bFALSE);
  82. }
  83.  
  84. #endif
  85.