FreeBASIC 1.08 Development

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: FreeBASIC 1.08 Development

Post by Josep Roca »

- growth rate field is stored with every string
There are instances in which an application may need to do hundreds or even thousands of string concatenations, e.g. my TypeLib Browser. In such cases, changing the grow rate makes a very big difference in speed.
- pre-allocates 260 bytes, even for empty strings
- overloads CAST() AS ANY PTR allowing ustring to be passed to any pointer type without warning/error.
- no awareness of CONST qualifiers
These are pragmatic approaches of a Windows programmer. It is very annoying to have to use casting in almost every line when you work with the Windows API. I also have several other classes to deal with BSTRings, Variants, Safe arrays, etc., and need it to allow to work transparently between them.

However, I fully understand that a class that will be distributed officially with FB must follow all the current conventions of the language.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: FreeBASIC 1.08 Development

Post by coderJeff »

Thanks, José. Yeah, I recognize you have goals well beyond what fbc can offer natively. I completely understand your motivation to optimize for your typical usage. fb can't come close to what you have developed and offer. For a var-len wstring substitute distributed with freebasic I'm going for the minimal implementation.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: FreeBASIC 1.08 Development

Post by coderJeff »

Juergen Kuehlwein wrote:"DWSTRING" should be avoided, because it is used in the compiler´s code.
Why? 'dwstring' and 'fb.dwstring' could co-exist in compiler without conflict. Same goes for any user code.

'fb.dwstring' will not break any existing code unless
1) '"dwstring' is already defined AND,
2) have '#include "ustring.bi"' AND,
3) 'using fb' statement.

The advantage of using a namespace.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: FreeBASIC 1.08 Development

Post by Juergen Kuehlwein »

Of course you are right!

Personally, even if it´s technically possible, i would do everything i can to avoid having "dwstring" and "fb.dwstring" in the same code. It´s too easy to make hard to find mistakes - YMMV.


JK
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBASIC 1.08 Development

Post by MrSwiss »

In order to further expose the new information, provided by "array.bi" in the upcoming
release version of: FBC 1.08.0 (currently still DEV status) I've written "arrayinfo.bi", in
order to 'get at the goods' in a comparatively simple way.

It contains currently only a Sub (ArrayInfo()) styled alike ScreenInfo() and, the for most
'match deciding' Function (ArrayType()) which returns: either static or dynamic (array).

The difference is:
ArrayInfo (Sub): spills everything contained in "array.bi" but since all arguments are
optional, the user decides which information is wanted/needed ...
ArrayType (Function): returns only 'is static' or 'is dynamic' (as Boolean).

Common to both is:
the Macro SetTypeArrayInfo(data-type) used to initialize the 'private overloaded'
versions of Sub/Function. (all common FB data-types, except ZString!)
All of them are called by the 'built-in' initializer and later discarded by FBC if not called.
Important NOTE:
If a user wants to employ a array of UDT, a explicit macro call is mandatory before
using above mentioned goodies (only the user knows her/his UDT's).
If "array.bi" is not loaded by user, "arrayinfo.bi" does so automatically (this means that
only a single #Include statement is needed in the users code).

arrayinfo.bi:

Code: Select all

' arrayinfo.bi -- (c) 2019-09-05, MrSwiss

' extended with ArrayType() -- 2019-11-05

#Ifndef __FBC_INT_ARRAY_BI__            ' if NOT loaded yet ...
#Include "fbc-int/array.bi"             ' load it now (otherwise nothing happens)
#EndIf  ' __FBC_INT_ARRAY_BI__


#Ifndef __FBC_INT_ARRAYINFO_BI__
#Define __FBC_INT_ARRAYINFO_BI__        ' prevent multiple inclusion


#If __FB_LANG__ = "fb"
namespace FBC                           ' compatibility with array.bi
#EndIf


#Macro SetTypeArrayInfo(DataType)       ' define datatype for overloaded procedures

Private Sub ArrayInfo OverLoad(  _      ' get array header information
          a()   As DataType,     _      ' _in_ mandatory: array as defined by macro
    ByRef pidx  As Any Ptr  = 0, _      ' _out_ opt. pointer to lowest index (0, 0, ...)
    ByRef pbase As Any Ptr  = 0, _      ' _out_ opt. pointer to allocated memory
    ByRef t_mem As UInteger = 0, _      ' _out_ opt. total memory (in Bytes)
    ByRef e_len As UInteger = 0, _      ' _out_ opt. element size (dito)
    ByRef dims_ As UInteger = 0, _      ' _out_ opt. number of dimensions
    ByRef flag_ As UInteger = 0, _      ' _out_ opt. flags (all as one)
    ByRef padim As FBARRAYDIM Ptr = 0 _ ' _out_ opt. pointer to internal type array
    )
    Dim As FBARRAY Ptr pa = _
        ArrayDescriptorPtr(a())         ' get array descriptor ptr (pa)

    pidx  = pa->index_ptr               ' see array.bi for explanation
    pbase = pa->base_ptr
    t_mem = pa->size
    e_len = pa->element_len      
    dims_ = pa->dimensions
    flag_ = pa->flags
    padim = @(pa->dimTb(0))
End Sub

Private Function ArrayType OverLoad( _  ' dynamic or static array?
          a()   As DataType _           ' _in_ mandatory: array as defined by macro
    ) As Boolean                        ' FALSE = dynamic | TRUE = static
    Dim As FBARRAY Ptr pa = _
        ArrayDescriptorPtr(a())         ' get array descriptor ptr (pa)

    If (pa->flags And FBARRAY_FLAGS_FIXED_LEN) Then _
        Return TRUE                     ' 1 = static array (fix-len)
    Return FALSE                        ' 0 = dynamic array (var-len)
End Function

#EndMacro

' initialize Arrayinfo with all common data types defined in FB (no ZString!)
' for UDT's you'll still have to make a explicit call to SetTypeArrayInfo(UDT)
SetTypeArrayInfo(Byte)                  ' 1 byte size (8 bit)       ' integer types
SetTypeArrayInfo(UByte)                 ' 1 byte size (8 bit)
SetTypeArrayInfo(Boolean)               ' 1 byte size (8 bit)
SetTypeArrayInfo(Short)                 ' 2 byte size (16 bit)
SetTypeArrayInfo(UShort)                ' 2 byte size (16 bit)
SetTypeArrayInfo(Long)                  ' 4 byte size (32 bit)
SetTypeArrayInfo(ULong)                 ' 4 byte size (32 bit)
SetTypeArrayInfo(Integer)               ' ptr size (32 or 64 bit)
SetTypeArrayInfo(UInteger)              ' ptr size (32 or 64 bit)
SetTypeArrayInfo(LongInt)               ' 8 byte size (64 bit)
SetTypeArrayInfo(ULongInt)              ' 8 byte size (64 bit)
SetTypeArrayInfo(Single)                ' 4 byte size (32 bit)      ' float types
SetTypeArrayInfo(Double)                ' 8 byte size (64 bit)
SetTypeArrayInfo(String)                ' 3 x ptr size (32 or 64 bit) string header
' end initializer


#If __FB_LANG__ = "fb"
end namespace
#EndIf


#EndIf  ' __FBC_INT_ARRAYINFO_BI__
' ----- EOF -----
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: FreeBASIC 1.08 Development

Post by Juergen Kuehlwein »

There is a similar thing in my PR for arrays:

Code: Select all

' i = array(specifier, array), returns requested value as integer, any ptr or boolean
'***********************************************************************************************
' desc = 60 'get descriptor information (only for internal use)
' data 'pointer to the first array element in memory
' dimensions '# of dimensions (same as UBOUND(array, 0)
' total_size 'in bytes
' total_count 'total # of elements in all dimensions
' size 'same as sizeof(array)
' isfixed 'fixed size array
' isdimmed 'dimmed or empty (zero dimensions)
' isattached 'attached or not
It returns the requested info (specifier) one info at a time, but can do that for all kinds (including UDTs) of arrays.


I agree that there must be an implementation exposing array internals to the user. Especially "fixed vs. dynamic" and "is already dimmed or not" are essential.

There are still fixes to be done in the array related RTL functions (e.g prevent redimming passed fixed size arrays et al.)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBASIC 1.08 Development

Post by MrSwiss »

The main difference seems to be, that I don't need any RTL changes at all.
(if array is static = dimentioned, I don't attempt any resizing operation)
UDT's are equally possible after defining the UDT (Macro call) to generate
the required data-type = UDT-name (can't be made automatically since,
the user can choose any name s/he likes).

Single argument means: 7 calls instead of only 1 (aka: too much user effort
required, IMO)
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: FreeBASIC 1.08 Development

Post by Juergen Kuehlwein »

@MrSwiss

You don´t need a macro at all in this case, you can just code:

Code: Select all

#Include once "fbc-int/array.bi" 


using fbc


type my_udt
  x as long
  y as long
  z as long
end type


dim u(1 to 2) as my_udt


  Dim As FBARRAY Ptr pa = ArrayDescriptorPtr(u())

  print "index ptr",,  hex(pa->index_ptr)
  print "base ptr",,   hex(pa->base_ptr)
  print "total size",, pa->size
  print "number of elements", pa->element_len
  print "dimensions",, pa->dimensions
  print "flags",,      bin(pa->flags, 8)
  print "dimTb()",,    hex(@(pa->dimTb(0)))


sleep
end
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBASIC 1.08 Development

Post by MrSwiss »

@JK,

seems to me that you don't understand, what it's all about and how the Macro
"works" within the context. You should take some time to study the code to
understand, "why" things are done in that particular way, in the implementation.

Btw: your provided code makes no sense to me ... the used types internals are
not even correct, to start off with (UInteger / Integer / Integer, NOT Long's).
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC 1.08 Development

Post by fxm »

Ugly but no macro.
If the following order is respected:
- Declaration of the procedure using 'Any' as data type
- Calls of the procedure with any type of data
- Definition of the procedure using a type of data
The program compiles.

Code: Select all

#Include "fbc-int/array.bi"
Using FBC
Declare Sub ArrayInfo (  _              ' get array header information
          a()   As Any,          _
    ByRef pidx  As Any Ptr  = 0, _      ' _out_ opt. pointer to lowest index (0, 0, ...)
    ByRef pbase As Any Ptr  = 0, _      ' _out_ opt. pointer to allocated memory
    ByRef t_mem As UInteger = 0, _      ' _out_ opt. total memory (in Bytes)
    ByRef e_len As UInteger = 0, _      ' _out_ opt. element size (dito)
    ByRef dims_ As UInteger = 0, _      ' _out_ opt. number of dimensions
    ByRef flag_ As UInteger = 0, _      ' _out_ opt. flags (all as one)
    ByRef padim As FBARRAYDIM Ptr = 0 _ ' _out_ opt. pointer to internal type array
    )
Declare Function ArrayType ( _          ' dynamic or static array?
          a()   As Any _
    ) As Boolean                        ' FALSE = dynamic | TRUE = static


Dim As String array(-2 To 3)

Dim As Uinteger element_length
ArrayInfo(array(), , , , element_length)
Print element_length

Print ArrayType(array())

Sleep


Sub ArrayInfo (  _                      ' get array header information
          a()   As Any Ptr,      _
    ByRef pidx  As Any Ptr  = 0, _      ' _out_ opt. pointer to lowest index (0, 0, ...)
    ByRef pbase As Any Ptr  = 0, _      ' _out_ opt. pointer to allocated memory
    ByRef t_mem As UInteger = 0, _      ' _out_ opt. total memory (in Bytes)
    ByRef e_len As UInteger = 0, _      ' _out_ opt. element size (dito)
    ByRef dims_ As UInteger = 0, _      ' _out_ opt. number of dimensions
    ByRef flag_ As UInteger = 0, _      ' _out_ opt. flags (all as one)
    ByRef padim As FBARRAYDIM Ptr = 0 _ ' _out_ opt. pointer to internal type array
    )
    Dim As FBARRAY Ptr pa = _
        ArrayDescriptorPtr(a())         ' get array descriptor ptr (pa)

    pidx  = pa->index_ptr               ' see array.bi for explanation
    pbase = pa->base_ptr
    t_mem = pa->size
    e_len = pa->element_len     
    dims_ = pa->dimensions
    flag_ = pa->flags
    padim = @(pa->dimTb(0))
End Sub

Function ArrayType ( _                  ' dynamic or static array?
          a()   As Any Ptr _
    ) As Boolean                        ' FALSE = dynamic | TRUE = static
    Dim As FBARRAY Ptr pa = _
        ArrayDescriptorPtr(a())         ' get array descriptor ptr (pa)

    If (pa->flags And FBARRAY_FLAGS_FIXED_LEN) Then _
        Return TRUE                     ' 1 = static array (fix-len)
    Return FALSE                        ' 0 = dynamic array (var-len)
End Function
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: FreeBASIC 1.08 Development

Post by Juergen Kuehlwein »

Yes! The point is, you don´t need overloaded functions and a macro wrapping them.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBASIC 1.08 Development

Post by MrSwiss »

NO, fxm's method doesn't work in a include-file (aka: .bi).
ERROR: "type mismatch" (as expected).
(seems to only work at the same level of code)

Explanation:
It should be as easy as possible for the user to make a call.
(not something like FBC.ArrayType(@array(LBound(array))))
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC 1.08 Development

Post by fxm »

My principle also works if the procedure definitions are in a separate compiled module or a library.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBASIC 1.08 Development

Post by MrSwiss »

fxm wrote:My principle also works if the procedure definitions are in a separate module.
??? do you mean, separate module than the implementation ?
Anyhow, separately compiled isn't the same as include-file (point at issue, right now!)
Last edited by MrSwiss on Nov 10, 2019 22:25, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC 1.08 Development

Post by fxm »

fxm wrote:My principle also works if the procedure definitions are in a separate compiled module or a library.
Post Reply