• Module classes

    I'm currently working on a new way of developing modules and modules features that I'm so excited that I want to share a little initial info with all of you

    I will call this new way "module classes" Why the term classes? Some reasons:
    • because it will be possible to use dot notation for module methods
    • because in some way it will be the start of a new way of programming modules that will be very close to OOP but compiled
    • because if you have latest Power Basic compiler 905 you will be able to use PowerBasic Classes in thinBasic modules and use the same classes in thinBasic scripts wrapping the needed modules
    For the moment I will not give you too much inside details but I will show you what kind of scripts you will be able to use and how easy will be to develop new modules in this new way.

    So let's start from a thinBasic script example. Pay attention to the new syntax.
    Uses "LL2"
    '---The above module implements a CLASS named cLinkedList
    '---That CLASS name will be usable to define new object variables
    '---derived from that available classes
     
    Type tData
      x As Long
      y As Long
      w As Long
      h As Long
      AnArray(100) As Ext
    End Type
     
    '---Define a variable of cLinkedList CLASS
    '---During DIM of a new class variable nothing will happen
    '---other than internal allocation of a new variable derived from
    '---a class. You have to later call NEW to create a real object
    '---Out from the indicated class. See below
    Dim MyLL As cLinkedList
     
    Dim lCounter As Long
    Dim t1 As Double
    Dim t2 As Double
    Dim MyData As tData
     
    '---Create the object and call (if present) default constructor
    '---Default constructor parsing is responsibility of module
    '---programmer so you can define whatever syntax for it
    '---In the below case syntax is:
    ' constructor[(ByVal UseStrings As Long [, ByVal SortItems As Long])]
    MyLL = New cLinkedList
     
    t1 = Timer
    For lCounter = 1 To 10000
      MyData.x = lCounter
      '---Call another method that will add some key/data pairs
      '---After method name, thinBasic Core engine will pass responsibility
      '---to parse syntax to module method so module programmer has all the freedom
      '---to define whatever syntax for the method
      '---In this case synatx is: .AddString(ByVal sKey As String, ByVal sData As String)
      Myll.AddString("CODE" & lCounter, MyData)
    Next
    t2 = Timer
     
    '---Call other methods
    MyLL.Count
     
    MsgBox 0, "Total time: " & Format$(t2 - t1, "#0.000")
     
    '---When appropriated (end of script or exit from function, ...), 
    '---default class destructor will be internally executed
    '---for every object variable
    '---Having a class destructor is not mandatory
    
    Now let's see what will be needed in module in order to tell thinBasic Core engine about new classes.
    Here below some PowerBasic code extrated from a module I'm working on as example and that I will distribute as source code for learning when done:

    '----------------------------------------------------------------------------
    Function LoadLocalSymbols Alias "LoadLocalSymbols" (Optional ByVal sPath As String) Export As Long
    ' This function is automatically called by thinCore whenever this DLL is loaded.
    ' This function MUST be present in every external DLL you want to use with thinBasic
    ' Use this function to initialize every variable you need and for loading the
    ' new symbol (read Keyword) you have created.
    '----------------------------------------------------------------------------
     
      Local RetCode As Long
      Local pClass As Long
     
      pClass = thinBasic_Class_Add("cLinkedList", 0)
     
      '---If class was created
      If pClass Then 
     
        RetCode = thinBasic_Class_AddMethod(pClass, "_Create" , %thinBasic_ReturnNumber , CodePtr(LList_Create ))
        RetCode = thinBasic_Class_AddMethod(pClass, "_Destroy" , %thinBasic_ReturnNumber , CodePtr(LList_Free ))
     
        RetCode = thinBasic_Class_AddMethod(pClass, "AddString" , %thinBasic_ReturnNumber , CodePtr(LList_AddString ))
        RetCode = thinBasic_Class_AddMethod(pClass, "Count" , %thinBasic_ReturnNumber , CodePtr(LList_Count ))
     
      End If
    End Function
    
    Of course thinBasic module can have the usual way of managing module functions and new module classes all at the same time. So nothing will change in usual way of handling modules, you will just have a new weapon.

    Conclusion
    In next few weeks I will give more info and possibly a new thinBasic beta version where you will be able to experiment the creation of new classes.
    For the moment this is all I can tell you.
    Just to add that the above thinBasic script example is already working here in my machine.

    I will now work on the following areas:
    • integrating module classes into numeric and string expressions (done)
    • passing objects to sub/functions as parameters (done)
    • managing arrays of objects (done)
    • integrate objects inside UDT

    Ciao
    Eros
    This article was originally published in forum thread: Module classes started by ErosOlmi View original post
    Comments 9 Comments
    1. zlatkoAB's Avatar
      zlatkoAB -
      So ,this news is ok.
      But still if i wanna write modul i must
      use Power Basic for this job.
      Best option will be if is posibile that
      we can make write modules with
      thinBasic like is posibile in DLib.
      In DLib are called userLibs...
      What you say about that?
    1. ErosOlmi's Avatar
      ErosOlmi -
      Well, I do not know DLib. As far as I can see many people loved it but actually it seems an abandoned project.
      thinBasic is not a compiler so it cannot create any library.

      In any case wait till I will release firt thinBasic version having the new class module classes inside.
      Than we will see
    1. zlatkoAB's Avatar
      zlatkoAB -
      Eros DLib is not compiler it is interpreter
      wich can bind source with runtime
      thats all.
      OK i will wait...
    1. ErosOlmi's Avatar
      ErosOlmi -
      An interpreter cannot create runtime libraries (DLLs). Is like asking a car to fly: you need a plane.
      An interpreted can emit code (converting from its native syntax to other languages that can compile) into a different language that can compile. Many so called "compiler" are in reality code emitters (translators) into some ASM language. Than ASM is used to compile on the fly.

      But because I'm curious and I always try to learn something new, I just read about those "UserLibs" to get the idea about them. Downloaded DLib, red help, binary inspected such files. It seems that such files (.DCS) are just a collection of DLib source code made available as compressed (maybe encrypted) way. So such files are mainly like include files.
      The idea is not bad but you can create that kind of files only starting from DLib native language functionalities. You create some functions and say to DLib to make available as compressed/encrypted. They are not compiled at all. In few words it is just source code you cannot read.

      What I'm doing to do here is something completely different because I'm changing the heart of thinBasic Core engine.
    1. zlatkoAB's Avatar
      zlatkoAB -
      Yes Eros this is just source codes
      like include files it is not compiled of
      course.
      OK...
    1. ErosOlmi's Avatar
      ErosOlmi -
      I'm almost ready to release this new feature. Stay tuned.
    1. largo_winch's Avatar
      largo_winch -
      hello eros, zlatko. I am just looking for a simple in freebasic written ( module ) example. there are any example for it? thanks, largo
      new edit: solved! found sdk folder with pb/fb files.

      I am curious to see new module functions to create with thinbasic.
    1. ErosOlmi's Avatar
      ErosOlmi -
      Starting from thinBasic version 1.8.8.0 module classes are a reality.
      First module class developed is called cTimer and is native in thinBasic Core module, so available in every script.
      Full documentation in thinBasic help file.
      This is just a starter in order to test all possible aspects of this implementation.
    1. maxim's Avatar
      maxim -
      %thinBasic_ReturnNone             =  &h0 %thinBasic_ReturnCodeByte         =  &h1 %thinBasic_ReturnCodeInteger      =  &h2 %thinBasic_ReturnCodeWord         =  &h3 %thinBasic_ReturnCodeDWord        =  &h4 %thinBasic_ReturnCodeLong         =  &h5 %thinBasic_ReturnCodeQuad         =  &h6 %thinBasic_ReturnCodeSingle       =  &h7 %thinBasic_ReturnCodeDouble       =  &h8 %thinBasic_ReturnCodeCurrency     =  &h9 %thinBasic_ReturnCodeExt          = &h10 %thinBasic_ReturnNumber           = &h20 %thinBasic_ReturnString           = &h30  Declare Function FunctionGetPtr Lib "thinCore.dll" Alias "thinBasic_FunctionGetPtr" (ByVal fName As String) As Long Declare Function thinBasic_LoadSymbol Lib "thinCore.dll" Alias "thinBasic_LoadSymbol" (ByVal SymbolName As String, ByVal ReturnCode As Long, ByVal FunctionOrSubPointer As Dword, Optional ByVal ForceOverWrite As Long) As Long Declare Function thinBasic_Class_Add Lib "thinCore.dll" Alias "thinBasic_Class_Add" (ByVal sClassName As String, ByVal pClassFunc As Long) As Long Declare Function thinBasic_Class_AddMethod Lib "thinCore.dll" Alias "thinBasic_Class_AddMethod" (ByVal pClass As Long, ByVal sMethodName As String, ByVal MethodReturnType As Long, ByVal pMethodFunc As Long) As Long  Local RetCode As Long  TYPE IrrlichtDevice DWORD 	run As Long END TYPE  Local IrrlichtDevicePtr As Long = thinBasic_Class_Add("IrrlichtDevice", Function_GetPtr(IrrlichtDevice_run)) If IrrlichtDevicePtr Then 	RetCode = thinBasic_Class_AddMethod(IrrlichtDevicePtr, "run", %thinBasic_ReturnNumber, Function_GetPtr(IrrlichtDevice_run)) 	RetCode = thinBasic_Class_AddMethod(IrrlichtDevicePtr, "getVersion", %thinBasic_ReturnString, Function_GetPtr(IrrlichtDevice_getVersion)) Else 	Uses "console" 	PrintL "ERROR thinBasic_Class_Add IrrlichtDevice" End If
      
      I'm trying to do so and it might work, but is not suitable for Function_GetPtr code (and TYPE are also under suspicion). Here is the correct function for passing a pointer?