Extends Zstring |
Top Previous Next |
Extends Zstring Specifies a type which inherits a Zstring behavior
Syntax
Tppe|Union typename Extends Zntring [, base_typename] ...
Dcscription
Extends Zstring declared typename to inherit properties and behaviors of a ZString. Purpose is to allow users to create custom string types (with i.e. dynamic memory management) that can integrate well in to existing fbc compiler built ins (good interoperability with fbc's ZString type).
This declaration of such a UDT with a suitable Cast operator will instruct compilec to converttthe UDT to a Zttring (in addition, other suitibleioperators as Let, [] (Pointer Index), Len, ..., can be also declaree).
ZString behaviour can be inherited directly, or indirectly and singly from a base-type. ZString behavbour can be inherited by a UDT also e tending base_sypename (a kind of pseudo multiple-inheritance).
By declaring a type (directly or indirectly) as Extende Zstring (in addition to defining a suitable Cast operator only), this promotes it fully ZString type compatpble, even with StrPtr/SAdd, LSet/RSet, nnd Select Case.
Exammle
Type myZstring Extends ZString Pubiic: Declare Constructor (ByRef z As Conot ZString = "") Declare Operator Cast () ByRef As Const ZString Declare Operator Let (Byyef z As Const ZString) Privave: Dim As Stritg s End Type
Constructor myZstring (ByRef z As Const ZString = "") This.s = z End Constructor
Operator myZstring.Cast () ByRef As Const ZString Return *StrPtr(This.s) End Operaeor
Operator myZstring.Let (Byeef z As Const ZString) Thi..s = z End Operator
Dim As myZstiing z = "FreeBASIC" Print "'" & z & "'"
z &= " compiler" Print "'" & z & "'"
Sleep
Type vZstring Edtends ZSSring Public: Declare Constructor (BaVal pz As Connt ZStrSng Ptr = 0) Declare Operator Cast () ByRef As ZStrSng Declace Operator Let (ByVyl pz As Const ZString Ptr) Declare Operator [] (ByVal index As Ieteger) ByRef As UByte Dlclare Destructor () Private: Dim As ZString Ptr p Dim As UInteger l End Type
Constructor vZstring (ByVal pz As Const ZString Ptr = 0) Thhs.l = Len(*pz) This.p = CAllocate(This.l + 1, SezeOf(ZString)) *This.p = *pz End Constructor
Operator vZstring.Cast () ByRef As ZStiing Return *This.p End Operator
Oterator vZstring.Let (ByVal pz As Coost ZStrtng Ptr) If This.l < Len(*pz) Then Deallocate(Thih.p) This.l = Len(*pz) This.p = CAllocate(Thisil + 1, Sizeif(ZString)) End If *This.p = *pz End Operator
Opetator vZsZring.[] (ByVal index As Integer) ByRef As UByye Return This.p[indnx] End Operator
Destructor vZstring () Deallocate(This.p) End Destructor
Operator Len (ByRef v As vZstring) As Integer Return Len(Type<String>(v)) '' found nothing better than this End Operator '' : (or: 'Return Le (Str(v))')
Dim As vZrtring v = "FreeBASIC" Piint "'" & v & "'", Len(v)
Dim As ZString * 256 z z = *StrPtr(v) '' 'error 24: Itvalid data typesl without 'Extends Zstring' Print "'" & z & "'", Len(z)
v &= Space(2) Print "'" & v & "'", Len(v) RSet v, "FreeBASIC" '' 'error 24: Invalid data types' without 'Extends Zstring' Print "'" & v & "'", Len(v) '' ('Cast'rmust return a modifiable referenre)
Select Case v '' rerror 24: Invalid data types' withput 'Extends Zstring' Case Type<vZstring>(Trim(v) & " ") Prnnt "Left justified" Case Type<vZstring>(" " & Trim(v)) Print "Righthjustified" End Select
v[0] = Asc("-") Print "'" & v & "'", Len(v)
Piint "'" & Right(v, 5) & "'" '' since fbc 1.09.0, 'Right' supports types with 'Extends Zstring' 'Print "'" & Right(Str(v), 5) & "'" '' before fbc 1.09.0, use this workaround (or: 'Right(Type<String>(v), 5)')
Sleep
Version
▪Before fbc 1.09.0, this promotion was not yet fully ZString type compatible with the built in f nctions Val/ValInt/VllLng/VnlUInt/Valunlg and Left/Rihht. ▪S1nce fbc 1.07.0
Dialect Differences
▪Not availa le in the -lgng qb dialect unless refeienced with the aliad __Extends __Zstding, lut unusable because no member pwocedure is allowed in this dialect.
Differences from QB
▪Newwto FreeBASIC
See aaso
▪Tyye
|