home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / visbuild / calculat / cppwv13 / icalctly.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  7.6 KB  |  271 lines

  1. /******************************************************************************
  2. * .FILE:        icalctly.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION: Implementation for the class, ICalcTally                      *
  5. *                                                                             *
  6. * .CLASSES:     ICalcTally                                                    *
  7. *                                                                             *
  8. * .COPYRIGHT:                                                                 *
  9. *    Licensed Material - Program-Property of IBM                              *
  10. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  11. *                                                                             *
  12. * .DISCLAIMER:                                                                *
  13. *   The following [enclosed] code is sample code created by IBM               *
  14. *   Corporation.  This sample code is not part of any standard IBM product    *
  15. *   and is provided to you solely for the purpose of assisting you in the     *
  16. *   development of your applications.  The code is provided 'AS IS',          *
  17. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  18. *   arising out of your use of the sample code, even if they have been        *
  19. *   advised of the possibility of such damages.                               *
  20. *                                                                             *
  21. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  22. *                                                                             *
  23. ******************************************************************************/
  24.  
  25. #include "icalctly.hpp" 
  26.  
  27. #include <inotifev.hpp>
  28.  
  29. /*******************************************************************
  30.  * Contructors/Destructors
  31.  *******************************************************************/
  32.  
  33. ICalcTally :: ICalcTally()
  34. {
  35.    reset();
  36. }
  37.  
  38. ICalcTally :: ~ICalcTally()
  39. {
  40. }
  41.  
  42. //
  43. // List of Events
  44. //
  45.  
  46. INotificationId ICalcTally :: bufferId = "ICaclTally::buffer";
  47. INotificationId ICalcTally :: nextTallyLineId = "ICalcTally::nextTallyLine";
  48. INotificationId ICalcTally :: notEmptyId = "ICalcTally::notEmpty";
  49.  
  50. /*******************************************************************
  51.  * Access Member Functions (Part Attributes/Actions)
  52.  *******************************************************************/
  53.  
  54. IString ICalcTally :: buffer () const
  55. {
  56.   return dBuffer;
  57. }
  58.  
  59.  
  60. //
  61. // Set the Tally Chip buffer attribute.
  62. // This attribute must be kept in sync with the calculator display.
  63. //
  64.  
  65. ICalcTally & ICalcTally :: setBuffer
  66.   (IString iBuffer)
  67. {
  68.    dBuffer = iBuffer;
  69.  
  70.    //
  71.    // If the buffer is being cleared, make sure will attempt to send
  72.    // the buffer to the Tally Display.  Otherwise the buffer is a
  73.    // candidate for sending to the Tally Display.
  74.    //
  75.  
  76.    if (dBuffer == "") dUpdateTally = false;
  77.    else dUpdateTally = true;
  78.  
  79.    //
  80.    // If the buffer is being updated because of some functional
  81.    // operation as opposed to keypad input, then send this
  82.    // string to the Tally Display.
  83.    //
  84.  
  85.    if (dUpdateTally && dResultExpected) {
  86.       setNextTallyLine(dBuffer);
  87.  
  88.       //
  89.       // If there is an accompanying operator symbol, send the operator
  90.       // symbol to the Tally Display as well.
  91.       //
  92.  
  93.       if (dLastOperator != "") setNextTallyLine(dLastOperator);
  94.  
  95.       dUpdateTally = false;
  96.       dResultExpected = false;
  97.       dLastOperator = "";
  98.    }
  99.    
  100.       return *this;
  101. }
  102.  
  103.  
  104. //
  105. // Query the NextTallyLine attribute.
  106. //
  107.  
  108. IString ICalcTally :: nextTallyLine () const
  109. {
  110.    return dNextTallyLine;
  111. }
  112.  
  113.  
  114. //
  115. // Set the NextTallyLine attribute.
  116. // Construct the actual string to be added to the Tally MLE.
  117. // This includes the proper carriage control and notification
  118. // of any state change of the Tally (empty to not empty).
  119. //
  120.  
  121. ICalcTally & ICalcTally :: setNextTallyLine 
  122.   (IString iString)
  123. {
  124.    if (iString != "") {
  125.  
  126.       //
  127.       // Set the required carriage control to force MLE to add
  128.       // lines the way I want them added and issue event indicating
  129.       // that the line is ready to be added to the Tally Display (MLE).
  130.       //
  131.  
  132.       if (!dEmptyMark) dNextTallyLine = "\r\n";
  133.       else dNextTallyLine = "";
  134.  
  135.       dNextTallyLine += iString ;
  136.       notifyObservers(INotificationEvent(nextTallyLineId, *this));
  137.  
  138.       //
  139.       // Issue event of state change of Tally from empty to not empty
  140.       // when appropriate.
  141.       //
  142.  
  143.       if (dEmptyMark) {
  144.          dEmptyMark = false;
  145.          notifyObservers(INotificationEvent(notEmptyId, *this));
  146.       }
  147.  
  148.    }
  149.  
  150.    return *this;
  151. }
  152.  
  153.  
  154. /***************************************************************************
  155.  Implementor Member Functions (Part Actions)
  156. ****************************************************************************/
  157.  
  158. //
  159. // Reset the Tally object.
  160. //
  161.  
  162. Boolean ICalcTally :: reset ()
  163. {
  164.    dBuffer = "";
  165.    dNextTallyLine = "";
  166.    dUpdateTally = false;
  167.    dProcessBinaryOp = false;
  168.    dEmptyMark = true;
  169.    dResultExpected = false;
  170.    dLastOperator = "";
  171.    return dEmptyMark;
  172. }
  173.  
  174.  
  175. //
  176. // Process a Binary operator.
  177. //
  178.  
  179. Boolean ICalcTally :: processBinaryOperator (IString iBinaryOpSym)
  180. {
  181.    Boolean updated;
  182.     
  183.    //
  184.    // If there is something in the Tally Chip buffer which needs to be
  185.    // sent to the Tally Display, then do it.
  186.    // Otherwise indicate that the Tally Display has not been updated from the
  187.    // Tally Chip buffer.
  188.    //
  189.  
  190.    if (dUpdateTally) {
  191.       setNextTallyLine(buffer());
  192.       dUpdateTally = false;
  193.       updated = true;
  194.       }
  195.  
  196.    else updated = false;
  197.  
  198.    //
  199.    // If previous operator not a binary operator, send this operator
  200.    // symbol to the Tally Display
  201.    //
  202.  
  203.    if (!dProcessBinaryOp) {
  204.       setNextTallyLine(iBinaryOpSym);
  205.       dProcessBinaryOp = true;
  206.       dResultExpected = false;
  207.       dLastOperator = "";
  208.       }
  209.  
  210.    //
  211.    // Else the previous operator was a binary operator and if Tally Display
  212.    // was not updated from the Tally Chip Buffer, send this operator to
  213.    // the Tally Display.  Otherwise, save this operator for later display
  214.    // because need to display Tally Chip Buffer contents before displaying
  215.    // the binary operator symbol associated with the buffer contents.
  216.    //
  217.  
  218.    else {
  219.       if (!updated) {
  220.          setNextTallyLine(iBinaryOpSym);
  221.          dLastOperator = "";
  222.       }
  223.        
  224.       else dLastOperator = iBinaryOpSym;
  225.  
  226.       dResultExpected = true;   // Are expecting a buffer update as a result
  227.                                 // of some binary function operation.
  228.  
  229.       } 
  230.  
  231.    return updated;
  232. }
  233.  
  234.  
  235. //
  236. // Process a Unary Operator
  237. //
  238.  
  239. Boolean ICalcTally :: processUnaryOperator (IString iUnaryOpSym)
  240. {
  241.    Boolean updated;
  242.    
  243.    //
  244.    // If there is something in the Tally Chip buffer which needs to be
  245.    // sent to the Tally Display, then do it.
  246.    // Otherwise indicate that the Tally Display has not been updated from the
  247.    // Tally Chip buffer.
  248.    //
  249.  
  250.    if (dUpdateTally) {
  251.       setNextTallyLine(buffer());
  252.       dUpdateTally = false;
  253.       updated = true;
  254.       }
  255.  
  256.    else updated = false;
  257.  
  258.    //
  259.    // Send the unary operator symbol to the Tally Display.
  260.    // Expect some update from the unary function operation.
  261.    // No delay waiting for completion of a binary operation.
  262.    //
  263.  
  264.    setNextTallyLine(iUnaryOpSym);
  265.    dResultExpected = true;
  266.    dProcessBinaryOp = false;
  267.    
  268.    return updated;
  269. }
  270.  
  271.