1 registered members (AndrewAMD),
678
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: WretchedSid]
#342070
09/23/10 07:37
09/23/10 07:37
|
Joined: Jan 2004
Posts: 3,023 The Netherlands
Helghast
Expert
|
Expert
Joined: Jan 2004
Posts: 3,023
The Netherlands
|
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); }
}
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: Lukas]
#342094
09/23/10 12:33
09/23/10 12:33
|
Joined: Jan 2004
Posts: 3,023 The Netherlands
Helghast
Expert
|
Expert
Joined: Jan 2004
Posts: 3,023
The Netherlands
|
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,
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: WretchedSid]
#342115
09/23/10 15:54
09/23/10 15:54
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
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.
Last edited by Joozey; 09/23/10 15:57.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: Joozey]
#342118
09/23/10 16:46
09/23/10 16:46
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
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.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: WretchedSid]
#342261
09/25/10 22:01
09/25/10 22:01
|
Joined: Apr 2009
Posts: 33 Germany
Bunsen
Newbie
|
Newbie
Joined: Apr 2009
Posts: 33
Germany
|
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); }
|
|
|
|