Page 1 of 1

Chronometer - Temporizer - OOP

Posted: Tue Apr 09, 2019 4:28 pm
by StarBootics
Hello everyone,

I'm not convince yet about writing code this way over purely Modular but I'm giving it a try. There is a Chronometer object :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Chronometer
; File Name : Chrono - OOP.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 09-04-2019
; Last Update : 09-04-2019
; PureBasic code : V5.70 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule Chrono

  Interface Chrono
    
    Reset()
    Start()
    Stop()
    Consult.q()
    Format.s(TimeMask.s)
    Free()

  EndInterface

  Declare.i New()

EndDeclareModule

Module Chrono

  Structure Private_Members

    VirtualTable.i
    StartTime.q
    TotalTime.q
    IsRunning.b
  
  EndStructure
  
  Procedure Reset(*This.Private_Members)
    
    *This\StartTime = 0
    *This\TotalTime = 0
    *This\IsRunning = #False
    
  EndProcedure
  
  Procedure Start(*This.Private_Members)
    
    If *This\IsRunning = #False
      *This\StartTime = ElapsedMilliseconds()
      *This\IsRunning = #True
    EndIf
    
  EndProcedure
  
  Procedure Stop(*This.Private_Members)
    
    If *This\IsRunning = #True
      *This\TotalTime = *This\TotalTime + ElapsedMilliseconds() - *This\StartTime
      *This\IsRunning = #False
    EndIf
    
  EndProcedure
  
  Procedure.q Consult(*This.Private_Members)
    
    If *This\IsRunning = #True
      TotalTime.q = *This\TotalTime + ElapsedMilliseconds() - *This\StartTime
    Else
      TotalTime = *This\TotalTime
    EndIf
    
    ProcedureReturn TotalTime
  EndProcedure
  
  Procedure.s Format(*This.Private_Members, TimeMask.s)
    
    MilliSeconds = Consult(*This)
    
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ; <<<<< On fait l'extraction des jours, Heures, minutes, secondes et des MS <<<<<
    
    If MilliSeconds < 0 
      MilliSeconds = MilliSeconds * -1
    EndIf 
    
    Days = MilliSeconds / 86400000 
    MilliSeconds % 86400000
    
    Hours = MilliSeconds / 3600000
    MilliSeconds % 3600000
    
    Minutes = MilliSeconds / 60000
    MilliSeconds % 60000
    
    Seconds = MilliSeconds / 1000
    MilliSeconds % 1000
    
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ; <<<<< On s'occupe du filtre de sortie <<<<<
    
    If FindString(TimeMask, "%dd", 1)
      TimeMask = ReplaceString(TimeMask,"%dd", RSet(Str(Days), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%hh", 1)
      TimeMask = ReplaceString(TimeMask,"%hh", RSet(Str(Hours), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%mm", 1)
      TimeMask = ReplaceString(TimeMask,"%mm", RSet(Str(Minutes), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%ss", 1)
      TimeMask = ReplaceString(TimeMask,"%ss", RSet(Str(Seconds), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%mss", 1)
      TimeMask = ReplaceString(TimeMask,"%mss", RSet(Str(MilliSeconds), 3, "0"))
    EndIf
    
    ProcedureReturn TimeMask
  EndProcedure
  
  Procedure Free(*This.Private_Members)

    ClearStructure(*This, Private_Members)
    FreeStructure(*This)

  EndProcedure

  Procedure.i New()

    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS
    *This\StartTime = 0
    *This\TotalTime = 0
    *This\IsRunning = #False
    
    ProcedureReturn *This
  EndProcedure

  DataSection
    START_METHODS:
    Data.i @Reset()
    Data.i @Start()
    Data.i @Stop()
    Data.i @Consult()
    Data.i @Format()
    Data.i @Free()
    END_METHODS:
  EndDataSection

EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  MyChronometer.Chrono::Chrono = Chrono::New()
  MyChronometer\Start()
  
  Delay(1526)
  
  MyChronometer\Stop()
  
  Debug MyChronometer\Consult()
  Debug MyChronometer\Format("%ss:%mss")
  
  MyChronometer\Free()
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
And a Temporizer object :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Temporizer
; File Name : Tempo - OOP.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 09-04-2019
; Last Update : 09-04-2019
; PureBasic code : V5.70 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule Tempo

  Interface Tempo
    
    Reset()
    SetTime(P_MilliSeconds.l, P_Seconds.l = 0, P_Minutes.l = 0, P_Hours.l = 0, P_Days.l = 0)
    Start()
    Stop()
    Consult.q()
    Format.s(TimeMask.s)
    Free()
    
  EndInterface

  Declare.i New(P_MilliSeconds.l, P_Seconds.l = 0, P_Minutes.l = 0, P_Hours.l = 0, P_Days.l = 0)

EndDeclareModule

Module Tempo

  Structure Private_Members

    VirtualTable.i
    StartTime.q
    TotalTime.q
    IsRunning.b
  
  EndStructure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< Convertir des secondes en MS <<<<<
  
  Macro SecondsToMilliseconds(Seconds)
    
    (Seconds * 1000)
    
  EndMacro 
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< Convertir des minutes en MS <<<<<
  
  Macro MinutesToMilliseconds(Minutes)
    
    (Minutes * 60000)
    
  EndMacro 
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< Convertir des heures en MS <<<<<
  
  Macro HoursToMilliseconds(Hours)
    
    (Hours * 3600000)
    
  EndMacro 
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< Convertir des Jours en MS <<<<<
  
  Macro DaysToMilliseconds(Days)
    
    (Days * 86400000)
    
  EndMacro 
  
  Procedure Reset(*This.Private_Members)
    
    *This\StartTime = 0
    *This\TotalTime = 0
    *This\IsRunning = #False
    
  EndProcedure
  
  Procedure SetTime(*This.Private_Members, P_MilliSeconds.l, P_Seconds.l = 0, P_Minutes.l = 0, P_Hours.l = 0, P_Days.l = 0)
    
    *This\StartTime = 0
    *This\TotalTime = P_MilliSeconds + SecondsToMilliseconds(P_Seconds) + MinutesToMilliseconds(P_Minutes) + HoursToMilliseconds(P_Hours) + DaysToMilliseconds(P_Days)
    *This\IsRunning = #False
    
  EndProcedure
  
  Procedure Start(*This.Private_Members)
    
    If *This\IsRunning = #False
      *This\StartTime = ElapsedMilliseconds()
      *This\IsRunning = #True
    EndIf
    
  EndProcedure
  
  Procedure Stop(*This.Private_Members)
    
    If *This\IsRunning = #True
      *This\TotalTime = *This\TotalTime - ElapsedMilliseconds() + *This\StartTime
      *This\IsRunning = #False
    EndIf
    
  EndProcedure
  
  Procedure.q Consult(*This.Private_Members)
    
    If *This\IsRunning = #True
      TotalTime.q = *This\TotalTime - ElapsedMilliseconds() + *This\StartTime
    Else
      TotalTime = *This\TotalTime
    EndIf
    
    ProcedureReturn TotalTime
  EndProcedure
  
  Procedure.s Format(*This.Private_Members, TimeMask.s)
    
    MilliSeconds = Consult(*This)
    
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ; <<<<< On fait l'extraction des jours, Heures, minutes, secondes et des MS <<<<<
    
    If MilliSeconds < 0 
      MilliSeconds = MilliSeconds * -1
      Sign.s = "-"
    Else
      Sign = ""
    EndIf 
    
    Days = MilliSeconds / 86400000 
    MilliSeconds % 86400000
    
    Hours = MilliSeconds / 3600000
    MilliSeconds % 3600000
    
    Minutes = MilliSeconds / 60000
    MilliSeconds % 60000
    
    Seconds = MilliSeconds / 1000
    MilliSeconds % 1000
    
    ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ; <<<<< On s'occupe du filtre de sortie <<<<<
    
    If FindString(TimeMask, "%dd", 1)
      TimeMask = ReplaceString(TimeMask,"%dd", RSet(Str(Days), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%hh", 1)
      TimeMask = ReplaceString(TimeMask,"%hh", RSet(Str(Hours), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%mm", 1)
      TimeMask = ReplaceString(TimeMask,"%mm", RSet(Str(Minutes), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%ss", 1)
      TimeMask = ReplaceString(TimeMask,"%ss", RSet(Str(Seconds), 2, "0"))
    EndIf 
    
    If FindString(TimeMask, "%mss", 1)
      TimeMask = ReplaceString(TimeMask,"%mss", RSet(Str(MilliSeconds), 3, "0"))
    EndIf
    
    ProcedureReturn Sign + TimeMask
  EndProcedure
  
  Procedure Free(*This.Private_Members)

    ClearStructure(*This, Private_Members)
    FreeStructure(*This)

  EndProcedure
  
  Procedure.i New(P_MilliSeconds.l, P_Seconds.l = 0, P_Minutes.l = 0, P_Hours.l = 0, P_Days.l = 0)
    
    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS
    *This\StartTime = 0
    *This\TotalTime = P_MilliSeconds + SecondsToMilliseconds(P_Seconds) + MinutesToMilliseconds(P_Minutes) + HoursToMilliseconds(P_Hours) + DaysToMilliseconds(P_Days)
    *This\IsRunning = #False
    
    ProcedureReturn *This
  EndProcedure

  DataSection
    START_METHODS:
    Data.i @Reset()
    Data.i @SetTime()
    Data.i @Start()
    Data.i @Stop()
    Data.i @Consult()
    Data.i @Format()
    Data.i @Free()
    END_METHODS:
  EndDataSection

EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  MyTemporizer.Tempo::Tempo = Tempo::New(1500)
  MyTemporizer\Start()
  
  Debug MyTemporizer\Format("%ss:%mss")
  
  Exit_Condition = #False
  
  While Exit_Condition = #False
    
    If MyTemporizer\Consult() <= 0
      MyTemporizer\Stop() 
      Exit_Condition = #True
    EndIf
    
  Wend

  Debug MyTemporizer\Format("%ss:%mss")
  MyTemporizer\Free()
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics