Cross-linked lists

General FreeBASIC programming questions.
monochromator
Posts: 42
Joined: Mar 05, 2013 5:37

Cross-linked lists

Postby monochromator » Jun 21, 2013 9:43

There is simple example of cross-linked list

Code: Select all


type sqlite3_file
    pMethods as sqlite3_io_methods ptr
end type

type sqlite3_io_methods
    iVersion as integer
    xClose as sqlite3_file ptr
end type

dim a1 as sqlite3_file, a2 as sqlite3_io_methods

a1.pMethods = @a2
a2.xClose = @a1



As i understood, FreeBASIC doesn't support such declarations.
There is the error during compilation:
COHLIST.BAS(3) error 14: Expected identifier, found 'sqlite3_io_methods' in 'pMe
thods as sqlite3_io_methods ptr'


But this type of references is used very frequently during construction of
difficult linked list structures.
It will be very useful to support such lists in FreeBASIC.

This question is not abstract, such code is contained in file "sqlite3.bi",
which is included in "sqlite3_test.bas" from examples of using databases.
And it can't be compiled because this error occurs:

C:\frebasic\inc\sqlite3.bi(139) error 14: Expected identifier, found 'sqlite3_io_methods' in 'pMethods as sqlite3_io_methods ptr'
TJF
Posts: 3486
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cross-linked lists

Postby TJF » Jun 21, 2013 10:11

monochromator wrote:But this type of references is used very frequently during construction of
difficult linked list structures.
It will be very useful to support such lists in FreeBASIC.

There're several ways to handle linked lists in FB, ie GLib offers more than five. Because you can use external libraries there is no reason to include linked list in FB.


monochromator wrote:There is the error during compilation:
COHLIST.BAS(3) error 14: Expected identifier, found 'sqlite3_io_methods' in 'pMe
thods as sqlite3_io_methods ptr'

I don't really understand what you want to do with your code, but when you #INCLUDE ONCE "sqlite3.bi" into your .bas file, the code will compile and you can use these structures:

Code: Select all

#INCLUDE ONCE "sqlite3.bi"

DIM AS       sqlite3_file a1
DIM AS sqlite3_io_methods a2

' ...
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: Cross-linked lists

Postby MOD » Jun 21, 2013 10:15

If you need to build such an construct, you can use Type Aliases:

Code: Select all

Type _sqlite3_io_methods As sqlite3_io_methods 'Create alias

Type sqlite3_file
   pMethods As _sqlite3_io_methods Ptr 'Use alias
End Type

Type sqlite3_io_methods
   iVersion As Integer
   xClose As sqlite3_file Ptr
End Type

Dim a1 As sqlite3_file, a2 As sqlite3_io_methods

a1.pMethods = @a2
a2.xClose = @a1
TJF
Posts: 3486
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cross-linked lists

Postby TJF » Jun 21, 2013 10:25

@MOD, be careful.

sqlite3_file and sqlite3_io_methods are UDTs from the SQLite3 library. They should be used as defined in the header sqlite3.bi. Ie xClose has a different declaration

Code: Select all

type sqlite3_io_methods
    iVersion as integer
    xClose as function cdecl(byval as sqlite3_file ptr) as integer
' ...
end type
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: Cross-linked lists

Postby MOD » Jun 21, 2013 10:32

It's just an example based on his code to show how it works. Of course you're right, one should use the existing headers instead of creating own types.
monochromator
Posts: 42
Joined: Mar 05, 2013 5:37

Re: Cross-linked lists

Postby monochromator » Jun 21, 2013 10:51

To MOD:
Your answer is really on the merits.
I didn't know about existence of this opportunity.
Thank you very much.

Simply I consider that it will be much better if the compiler automatically
supports such forward references as it occurs in C/C++ and Turbo Pascal.

But problem with sqlite3 example still persist.
Program "sqlite3_test.bas" is not compiled.
This is error message:


C:\frebasic\inc\sqlite3.bi(137) error 14: Expected identifier, found 'sqlite3_io_methods' in 'pMethods as sqlite3_io_methods ptr'

C:\frebasic\inc\sqlite3.bi(258) error 58: Illegal specification, at parameter 2 of sqlite3_vmprintf() in 'declare function sqlite3_vmprintf (byval as zstring ptr, byval as va_list) as zstring ptr'

C:\frebasic\inc\sqlite3.bi(260) error 58: Illegal specification, at parameter 4 of sqlite3_vsnprintf() in 'declare function sqlite3_vsnprintf (byval as integer, byval as zstring ptr, byval as zstring
ptr, byval as va_list) as zstring ptr'

C:\frebasic\inc\sqlite3.bi(470) error 4: Duplicated definition, sqlite3_module in 'type sqlite3_module'

C:\frebasic\inc\sqlite3.bi(513) error 4: Duplicated definition, sqlite3_index_info in 'type sqlite3_index_info'

C:\frebasic\inc\sqlite3.bi(536) error 4: Duplicated definition, sqlite3_vtab in 'type sqlite3_vtab'

C:\frebasic\inc\sqlite3.bi(542) error 4: Duplicated definition, sqlite3_vtab_cursor in 'type sqlite3_vtab_cursor'

C:\frebasic\inc\sqlite3.bi(689) error 58: Illegal specification, at parameter 1 in 'declare function sqlite3_rtree_geometry_callback (byval db as sqlite3 ptr, byval zGeom as zstring ptr, byval xGeom as
function cdecl(byval as sqlite3_rtree_geometry ptr,
byval as integer, byval as double ptr, byval as integer ptr) as integer, byval pContext as any ptr) as integer'

sqlite3_test.bas(52) warning 3(1): Passing different pointer types, at parameter 3 of SQLITE3_EXEC()

TJF
Posts: 3486
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cross-linked lists

Postby TJF » Jun 21, 2013 11:30

It seems that the file sqlite3.bi is broken.

Try the replacement in the fbc 0.24.0 Update Packet.

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests