auto-updating Type's

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

auto-updating Type's

Post by MrSwiss »

Hi,

have you ever wondered, whether something like that is possible in FB ?
The answer is obviously: YES (otherwise, this post whould be pointless).

I'm not the person for a lot of (useless?) words I prefer it, that the code and the comments in it, do the explaining instead.

As this example shows, it is not only auto-updating but also auto-converting it's own internals
(US-Date to ISO-Date and Time) which probably all none-US-members here, will appreciate:

Code: Select all

' DtTm_t-test1.bas -- (c) 2021-01-31, MrSwiss
'
' compile: -s console
'

Type DtTm_t                             ' automatic US-/ISO-date conv. & update
  Public:                               ' interface: externally accessible stuff
    Declare Constructor()               ' default constructor (type initializer)
    Declare Destructor()                ' default destructor
    Declare Operator Cast () As String  ' date & time (default return)
    Declare Function sdate() As String  ' date only
    Declare Function stime() As String  ' time only
  Private:                              ' any external access prohibited
    Declare Sub      update()           ' only internally callable (as below)
    Declare Sub      mkISO(ByVal As Const ZString Ptr, ByVal As ZString Ptr)
    As ZString * 11  cdate, adate       ' uses less memory than string, e.g. _
    As ZString * 9   ctime              ' with string/FBC 64: 72 bytes
    As UByte         padd               ' type alignment only (to 32 bytes)
End Type

Constructor DtTm_t()
    With This
        .ctime = Time
        .adate = Date
        .mkISO(.adate, .cdate)          ' convert US- to ISO-date and assign
    End With
End Constructor

Destructor DtTm_t()                     ' default dtor (free allocated memory)
    With This
        DeAllocate(@.cDate)             ' deallocate zstring's memory
        DeAllocate(@.aDate)
        DeAllocate(@.cTime)
    End With
End Destructor

Operator DtTm_t.Cast() As String
    With This
        .update()                       ' assure data is current
        Return .cdate + " " + .ctime
    End With
End Operator

Private Sub DtTm_t.update()             ' when none changed, do nothing
    Var ts = Time                       ' instead of multiple 'Time' calls
    With This
        If ts > .ctime Then             ' only new time
            .ctime = ts
        ElseIf ts < .ctime Then         ' new time & date (date change!)
            .ctime = ts
            .adate = Date
            .mkISO(.adate, .cdate)      ' convert US- to ISO-date and assign
        End If
    End With
End Sub

' separated: to avoid redundant code (ctor() and update() use it)
Private Sub DtTm_t.mkISO( _             ' US-date to ISO-date converter
    ByVal pad   As Const ZString Ptr, _ ' source ptr, read only
    ByVal pcd   As ZString Ptr        _ ' target ptr, read/write
    )                                   ' MANDATORY: ptr's must be dereferenced!
    *pcd = Right(*pad, 4) + "-" + _     ' year (first in ISO)
           Left(*pad, 2) + "-" +  _     ' month (second in ISO)
           Mid(*pad, 4, 2)              ' day (last/third in ISO)
End Sub

Function DtTm_t.sdate() As String
    This.update()                       ' assure data is current
    Return This.cdate
End Function

Function DtTm_t.stime() As String
    This.update()                       ' assure data is current
    Return This.ctime
End Function
' end type procedures


' ===== test1 =====
Dim As DtTm_t   ISO                     ' one instance of type (ctor call)

Print "DtTm_t size:"; SizeOf(DtTm_t); " Bytes"  ' show size of type
Print
Print ISO                               ' Cast Operator call, date & time
Sleep(1750, 1)                          ' prove the 'automatic update' feature
Print : Print ISO.stime()               ' just the time
Print ISO.sdate()                       ' just the date
Print : Print
Print "... done ... ";

Sleep
' ===== end test1 ===== ' ----- EOF -----
Just for informational purposes: this is a simplified version of the one I'm using myself.
IOW, there is quite a lot of improving it to be done, which I'm leaving to: DIY extensions.
(I'm NOT going to post the other version here.)
Xusinboy Bekchanov
Posts: 789
Joined: Jul 26, 2018 18:28

Re: auto-updating Type's

Post by Xusinboy Bekchanov »

MrSwiss wrote: Destructor DtTm_t() ' default dtor (free allocated memory)
With This
DeAllocate(@.cDate) ' deallocate zstring's memory
DeAllocate(@.aDate)
DeAllocate(@.cTime)
End With
End Destructor
This seems to me to be the wrong cleansing. Or is it the right one?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: auto-updating Type's

Post by MrSwiss »

Xusinboy Bekchanov wrote:This seems to me to be the wrong cleansing. Or is it the right one?
I'm just using it, for "demo purposes" but I suppose, that FB's default dtor for fix-len ZString does it alike.
DeAllocate() wants a Ptr to the memory that is to be freed, therefore, @ operator is mandatory.
Xusinboy Bekchanov
Posts: 789
Joined: Jul 26, 2018 18:28

Re: auto-updating Type's

Post by Xusinboy Bekchanov »

MrSwiss wrote:
Xusinboy Bekchanov wrote:This seems to me to be the wrong cleansing. Or is it the right one?
I'm just using it, for "demo purposes" but I suppose, that FB's default dtor for fix-len ZString does it alike.
DeAllocate() wants a Ptr to the memory that is to be freed, therefore, @ operator is mandatory.
It cleans itself twice, once by you, the second time by the system.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: auto-updating Type's

Post by MrSwiss »

Xusinboy Bekchanov wrote:It cleans itself twice, once by you, the second time by the system.
That is pretty unlikely, because general dtor's typically check first, if there is something to deallocate, before actually doing it.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: auto-updating Type's

Post by fxm »

Nothing to be deallocated in the destructor (ditto for a var-len string member).

The rule is simple:
- no Allocate/Callocate/Reallocate => no Deallocate
- no New => no Delete
Post Reply