home *** CD-ROM | disk | FTP | other *** search
- unit Accum;
- {*******************************}
- { Author: Eric Uber }
- { CIS Account: 71102,3034 }
- { Written: June 14th 1995. }
- { Freeware, distribute at will. }
- {*******************************}
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs;
-
- const
- LONGINT_MIN = -2147483647;
- LONGINT_MAX = 2147483647;
-
- type
- EMaxExceedError = class(Exception);
- TAccumulator = class(TComponent)
- private
- { Private declarations }
- FliValue : LongInt;
- FliStartValue : Longint;
- FliMaxValue : Longint;
- FbExceptOnMaxValExceed: Boolean;
-
- fOnDivide : TNotifyEvent;
- fOnMultiply : TNotifyEvent;
- fOnAdd : TNotifyEvent;
- fOnSubtract : TNotifyEvent;
- fOnInc : TNotifyEvent;
- fOnDec : TNotifyEvent;
-
- fOnSuccessDivide : TNotifyEvent;
- fOnSuccessMultiply : TNotifyEvent;
- fOnSuccessAdd : TNotifyEvent;
- fOnSuccessSubtract : TNotifyEvent;
- fOnSuccessInc : TNotifyEvent;
- fOnSuccessDec : TNotifyEvent;
-
- protected
- { Protected declarations }
- procedure SetFliMaxValue(liInValue:LongInt);
- procedure SetFliStartValue(liInValue:LongInt);
- public
- { Public declarations }
- function AccInc: Longint;
- function AccDec: Longint;
- function Add(liInValue: LongInt): LongInt;
- function Subtract(liInValue: LongInt): LongInt;
- function Multiply(liInValue: LongInt): LongInt;
- function Divide(liInValue: LongInt): LongInt;
- function GetCurrentValue: LongInt;
- procedure Reset;
- published
- { Published declarations }
- property MaxValue : Longint read FliMaxValue write SetFliMaxValue;
- property StartValue : Longint read FliStartValue write SetFliStartValue;
- property ExceptOnMaxValExceed: Boolean read FbExceptOnMaxValExceed
- Write FbExceptOnMaxValExceed;
- property OnBeginAdd: TNotifyEvent read fOnAdd write fOnAdd;
- property OnBeginSubtract: TNotifyEvent read fOnSubtract write fOnSubtract;
- property OnBeginMultiply: TNotifyEvent read fOnMultiply write fOnMultiply;
- property OnBeginDivide: TNotifyEvent read fOnDivide write fOnDivide;
- property OnBeginInc: TNotifyEvent read fOnInc write fOnInc;
- property OnBeginDec: TNotifyEvent read fOnDec write fOnDec;
-
- property OnSuccessAdd: TNotifyEvent read fOnSuccessAdd write fOnSuccessAdd;
- property OnSuccessSubtract: TNotifyEvent read fOnSuccessSubtract write fOnSuccessSubtract;
- property OnSuccessMultiply: TNotifyEvent read fOnSuccessMultiply write fOnSuccessMultiply;
- property OnSuccessDivide: TNotifyEvent read fOnSuccessDivide write fOnSuccessDivide;
- property OnSuccessInc: TNotifyEvent read fOnSuccessInc write fOnSuccessInc;
- property OnSuccessDec: TNotifyEvent read fOnSuccessDec write fOnSuccessDec;
- end;
-
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('Custom', [TAccumulator]);
- end;
-
-
- {----------------------------------------------------------}
- { Public Method: function TAccumulator.AccInc: LongInt; }
- {----------------------------------------------------------}
- { Purpose: Increments the accumulators current value. }
- {----------------------------------------------------------}
- { Returns: Result of computation or 0 (if }
- { ExceptOnMaxValExceed is set to False and result }
- { exceeds MaxValue). If ExceptOnMaxValExceed }
- { is set to true and the result exceeds MaxValue, }
- { an exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.AccInc: LongInt;
- begin
- if Assigned(fOnInc) then
- fOnInc(self);
-
- if ( FliValue + 1 <= MaxValue ) or (MaxValue = 0) then
- begin
- Inc(FliValue);
- if Assigned(fOnSuccessInc) then
- fOnSuccessInc(self);
- result := FliValue;
- end { End If }
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('AccInc result exceeds MaxValue');
- result := 0
- end; { End Else }
- end; { End Method. }
-
-
- {----------------------------------------------------------}
- { Public Method: function TAccumulator.AccDec: LongInt; }
- {----------------------------------------------------------}
- { Purpose: Decrements the accumulators current value. }
- {----------------------------------------------------------}
- { Returns: Result of computation or 0 (if }
- { ExceptOnMaxValExceed is set to False and result }
- { exceeds MaxValue). If ExceptOnMaxValExceed }
- { is set to true and the result exceeds MaxValue, }
- { an exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.AccDec: LongInt;
- begin
- if Assigned(fOnDec) then
- fOnDec(self);
-
- if ( FliValue - 1 <= MaxValue ) or (MaxValue = 0) then
- begin
- Dec(FliValue);
- if Assigned(fOnSuccessDec) then
- fOnSuccessDec(self);
- result := FliValue;
- end { End If }
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('AccDec result exceeds MaxValue');
- result := 0
- end; { End Else }
- end; { End Method. }
-
-
- {----------------------------------------------------------}
- { Public Method: Add(liInValue: LongInt): LongInt; }
- {----------------------------------------------------------}
- { Purpose: Adds the value passed in Param1 to Accumulator }
- {----------------------------------------------------------}
- { Returns: Result of computation or 0 (if }
- { ExceptOnMaxValExceed is set to False and result }
- { exceeds MaxValue). If ExceptOnMaxValExceed }
- { is set to true and the result exceeds MaxValue, }
- { an exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.Add(liInValue: LongInt): LongInt;
- begin
- if Assigned(fOnAdd) then
- fOnAdd(self);
-
- if ( (FliValue + liInValue) <= MaxValue ) or (MaxValue = 0) then
- begin
- FliValue := ( FliValue + liInValue );
- if Assigned(fOnSuccessAdd) then
- fOnSuccessAdd(self);
- result := FliValue;
- end { End If }
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('Add result exceeds MaxValue');
- result := 0
- end; { End Else }
- end; { End Method. }
-
- {----------------------------------------------------------}
- { Public Method: function TAccumulator.Subtract( }
- { liInValue: LongInt): LongInt; }
- {----------------------------------------------------------}
- { Purpose: Sets the Accumulator to the result of its }
- { (current value - Param1). }
- {----------------------------------------------------------}
- { Returns: Result of computation or 0 (if }
- { ExceptOnMaxValExceed is set to False and result }
- { exceeds MaxValue). If ExceptOnMaxValExceed }
- { is set to true and the result exceeds MaxValue, }
- { an exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.Subtract(liInValue: LongInt): LongInt;
- begin
- if Assigned(fOnSubtract) then
- fOnSubtract(self);
-
- if ( (FliValue - liInValue) <= MaxValue ) or (MaxValue = 0) then
- begin
- FliValue := ( FliValue - liInValue );
- if Assigned(fOnSuccessSubtract) then
- fOnSuccessSubtract(self);
- result := FliValue;
- end { End If }
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('Subtract result exceeds MaxValue');
- result := 0
- end; { End Else }
- end; { End Method. }
-
- {----------------------------------------------------------}
- { Public Method: function TAccumulator.Multiply( }
- { liInValue: LongInt): LongInt; }
- {----------------------------------------------------------}
- { Purpose: Sets the Accumulator to the result of its }
- { current value * Param1. }
- {----------------------------------------------------------}
- { Returns: Result of computation or 0 (if }
- { ExceptOnMaxValExceed is set to False and result }
- { exceeds MaxValue). If ExceptOnMaxValExceed }
- { is set to true and the result exceeds MaxValue, }
- { an exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.Multiply(liInValue: LongInt): LongInt;
- begin
- if Assigned(fOnMultiply) then
- fOnMultiply(self);
-
- if ( (FliValue * liInValue) <= MaxValue ) or (MaxValue = 0) then
- begin
- FliValue := ( FliValue * liInValue );
- if Assigned(fOnSuccessMultiply) then
- fOnSuccessMultiply(self);
- result := FliValue;
- end { End If }
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('Multiply result exceeds MaxValue');
- result := 0
- end; { End Else }
- end; { End Method. }
-
- {----------------------------------------------------------}
- { Public Method: function TAccumulator.Divide( }
- { liInValue: LongInt ): LongInt; }
- {----------------------------------------------------------}
- { Purpose: Sets the Accumulator to the result of its }
- { current value DIV Param1. }
- {----------------------------------------------------------}
- { Returns: Result of computation or 0 (if }
- { ExceptOnMaxValExceed is set to False and result }
- { exceeds MaxValue). If ExceptOnMaxValExceed }
- { is set to true and the result exceeds MaxValue, }
- { an exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.Divide(liInValue: LongInt): LongInt;
- begin
- if Assigned(fOnDivide) then
- fOnDivide(self);
-
- if ( (FliValue div liInValue) <= MaxValue ) or (MaxValue = 0) then
- begin
- FliValue := ( FliValue div liInValue );
- if Assigned(fOnSuccessDivide) then
- fOnSuccessDivide(self);
- result := FliValue;
- end { End If }
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('Divide result exceeds MaxValue');
- result := 0
- end; { End Else }
- end; { End Method. }
-
- {----------------------------------------------------------}
- { Public Method: function GetCurrentValue: LongInt; }
- {----------------------------------------------------------}
- { Purpose: To retrieve the accumulators current value. }
- {----------------------------------------------------------}
- { Returns: The Accumulators current value. If }
- { ExceptOnMaxValExceed is set to true and the }
- { Accumulators current value exceeds MaxValue, an }
- { exception called EMaxExceedError will occur. }
- {----------------------------------------------------------}
- function TAccumulator.GetCurrentValue: LongInt;
- begin
- if ( FliValue <= MaxValue ) or (MaxValue = 0) then
- result := FliValue
- else
- begin
- if (FbExceptOnMaxValExceed) then
- raise EMaxExceedError.Create('Current value exceeds MaxValue');
- result := FliValue;
- end; { End Else }
- end; { End Method. }
-
- {----------------------------------------------------------}
- { Public Method: procedure TAccumulator.Reset; }
- {----------------------------------------------------------}
- { Purpose: Resets Accumulator to value of StartValue }
- {----------------------------------------------------------}
- { Returns: Nothing. This is a procedure. }
- {----------------------------------------------------------}
- procedure TAccumulator.Reset;
- begin
- FliValue := StartValue;
- end; { End Method. }
-
- {----------------------------------------------------------}
- { Protected Method: procedure SetFliStartValue( }
- { liInValue:LongInt ); }
- {----------------------------------------------------------}
- { Purpose: Sets Accumulator to value of StartValue when }
- { StartValue property is assigned. }
- {----------------------------------------------------------}
- { Returns: Nothing. This is a procedure. }
- {----------------------------------------------------------}
- procedure TAccumulator.SetFliStartValue(liInValue:LongInt);
- begin
- if ( liInValue < MaxValue ) or (MaxValue = 0) then
- begin
- FliStartValue := liInValue;
- FliValue := liInValue;
- end
- else
- ShowMessage('Start Value must be a smaller value then MaxValue');
- end; { End Method. }
- {----------------------------------------------------------}
- procedure TAccumulator.SetFliMaxValue(liInValue:LongInt);
- begin
- if ( liInValue > StartValue ) or (liInValue = 0) then
- FliMaxValue := liInValue
- else
- ShowMessage('Start Value must be a smaller value then MaxValue');
- end;
- end. { End Unit }
-