An object to facilitate dynamic arrays in types

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

An object to facilitate dynamic arrays in types

Post by cha0s »

Check the tests to see how it works. Feel free to ask any questions you may have.

SRC + Win32 Binaries: http://fileanchor.com/87839-d

I'll post the array in type test:

Code: Select all

#include "../array.bi"

type testing_type
    
    '' 3 dynamic arrays, with 0 - 9
	integer_array as dynamicArray(integer) = 10
	double_array as dynamicArray(double) = 10
	string_array as dynamicArray(string) = 10
	
end type

dim as testing_type this_is_only_a_test

'' put crap in them
for i as integer = 0 to 9
	this_is_only_a_test.string_array.data(i)  = str( 10 - i )
	this_is_only_a_test.integer_array.data(i) = 10 - i
	this_is_only_a_test.double_array.data(i)  = 10 - i
next

'' print em
print "Strings:"
print this_is_only_a_test.string_array
print 
print "Integers:"
print this_is_only_a_test.integer_array
print 
print "Doubles:"
print this_is_only_a_test.double_array
print 

'' resize them, 0 - 19
this_is_only_a_test.string_array.redim(20)
this_is_only_a_test.integer_array.redim(20)
this_is_only_a_test.double_array.redim(20)

'' put crap in them
for i as integer = 10 to 19
	this_is_only_a_test.string_array.data(i)  = str( 10 - i )
	this_is_only_a_test.integer_array.data(i) = 10 - i
	this_is_only_a_test.double_array.data(i)  = 10 - i
next

'' print em
print "Strings:"
print this_is_only_a_test.string_array
print 
print "Integers:"
print this_is_only_a_test.integer_array
print 
print "Doubles:"
print this_is_only_a_test.double_array
print 
Yeah, there's a cast to string, so you can print arrays like a normal variable.
Last edited by cha0s on Jan 17, 2007 4:11, edited 2 times in total.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Changelog:

-Added copy ctors, to make 'var = dynamic_array' safe.
-Fixed CAST to return a pointer of type, instead of ANY.
-Iterated in dtor, if strings, to free string mem.
-Changed 'exit sub' to 'exit constructor' (You'll now need a pretty recent CVS to compile it...)
-The library now does not contain any information about itself's location anymore, due to preprocessor directives.
Last edited by cha0s on Jan 17, 2007 4:13, edited 2 times in total.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

great work :)
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

For a moment i was like.. what the.. since when FB supports templates (the "type dynamicArray( __TYPE__ ) part"), great trick :).

Btw, with data being stored in objects, you should never use #libpath or #inclib with absolute paths, for example, libarray.a includes: "D:\prg\fb\cha0s\chi\dynarray".
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

^_^;; Cool :P
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Post by VonGodric »

link gives me: DB error 1203 in page initializing.
attacke
Posts: 55
Joined: Mar 19, 2006 11:57
Contact:

Post by attacke »

D:/FreeBASIC/Other/dynarray/tests/../array.bi(167) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( byte )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(167) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( byte )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(168) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( short )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(168) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( short )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(169) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( integer )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(169) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( integer )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(170) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( longint )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(170) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( longint )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(171) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( single )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(171) error 14: Expected identifier, found 'data' in 'DECLARE_DYNAMIC_ARRAY( single )'
D:/FreeBASIC/Other/dynarray/tests/../array.bi(171) error 124: Too many errors, exiting
:(
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Post by VonGodric »

you probably using an old version of fb. get latest fb cvs build (see under Linux/windows/dos -whatever platform you use) thread for daily builds.
attacke
Posts: 55
Joined: Mar 19, 2006 11:57
Contact:

Post by attacke »

im afraid to do that... everytime i do it feels like a new programming language... everythin i have gets 100+ errors...
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

attacke wrote:im afraid to do that... everytime i do it feels like a new programming language... everythin i have gets 100+ errors...
I'm like that but my situation's the opposite. When I update I'm afraid to use older versions because I tend to code cleanly with the newest features available. Oh wait nvr mind,

-lang deprecated

You should think about using it. Or -lang qb at least
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

it doesn't work with cvs buid from 01/16/2007

Joshy
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

D.J.Peters wrote:it doesn't work with cvs buid from 01/16/2007

Joshy
Specify errors, thanks.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

build

Code: Select all

C:\FBEXAM~1\dynarray>fbc array.bas array-byte.bas array-short.bas array-integer.bas array-longint.bas array-single.bas array-double.bas array-string.bas -lib -exx
array-byte.bas(3) error 205: Illegal outside a SUB block, found 'sub' in 'DEFINE
_DYNAMIC_ARRAY( byte )'
cd tests
build

Code: Select all

C:\FBEXAM~1\dynarray\tests>fbc array_in_type.bas
array_in_type.o:fake:(.text+0x74): undefined reference to `_ZN23__D_Y_N_A_R_R_A_
YSTRING11DATA__set__EjR8FBSTRING@12'
array_in_type.o:fake:(.text+0x98): undefined reference to `_ZN24__D_Y_N_A_R_R_A_
YINTEGER11DATA__set__EjRi@12'
array_in_type.o:fake:(.text+0xba): undefined reference to `_ZN23__D_Y_N_A_R_R_A_
YDOUBLE11DATA__set__EjRd@12'
array_in_type.o:fake:(.text+0x1c8): undefined reference to `_ZN23__D_Y_N_A_R_R_A
_YSTRING11DATA__set__EjR8FBSTRING@12'
array_in_type.o:fake:(.text+0x1ec): undefined reference to `_ZN24__D_Y_N_A_R_R_A
_YINTEGER11DATA__set__EjRi@12'
array_in_type.o:fake:(.text+0x20e): undefined reference to `_ZN23__D_Y_N_A_R_R_A
_YDOUBLE11DATA__set__EjRd@12
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Yeah, you used to have to do 'exit sub' instead of 'exit construtor', after rebuilding my CVS and the library, I found that. Thanks.

Original post updated.
subxero
Posts: 142
Joined: May 28, 2005 22:18
Location: Quincy, IL
Contact:

Post by subxero »

Could it be possible to have a default property for a class/type? Something like "declare property foo default (parameters)" ? That way, you could make the data property default, so you could do array(index) instead of array.data(index) .

That'd be awesome.
Post Reply