Wiki improvements

Forum for discussion about the documentation project.
dodicat
Posts: 6137
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wiki improvements

Postby dodicat » Feb 08, 2020 13:10

For fun.

strings.bas compile -dll

Code: Select all

'strings.bas compile with -dll switch
Enum
    up
    down
End Enum
type strings
    as string original
    as string sorted
    as byte direction
    declare constructor
    declare constructor(as string, as byte,as long,as long)
    declare function sort(as strings,as long,as long) as string
end type

constructor strings export
end constructor

constructor strings(tosort as string, dirn as byte,l as long,u as long) export
original=tosort
direction=dirn
sorted=sort(this,l,u)
end constructor

function strings.sort(ss As strings,begin As Long,Finish As Long) as string
    static as strings s:s=ss
    Dim As Long i=begin,j=finish
    Dim As ubyte x =S.original[(I+J)\2]
    While  I <= J
    if direction=down then
        While S.original[I] > X:I+=1:Wend
        While S.original[J] < X:J-=1:Wend
    else
        While S.original[I] < X:I+=1:Wend
        While S.original[J] > X:J-=1:Wend
    end if
    If I<=J Then Swap S.original[I],S.original[J]: I+=1:J-=1 
    wend
    If J > begin Then sort(S,begin,J)
    If I < Finish Then sort(S,I,Finish)
    return s.original
End function

 


testfile (in same folder as the dll)

Code: Select all


#inclib "strings"

Enum
    up
    down
End Enum

type strings
    as string original
    as string sorted
    as byte direction
    declare constructor
    declare constructor(as string, as byte,as long,as long)
end type


dim as string f="FreeBASIC example of dynamic library file for exported constructors (.dll)"

dim as strings g


g=type(f,up,0,len(f)-1)
print g.original
print g.sorted
print
g=type(f,down,0,len(f)-1)
print g.original
print g.sorted
print
g=type("0123456789",down,0,9)
print g.sorted
print "Done"


sleep
dodicat
Posts: 6137
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wiki improvements

Postby dodicat » Feb 08, 2020 18:35

Here is the .def file for strings.dll (above post)
. . .
;
; Definition file of strings.dll
; Automatic generated by gendef
; written by Kai Tietz 2008
;
LIBRARY "strings.dll"
EXPORTS
_ZN7STRINGSC1ER8FBSTRINGaii
_ZN7STRINGSC1Ev
. . .
So it looks like you can convert constructors to functions (OOP to procedural)

Code: Select all

#inclib "strings"

Enum
    up
    down
End Enum

type udt
    as string original
    as string sorted
    as byte direction
end type


declare function sort cdecl alias"_ZN7STRINGSC1ER8FBSTRINGaii"(as string, as byte=up,as long=0,as long=0) as udt


dim as string f="FreeBASIC example of dynamic library file for exported constructors (.dll)"



print f
print sort(f,up,0,len(f)-1).sorted
print sort(f,down,0,len(f)-1).sorted
print sort(f).original
print sort("0123456789",down,0,9).sorted
print "Done"


sleep
fxm
Posts: 9458
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Postby fxm » Feb 09, 2020 14:00

D.J.Peters wrote:A short OOP example for static and shared lib's would be a nice to have :-)
fxm wrote:For example:
viewtopic.php?p=236630#p236630

Perhaps a similar example could be added in the 'Static Libraries' or/and 'Shared Libraries (DLLs)' pages of the 'Programmer's Guide' ?

Usage proposal for Type/Class in a library, showing that exhaustive declarations in the user code are mandatory for overload operators (otherwise the prebuilt-in Len operator is called and applied on the object variable providing the length of its member data):

Code: Select all

'' header file: 'varZstring.bi'

