home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / ALERTDRV.ZIP / INTEGER.CPP < prev    next >
Text File  |  1994-01-25  |  10KB  |  255 lines

  1. /*------------------------------------------------------------------------------
  2.    Copyright           : (c)1993 by Logical Operators
  3.                          All Rights Reserved.
  4.    Filename            : Integer.CPP
  5.    Header File         : Integer.H
  6.    Purpose             : Sample AlertableObject which implements an Integer
  7.                          class.
  8.    Compiler Directives : None
  9.  
  10.    Modification History:
  11.    Version   Date    Programmer and Description of Changes
  12.    ------- --------  --------------------------------------------------------
  13.     1.00   01/20/94  Original version by Warren J. Hairston.
  14.    ---------------------------------------------------------------------------*/
  15.  
  16.  
  17.  
  18.    //included files
  19.    //--------------
  20.    #ifndef INTEGER_H
  21.       #include <INTEGER.H>   //AlertableObject class declaration
  22.    #endif   //!INTEGER_H
  23.  
  24.    #include <limits.h>     //for INT_MIN, INT_MAX declarations
  25.  
  26.  
  27.  
  28. /*============================================================================*/
  29.  
  30.  
  31.  
  32.    //Integer constructor(s)
  33.    //----------------------
  34.  
  35.  
  36.  
  37.    Integer::Integer(int aValue) :
  38.       AlertableObject()
  39.    /*---------------------------------------------------------------------------
  40.       Purpose:  Initializes the object.
  41.       Parameters:  aValue = value to which the object will be initialized.
  42.       Return Value:  None.
  43.       ------------------------------------------------------------------------*/
  44.    {
  45.       val = aValue;
  46.       HandleInfo(infoIntConstruct);
  47.    };   //constructor for Integer
  48.  
  49.  
  50.  
  51. /*============================================================================*/
  52.  
  53.  
  54.  
  55.    //Integer destructor
  56.    //------------------
  57.  
  58.  
  59.  
  60.    Integer::~Integer()
  61.    /*---------------------------------------------------------------------------
  62.       Purpose:  Destructs the object.
  63.       Parameters:  None.
  64.       Return Value:  None.
  65.       ------------------------------------------------------------------------*/
  66.    {
  67.       HandleMessage(msgIntDestruct);
  68.       ClearMessage();
  69.    };   //destructor for Integer
  70.  
  71.  
  72.  
  73. /*============================================================================*/
  74.  
  75.  
  76.  
  77.    //Integer member function(s)
  78.    //--------------------------
  79.  
  80.  
  81.  
  82.    Integer& Integer::operator = (long aValue)
  83.    /*---------------------------------------------------------------------------
  84.       Purpose:  Sets the variable to the specified value.
  85.       Parameters:  aValue = value to which the object will be set.
  86.       Return Value:  Reference to this object.
  87.       ------------------------------------------------------------------------*/
  88.    {
  89.       unsigned short warningResult;
  90.  
  91.  
  92.  
  93.       warningResult = HandleWarning(warnIntChange, adsYES);
  94.       if ((warningResult == adsYES) || (warningResult == adsNODRIVER))
  95.       {
  96.          if ((aValue >= INT_MIN) && (aValue <= INT_MAX))
  97.          {
  98.             val = aValue;
  99.          }   //aValue is legal for an int
  100.          else
  101.          {
  102.             HandleError(errIntOverflow);
  103.             val = INT_MAX;
  104.          };   //report an error here
  105.       };   //user wants to change the value
  106.       return *this;
  107.    };   //overridden operator Integer::operator =
  108.  
  109.  
  110.  
  111.    int Integer::operator / (int aValue)
  112.    /*---------------------------------------------------------------------------
  113.       Purpose:  Divides the variable by the specified value.
  114.       Parameters:  aValue = value by which the object will be divided.
  115.       Return Value:  The value derived by dividing data member val by aValue.
  116.       ------------------------------------------------------------------------*/
  117.    {
  118.       int result;   //holds the function result
  119.  
  120.  
  121.  
  122.       if (aValue)
  123.       {
  124.          result = val / aValue;
  125.       }   //specified value is not 0
  126.       else
  127.       {
  128.          HandleError(errIntDivByZero);
  129.          result = INT_MAX;
  130.       };   //specified value is 0
  131.       return result;
  132.    };   //overloaded operator Integer::operator /
  133.  
  134.  
  135.  
  136.    void Integer::GetErrorText(const long errorCode, char *text)
  137.    /*---------------------------------------------------------------------------
  138.       Purpose:  Converts the specified error code into a text string describing
  139.                 the error.
  140.       Parameters:  errorCode = An errXXXX or user-defined code which denotes
  141.                                the error to be handled.
  142.                    text = Upon return from this function, text holds a string
  143.                           describing the error referenced by errorCode.
  144.       Return Value:  None.
  145.       ------------------------------------------------------------------------*/
  146.    {
  147.       switch (errorCode)
  148.       {
  149.          case errIntDivByZero : strcpy(text,
  150.                                        "Divide by zero in Integer object.");
  151.                                 break;
  152.          case errIntOverflow  : strcpy(text,
  153.                                        "Value too large for Integer object.");
  154.                                 break;
  155.          default              : AlertableObject::GetErrorText(errorCode, text);
  156.                                 break;
  157.       };   //switch statement to determine error text
  158.    };   //member function Integer::GetErrorText
  159.  
  160.  
  161.  
  162.    void Integer::GetInfoText(const long infoCode, char *text)
  163.    /*---------------------------------------------------------------------------
  164.       Purpose:  Converts the specified info code into a text string describing
  165.                 the information.
  166.       Parameters:  infoCode = An infoXXXX or user-defined code which denotes
  167.                               the information to be handled.
  168.                    text = Upon return from this function, text holds a string
  169.                           describing the info referenced by infoCode.
  170.       Return Value:  None.
  171.       ------------------------------------------------------------------------*/
  172.    {
  173.       switch (infoCode)
  174.       {
  175.          case infoIntConstruct : strcpy(text, "Integer object constructed.");
  176.                                  break;
  177.          default               : AlertableObject::GetInfoText(infoCode, text);
  178.                                  break;
  179.       };   //switch statement to determine info text
  180.    };   //member function Integer::GetInfoText
  181.  
  182.  
  183.  
  184.    void Integer::GetMessageText(const long msgCode, char *text)
  185.    /*---------------------------------------------------------------------------
  186.       Purpose:  Converts the specified message code into a text string
  187.                 describing the message.
  188.       Parameters:  msgCode = An msgXXXX or user-defined code which denotes
  189.                                the message to be handled.
  190.                    text = Upon return from this function, text holds a string
  191.                           describing the message referenced by msgCode.
  192.       Return Value:  None.
  193.       ------------------------------------------------------------------------*/
  194.    {
  195.       switch (msgCode)
  196.       {
  197.          case msgIntDestruct : strcpy(text, "Integer object destructed.");
  198.                                break;
  199.          default             : AlertableObject::GetMessageText(msgCode, text);
  200.                                break;
  201.       };   //switch statement to determine message text
  202.    };   //member function Integer::GetMessageText
  203.  
  204.  
  205.  
  206.    void Integer::GetWarningText(const long warnCode, char *text)
  207.    /*---------------------------------------------------------------------------
  208.       Purpose:  Converts the specified warning code into a text string
  209.                 describing the warning.
  210.       Parameters:  warnCode = A warnXXXX or user-defined code which denotes
  211.                                the warning to be handled.
  212.                    text = Upon return from this function, text holds a string
  213.                           describing the warning referenced by warnCode. The
  214.                           space allocated for holding this string must be
  215.                           allocated outside this function.
  216.       Return Value:  None.
  217.       ------------------------------------------------------------------------*/
  218.    {
  219.       switch (warnCode)
  220.       {
  221.          case warnIntChange : strcpy(text,
  222.            "Are you sure you want to change the value of this Integer object?");
  223.                               break;
  224.          default            : AlertableObject::GetWarningText(warnCode, text);
  225.                               break;
  226.       };   //switch statement to determine warning text
  227.    };   //member function Integer::GetWarningText
  228.  
  229.  
  230.  
  231.    void Integer::TestAlertDriver(void)
  232.    /*---------------------------------------------------------------------------
  233.       Purpose:  Tests the AlertDriver for this object by reporting undefined
  234.                 codes and literal strings.
  235.       Parameters:  None.
  236.       Return Value:  None.
  237.       ------------------------------------------------------------------------*/
  238.    {
  239.       //test undefined codes for the object
  240.       HandleMessage(13);   /*this message stays on the screen until the next one
  241.                              or until a call to ClearMessage()*/
  242.       HandleError(14);
  243.       HandleWarning(16,adsYES);
  244.  
  245.       //test literal strings
  246.       ReportMessage("Literal:  This is a message string.");
  247.       ReportError("Literal:  This is an error string.");
  248.       ReportWarning("Literal:  Acknowledge this warning string?", adsNO);
  249.  
  250.       ClearMessage();
  251.    };   //member function Integer::TestAlertDriver
  252.  
  253.  
  254.  
  255.