Posted By: frankjiang
is possible lite-C`struct contain any functions - 09/03/10 08:31
is possible lite-C`struct contain any functions
---------------------------------------------------------------
void func(){
printf("hello world");
}
typedef struct{
var x;
var y;
int (*func)(int,int);
}SPOT;
---------------------------------------------------------------
Posted By: Tobias
Re: is possible lite-C`struct contain any functions - 09/03/10 08:39
Yes, but with different syntax, the normal lite-C structs use the EVENT type for function pointers. You can see examples in atypes.h. I believe EVENT is typedef void* but I'm not sure.
Posted By: frankjiang
Re: is possible lite-C`struct contain any functions - 09/03/10 09:08
Error in 'MAIN' line 21: Call function @temp_00093 error< spot.func();>
Posted By: MasterQ32
Re: is possible lite-C`struct contain any functions - 09/03/10 09:34
you need to do it like this:Adjust the font size
Make textarea smaller
Make textarea bigger
typedef struct
{
var x;
var y;
void fn(var x); //The function pointer
} SPOT;
void func(var x) //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = sys_malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
SPOT* spot = setup_spot(); //create SPOT
spot->fn(5); //call func();
}
EDIT: Sorry, old code won't work!
don't know why code doesn't work without parameters...
with params it works
Posted By: Helghast
Re: is possible lite-C`struct contain any functions - 09/03/10 09:40
I'm doing it slightly different.
I store a string in my struct, and use engine_getfunction together with a prototype function to call custom functions.
If you dont know what I mean, i can give an example when i'm back home.
regards,
Posted By: frankjiang
Re: is possible lite-C`struct contain any functions - 09/03/10 09:43
Error in 'MAIN' line 21: '@temp_00716': wrong number/type of parameters<spot->fn(); //call func();>
Posted By: MasterQ32
Re: is possible lite-C`struct contain any functions - 09/03/10 09:44
changed post!
typedef struct
{
var x;
var y;
void fn(var x); //The function pointer
} SPOT;
void func(var x) //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = sys_malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
SPOT* spot = setup_spot(); //create SPOT
spot->fn(5); //call func();
}
don't know why code doesn't work without parameters...
with params it works
Posted By: frankjiang
Re: is possible lite-C`struct contain any functions - 09/03/10 09:45
Error in 'MAIN' line 21:
'@temp_00716': wrong number/type of parameters
< spot->fn(); //call func();
>
Posted By: Damocles_
Re: is possible lite-C`struct contain any functions - 09/03/10 11:05
This works: (using int)
(the includes are not perfect..)
#include <default.c>
#include <litec.h>
typedef struct
{
int x;
int y;
void fn(int x); //The function pointer
} SPOT;
void func(int x) //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
SPOT* spot = setup_spot(); //create SPOT
spot->fn(5); //call func();
}
Posted By: Joozey
Re: is possible lite-C`struct contain any functions - 09/03/10 11:33
@damocles: It does? Is that too for A7? I was under the impression that it was not possible to call structure elements as functions...
I always do this, seems to work just fine, but not sure if it could go wrong with an integer pointer as function pointer storage:
typedef struct
{
int *spot_func;
} SPOT;
void spot_func_prototype(SPOT *s);
...
void spot_remove(SPOT s)
{
error("yay");
}
void main()
{
SPOT spot;
spot.func = spot_remove;
spot_func_prototype = spot.func;
spot_func_prototype( spot );
}
Posted By: MasterQ32
Re: is possible lite-C`struct contain any functions - 09/03/10 11:38
i think everybody has his own, working ideas and there are more ways than we can count, but we can't get them into our mind
Posted By: Damocles_
Re: is possible lite-C`struct contain any functions - 09/03/10 11:56
well it works (did test with a7)
Posted By: Damocles_
Re: is possible lite-C`struct contain any functions - 09/03/10 12:16
Works also with return values.
Basically you can almost work as in an object oriented approach.
#include <default.c>
typedef struct spotpoint
{
int x;
int y;
int fn(struct spotpoint * myself, int add_x); //The function pointer
} SPOT;
int func(SPOT* myself, int add_x) //your function
{
int itis=myself->x + add_x;
return itis;
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
//create the objects
SPOT* spot = setup_spot(); //create SPOT
SPOT* spot2 = setup_spot(); //create SPOT
//alter attributes in objects
spot->x=4; //set values
spot2->x=3; //set values
//call function in objects, so they work on their personal attributes
int whatis= spot->fn(spot,5); //call func();
int whatis2= spot2->fn(spot2,5); //call func();
if(whatis==9) error("Correct value for spot returned!");
else error("False value spot returned!");
if(whatis2==9) error("Correct value spot2 returned!");
else error("False value spot2 returned!");
}
Posted By: Aku_Aku
Re: is possible lite-C`struct contain any functions - 09/03/10 20:23
I am totally impressed by Damocles_ and Helghast work.
Posted By: FBL
Re: is possible lite-C`struct contain any functions - 09/22/10 21:20
Now all we need is "this"
Posted By: Joozey
Re: is possible lite-C`struct contain any functions - 09/22/10 22:33
Couldn't figure out a "this" by workarounds. Anyone?
Posted By: WretchedSid
Re: is possible lite-C`struct contain any functions - 09/23/10 06:58
Write a runtime that gets called wenn calling a function in a struct. In this function set a pointer called this to the struct and then call the actual function.
It's that easy.
Posted By: Helghast
Re: is possible lite-C`struct contain any functions - 09/23/10 07:37
Write a runtime that gets called wenn calling a function in a struct. In this function set a pointer called this to the struct and then call the actual function.
It's that easy.
Yep exactly as JustSid said, but here's an example with that:
usage of "this"
typedef struct
{
var x;
var y;
STRING *funcName; //The function pointer
} SPOT;
void callingFunction(SPOT *this) //your function
{
STRING *tempStr = str_create("");
str_cpy(tempStr, "this position x = ");
str_cat(tempStr, str_for_num(NULL, this.x));
str_cat(tempStr, ", and this position y = ");
str_cat(tempStr, str_for_num(NULL, this.y));
error(tempStr);
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = sys_malloc(sizeof(SPOT));
spot->x = random(100);
spot->y = random(100);
spot->funcName = str_create(""); //Set fn as func
str_cpy(spot->funcName, "callingFunction");
return(spot);
}
// empty prototype function
void PrototypeCallFunction(SPOT *this);
void main()
{
random_seed(0);
SPOT* spot = setup_spot(); //create SPOT
PrototypeCallFunction = engine_getscript(_chr(spot->funcName));
if(PrototypeCallFunction) { PrototypeCallFunction(spot); }
}
Posted By: Lukas
Re: is possible lite-C`struct contain any functions - 09/23/10 12:22
Why use a string instead of a direct function pointer?
Posted By: Helghast
Re: is possible lite-C`struct contain any functions - 09/23/10 12:33
Why use a string instead of a direct function pointer?
Was having problems running that, and copying a string name is easier to do from an external setup (think textfiles etc) then a direct function name... You can obviously change it to your own personal liking, but to me this was easiest
regards,
Posted By: Joozey
Re: is possible lite-C`struct contain any functions - 09/23/10 15:54
engine_getscript is dirty, calling the function from the structure is what we want.
spot->function();
But when using a dummy function inbetween that passes the object as "this", you can't give other parameters.
Posted By: WretchedSid
Re: is possible lite-C`struct contain any functions - 09/23/10 16:46
Well, actually you can. The trick is called variadic functions which are sadly not available in Lite-C. However, you can use a DLL for this stuff and everything is fine.
I tried something like this already with Lite-ObjectiveC but no one was interested in this so there is no Lite-C version anymore.
Posted By: Bunsen
Re: is possible lite-C`struct contain any functions - 09/25/10 22:01
Interesting thread. This would be my try:
//-------------------------------------------------------------------------
// Some Prototypes
//-------------------------------------------------------------------------
void delegate(void); // Simple void function
#define CALL(this, func) delegate = this.aFunc; delegate()
void delegate1(int x); // Void function with int argument
#define CALL1(this, func, arg) delegate1 = this.aFunc; delegate1(arg)
int delegate2(int x); // Int function with int argument
#define CALL2(retVal, this, func, arg) delegate2 = this.aFunc; retVal = delegate2(arg)
// Expand this here ...
//-------------------------------------------------------------------------
// Some Functions
//-------------------------------------------------------------------------
void func1() // A simple void function
{
printf("Hallo world!");
}
void func2(int x) // A void function which process an argument
{
printf("Result = %d", x);
}
int func3(int x) // A function which process an argument and returns a result
{
return x * 2;
}
//-------------------------------------------------------------------------
// A structure with a function member
//-------------------------------------------------------------------------
typedef struct
{
int a,b;
long aFunc; // Simply use a long integer to store the function address
} AStruct;
//-------------------------------------------------------------------------
// Use it
//-------------------------------------------------------------------------
void main()
{
AStruct obj[3]; // 3 Instances
obj[0].aFunc = func1; // Obj 0 gets a simple void function
CALL(obj[0], aFunc);
obj[1].aFunc = func2; // Obj 1 gets a void function which process an argument
CALL1(obj[1], aFunc, 42);
obj[2].aFunc = func3; // Obj 2 gets a function which process an argument and returns a result
CALL2(int retVal, obj[2], aFunc, 42);
printf("retVal = %d", retVal);
}