is it possible to do this?

New to FreeBASIC? Post your questions here.
Post Reply
icecubed
Posts: 8
Joined: Oct 05, 2007 0:30

is it possible to do this?

Post by icecubed »

it goes something like this.

one program, plus multiple compiled or source files.

6 different files, have the same functions but are different inside.

if you were to include A.BI or something like that instead of B.BI, you get 333 as the result by the "PrintNumber" command.

Using B.BI you will get 222 as the result.

Because (name).BI assigns shared dim variable, NUM to a certain integer.

Example:

Dim Shared A as Integer
Dim B as Integer

Input "Enter the file number.", B

Select Case B
Case 1
use "A.BI" 'Or a compiled file.
case 2
use "B.BI"
End Select

PrintNumber

Print A


the USE command is not real unless if there is a way to do just that.

In A.BI

Sub PrintNumber()
A=333
End Sub

In B.BI

Sub PrintNumber()
A=222
End Sub

i dunno if anyone has done something similar to this or not.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

You can use namespaces for something like that instead, so you can just use the a.PrintNumber() namespace, or the b.PrintNumber namespace. You could also put those functions in a TYPE and use them as methods:

Namespaces:

Code: Select all

namespace a
  function printNumber() as integer
    return 222
  end function
end namespace

namespace b
  function printNumber() as integer
    return 333
  end function
end namespace
Methods:

Code: Select all

type a
  '' we don't actually use the field member 'number'  It's just a filler
  '' because types can't just have functions.
  number as integer
  declare function printNumber() as integer
end type

  function a.printNumber() as integer
    return 222
  end function


type b
  number as integer
  declare function printNumber() as integer
end type

  function b.printNumber() as integer
    return 333
  end function



dim as a myA
dim as b myB

print myA.printNumber()
print myB.printNumber()
sleep
OR!, the very closest thing to what you are thinking is to use DLLs and load that code dynamically. The thing is, #include'ing is done on the PREPROCESSOR, so all that information is known before the code's even done COMPILING, so it can't be changed at RUNTIME.

However, if you use DLLs, you can load this code at RUNTIME and change which function to use simply by loading it from the DLL File. The FreeBASIC wiki has more information on DLLs.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Using dll's

Post by RockTheSchock »

icecubed
Posts: 8
Joined: Oct 05, 2007 0:30

Post by icecubed »

it would be a lot more useful if they can be used as arrays.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

icecubed wrote:it would be a lot more useful if they can be used as arrays.
What do you mean?
Josh
Posts: 79
Joined: Nov 05, 2007 20:14

Post by Josh »

probably cause he knows how to use arrays and is not familiar with that method. i can honestly say im with him on that.

you guys need to think more on a novice level your wisdom is great but you leave far to many fill in the blanks... and to say thats wrong do it this way isnt exactly beneficial advice to someone who has never done it your way.

im not knockin yah Chaos as i said your knowledgable, but you think UP here when others are thinking down here. im sure im not the only noob with that complaint.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

cha0s wrote:
icecubed wrote:it would be a lot more useful if they can be used as arrays.
What do you mean?
I think he means an array of procvars (function pointers)
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Oh I see. I could write a code example but... you can't use shared vars like that from within a shared library. The shared lib will get its own version of the variable. You'd have to pass a reference of some sort to the actual proc. Here's a functional example:

First create these 4 files in the same directory, and name them as I tell you

libfuncs.bas

Code: Select all

'' the 'export's are necessary
'' 
'' also, FreeBASIC will make these names CAPS, so remember that.

sub some_func0( byref i as integer ) export
	i = 222
end sub

sub some_func1( byref i as integer ) export
	i = 333
end sub
dylib.bi

Code: Select all

#pragma once

type DYLIB_ARRAY
	
	lib_core as any ptr
	
	declare constructor( byref lib_ as string )
	declare destructor( )
	
	declare function proc _ 
		( _ 
			byval proc_num as integer _
		) as any ptr
	
end type
dylib.bas

Code: Select all

'' see the note in libfuncs.bas about caps
const as string PROC_ID = "SOME_FUNC"

#include "dylib.bi"

'' called when a var is dimmed
constructor DYLIB_ARRAY( byref lib_ as string )
	lib_core = dylibload( lib_ )
end constructor

'' called when a var dies
destructor DYLIB_ARRAY( )
	dylibfree( lib_core )
end destructor

function DYLIB_ARRAY.proc _
	( _ 
		byval proc_num as integer _
	) as any ptr
	
	'' get the function, if proc is 2, then it's looking for "some_func2"
	function = dylibsymbol( lib_core, PROC_ID & proc_num )
	
end function
test.bas

Code: Select all

#include "dylib.bi"

dim as DYLIB_ARRAY my_lib = "libfuncs"

dim as integer p, a
Input "Enter the proc number.", p

'' notice this matches what's in libfuncs.bas
dim as sub( byref i as integer ) my_proc

'' like an array?
my_proc = my_lib.proc(p)

'' 0 means it wasn't found
if( my_proc = 0 ) then
	print "I couldn't load that proc"
else
	'' call it like a sub
	my_proc( a )
end if

print a

sleep
Now, go to a command prompt inside the directory you created the files. Issue these commands:

Code: Select all

fbc libfuncs.bas -dylib
fbc test.bas dylib.bas
Now, run test.exe. Since some_func0 and some_func1 are defined, you can enter 0 or 1 at the prompt. Hope you can learn from it.
Post Reply