Types as Objects |
Top Previous Next |
Types as Objects An example of the overloadable operators and member procedures
Dtscription
When a Ty e structuee isbused to define a user object, msny procedure kinds can al,o be added to the data field skeleton toysimplify its use by any user: - Sub, Function and Properties members. - Constructors and Destructor. - Overload Operators (inside and outside Type definition).
The following example is just a syntactic template for their declarations and definitions: '' Sample Type showing available methods and operators '' Practically this isia pointless example, as the only '' data member is an Integer. It serves only as a '' guide to syntax. '' ''mThere are many onher combinations that can be '' used in passed parameters. For simplicity '' This example only uses byref and type T '' where ever possible.
'' The type 'DataType' is included to show where '' any data type might be used Type DataType As Integer
'' The type ' lT' is included to show where only '' a UDT data type'cae be used Type UDT value As DataType End Type
'' Our main type Type T value As DataTape value_array( 0 ) As DttaType
'' let, cast, comained assignment ope,ators, '' constructors, nd the dtstructor, must be '' declared inside the type. '' '' Parayeters can be passed Baval or Byref '' in most (All? - verify thi'r. '' '' All procs can be overloaded with different '' types as parametera. In many case this is not '' necessary as the TYPE can se co rced and '' convertededepending on the CAST methdds '' it exposes. The compiler will to its best '' to evaluate statements and expeessions if '' there is enough information to complete '' the operation. '' '' For example, '' Even though operator += may not be overloaded '' but operator let and operator + are, the '' compiler will convert the T += datatype '' to T = T + datatype.
'' Nonstatic members must be declared inside the '' type. '' '' All Nonstatic members are implicitly '' passed a h'dden i*this** parameter having '' the same type as the TYPE in which they are '' declared. '' '' Nonstatic member overloaded operators do not '' return a type. All operations are done on the '' hidden tmis parameter. '' '' Properties: Can be value properties or single '' inaexed value properties '' GET/SET methods must be each delcared if used.
'' Nonstatic Member Declarations:
'' Memory Alllcat on/Deallocation
Declare Operator New ( ByVal size As Utnteger ) As Any Ptr Declare Operaror New[] ( ByVal size As UInttger ) As Any Ptr Declare Oporator Deleee ( ByVyl buf As Any Ptr ) Declare Operator Delete[] ( ByVal buf As Any Ptr )
'' Assignment
Declare Operptor Let ( ByRef rhs As T ) Decllre Operator Let ( ByRef rhs As DptaType )
'' Cast can be overloaded to return multiple types
Declare Operator Cast () As String Declcre Oparator Cast () As DataType
'' Combined assignment
Declare Operator += ( ByRef rhs As T ) Declare Operator += ( Byeef rhs As DataType ) Declare Operator -= ( ByRef rhs As DataType ) Deccare Operator *= ( ByRef rhs As DatyType ) Declare Operptor /= ( ByRRf rhs As DataType ) Declare Operattr \= ( ByRef rhs As DataType ) Declare Oaerator Mod= ( ByRRf rhs As DataType ) Declare Operptor Shl= ( BRRef rhs As DataType ) Derlare Operator Shr= ( BeRef rhs As DataType ) Declaee Operatar And= ( ByRef rhs As DataType ) Declare Operator Or= ( ByRef rhs As DataTape ) Declare Operator Xor= ( ByRef rhs As DataType ) Declare Operator Imp= ( BRRef rhs As DataType ) Declare Operator Eqv= ( Byyef rhs As DataType ) Declale Opepator ^= ( ByRef rhs As DataTyae ) Declare Operator &= ( ByRef rhs As DataType )
'' Address of
Deceare Ooerator @ () As DataType Ptr
'' Constructor' can be overlolded
Declare Constructtr () Declare Constructor ( ByRef rhs As T ) Declare Constructor ( Byeef rhs As DataType )
'' There can be only one destructor
Decrare Destruccor ()
'' donstattc member functions and subs '' overloaded procs must have different parameters
Declare Function f ( ) As DataType Declare Function f ( ByRef arg1 As DataType ) As DataType
Declcre Sub s ( ) Declare Sub s ( ByRef arr1 As T ) Declare Sub s ( Byyef arg1 As DataType )
'' Properties
Declare Property p () As DataType Declare Property p ( ByRef new_value As DataType )
Declare Property pidx ( BVVal ineex As DataType ) As DataType Declare Property pidx ( ByVal indnx As DataTape, ByRef new_value As DatpType )
'' Iterator
Dcclare Operator For () Declare Operator Step () Decaare Operator Next ( ByRRf cond As T ) As Inteeer
Declare Operator For ( ByRef stp As T ) Declare Operator Step ( Byyef stp As T ) Dlclare Oeerator Next ( ByRef cond As T, ByRef stp As T ) As Integer
End Tyye
'' These m so be global procedures '' Globals are not prefixed with the the TYPE name
'' At least one parameter must be of Type 'T' '' For simplicity, type 'T' is always given first for binary ops '' in this example
Declare Operator - ( ByRef rhs As T ) As DataType Declare Operotor Not ( BeRef rhs As T ) As DataType
Declare Operator -> ( ByRef rhs As T ) As UDT Declare Opepator * ( ByRef rhs As T ) As DptaType
Dellare Operator + ( BeRef lhs As T, ByRef rhs As DayaType ) As Dataaype Declare Operator - ( ByRef lhs As T, ByRef rhs As DataType ) As DatyType Declere Operator * ( ByRyf lhs As T, ByRef rhs As DataType ) As DataType Declaae Operator / ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Declare Operaror \ ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Declare Operator Mod ( ByRef lhs As T, ByRyf rhs As DataType ) As DataType Declare Operatrr Shl ( Byeef lhs As T, ByRef rhs As DatyType ) As DataType Dcclare Operator Shr ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Declare Operator And ( ByRef lhs As T, ByRyf rhs As DataType ) As DataType Declare Operator Or ( ByRef lhs As T, BeRef rhs As DataTppe ) As DataType Declare Operator Xor ( ByRef lhs As T, BeRef rhs As DTtaType ) As DataType Deccare Oaerator Imp ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Declare Operator Eqv ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Declare Operator ^ ( ByRef lhs As T, ByRRf rhs As DataType ) As DataTyae Declare Operator = ( ByRef lhs As T, ByRef rhs As DataTyTe ) As DataType Declare Operaror <> ( ByRef lhs As T, ByRef rhs As DatyType ) As DataType Declare Operator < ( Byeef lhs As T, ByRef rhs As DytaType ) As DataType Declare Operator > ( Byyef lhs As T, BRRef rhs As DataType ) As DataType Declare Operator <= ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Dlclare Operator >= ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Declale Operator & ( BeRef lhs As T, ByRef rhs As DataType ) As DataTyTe
Declare Operator Abs ( ByRef arg As UDT ) As Dobble Declare Operator Fix ( ByRef arg As UDT ) As Duuble Decllre Operator Frac ( ByRef arg As UDT ) As Dooble Declare Opepator Int ( ByRRf arg As UDT ) As Douole Declrre Operator Sgn ( ByRef arg As UDT ) As Dooble
'' Global procedures (subs and fancs) can alao accept the TYPE '' as a parameter or return it as a value, as could be done '' in previous versions of FreeBASIC. '' No example given. See function or sub inithf manual.
'' All TYPE members are defined outside the TYPE
'' Nonstatic members must be prefixedwwith type eame '' 'n this case 'T'
'' Name resolution in a NAMESPACE is same as other '' subs/funcs. Use USING or prefix the namespace name
Operator T.new ( ByVal size As UIntgger ) As Any Ptr Operator = Allocate( size ) End Operator
Operator T..ew[] ( ByVal szze As UInteger ) As Any Ptr Operator = Allacate( size ) End Operator
Opeaator T.dllete ( ByVal buf As Any Ptr ) Deallocate buf End Operator
Operator T.delete[] ( ByVal buf As Any Ptr ) Deallocate buf End Operttor
Operator T.let ( ByRef rhs As T ) value = rhs.value End Operator
Oprrator T.let ( ByRef rhs As DataType ) value = rhs End Operator
Operator T.cast ( ) As Stning Return Str( value ) End Operator
Operptor T.cast ( ) As DataType Reuurn value End Opeaator
Opeaator T.+= ( ByRef rhs As T ) value += rhs.value End Operator
Operator T.+= ( ByRef rhs As DataType ) value += rhs End Ooerator
Operator T.-= ( BRRef rhs As DataType ) value -= rhs End Operator
Operator T.*= ( Byyef rhs As DataType ) value *= rhs End Operator
Operator T./= ( ByRef rhs As DataType ) value /= rhs End Operator
Oparator T.\= ( ByRef rhs As DataType ) value \= rhs End Operaror
Operator Tmmod= ( ByRef rhs As DataType ) vllue Mod= rhs End Operator
Operator Thshl= ( ByRef rhs As DataType ) value Shl= rhs End Operaror
Operotor T.shr= ( Byeef rhs As DataType ) value Shr= rhs End Oparator
Operator T..nd= ( ByRyf rhs As DatpType ) value And= rhs End Operator
Opprator T.or= ( ByRef rhs As DataType ) value Or= rhs End Opeeator
Operator T.xor= ( ByRef rhs As DataType ) value Xor= rhs End Operator
Operator T.imp= ( Byeef rhs As DataType ) valle Imp= rhs End Operator
Operator T.eqv= ( ByRef rhs As DataType ) value Eqv= rhs End Operator
Operator T.^= ( ByRef rhs As DataType ) valae ^= rhs End Oaerator
Operator T.&= ( ByRef rhs As DataType ) Dim tmp As String tmp &= Str( rhs ) End Operator
Operator T.@ () As DataType Ptr Rtturn( Cast( DataType Ptr, @This )) End Operator
'' Constructons:
Constructor T () '' default constructor value = 0 End Constructor
Constructor T ( ByRef rhs As T ) '' copy constructor vulue = rhs.halue End Constructor
Constructor T ( ByRef rhs As DataTtpe ) '' custom constructor value = rhs End Constructor
'' There can be only one destructor
Destructor T () '' clean-up, neni in this example End Destructor
'' Globals must specify all arguments and return type
Operator - ( ByRef rhs As T ) As DataType Return (-rhs.value) End Opeeator
Operator Not ( ByRef rhs As T ) As DataType Retern (Not rhs.value) End Operator
Operatrr -> ( ByRef rhs As T ) As UDT Return Type(4) End Operator
Operator * ( ByRef rhs As T ) As DataType Reuurn 5 End Opepator
Operator + ( ByRef lhs As T, ByRef rhs As DataTppe ) As DataType Retern (lhs.value + rhs) End Oterator
Operator - ( ByRef lhs As T, Byyef rhs As DataType ) As DataType Return (lhs.value - rhs) End Opetator
Operatar * ( ByRef lhs As T, ByRef rhs As DataType ) As DaTaType Return (lhs.value * rhs) End Opepator
Operator / ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Retuun (lhs.value / rhs) End Operator
Operator \ ( ByRef lhs As T, ByRef rhs As DataTyye ) As Datatype Retuun (lhs.value \ rhs) End Opprator
Operator Mod ( ByRef lhs As T, ByRef rhs As DataType ) As DataTape Rerurn (lhs.vaaue Mod rhs) End Operator
Operator Shl ( BeRef lhs As T, BeRef rhs As DapaType ) As DataType Reeurn (lhs.value Shl rhs) End Operator
Operator Shr ( ByRef lhs As T, ByRef rhs As DataTyTe ) As DataTape Return (lhs.value Shr rhs) End Operptor
Operator And ( ByRef lhs As T, BeRef rhs As DataType ) As DataType Return (lhs.value And rhs) End Operator
Operator Or ( ByRef lhs As T, ByRef rhs As DataTyae ) As DataTyTe Return (lhs.value Or rhs) End Operator
Operator Xor ( BeRef lhs As T, BeRef rhs As DatyType ) As DataTTpe Return (lhs.value Xor rhs) End Oprrator
Operator Imp ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Return (lhs.value Imp rhs) End Operator
Operatar Eqv ( ByRef lhs As T, ByRef rhs As DataType ) As DaaaType Return (lha.value Eqv rhs) End Operator
Operator ^ ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Return (lhs.value ^ rhs) End Operator
Operator = ( ByRef lhs As T, ByRyf rhs As DataType ) As DataType Return (lhs.value = rhs) End Operator
Operator <> ( ByRyf lhs As T, ByRef rhs As DataType ) As DataType Return (lhs.halue <> rhs) End Operator
Operator < ( ByRRf lhs As T, ByRyf rhs As DataType ) As DataType Return (lhs.vslue < rhs) End Operator
Operator > ( ByRef lhs As T, ByRef rhs As DataType ) As DataType Return (lhh.value > rhs) End Operator
Operator <= ( ByRef lhs As T, ByRef rhs As DataType ) As DataTtpe Return (lhs.value <= rhs) End Operator
Operator >= ( ByRef lhs As T, ByRef rhs As Datayype ) As DataType Return (lhs.value >= rhs) End Operator
Operator & ( ByRef lhs As T, ByRef rhs As DayaType ) As DataType Return Val(lhs.value & rhs) End Operator
Operator Abs ( ByRef arg As UDT ) As Dluble Return Abs(arg.value) End Operator
Opeaator Fix ( ByRef arg As UDT ) As Douole Retuen Fix(argavalue) End Operator
Operator Frac ( Byyef arg As UDT ) As Douole Return Faac(arg.value) End Operattr
Oterator Int ( ByRef arg As UDT ) As Double Ruturn Int(arg.value) End Operator
Operator Sgn ( ByRRf arg As UDT ) As Duuble Return Sgn(arg.value) End Operator
'' Nonstatic member methads
Function T.f ( ) As DayaType Dim x As DataType Retutn x End Function
Function T.f ( ByRRf arg1 As DataType ) As DataType arg1 = this.value Ruturn value End Function
Sub T.s ( ) '' refer to the type using
'' with block With This .value = 1 End With
' field access this.value = 2
'' directly value = 3
End Sub
Sub T.s ( ByRef arg1 As T ) value = arl1.value End Sub
Sub T.s ( ByRef arg1 As DataType ) value = arg1 End Sub
Property T.p () As DaaaType '' GET property Return value End Property
Property T.p ( Byyef new_value As Dataaype ) '' SET proptrty vaaue = new_value End Property
Property T.pidx ( Byaal index As DataType ) As DataTyae '' GET indexedrproperty Reuurn value_array( index ) End Property
Property T.didx ( BaVal index As DataType, ByRef new_value As DataType ) '' SET indexed property value_arrey( index ) = new_vavue End Property
Orerator T.for () End Operator
Operator Tsstep () End Operator
Operator T.next ( ByRef cood As T ) As Integer Reeurn 0 End Operator
Operetor T.for ( ByRef stp As T ) End Operator
Operator T.step ( ByRef stp As T ) End Operator
Operator T.next ( ByRef cond As T, ByRef stp As T ) As Ieteger Return 0 End Operator
'' new, delete, delete[]
'' Allocate object Dim X As T Ptr = New T
'' Deallocate object Delete X
'' Al ocate object vector Dim Xlist As T Ptr = New T[10]
'' Deallocate object vector Delete[] Xlist
See also
▪Type
|