Type varZstring Extends Zstring
    Public:
        Declare Constructor (Byref z As Const Zstring)
        Declare Operator Cast () Byref As Zstring
        Declare Operator Let (Byref z As Const Zstring)
        Declare Property allocated () As Integer
        Declare Destructor ()
    Private:
        Dim As Zstring Ptr _p
        Dim As Uinteger _allocated
End Type

Declare Operator Len (Byref v As varZstring) As Integer  '' mandatory for the user code to call
                                                         ''    the overload Len operator and
                                                         ''    not the prebuilt-in Len operator

Code: Select all

'' library module: 'varZstring.bas'

#include "varZstring.bi"

Constructor varZstring (Byref z As Const Zstring)
    If This._p <> 0 Then
        Deallocate(This._p)
    End If
    This._allocated = Len(z) + 1
    This._p = Callocate(This._allocated, Sizeof(Zstring))
    *This._p = z
End Constructor

Operator varZstring.Cast () Byref As Zstring
    Return *This._p
End Operator

Operator varZstring.Let (Byref z As Const Zstring)
    If This._allocated < Len(z) + 1 Then
    Deallocate(This._p)
    This._allocated = Len(z) + 1
    This._p = Callocate(This._allocated, Sizeof(Zstring))
  End If
  *This._p = z
End Operator

Property varZstring.allocated () As Integer
    Return This._allocated
End Property

Destructor varZstring ()
    Deallocate(This._p)
    This._p = 0
End Destructor

Operator Len (Byref v As varZstring) As Integer
    Return Len(Type<String>(v))  '' found nothing better than this
End Operator                     ''     (or: 'Return Len(Str(v))')

Code: Select all

'' test program: 'varZstringTest.bas'

#include "varZstring.bi"  '' must contain also the overload Len operator declaration,
                          ''    otherwise the prebuilt-in Len operator is called and applied
                          ''    on the object variable (providing the length of its member data)
#inclib "varZstring"

Print "VARIABLE",,, "|     LEN|  SIZEOF|"
Print "------------------------------------------|--------|--------|"

Dim As varZstring v = "FreeBASIC"  '' adjusts memory allocation to minimum
Print "varZstring v:", "'" & v & "'",, "|"; Using "########|"; Len(v); v.allocated

Dim As Zstring * 256 z
z = v
Print "Zstring    z:", "'" & z & "'",, "|"; Using "########|"; Len(z); Sizeof(z)

v = z & " & SourceForge"  '' only increases memory allocation if necessazy
Print "varZstring v:", "'" & v & "'", "|"; Using "########|"; Len(v); v.allocated

v = z & " & Wiki" '' only increases memory allocation if necessazy
Print "varZstring v:", "'" & v & "'", "|"; Using "########|"; Len(v); v.allocated

v.Constructor(v)  '' readjusts memory allocation to minimum
Print "varZstring v:", "'" & v & "'", "|"; Using "########|"; Len(v); v.allocated

Sleep
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

Re: Wiki improvements

Postby D.J.Peters » Feb 09, 2020 14:51

I self know how to use oop in lib's but I was thinking would be a nice have "Wiki improvment"
to add this missing oop library topic to the wiki = help file.

My last FB OOP library are 31 classes = *.bi files and ~300 constructors,methods,functions,properties in 245 *.bas files !

Joshy
fxm
Posts: 9458
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Postby fxm » Feb 09, 2020 19:16

Done (according to my post above):
ProPgStaticLibraries → fxm [added advanced example of OOP static library]
badidea
Posts: 1764
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Wiki improvements

Postby badidea » Feb 09, 2020 21:37

fxm wrote:The simplest would it not be to put one single constant link to the topic "News" of the forum:
News forum
and just add a comment saying to refer to the "Documentation" paragraph of the post for the version considered?

I gave the page (https://freebasic.net/wiki/wikka.php?wa ... iDownloads) a makeover.

For some reason, this wiki software wants to make links of "SourceForge", "KchmViewer" and "WikkaWiki". I dont know why.
Ah, CamelCase. Fixed now.

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 2 guests